UNPKG

@workday/canvas-kit-react

Version:

The parent module that contains all Workday Canvas Kit React components

44 lines (43 loc) 1.62 kB
import React from 'react'; import { createModelHook, useUniqueId } from '@workday/canvas-kit-react/common'; export const useDisclosureModel = createModelHook({ defaultConfig: { /** ID reference of the list. Children ids can be derived from this id */ id: '', /** * The initial visibility of the disclosed content * @default 'hidden' */ initialVisibility: 'hidden', }, })(config => { const id = useUniqueId(config.id); const [visibility, setVisibility] = React.useState(config.initialVisibility || 'hidden'); const state = { /** ID reference of the list. Children ids can be derived from this id */ id, /** * Visibility state of the disclosed content. Models are allowed to extend the states to fit * their needs, so if you need to consistently determine "not hidden", use * `visibility !== 'hidden'` rather than `visibility === 'visible'` */ visibility, }; const events = { /** * Start showing the disclosed content. If a DOM event triggered this event, the event data will * be passed along. This data can be used by guards and callbacks. */ show(event) { setVisibility('visible'); }, /** * Start hiding this disclosed content. If a DOM event triggered this event, the event data will * be passed along. This data can be used by guards and callbacks. */ hide(event) { setVisibility('hidden'); }, }; return { state, events }; });