Drawer API
Import
Section titled “Import”Cimpress UI exports four drawer-related components:
DrawerRoot: component that connects a trigger button with its associated drawer.Drawer: component that displays an overlay element which blocks interaction with outside elements.DrawerBody: component that contains the main content of the drawer.DrawerActions: component that contains the actions of the drawer.
import { Drawer, DrawerActions, DrawerBody, DrawerRoot } from '@cimpress-ui/react';Accessibility notes
Section titled “Accessibility notes”While a drawer is open, all content outside of it is hidden from assistive technologies.
By default, drawers can be closed by interacting outside or by pressing the Escape key. This can be disabled by setting the isDismissible prop to false. If you do this, you must provide a way for the user to close the drawer.
If your drawer contains form fields, we recommend to automatically focus one of the fields when the drawer is opened. This can be achieved by setting the autoFocus prop on the field that you want focused.
Drawer requires a textual label to remain accessible to assistive technologies. See our accessibility guide for more details.
Triggering a drawer
Section titled “Triggering a drawer”To open a drawer when a trigger button is pressed, wrap both the trigger button and the drawer component with a DrawerRoot component. This will automatically associate the button with the drawer, without any manual setup.
It’s also possible to display a drawer programmatically (not as a result of a user action), or render it in a different part of the component tree than its trigger. In this case, you’ll have to manage the drawer’s open state manually.
Automatic trigger
Manual trigger
import { Button, Drawer, DrawerBody, DrawerRoot, Stack, Text } from '@cimpress-ui/react';import { useState } from 'react';
export default function Demo() { const [isOpen, setIsOpen] = useState(false);
return ( <Stack gap={32}> <Stack gap={16}> <Text as="h2" variant="title-4"> Automatic trigger </Text>
<DrawerRoot> <Button>Open drawer</Button> <Drawer title="Drawer"> <DrawerBody>This drawer can be opened by an automatically attached trigger button.</DrawerBody> </Drawer> </DrawerRoot> </Stack>
<Stack gap={16}> <Text as="h2" variant="title-4"> Manual trigger </Text>
<Button onPress={() => setIsOpen(true)}>Open drawer</Button>
<Drawer title="Drawer" isOpen={isOpen} onOpenChange={setIsOpen}> <DrawerBody>This drawer's open state is managed manually.</DrawerBody> </Drawer> </Stack> </Stack> );}Closing programmatically
Section titled “Closing programmatically”Drawers can be closed programmatically by passing a function as children to the Drawer component, and using the close function provided as an argument to that function.
import { Button, Drawer, DrawerActions, DrawerBody, DrawerRoot } from '@cimpress-ui/react';
export default function Demo() { return ( <DrawerRoot> <Button>Open drawer</Button> <Drawer title="Closing example"> {({ close }) => ( <> <DrawerBody>Pressing the button below will close this drawer.</DrawerBody> <DrawerActions> <Button onPress={close}>Close drawer</Button> </DrawerActions> </> )} </Drawer> </DrawerRoot> );}API reference
Section titled “API reference”DrawerRoot
Section titled “DrawerRoot”Encapsulates a drawer trigger and its associated drawer. The trigger can be any Cimpress UI button, and the drawer will be displayed when the trigger is activated.
DrawerRootProps
- ReactNode
children *
Section titled “ children * ” -
The drawer trigger with its associated drawer. Provide the trigger as the first child, and the drawer as the second child.
- boolean
isOpen
Section titled “ isOpen ” -
Whether the drawer is open (controlled).
- boolean
defaultOpen
Section titled “ defaultOpen ” -
Whether the drawer is open by default (uncontrolled).
- (isOpen: boolean) => void
onOpenChange
Section titled “ onOpenChange ” -
Handler that is called when the drawer's open state changes.
Drawer
Section titled “Drawer”Displays an overlay element which blocks interaction with outside elements.
- Ref<HTMLElement>
-
The
reftype for this component.
DrawerProps
- ReactNode | ((renderProps: DrawerRenderProps) => ReactNode)
children *
Section titled “ children * ” -
The contents of the drawer. A function may be provided to access a function to close the drawer.
- string
title
Section titled “ title ” -
The title of the drawer.
- 'small' | 'medium' | 'large'
- Defaults to 'small' .
The size of the drawer. This prop is ignored on small devices - drawers on small devices will always take up all available width.
- boolean
isDismissible
Section titled “ isDismissible ” - Defaults to true .
Whether the drawer can be closed by clicking the close button, clicking outside of the drawer, or pressing the Escape key. When
false, the drawer can only be closed programmatically. - boolean
isOpen
Section titled “ isOpen ” -
Whether the drawer is open (controlled). If using
DrawerRoot, this prop has no effect - provideisOpentoDrawerRootinstead. - boolean
defaultOpen
Section titled “ defaultOpen ” -
Whether the drawer is open by default (uncontrolled). If using
DrawerRoot, this prop has no effect - providedefaultOpentoDrawerRootinstead. - (isOpen: boolean) => void
onOpenChange
Section titled “ onOpenChange ” -
Handler that is called when the drawer's open state changes. If using
DrawerRoot, this prop has no effect - provideonOpenChangetoDrawerRootinstead. - 'end' | 'start'
UNSTABLE_placement
Section titled “ UNSTABLE_placement ” - Defaults to 'end' .
The placement of the drawer.
- string
-
The element's unique identifier. See MDN.
- boolean
data-cim-style-root
Section titled “ data-cim-style-root ” -
Use this attribute to "claim" the component tree for exclusive Cimpress UI usage.
- string
UNSAFE_className
Section titled “ UNSAFE_className ” -
Sets the CSS className for the element. Only use as a last resort. Use style props instead.
See styling guide.
- CSSProperties
UNSAFE_style
Section titled “ UNSAFE_style ” -
Sets the CSS style for the element. Only use as a last resort. Use style props instead.
See styling guide.
- string
aria-label
Section titled “ aria-label ” -
Defines a string value that labels the current element.
- string
aria-labelledby
Section titled “ aria-labelledby ” -
Identifies the element (or elements) that labels the current element.
- string
aria-describedby
Section titled “ aria-describedby ” -
Identifies the element (or elements) that describes the object.
- string
aria-details
Section titled “ aria-details ” -
Identifies the element (or elements) that provide a detailed, extended description for the object.
DrawerBody
Section titled “DrawerBody”Renders content within Drawer.
DrawerBodyProps
- ReactNode
children *
Section titled “ children * ” -
The content to render within the drawer.
DrawerActions
Section titled “DrawerActions”Renders actions within Drawer.
DrawerActionsProps
- ReactNode
children *
Section titled “ children * ” -
Actions that should be available in the drawer.