usePressedState
The usePressedState
hook is a powerful utility in Pearl UI that not only manages a pressed state but also transforms the _pressed
props into appropriate styles. This hook is instrumental in creating dynamic, responsive components that react to press events with custom styles.
#
Importimport { usePressedState } from "pearl-ui";
#
Return valueThe usePressedState
hook returns an object with three properties:
pressed
: A boolean representing the current pressed state. This is a local state which is useful in case theparentStateValue
is not provided.setPressed
: A function that can be used to set thepressed
state. This function updates the local state.propsWithPressedStyles
: The props object of the component with updated styles according to the current 'pressed' 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 usePressedState
to manage a pressed state and compose extra styling while a button is pressed:
import { usePressedState } from "pearl-ui";import { useState } from "react";import { Button as RNButton } from "react-native";
const Button = ({ isPressed, ...props }) => { // Use the hook to transform the props based on the pressed state const { pressed, setPressed, propsWithPressedStyles } = usePressedState( props, allStyleFunctions, "basic", false, isPressed );
// Render the button with the transformed props return <RNButton {...propsWithPressedStyles} />;};
const App = () => { // Use a state variable to manage the pressed state const [isPressed, setIsPressed] = useState(false);
// Render the Button and a toggle button to change the pressed state return ( <div> <Button isPressed={isPressed} // Specify the styling of the component when the isPressed value is true _pressed={{ backgroundColor: "blue", color: "white" }} /> <button onClick={() => setIsPressed(!isPressed)}> Toggle Pressed State </button> </div> );};
In the provided example, the usePressedState
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 isPressed
prop. Specifically, when isPressed
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 'pressed' state instead of the local state value. |