Skip to content

useDisclosure

Hook to handle disclosure state.

Import

import {useDisclosure} from '@kaiverse/k/hooks'

Usage

useDisclosure hook returns open, close, and toggle helpers that can be handy for managing controlled <dialog>, <details>, and other disclosure components.

Examples

Controlled <details> element

State: false

System Requirements

Requires a computer running an operating system. The computer must have some memory and ideally some kind of long-term storage. An input device as well as some form of output device is recommended.

<Dialog> element

use-disclosure.tsx
import {useDisclosure} from '@kaiverse/k/hooks'
import {Dialog} from '@kaiverse/k/components'
const [opened, {open, close}] = useDisclosure(false)
<button type="button" onClick={open}>
Open Dialog
</button>
<Dialog open={opened} onClose={close}>
...
</Dialog>

Callbacks

  • onOpen: will be called when the disclosure state is set to true.
  • onClose: will be called when the disclosure state is set to false.
use-disclosure-callbacks.tsx
useDisclosure(false, {
onOpen: () => {...},
onClose: () => {...},
})

Type definition

function useDisclosure(
defaultOpened?: boolean,
callbacks?: {onOpen?: () => void; onClose?: () => void},
): Readonly<[boolean, {open: () => void; close: () => void; toggle: () => void}]>