apps/mantine.dev/src/pages/hooks/use-disclosure.mdx
import { Layout } from '@/layout'; import { MDX_DATA } from '@/mdx';
export default Layout(MDX_DATA.useDisclosure);
The use-disclosure hook manages boolean state. It provides open, close, and toggle handlers
and accepts optional onOpen and onClose callbacks. You can use it to manage controlled modals,
popovers, and other similar components:
import { useDisclosure } from '@mantine/hooks';
function Demo() {
const [opened, handlers] = useDisclosure(false);
// Sets opened to true
handlers.open();
// Sets opened to false
handlers.close();
// Sets opened to true if it was false and vice versa
handlers.toggle();
}
The onOpen and onClose callbacks execute when the opened state changes:
import { useDisclosure } from '@mantine/hooks';
function Demo() {
const [opened, handlers] = useDisclosure(false, {
onOpen: () => console.log('Opened'),
onClose: () => console.log('Closed'),
});
// Calls `onOpen` callback and sets opened to true
handlers.open();
// Does nothing, opened is already true
handlers.open();
// Calls `onClose` callback and sets opened to false
handlers.close();
// Does nothing, opened is already false
handlers.close();
// Calls `onOpen` or `onClose` depending on the current state
handlers.toggle();
}
interface UseDisclosureOptions {
onOpen?: () => void;
onClose?: () => void;
}
interface UseDisclosureHandlers {
set: (value: boolean) => void;
open: () => void;
close: () => void;
toggle: () => void;
}
type UseDisclosureReturnValue = [boolean, UseDisclosureHandlers];
function useDisclosure(
initialState?: boolean,
options?: UseDisclosureOptions,
): UseDisclosureReturnValue
UseDisclosureOptions, UseDisclosureHandlers and UseDisclosureReturnValue types are exported from the @mantine/hooks package;
you can import them in your application:
import type { UseDisclosureOptions, UseDisclosureHandlers, UseDisclosureReturnValue } from '@mantine/hooks';