UNPKG

@workday/canvas-kit-preview-react

Version:

Canvas Kit Preview is made up of components that have the full design and a11y review, are part of the DS ecosystem and are approved for use in product. The API's could be subject to change, but not without strong communication and migration strategies.

71 lines (70 loc) 2.04 kB
import * as React from 'react'; export const SidePanelContext = React.createContext({ state: 'expanded', origin: 'left', }); import { useUniqueId } from '@workday/canvas-kit-react/common'; /** * * This hook manages the state and `aria-` attributes for the SidePanel. It takes an optional * configuration object: * * ```tsx * import {useSidePanel} from '@workday/canvas-kit-preview-react/side-panel'; * * const {expanded, setExpanded, panelProps, labelProps, controlProps} = useSidePanel({ * initialExpanded: false, * panelId: 'custom-panel-id', * labelId: 'custom-label-id', * }); * ``` */ export const useSidePanel = (config) => { const [touched, setTouched] = React.useState(false); const configParams = config ? config : { initialExpanded: true, panelId: undefined, labelId: undefined, }; const { initialExpanded = true, panelId: panelIdParam, labelId: labelIdParam } = configParams; const [expanded, setExpanded] = React.useState(initialExpanded); const panelId = useUniqueId(panelIdParam); const labelId = useUniqueId(labelIdParam); // This prevents keyframes animations from rendering prematurely const handleSetTouched = () => { if (touched === false) { setTouched(true); } }; const handleClick = () => { handleSetExpanded(!expanded); }; const handleSetExpanded = (newExpandedState) => { handleSetTouched(); setExpanded(newExpandedState); }; const panelProps = { expanded: expanded, id: panelId, 'aria-labelledby': labelId, touched, }; const labelProps = { id: labelId, }; const controlProps = { 'aria-controls': panelId, 'aria-expanded': expanded, 'aria-labelledby': labelId, onClick: handleClick, }; return { expanded, setExpanded: handleSetExpanded, panelProps, labelProps, controlProps, }; };