Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: controlled state #408

Merged
merged 11 commits into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 16 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ function MyComponent() {
<Drawer.Trigger>Open</Drawer.Trigger>
<Drawer.Portal>
<Drawer.Content>
<Drawer.Handle />
<p>Content</p>
</Drawer.Content>
<Drawer.Overlay />
Expand Down Expand Up @@ -63,20 +62,32 @@ Additional props:

`modal`: When `false` it allows to interact with elements outside of the drawer without closing it. Defaults to `true`.

`handleOnly`: When `true` only allows the drawer to be dragged by the `<Drawer.Handle />` component. Defaults to `false`.

`direction`: Direction of the drawer. Can be `top` or `bottom`, `left`, `right`. Defaults to `bottom`.

`preventScrollRestoration`: When `true` it prevents scroll restoration when the drawer is closed after a navigation happens inside of it. Defaults to `true`.

`disablePreventScroll`: When `true` scroll prevention mechanism will be disabled. Scroll prevention ensures that page will not scroll on mobile when opening drawer. However this mechanism gets confused when drawer has an input with autofocus and therefore opens simulataneosly with touch keyboard. Defaults to `true`. `modal` set to `false` also disables it.

`noBodyStyles`: When `true` the `body` doesn't get any styles assigned from Vaul.

`setBackgroundColorOnScale`: When `false` we don't change body's background color when the drawer is open. `true` by default.

`defaultOpen`: Opened by default, still reacts to `open` state changes.

`[data-vaul-no-drag]`: When interacting with an element with this data attribute, the drawer won't be dragged.

### Controlled Drawer

Drawer can be controlled programmatically by providing the `open` prop. If you want to react to open state changes from within the Drawer use the `onOpenChange` prop, this will allow you to provide your own open state while still closing the drawer when the escape key is pressed for example.

```
const [open, setOpen] = React.useState(false);

// ...

<Drawer.Root open={open} onOpenChange={setOpen}>
// ...
</Drawer.Root>
```

### Trigger

The button that opens the dialog. [Props](https://www.radix-ui.com/docs/primitives/components/dialog#trigger).
Expand All @@ -101,10 +112,6 @@ An optional accessible description to be announced when the dialog is opened. [P

The button that closes the dialog. [Props](https://www.radix-ui.com/docs/primitives/components/dialog#close).

### Handle

A drag hint (also known as grabber). Shows people that they can drag the drawer to resize it; they can also tap it to cycle through the snap points, and double tap quickly to close the drawer. Set `preventCycle={true}` to stop this behavior. If you want to change the handle's hit area you can do so by styling the `[data-vaul-handle-hitarea]` selector (Defaults to 44x44 on mobile devices).

### Portal

Portals your drawer into the body.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,6 @@
},
"packageManager": "pnpm@8.8.0",
"dependencies": {
"@radix-ui/react-dialog": "^1.0.4"
"@radix-ui/react-dialog": "^1.1.1"
}
}
192 changes: 89 additions & 103 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,15 @@ export const TRANSITIONS = {
};

export const VELOCITY_THRESHOLD = 0.4;

export const CLOSE_THRESHOLD = 0.25;

export const SCROLL_LOCK_TIMEOUT = 100;

export const BORDER_RADIUS = 8;

export const NESTED_DISPLACEMENT = 16;

export const WINDOW_TOP_OFFSET = 26;

export const DRAG_CLASS = 'vaul-dragging';
6 changes: 0 additions & 6 deletions src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ interface DrawerContextValue {
onNestedOpenChange: (o: boolean) => void;
onNestedRelease: (event: React.PointerEvent<HTMLDivElement>, open: boolean) => void;
dismissible: boolean;
handleOnly: boolean;
isOpen: boolean;
isDragging: boolean;
keyboardIsOpen: React.MutableRefObject<boolean>;
Expand All @@ -22,9 +21,7 @@ interface DrawerContextValue {
shouldFade: boolean;
activeSnapPoint?: number | string | null;
setActiveSnapPoint: (o: number | string | null) => void;
visible: boolean;
closeDrawer: () => void;
setVisible: (o: boolean) => void;
openProp?: boolean;
onOpenChange?: (o: boolean) => void;
direction?: DrawerDirection;
Expand All @@ -42,7 +39,6 @@ export const DrawerContext = React.createContext<DrawerContextValue>({
onNestedRelease: () => {},
openProp: undefined,
dismissible: false,
handleOnly: false,
isOpen: false,
isDragging: false,
keyboardIsOpen: { current: false },
Expand All @@ -53,9 +49,7 @@ export const DrawerContext = React.createContext<DrawerContextValue>({
activeSnapPoint: null,
onOpenChange: () => {},
setActiveSnapPoint: () => {},
visible: false,
closeDrawer: () => {},
setVisible: () => {},
direction: 'bottom',
});

Expand Down
Loading
Loading