useHoveredState
info
Please note that this hook is primarily targeted for web applications as the hover functionality does not exist on mobile platforms.
The useHoveredState
hook is a powerful utility in Pearl UI that not only manages a hovered state but also transforms the _hovered
props into appropriate styles. This hook is instrumental in creating dynamic, responsive components that react to hover events with custom styles.
#
Importimport { useHoveredState } from "pearl-ui";
#
Return valueThe useHoveredState
hook returns an object with three properties:
hovered
: A boolean representing the current hovered state. This is a local state which is useful in case theparentStateValue
is not provided.setHovered
: A function that can be used to set thehovered
state. This function updates the local state.propsWithHoveredStyles
: The props object of the component with updated styles according to the current 'hovered' state. This is achieved by using theuseDynamicStateStyle
hook internally, which manages a dynamic state and composes extra styling while a component is in a certain state.
#
UsageHere's an example of how you can use useHoveredState
to manage a hovered state and compose extra styling while a button is hovered:
import { useHoveredState } from "pearl-ui";import { useState } from "react";import { Button as RNButton } from "react-native";
const Button = ({ isHovered, ...props }) => { // Use the hook to transform the props based on the hovered state const { hovered, setHovered, propsWithHoveredStyles } = useHoveredState( props, allStyleFunctions, "basic", false, isHovered );
// Render the button with the transformed props return <RNButton {...propsWithHoveredStyles} />;};
const App = () => { // Use a state variable to manage the hovered state const [isHovered, setIsHovered] = useState(false);
// Render the Button and a toggle button to change the hovered state return ( <div> <Button isHovered={isHovered} // Specify the styling of the component when the isHovered value is true _hovered={{ backgroundColor: "blue", color: "white" }} /> <button onClick={() => setIsHovered(!isHovered)}> Toggle Hovered State </button> </div> );};
In the provided example, the useHoveredState
hook is utilized within a Button
component. The purpose of this hook is to dynamically alter the button's background and text colors in response to changes in the isHovered
prop. Specifically, when isHovered
is set to true, the button's background color transitions to blue, and the text color changes to white.
#
ParametersName | Required | Type | Default | Description |
---|---|---|---|---|
props | Yes | The props of the component. | ||
styleFunctions | No | boxStyleFunctions | The style functions to use. | |
activeComponentType | No | "basic" | The active component type. | |
animateable | No | true | Whether the component is animateable. | |
parentStateValue | No | undefined | A override value to control the 'hovered' state instead of the local state value. |