useDisclosure

A hook for managing boolean open/close states. Useful for modals, drawers, dropdowns, accordions, and any toggleable UI element. Provides open, close, and toggle functions with optional lifecycle callbacks.

Import

import { useDisclosure } from '@wavebooking/aqua-fusion';

Usage

function ModalExample() {
  const { isOpen, open, close } = useDisclosure();
 
  return (
    <>
      <Button onClick={open}>Open Modal</Button>
      <Dialog open={isOpen} onClose={close}>
        <DialogHeader>
          <DialogTitle>Confirm Action</DialogTitle>
        </DialogHeader>
        <DialogFooter>
          <Button variant="outline" onClick={close}>Cancel</Button>
          <Button onClick={close}>Confirm</Button>
        </DialogFooter>
      </Dialog>
    </>
  );
}

With callbacks

const { isOpen, toggle } = useDisclosure({
  defaultOpen: false,
  onOpen: () => trackEvent('menu_opened'),
  onClose: () => trackEvent('menu_closed'),
});

Parameters

ParameterTypeDescription
optionsUseDisclosureOptionsOptional configuration object.

UseDisclosureOptions

PropertyTypeDefaultDescription
defaultOpenbooleanfalseInitial open state.
onOpen() => void--Callback fired when the disclosure opens.
onClose() => void--Callback fired when the disclosure closes.

Return Value

PropertyTypeDescription
isOpenbooleanCurrent open state.
open() => voidFunction to set state to open.
close() => voidFunction to set state to closed.
toggle() => voidFunction to toggle between open and closed.
setIsOpen(value: boolean) => voidFunction to set the open state directly.