• English
  • Drawer

    Open content from a page edge to view details or complete actions.

    Basic usage

    Live previewBasic usage
    basic.tsx
    import { Button, Drawer, Form, FormField, Input, Select } from '@sudden3/leaf-ui';
    import { useState } from 'react';
    export function DrawerBasic() {
      const [placement, setPlacement] = useState<'left' | 'right' | 'top' | 'bottom'>('right');
      const [open, setOpen] = useState(false);
      return (
        <div className="leaf-demo-stack leaf-demo-stack--wide">
          <div className="leaf-demo-row">
            {(['left', 'right', 'top', 'bottom'] as const).map((side) => (
              <Button
                key={side}
                variant="outline"
                onClick={() => {
                  setPlacement(side);
                  setOpen(true);
                }}
              >
                {side}
              </Button>
            ))}
          </div>
          <Drawer
            open={open}
            placement={placement}
            title="Project settings"
            onClose={() => setOpen(false)}
            footer={
              <>
                <Button variant="outline" onClick={() => setOpen(false)}>
                  Cancel
                </Button>
                <Button type="submit" form="drawer-project">
                  Save
                </Button>
              </>
            }
          >
            <Form
              id="drawer-project"
              onSubmit={(event) => {
                event.preventDefault();
                setOpen(false);
              }}
            >
              <FormField label="Project name" required>
                <Input name="name" defaultValue="Leaf Garden" />
              </FormField>
              <FormField label="Project type">
                <Select
                  name="type"
                  defaultValue="design"
                  options={[
                    { value: 'design', label: 'Design' },
                    { value: 'development', label: 'Development' },
                  ]}
                />
              </FormField>
            </Form>
          </Drawer>
        </div>
      );
    }
    

    Drawer API

    PropTypeDefaultDescription
    openbooleanRequiredWhether the drawer is open
    titleReactNode—Title
    childrenReactNode—Drawer content
    placement'left' | 'right' | 'top' | 'bottom''right'Opening edge
    widthnumber | string380Width for left and right drawers
    heightnumber | string300Height for top and bottom drawers
    extraReactNode—Actions beside the title
    footerReactNode—Footer content
    closable / maskClosable / keyboardbooleantrueClose button / backdrop click / Escape
    onClose() => void—Close request; update open in application state
    afterClose() => void—Called after the closing transition

    Types

    Import the component props and item types when preparing your own values.

    import type { DrawerProps } from '@sudden3/leaf-ui';

    Usage notes

    Place actions in footer. A submit button can use its form attribute to target a form inside the drawer. Provide aria-label if there is no title. Nested dialogs are supported, and focus returns to the opener after closing.