Playground

useDisclosure

Open and closed state with open, close and toggle functions, and callbacks when it changes.

  • No DOM output. Pair with aria-expanded and aria-controls on the trigger.
  • Callbacks run only when the state actually changes, not on mount.

Import

import '@monoline/css';
import {useDisclosure} from '@monoline/react';

Not installed yet? Pick a route.

Copies the source and its styles into your project.

npx monoline add use-disclosure

Usage

import {Button, useDisclosure} from '@monoline/react';

function DisclosureDemo() {
  const {isOpen, toggle} = useDisclosure();
  return (
    <div>
      <Button variant="line" aria-expanded={isOpen} onClick={toggle}>{isOpen ? 'Hide details' : 'Show details'}</Button>
      {isOpen && <p>Details appear here.</p>}
    </div>
  );
}

<DisclosureDemo />

Examples

Toggle

open, close and toggle

Props

UseDisclosureOptions

PropTypeDefaultDescription
initialOpenbooleanfalseOpen on first render. Default `false`.
onOpen(() => void)–Called when the state changes from closed to open. Not called on mount.
onClose(() => void)–Called when the state changes from open to closed. Not called on mount.

UseDisclosureReturn

PropTypeDefaultDescription
isOpen requiredboolean–
open required() => void–
close required() => void–
toggle required() => void–

Behaviour

  • No DOM output. Pair with aria-expanded and aria-controls on the trigger.
  • Callbacks run only when the state actually changes, not on mount.