useDrawer hook provides access to drawer state and drawer actions from drawer context, and it can be used in two different modes: global mode (without key) and scoped mode (with key).ts
import { useDrawer } from "@arkyn/components";
key (optional)stringkey is provided.DrawerContextProps<T> | { drawerIsOpen: boolean; drawerData: T; openDrawer: (data?: T) => void; closeDrawer: () => void }key (global mode)drawerIsOpen(key: string): boolean
Checks whether a specific drawer is open.drawerData(key: string): T
Reads the data associated with a specific drawer key.openDrawer(key: string, data?: T): void
Opens the drawer for the provided key and optionally stores data.closeDrawer(key: string): void
Closes the drawer for the provided key.key (scoped mode)drawerIsOpen: boolean
Boolean state for the current scoped drawer.drawerData: T
Data stored for the current scoped drawer.openDrawer(data?: T): void
Opens only the scoped drawer and optionally stores data.closeDrawer(): void
Closes only the scoped drawer.key)tsx
import { DrawerProvider, useDrawer } from "@arkyn/components";function DrawerActions() {const { drawerIsOpen, drawerData, openDrawer, closeDrawer } = useDrawer<{category: string;}>();const filtersOpen = drawerIsOpen("filters");const filtersData = drawerData("filters");return (<div><p>Is filters drawer open? {String(filtersOpen)}</p><p>Current category: {filtersData?.category ?? "none"}</p><buttononClick={() => openDrawer("filters", { category: "electronics" })}>Open filters</button><button onClick={() => closeDrawer("filters")}>Close filters</button></div>);}function App() {return (<DrawerProvider><DrawerActions /></DrawerProvider>);}
key)tsx
import { DrawerProvider, useDrawer } from "@arkyn/components";function FiltersDrawer() {const { drawerIsOpen, drawerData, closeDrawer } = useDrawer<{category: string;}>("filters");if (!drawerIsOpen) return null;return (<aside><p>Category: {drawerData?.category}</p><button onClick={closeDrawer}>Close</button></aside>);}function DrawerActions() {const { openDrawer } = useDrawer<{ category: string }>("filters");return (<button onClick={() => openDrawer({ category: "electronics" })}>Open filters</button>);}function App() {return (<DrawerProvider><DrawerActions /><FiltersDrawer /></DrawerProvider>);}
useDrawer outside provider scope throws an error.