@pickle-ui/react 1.0.0-beta.17 → 1.0.0-beta.19

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (40) hide show
  1. package/README.md +2 -1
  2. package/dist/components/BottomSheet.d.ts +48 -0
  3. package/dist/components/BottomSheet.d.ts.map +1 -0
  4. package/dist/components/BottomSheet.js +97 -0
  5. package/dist/components/BottomSheet.js.map +1 -0
  6. package/dist/components/Drawer.d.ts +9 -1
  7. package/dist/components/Drawer.d.ts.map +1 -1
  8. package/dist/components/Drawer.js +15 -5
  9. package/dist/components/Drawer.js.map +1 -1
  10. package/dist/components/Tabs.d.ts +2 -9
  11. package/dist/components/Tabs.d.ts.map +1 -1
  12. package/dist/components/Tabs.js +3 -13
  13. package/dist/components/Tabs.js.map +1 -1
  14. package/dist/components/_internal/dialog-styles.d.ts +1 -1
  15. package/dist/components/_internal/dialog-styles.d.ts.map +1 -1
  16. package/dist/components/_internal/dialog-styles.js +1 -1
  17. package/dist/components/_internal/dialog-styles.js.map +1 -1
  18. package/dist/entries/bottom-sheet.d.ts +3 -0
  19. package/dist/entries/bottom-sheet.d.ts.map +1 -0
  20. package/dist/entries/bottom-sheet.js +2 -0
  21. package/dist/entries/bottom-sheet.js.map +1 -0
  22. package/dist/entries/tabs.d.ts +1 -1
  23. package/dist/entries/tabs.d.ts.map +1 -1
  24. package/dist/index.d.ts +1 -0
  25. package/dist/index.d.ts.map +1 -1
  26. package/dist/index.js +1 -0
  27. package/dist/index.js.map +1 -1
  28. package/package.json +1 -1
  29. package/skills/pickle-ui-react/SKILL.md +1 -0
  30. package/src/components/BottomSheet.stories.tsx +200 -0
  31. package/src/components/BottomSheet.test.tsx +143 -0
  32. package/src/components/BottomSheet.tsx +218 -0
  33. package/src/components/Drawer.tsx +41 -16
  34. package/src/components/Tabs.stories.tsx +2 -5
  35. package/src/components/Tabs.test.tsx +1 -2
  36. package/src/components/Tabs.tsx +4 -25
  37. package/src/components/_internal/dialog-styles.ts +1 -1
  38. package/src/entries/bottom-sheet.ts +12 -0
  39. package/src/entries/tabs.ts +1 -7
  40. package/src/index.ts +1 -0
@@ -0,0 +1,200 @@
1
+ import type { Meta, StoryObj } from '@storybook/react-vite';
2
+
3
+ import { zodResolver } from '@hookform/resolvers/zod';
4
+ import * as React from 'react';
5
+ import { useForm } from 'react-hook-form';
6
+ import { z } from 'zod/v4';
7
+
8
+ import { type ComponentMatrixCellProps, Story } from './_internal/Story';
9
+ import { BottomSheet } from './BottomSheet';
10
+ import { Button } from './Button';
11
+ import { Form } from './form/Form';
12
+ import { Input } from './form/Input';
13
+ import { Text } from './Text';
14
+
15
+ const meta = {
16
+ component: BottomSheet,
17
+ parameters: {
18
+ layout: 'fullscreen',
19
+ },
20
+ title: 'components/BottomSheet',
21
+ } satisfies Meta<typeof BottomSheet>;
22
+
23
+ export default meta;
24
+ type ExampleContent = 'close-button' | 'form' | 'list' | 'snap-points';
25
+
26
+ type StoryDefinition = StoryObj<typeof meta>;
27
+
28
+ const CONTENT_COLUMNS: {
29
+ description: string;
30
+ key: ExampleContent;
31
+ label: string;
32
+ }[] = [
33
+ { description: 'Header, panel and footer', key: 'form', label: 'Form' },
34
+ {
35
+ description: 'The panel scrolls between header and footer',
36
+ key: 'list',
37
+ label: 'Long content',
38
+ },
39
+ {
40
+ description: 'showCloseButton, no footer',
41
+ key: 'close-button',
42
+ label: 'With close button',
43
+ },
44
+ ];
45
+
46
+ const SNAP_POINTS = [0.5, 1];
47
+
48
+ const SNAP_POINTS_COLUMNS: {
49
+ description: string;
50
+ key: ExampleContent;
51
+ label: string;
52
+ }[] = [
53
+ {
54
+ description: 'No footer: at half height it would sit below the screen',
55
+ key: 'snap-points',
56
+ label: 'Long content',
57
+ },
58
+ ];
59
+
60
+ const profileSchema = z.object({
61
+ name: z.string().trim().min(1, 'Enter your name.'),
62
+ username: z.string().trim().min(2, 'Use at least 2 characters.'),
63
+ });
64
+
65
+ const ACTIVITY = Array.from({ length: 24 }, (_, index) => ({
66
+ id: index,
67
+ label: `Deployment #${240 - index}`,
68
+ }));
69
+
70
+ function Activity({ description }: { description: string }) {
71
+ return (
72
+ <>
73
+ <BottomSheet.Header>
74
+ <BottomSheet.Title>Activity</BottomSheet.Title>
75
+ <BottomSheet.Description>{description}</BottomSheet.Description>
76
+ </BottomSheet.Header>
77
+ <BottomSheet.Panel className="grid content-start gap-2">
78
+ {ACTIVITY.map(({ id, label }) => (
79
+ <div className="rounded-lg border px-3 py-2" key={id}>
80
+ <Text variant="body">{label}</Text>
81
+ </div>
82
+ ))}
83
+ </BottomSheet.Panel>
84
+ </>
85
+ );
86
+ }
87
+
88
+ function BottomSheetExample({ content, trigger }: { content: ExampleContent; trigger: string }) {
89
+ const [open, setOpen] = React.useState(false);
90
+
91
+ return (
92
+ <BottomSheet
93
+ onOpenChange={setOpen}
94
+ open={open}
95
+ snapPoints={content === 'snap-points' ? SNAP_POINTS : undefined}
96
+ >
97
+ <BottomSheet.Trigger render={<Button variant="outline" />}>{trigger}</BottomSheet.Trigger>
98
+ <BottomSheet.Content showCloseButton={content === 'close-button'}>
99
+ {content === 'form' ? <ProfileForm onSaved={() => setOpen(false)} /> : null}
100
+ {content === 'list' ? (
101
+ <>
102
+ <Activity description="The last 24 deployments." />
103
+ <BottomSheet.Footer>
104
+ <BottomSheet.Close render={<Button variant="outline" />}>Close</BottomSheet.Close>
105
+ </BottomSheet.Footer>
106
+ </>
107
+ ) : null}
108
+ {content === 'snap-points' ? (
109
+ <Activity description="Drag up to see all 24 deployments." />
110
+ ) : null}
111
+ {content === 'close-button' ? (
112
+ <>
113
+ <BottomSheet.Header>
114
+ <BottomSheet.Title>Share link</BottomSheet.Title>
115
+ <BottomSheet.Description>
116
+ Anyone with the link can view this project. Swipe down to close.
117
+ </BottomSheet.Description>
118
+ </BottomSheet.Header>
119
+ <BottomSheet.Panel>
120
+ <Input readOnly value="https://pickle-ui.dev/p/8f2a" />
121
+ </BottomSheet.Panel>
122
+ </>
123
+ ) : null}
124
+ </BottomSheet.Content>
125
+ </BottomSheet>
126
+ );
127
+ }
128
+
129
+ function ContentCell({ column }: ComponentMatrixCellProps) {
130
+ return <BottomSheetExample content={column.key as ExampleContent} trigger="Open" />;
131
+ }
132
+
133
+ /**
134
+ * A `Form` around the header, panel and footer, so `Form.Button` in the footer
135
+ * submits it. `contents` (and no `space-y`) keeps the three parts laid out
136
+ * by the sheet.
137
+ */
138
+ function ProfileForm({ onSaved }: { onSaved: () => void }) {
139
+ const form = useForm<z.infer<typeof profileSchema>>({
140
+ defaultValues: { name: 'Ada Lovelace', username: 'ada' },
141
+ resolver: zodResolver(profileSchema),
142
+ });
143
+
144
+ return (
145
+ <Form className="contents space-y-0" form={form} onSubmit={onSaved}>
146
+ <BottomSheet.Header>
147
+ <BottomSheet.Title>Edit profile</BottomSheet.Title>
148
+ <BottomSheet.Description>
149
+ Make changes to your profile, then save when you are done.
150
+ </BottomSheet.Description>
151
+ </BottomSheet.Header>
152
+ <BottomSheet.Panel className="space-y-6">
153
+ <Form.Field label="Name" name="name">
154
+ <Form.Input autoComplete="name" />
155
+ </Form.Field>
156
+ <Form.Field description="Shown on your public profile." label="Username" name="username">
157
+ <Form.Input autoComplete="username" />
158
+ </Form.Field>
159
+ </BottomSheet.Panel>
160
+ <BottomSheet.Footer>
161
+ <BottomSheet.Close render={<Button variant="outline" />}>Cancel</BottomSheet.Close>
162
+ <Form.Button type="submit">Save changes</Form.Button>
163
+ </BottomSheet.Footer>
164
+ </Form>
165
+ );
166
+ }
167
+
168
+ export const All: StoryDefinition = {
169
+ render: () => (
170
+ <Story.Layout className="max-w-5xl" title="BottomSheet">
171
+ <Story.Section title="Content">
172
+ <Story.Matrix
173
+ Cell={ContentCell}
174
+ columns={CONTENT_COLUMNS}
175
+ rows={[
176
+ {
177
+ description: 'Rises from the bottom edge, closes on a swipe down',
178
+ key: 'content',
179
+ label: 'content',
180
+ },
181
+ ]}
182
+ />
183
+ </Story.Section>
184
+
185
+ <Story.Section title="Snap points">
186
+ <Story.Matrix
187
+ Cell={ContentCell}
188
+ columns={SNAP_POINTS_COLUMNS}
189
+ rows={[
190
+ {
191
+ description: 'Opens at half height, drags up to full height',
192
+ key: 'snap-points',
193
+ label: 'snapPoints={[0.5, 1]}',
194
+ },
195
+ ]}
196
+ />
197
+ </Story.Section>
198
+ </Story.Layout>
199
+ ),
200
+ };
@@ -0,0 +1,143 @@
1
+ import { Drawer as DrawerPrimitive } from '@base-ui/react/drawer';
2
+ import { fireEvent, render, screen, waitFor, within } from '@testing-library/react';
3
+ import { describe, expect, it } from 'vitest';
4
+
5
+ import { BottomSheet, type BottomSheetContentProps } from './BottomSheet';
6
+
7
+ async function openSheet() {
8
+ fireEvent.click(screen.getByRole('button', { name: 'Open cart' }));
9
+ return screen.findByRole('dialog', { name: 'Cart' });
10
+ }
11
+
12
+ function renderSheet(contentProps?: BottomSheetContentProps) {
13
+ return render(
14
+ <BottomSheet>
15
+ <BottomSheet.Trigger>Open cart</BottomSheet.Trigger>
16
+ <BottomSheet.Content {...contentProps}>
17
+ <BottomSheet.Header>
18
+ <BottomSheet.Title>Cart</BottomSheet.Title>
19
+ <BottomSheet.Description>Review your items.</BottomSheet.Description>
20
+ </BottomSheet.Header>
21
+ <BottomSheet.Panel>Two items</BottomSheet.Panel>
22
+ <BottomSheet.Footer>
23
+ <BottomSheet.Close>Keep shopping</BottomSheet.Close>
24
+ </BottomSheet.Footer>
25
+ </BottomSheet.Content>
26
+ </BottomSheet>,
27
+ );
28
+ }
29
+
30
+ describe('BottomSheet', () => {
31
+ it('opens from the trigger with an accessible name and description', async () => {
32
+ renderSheet();
33
+
34
+ const sheet = await openSheet();
35
+
36
+ expect(sheet).toHaveAccessibleDescription('Review your items.');
37
+ expect(sheet).toHaveAttribute('data-slot', 'bottom-sheet-content');
38
+ expect(screen.getByRole('heading', { name: 'Cart' })).toHaveAttribute(
39
+ 'data-slot',
40
+ 'bottom-sheet-title',
41
+ );
42
+ expect(document.querySelector('[data-slot=bottom-sheet-backdrop]')).toBeTruthy();
43
+ });
44
+
45
+ it('names every part it builds on Drawer with its own slot', async () => {
46
+ renderSheet({ showCloseButton: true });
47
+ const trigger = screen.getByRole('button', { name: 'Open cart' });
48
+
49
+ const sheet = await openSheet();
50
+
51
+ expect(trigger).toHaveAttribute('data-slot', 'bottom-sheet-trigger');
52
+ for (const part of ['backdrop', 'header', 'description', 'panel', 'footer']) {
53
+ expect(document.querySelector(`[data-slot=bottom-sheet-${part}]`)).toBeTruthy();
54
+ }
55
+ for (const name of ['Close', 'Keep shopping']) {
56
+ expect(within(sheet).getByRole('button', { name })).toHaveAttribute(
57
+ 'data-slot',
58
+ 'bottom-sheet-close',
59
+ );
60
+ }
61
+ expect(document.querySelector('[data-slot^=drawer-]')).toBeNull();
62
+ });
63
+
64
+ it('rises from the bottom with a hidden grab bar', async () => {
65
+ renderSheet();
66
+
67
+ const sheet = await openSheet();
68
+
69
+ expect(sheet).toHaveAttribute('data-swipe-direction', 'down');
70
+ expect(sheet.querySelector('[data-slot=bottom-sheet-bar]')).toHaveAttribute(
71
+ 'aria-hidden',
72
+ 'true',
73
+ );
74
+ });
75
+
76
+ it('never closes on a swipe in the panel or the footer', async () => {
77
+ renderSheet();
78
+
79
+ const sheet = await openSheet();
80
+
81
+ expect(within(sheet).getByText('Two items')).toHaveAttribute('data-base-ui-swipe-ignore');
82
+ expect(sheet.querySelector('[data-slot=bottom-sheet-footer]')).toHaveAttribute(
83
+ 'data-base-ui-swipe-ignore',
84
+ );
85
+ });
86
+
87
+ it.each([
88
+ { defaultSnapPoint: '20rem', scrolls: false },
89
+ { defaultSnapPoint: '40rem', scrolls: true },
90
+ ])(
91
+ 'with snapPoints, lets a swipe in the panel move the sheet; it scrolls only at the top one ($defaultSnapPoint)',
92
+ async ({ defaultSnapPoint, scrolls }) => {
93
+ render(
94
+ <BottomSheet defaultSnapPoint={defaultSnapPoint} snapPoints={['20rem', '40rem']}>
95
+ <BottomSheet.Trigger>Open cart</BottomSheet.Trigger>
96
+ <BottomSheet.Content>
97
+ <BottomSheet.Title>Cart</BottomSheet.Title>
98
+ <BottomSheet.Panel>Two items</BottomSheet.Panel>
99
+ </BottomSheet.Content>
100
+ </BottomSheet>,
101
+ );
102
+
103
+ const panel = within(await openSheet()).getByText('Two items');
104
+
105
+ expect(panel).not.toHaveAttribute('data-base-ui-swipe-ignore');
106
+ expect(panel.classList.contains('overflow-hidden')).toBe(!scrolls);
107
+ },
108
+ );
109
+
110
+ it('closes from BottomSheet.Close', async () => {
111
+ renderSheet();
112
+
113
+ const sheet = await openSheet();
114
+ fireEvent.click(within(sheet).getByRole('button', { name: 'Keep shopping' }));
115
+
116
+ await waitFor(() => expect(screen.queryByRole('dialog')).not.toBeInTheDocument());
117
+ });
118
+
119
+ it('closes from the close button, named with closeLabel', async () => {
120
+ renderSheet({ closeLabel: 'Fermer', showCloseButton: true });
121
+
122
+ const sheet = await openSheet();
123
+ fireEvent.click(within(sheet).getByRole('button', { name: 'Fermer' }));
124
+
125
+ await waitFor(() => expect(screen.queryByRole('dialog')).not.toBeInTheDocument());
126
+ });
127
+
128
+ it("opens from a trigger outside it, through a Base UI drawer's handle", async () => {
129
+ const handle = DrawerPrimitive.createHandle();
130
+ render(
131
+ <>
132
+ <BottomSheet.Trigger handle={handle}>Open cart</BottomSheet.Trigger>
133
+ <BottomSheet handle={handle}>
134
+ <BottomSheet.Content>
135
+ <BottomSheet.Title>Cart</BottomSheet.Title>
136
+ </BottomSheet.Content>
137
+ </BottomSheet>
138
+ </>,
139
+ );
140
+
141
+ expect(await openSheet()).toBeInTheDocument();
142
+ });
143
+ });
@@ -0,0 +1,218 @@
1
+ 'use client';
2
+
3
+ import { Drawer as DrawerPrimitive } from '@base-ui/react/drawer';
4
+ import * as React from 'react';
5
+
6
+ import { cn } from '../lib/utils';
7
+ import {
8
+ Drawer,
9
+ DrawerBackdrop,
10
+ DrawerCloseButton,
11
+ type DrawerCloseProps,
12
+ type DrawerDescriptionProps,
13
+ type DrawerFooterProps,
14
+ type DrawerHeaderProps,
15
+ type DrawerPanelProps,
16
+ drawerSwipeTransitionClassName,
17
+ type DrawerTitleProps,
18
+ type DrawerTriggerProps,
19
+ } from './Drawer';
20
+
21
+ // The top padding leaves 3rem of the page above a sheet as tall as the viewport.
22
+ // `touch-none` keeps the page from panning or pulling to refresh under a drag.
23
+ const bottomSheetViewportClassName =
24
+ 'fixed inset-0 z-50 flex touch-none flex-col justify-end pt-12';
25
+
26
+ const bottomSheetPopupClassName = cn(
27
+ 'bg-popover text-popover-foreground relative flex max-h-full w-full min-w-0 flex-col rounded-t-2xl pt-3 shadow-lg/5 outline-none not-dark:bg-clip-padding',
28
+ // Base UI's keyboard provider sets `--drawer-keyboard-inset`, so the keyboard lifts the
29
+ // sheet. A footer takes this padding instead, so its fill reaches the screen's edge.
30
+ '[--sheet-inset:max(env(safe-area-inset-bottom,0px),var(--drawer-keyboard-inset,0px))] pb-(--sheet-inset) has-data-[slot=bottom-sheet-footer]:pb-0',
31
+ drawerSwipeTransitionClassName,
32
+ // `snapPoints` on the root rest the sheet lower by `--drawer-snap-point-offset`.
33
+ '[transform:translateY(calc(var(--drawer-snap-point-offset,0px)+var(--drawer-swipe-movement-y,0px)))] data-ending-style:[transform:translateY(100%)] data-starting-style:[transform:translateY(100%)]',
34
+ // Fills the gap past the screen edge when a drag pulls the sheet up, in the footer's tint
35
+ // if it has one; opaque and 1px inside the edge, so a fractional drag leaves no seam.
36
+ 'before:bg-popover before:pointer-events-none before:absolute before:inset-x-0 before:top-[calc(100%-1px)] before:h-12',
37
+ 'has-data-[slot=bottom-sheet-footer]:before:bg-linear-to-b has-data-[slot=bottom-sheet-footer]:before:from-muted/72 has-data-[slot=bottom-sheet-footer]:before:to-muted/72',
38
+ );
39
+
40
+ type BottomSheetCloseProps = DrawerCloseProps;
41
+ type BottomSheetContentProps = React.ComponentProps<typeof DrawerPrimitive.Popup> & {
42
+ /** Accessible name of the close icon button. Defaults to `"Close"`. */
43
+ closeLabel?: string;
44
+ /** Renders a close icon button in the top-right corner. Defaults to `false`. */
45
+ showCloseButton?: boolean;
46
+ };
47
+ type BottomSheetDescriptionProps = DrawerDescriptionProps;
48
+ type BottomSheetFooterProps = DrawerFooterProps;
49
+ type BottomSheetHeaderProps = DrawerHeaderProps;
50
+ type BottomSheetPanelProps = DrawerPanelProps;
51
+ // The sheet always swipes down to close.
52
+ type BottomSheetProps = Omit<React.ComponentProps<typeof DrawerPrimitive.Root>, 'swipeDirection'>;
53
+ type BottomSheetTitleProps = DrawerTitleProps;
54
+ type BottomSheetTriggerProps = DrawerTriggerProps;
55
+
56
+ // Where the sheet rests: Base UI marks it expanded only at snap point 1, whatever the top one.
57
+ // `none` for no snap points or one, `lower` below the top one, `top` at it.
58
+ const SnapPointsContext = React.createContext<'lower' | 'none' | 'top'>('none');
59
+
60
+ function BottomSheetClose(props: BottomSheetCloseProps) {
61
+ return <Drawer.Close data-slot="bottom-sheet-close" {...props} />;
62
+ }
63
+
64
+ function BottomSheetContent({
65
+ children,
66
+ className,
67
+ closeLabel = 'Close',
68
+ showCloseButton = false,
69
+ ...props
70
+ }: BottomSheetContentProps) {
71
+ const hasSnapPoints = React.useContext(SnapPointsContext) !== 'none';
72
+
73
+ return (
74
+ // Inside the root and around the viewport, where it writes the keyboard inset.
75
+ <DrawerPrimitive.VirtualKeyboardProvider>
76
+ <DrawerPrimitive.Portal>
77
+ <DrawerBackdrop
78
+ // Base UI's progress hides the backdrop at a lower snap point; keep it whole.
79
+ className={cn(hasSnapPoints && 'opacity-100')}
80
+ data-slot="bottom-sheet-backdrop"
81
+ />
82
+ <DrawerPrimitive.Viewport className={bottomSheetViewportClassName}>
83
+ <DrawerPrimitive.Popup
84
+ className={cn(
85
+ bottomSheetPopupClassName,
86
+ // Keeps the title clear of the close button.
87
+ showCloseButton && '**:data-[slot=bottom-sheet-header]:pr-12',
88
+ className,
89
+ )}
90
+ data-slot="bottom-sheet-content"
91
+ {...props}
92
+ >
93
+ {children}
94
+ <div
95
+ aria-hidden
96
+ className="bg-input absolute top-1.5 left-1/2 h-1 w-10 -translate-x-1/2 rounded-full"
97
+ data-slot="bottom-sheet-bar"
98
+ />
99
+ {showCloseButton ? (
100
+ <DrawerCloseButton aria-label={closeLabel} data-slot="bottom-sheet-close" />
101
+ ) : null}
102
+ </DrawerPrimitive.Popup>
103
+ </DrawerPrimitive.Viewport>
104
+ </DrawerPrimitive.Portal>
105
+ </DrawerPrimitive.VirtualKeyboardProvider>
106
+ );
107
+ }
108
+
109
+ function BottomSheetDescription(props: BottomSheetDescriptionProps) {
110
+ return <Drawer.Description data-slot="bottom-sheet-description" {...props} />;
111
+ }
112
+
113
+ /**
114
+ * A touch swipe on the footer never closes the sheet, so its buttons take taps. At a lower
115
+ * snap point, it waits below the screen until the sheet rises.
116
+ */
117
+ function BottomSheetFooter({ className, ...props }: BottomSheetFooterProps) {
118
+ return (
119
+ <Drawer.Footer
120
+ className={cn('pb-[calc(1rem+var(--sheet-inset))]', className)}
121
+ data-base-ui-swipe-ignore=""
122
+ data-slot="bottom-sheet-footer"
123
+ {...props}
124
+ />
125
+ );
126
+ }
127
+
128
+ function BottomSheetHeader(props: BottomSheetHeaderProps) {
129
+ return <Drawer.Header data-slot="bottom-sheet-header" {...props} />;
130
+ }
131
+
132
+ /**
133
+ * The scrolling body between the header and the footer. A touch swipe in it scrolls it and
134
+ * never closes the sheet, but with snap points, Base UI moves the sheet when it can't scroll.
135
+ */
136
+ function BottomSheetPanel({ className, ...props }: BottomSheetPanelProps) {
137
+ const snapState = React.useContext(SnapPointsContext);
138
+
139
+ return (
140
+ <Drawer.Panel
141
+ className={cn(
142
+ 'touch-pan-y',
143
+ // Scrolls once the sheet is at its top snap point, so a drag below it moves the sheet.
144
+ snapState === 'lower' && 'overflow-hidden',
145
+ className,
146
+ )}
147
+ data-base-ui-swipe-ignore={snapState === 'none' ? '' : undefined}
148
+ data-slot="bottom-sheet-panel"
149
+ {...props}
150
+ />
151
+ );
152
+ }
153
+
154
+ /** List `snapPoints` from the lowest to the highest: the Panel scrolls at the last one. */
155
+ function BottomSheetRoot({ onSnapPointChange, ...props }: BottomSheetProps) {
156
+ const { defaultSnapPoint, snapPoint, snapPoints = [] } = props;
157
+ const firstSnapPoint = defaultSnapPoint ?? snapPoints[0] ?? null;
158
+ // Mirrors Base UI's own snap point when uncontrolled, as it reads only within its parts.
159
+ const [restingSnapPoint, setRestingSnapPoint] =
160
+ React.useState<BottomSheetProps['snapPoint']>(firstSnapPoint);
161
+ const activeSnapPoint =
162
+ snapPoint !== undefined
163
+ ? snapPoint
164
+ : snapPoints.some((point) => Object.is(point, restingSnapPoint))
165
+ ? restingSnapPoint
166
+ : firstSnapPoint;
167
+ const snapState =
168
+ snapPoints.length < 2
169
+ ? 'none'
170
+ : Object.is(activeSnapPoint, snapPoints[snapPoints.length - 1])
171
+ ? 'top'
172
+ : 'lower';
173
+
174
+ return (
175
+ <SnapPointsContext.Provider value={snapState}>
176
+ <DrawerPrimitive.Root
177
+ onSnapPointChange={(nextSnapPoint, eventDetails) => {
178
+ onSnapPointChange?.(nextSnapPoint, eventDetails);
179
+ if (!eventDetails.isCanceled) setRestingSnapPoint(nextSnapPoint);
180
+ }}
181
+ swipeDirection="down"
182
+ {...props}
183
+ />
184
+ </SnapPointsContext.Provider>
185
+ );
186
+ }
187
+
188
+ function BottomSheetTitle(props: BottomSheetTitleProps) {
189
+ return <Drawer.Title data-slot="bottom-sheet-title" {...props} />;
190
+ }
191
+
192
+ function BottomSheetTrigger(props: BottomSheetTriggerProps) {
193
+ return <Drawer.Trigger data-slot="bottom-sheet-trigger" {...props} />;
194
+ }
195
+
196
+ const BottomSheet = Object.assign(BottomSheetRoot, {
197
+ Close: BottomSheetClose,
198
+ Content: BottomSheetContent,
199
+ Description: BottomSheetDescription,
200
+ Footer: BottomSheetFooter,
201
+ Header: BottomSheetHeader,
202
+ Panel: BottomSheetPanel,
203
+ Title: BottomSheetTitle,
204
+ Trigger: BottomSheetTrigger,
205
+ });
206
+
207
+ export { BottomSheet };
208
+ export type {
209
+ BottomSheetCloseProps,
210
+ BottomSheetContentProps,
211
+ BottomSheetDescriptionProps,
212
+ BottomSheetFooterProps,
213
+ BottomSheetHeaderProps,
214
+ BottomSheetPanelProps,
215
+ BottomSheetProps,
216
+ BottomSheetTitleProps,
217
+ BottomSheetTriggerProps,
218
+ };
@@ -16,8 +16,9 @@ import { Button } from './Button';
16
16
  const drawerMotion =
17
17
  'duration-450 ease-[cubic-bezier(0.32,0.72,0,1)] motion-reduce:transition-none';
18
18
 
19
- const drawerBackdropClassName = cn(
20
- 'fixed inset-0 z-50 bg-black/32 opacity-[calc(1-var(--drawer-swipe-progress,0))] backdrop-blur-sm transition-opacity data-ending-style:opacity-0 data-starting-style:opacity-0 data-swiping:duration-0',
19
+ // How a drawer's popup follows a drag and settles; `BottomSheet` shares it.
20
+ const drawerSwipeTransitionClassName = cn(
21
+ 'transition-transform data-ending-style:duration-[calc(var(--drawer-swipe-strength,1)*400ms)] data-swiping:select-none',
21
22
  drawerMotion,
22
23
  );
23
24
 
@@ -30,8 +31,7 @@ const drawerEdgeFillClassName =
30
31
  const drawerPopupVariants = cva(
31
32
  cn(
32
33
  'bg-popover text-popover-foreground relative ml-auto flex w-[calc(100%-3rem)] min-w-0 flex-col shadow-lg/5 outline-none not-dark:bg-clip-padding',
33
- 'transition-transform data-ending-style:duration-[calc(var(--drawer-swipe-strength,1)*400ms)] data-swiping:select-none',
34
- drawerMotion,
34
+ drawerSwipeTransitionClassName,
35
35
  '[transform:translateX(var(--drawer-swipe-movement-x,0px))] data-ending-style:[transform:translateX(calc(100%+var(--inset,0px)))] data-starting-style:[transform:translateX(calc(100%+var(--inset,0px)))]',
36
36
  ),
37
37
  {
@@ -61,6 +61,10 @@ const drawerPopupVariants = cva(
61
61
  },
62
62
  );
63
63
 
64
+ type DrawerBackdropProps = Omit<
65
+ React.ComponentProps<typeof DrawerPrimitive.Backdrop>,
66
+ 'className'
67
+ > & { className?: string };
64
68
  type DrawerCloseProps = React.ComponentProps<typeof DrawerPrimitive.Close>;
65
69
 
66
70
  type DrawerContentProps = React.ComponentProps<typeof DrawerPrimitive.Popup> & {
@@ -99,10 +103,39 @@ type DrawerTitleProps = React.ComponentProps<typeof DrawerPrimitive.Title>;
99
103
  type DrawerTriggerProps = React.ComponentProps<typeof DrawerPrimitive.Trigger>;
100
104
  type DrawerVariant = NonNullable<VariantProps<typeof drawerPopupVariants>['variant']>;
101
105
 
106
+ /** The dimmed page behind the drawer; `BottomSheet` shares it. */
107
+ function DrawerBackdrop({ className, ...props }: DrawerBackdropProps) {
108
+ return (
109
+ <DrawerPrimitive.Backdrop
110
+ className={cn(
111
+ 'fixed inset-0 z-50 bg-black/32 opacity-[calc(1-var(--drawer-swipe-progress,0))] backdrop-blur-sm transition-opacity data-ending-style:opacity-0 data-starting-style:opacity-0 data-swiping:duration-0',
112
+ drawerMotion,
113
+ className,
114
+ )}
115
+ data-slot="drawer-backdrop"
116
+ {...props}
117
+ />
118
+ );
119
+ }
120
+
102
121
  function DrawerClose({ ...props }: DrawerCloseProps) {
103
122
  return <DrawerPrimitive.Close data-slot="drawer-close" {...props} />;
104
123
  }
105
124
 
125
+ /** The close icon button in the popup's top-right corner; `BottomSheet` shares it. */
126
+ function DrawerCloseButton(props: DrawerCloseProps) {
127
+ return (
128
+ <DrawerPrimitive.Close
129
+ className="absolute top-3 right-3"
130
+ data-slot="drawer-close"
131
+ render={<Button size="icon-sm" variant="ghost" />}
132
+ {...props}
133
+ >
134
+ <IconX />
135
+ </DrawerPrimitive.Close>
136
+ );
137
+ }
138
+
106
139
  function DrawerContent({
107
140
  children,
108
141
  className,
@@ -114,7 +147,7 @@ function DrawerContent({
114
147
  }: DrawerContentProps) {
115
148
  return (
116
149
  <DrawerPrimitive.Portal>
117
- <DrawerPrimitive.Backdrop className={drawerBackdropClassName} data-slot="drawer-backdrop" />
150
+ <DrawerBackdrop />
118
151
  <DrawerPrimitive.Viewport className={drawerViewportClassName}>
119
152
  <DrawerPrimitive.Popup
120
153
  className={cn(
@@ -129,16 +162,7 @@ function DrawerContent({
129
162
  {...props}
130
163
  >
131
164
  {children}
132
- {showCloseButton ? (
133
- <DrawerPrimitive.Close
134
- aria-label={closeLabel}
135
- className="absolute top-3 right-3"
136
- data-slot="drawer-close"
137
- render={<Button size="icon-sm" variant="ghost" />}
138
- >
139
- <IconX />
140
- </DrawerPrimitive.Close>
141
- ) : null}
165
+ {showCloseButton ? <DrawerCloseButton aria-label={closeLabel} /> : null}
142
166
  </DrawerPrimitive.Popup>
143
167
  </DrawerPrimitive.Viewport>
144
168
  </DrawerPrimitive.Portal>
@@ -223,7 +247,8 @@ const Drawer = Object.assign(DrawerRoot, {
223
247
  Trigger: DrawerTrigger,
224
248
  });
225
249
 
226
- export { Drawer };
250
+ // The parts `BottomSheet` builds on; `entries/drawer.ts` exports only `Drawer`.
251
+ export { Drawer, DrawerBackdrop, DrawerCloseButton, drawerSwipeTransitionClassName };
227
252
  export type {
228
253
  DrawerCloseProps,
229
254
  DrawerContentProps,