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