@yamatozhen/design_system 1.0.0 → 1.1.0
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.
- package/LICENSE +201 -201
- package/README.md +101 -101
- package/package.json +50 -49
- package/scripts/postinstall.js +91 -105
- package/template/app/ClientNav.tsx +258 -310
- package/template/app/globals.css +2 -167
- package/template/app/layout.tsx +94 -128
- package/template/app/themes/dark-mc 2.css +51 -0
- package/template/app/themes/dark-mc.css +51 -51
- package/template/app/themes/dark.css +51 -51
- package/template/app/themes/light 2.css +51 -0
- package/template/app/themes/light-mc 2.css +51 -0
- package/template/app/themes/light-mc.css +51 -51
- package/template/app/themes/light.css +51 -51
- package/template/app/themes/theme 2.css +272 -0
- package/template/app/themes/theme.css +272 -82
- package/template/components/{layout/button → (buttons)}/Button.module.css +351 -305
- package/template/components/{layout/button → (buttons)}/ButtonGroup.tsx +414 -503
- package/template/components/{layout/button → (buttons)}/FAB.tsx +119 -118
- package/template/components/(buttons)/button.tsx +164 -0
- package/template/components/(controls)/slider/Slider.module.css +250 -0
- package/template/components/(controls)/slider/Slider.tsx +386 -0
- package/template/components/(controls)/switch/Switch.module.css +108 -0
- package/template/components/(controls)/switch/Switch.tsx +69 -0
- package/template/components/(date & time)/calendar/Calendar.module.css +125 -0
- package/template/components/(date & time)/calendar/Calendar.tsx +206 -0
- package/template/components/(date & time)/dateField/DateField.module.css +25 -0
- package/template/components/(date & time)/dateField/DateField.tsx +193 -0
- package/template/components/(date & time)/datePicker/DatePicker.tsx +118 -0
- package/template/components/(date & time)/dateRangePicker/DateRangePicker.tsx +144 -0
- package/template/components/(date & time)/rangeCalendar/RangeCalendar.module.css +203 -0
- package/template/components/(date & time)/rangeCalendar/RangeCalendar.tsx +263 -0
- package/template/components/(date & time)/shared/dateUtils.ts +107 -0
- package/template/components/(date & time)/timeField/TimeField.module.css +39 -0
- package/template/components/(date & time)/timeField/TimeField.tsx +158 -0
- package/template/components/(feedback)/circularProgress/CircularProgress.tsx +646 -0
- package/template/components/(feedback)/linearProgress/LinearProgress.tsx +337 -0
- package/template/components/(feedback)/meter/Meter.tsx +111 -0
- package/template/components/(feedback)/skeleton/Skeleton.module.css +49 -0
- package/template/components/(feedback)/skeleton/Skeleton.tsx +32 -0
- package/template/components/(feedback)/spinner/Spinner.module.css +27 -0
- package/template/components/(feedback)/spinner/Spinner.tsx +60 -0
- package/template/components/(forms)/checkbox/Checkbox.tsx +121 -0
- package/template/components/(forms)/checkboxGroup/CheckboxGroup.tsx +83 -0
- package/template/components/(forms)/description/Description.tsx +34 -0
- package/template/components/(forms)/errorMessage/ErrorMessage.tsx +31 -0
- package/template/components/(forms)/fieldError/FieldError.tsx +39 -0
- package/template/components/(forms)/fieldset/Fieldset.tsx +51 -0
- package/template/components/(forms)/form/Form.tsx +54 -0
- package/template/components/(forms)/input/Input.tsx +50 -0
- package/template/components/(forms)/inputGroup/InputGroup.tsx +65 -0
- package/template/components/(forms)/inputOtp/InputOtp.tsx +139 -0
- package/template/components/(forms)/label/Label.tsx +46 -0
- package/template/components/(forms)/numberField/NumberField.tsx +292 -0
- package/template/components/(forms)/radioGroup/RadioGroup.tsx +175 -0
- package/template/components/(forms)/searchField/SearchField.tsx +139 -0
- package/template/components/(forms)/shared/field.module.css +75 -0
- package/template/components/(forms)/shared/fieldContext.ts +28 -0
- package/template/components/(forms)/textArea/TextArea.tsx +55 -0
- package/template/components/(forms)/textField/TextField.tsx +91 -0
- package/template/components/{layout → (layout)}/Lightbox/LightboxCard.tsx +100 -100
- package/template/components/{layout → (layout)}/Lightbox/LightboxGallery.module.css +249 -249
- package/template/components/{layout → (layout)}/Lightbox/LightboxGallery.tsx +72 -72
- package/template/components/{layout → (layout)}/Lightbox/LightboxGrid.tsx +185 -186
- package/template/components/{layout → (layout)}/Lightbox/LightboxModal.module.css +395 -395
- package/template/components/{layout → (layout)}/Lightbox/LightboxModal.tsx +498 -500
- package/template/components/{layout → (layout)}/Lightbox/demo.tsx +396 -412
- package/template/components/{layout → (layout)}/Lightbox/index.ts +30 -30
- package/template/components/{layout → (layout)}/Lightbox/types.ts +101 -113
- package/template/components/{layout → (layout)}/Lightbox/useLightboxFilters.ts +140 -140
- package/template/components/(layout)/accordion/Accordion.module.css +108 -0
- package/template/components/(layout)/accordion/Accordion.tsx +251 -0
- package/template/components/(layout)/card/Card.module.css +136 -0
- package/template/components/(layout)/card/Card.tsx +371 -0
- package/template/components/(layout)/list/List.module.css +83 -0
- package/template/components/(layout)/list/List.tsx +332 -0
- package/template/components/{layout → (layout)}/navigation/Wrap.tsx +41 -41
- package/template/components/(layout)/navigation/breadcrumbs/Breadcrumbs.tsx +145 -0
- package/template/components/{layout/navigation/nav/Nav.module.css → (layout)/navigation/nav/Nav.module.css.old} +474 -508
- package/template/components/{layout → (layout)}/navigation/nav/Nav.tsx +186 -167
- package/template/components/{layout → (layout)}/navigation/nav/NavBar.tsx +396 -419
- package/template/components/{layout → (layout)}/navigation/nav/NavContext.tsx +259 -259
- package/template/components/{layout → (layout)}/navigation/nav/NavItem.tsx +195 -173
- package/template/components/{layout → (layout)}/navigation/nav/NavNew.module.css +406 -373
- package/template/components/{layout → (layout)}/navigation/nav/NavRail.tsx +317 -315
- package/template/components/{layout → (layout)}/navigation/nav/NavSection.tsx +101 -102
- package/template/components/{layout → (layout)}/navigation/nav/NavTab.tsx +25 -25
- package/template/components/{layout → (layout)}/navigation/nav/navftb/NavFTB.module.css +253 -252
- package/template/components/{layout → (layout)}/navigation/nav/navftb/NavFTB.tsx +448 -399
- package/template/components/{layout → (layout)}/navigation/nav/navftb/useTabIndicator.ts +498 -484
- package/template/components/{layout → (layout)}/navigation/nav/navheader/NavHeader.module.css +59 -59
- package/template/components/{layout → (layout)}/navigation/nav/navheader/NavHeader.tsx +465 -478
- package/template/components/(layout)/navigation/pagination/Pagination.tsx +261 -0
- package/template/components/{layout → (layout)}/pages/loadingScreen.tsx +117 -102
- package/template/components/{layout → (layout)}/pages/maintenance/Maintenance.tsx +52 -52
- package/template/components/{layout → (layout)}/pages/maintenance/maintenance.module.css +162 -162
- package/template/components/(layout)/scrollShadow/ScrollShadow.module.css +81 -0
- package/template/components/(layout)/scrollShadow/ScrollShadow.tsx +161 -0
- package/template/components/(layout)/scrollShadow/useScrollShadow.ts +130 -0
- package/template/components/(layout)/separator/Separator.tsx +112 -0
- package/template/components/(overlays)/accessibilityModal/AccessibilityModal.tsx +307 -0
- package/template/components/(overlays)/drawer/Drawer.tsx +953 -0
- package/template/components/(overlays)/modal/Modal.tsx +654 -0
- package/template/components/(overlays)/popover/Popover.tsx +420 -0
- package/template/components/(overlays)/toast/Toast.tsx +692 -0
- package/template/components/(pickers)/autocomplete/Autocomplete.tsx +24 -0
- package/template/components/(pickers)/comboBox/ComboBox.tsx +21 -0
- package/template/components/(pickers)/select/Select.tsx +188 -0
- package/template/components/(pickers)/shared/FilterableField.tsx +223 -0
- package/template/components/(pickers)/shared/PickerItem.ts +13 -0
- package/template/components/tools/CustomScrollbar 2.tsx +107 -0
- package/template/components/tools/CustomScrollbar.tsx +107 -0
- package/template/components/tools/Divider 2.tsx +11 -0
- package/template/components/tools/Divider.tsx +11 -11
- package/template/components/tools/ReduceMotionContext 2.tsx +5 -0
- package/template/components/tools/ReduceMotionContext.tsx +5 -5
- package/template/components/tools/Sinewave 2.tsx +206 -0
- package/template/components/tools/Sinewave.tsx +396 -199
- package/template/components/tools/SinewaveProgress 2.tsx +140 -0
- package/template/components/tools/SinewaveProgress.tsx +140 -135
- package/template/components/tools/badge/Badge 2.tsx +28 -0
- package/template/components/tools/badge/Badge.tsx +28 -28
- package/template/components/tools/dropdown/drop.module.css +196 -0
- package/template/components/tools/dropdown/drop.tsx +568 -0
- package/template/components/tools/easing.ts +118 -0
- package/template/components/tools/flubber.d 2.ts +7 -0
- package/template/components/tools/flubber.d.ts +6 -6
- package/template/components/tools/footer/Footer.tsx +20 -0
- package/template/components/tools/input/Input 2.tsx +99 -0
- package/template/components/tools/input/Input.module 2.css +45 -0
- package/template/components/tools/input/Input.module.css +45 -0
- package/template/components/tools/input/Input.tsx +99 -0
- package/template/components/tools/label/Icon 2.tsx +39 -0
- package/template/components/tools/label/Icon.tsx +39 -0
- package/template/components/tools/label/Label 2.tsx +150 -0
- package/template/components/tools/label/Label.tsx +156 -0
- package/template/components/tools/mobileViewProvider 2.tsx +53 -0
- package/template/components/tools/mobileViewProvider.tsx +53 -58
- package/template/components/tools/ripple/ripple 2.css +38 -0
- package/template/components/tools/ripple/ripple 2.tsx +78 -0
- package/template/components/tools/ripple/ripple.css +38 -41
- package/template/components/tools/ripple/ripple.tsx +78 -53
- package/template/components/tools/shape/{ShapeMorph.tsx → Shape 2.tsx } +313 -190
- package/template/components/tools/shape/Shape.module 2.css +21 -0
- package/template/components/tools/shape/Shape.module.css +25 -0
- package/template/components/tools/shape/Shape.tsx +316 -0
- package/template/components/tools/shape/ShapeLoader 2.tsx +104 -0
- package/template/components/tools/shape/ShapeLoader.tsx +104 -101
- package/template/components/tools/shape/ShapePaths 2.ts +116 -0
- package/template/components/tools/shape/ShapePaths.ts +116 -116
- package/template/components/{layout/switch/Switch.tsx → tools/switch/Switch 2.tsx } +48 -48
- package/template/components/{layout/switch/Switch.module.css → tools/switch/Switch.module 2.css } +102 -102
- package/template/components/tools/switch/Switch.module.css +102 -102
- package/template/components/tools/switch/Switch.tsx +48 -48
- package/template/components/tools/useBodyScrollLock 2.tsx +27 -0
- package/template/components/tools/useBodyScrollLock.tsx +27 -27
- package/template/components/tools/useImageColor 2.ts +47 -0
- package/template/components/tools/useImageColor.ts +47 -47
- package/template/components/tools/useNavSheetDrag 2.tsx +196 -0
- package/template/components/tools/useNavSheetDrag.tsx +196 -196
- package/template/public/fonts/material-symbols/MaterialSymbolsRounded-VariableFont_FILL,GRAD,opsz,wght 2.woff2 +0 -0
- package/template/components/layout/accordion/Accordion.module.css +0 -149
- package/template/components/layout/accordion/Accordion.tsx +0 -114
- package/template/components/layout/button/button.tsx +0 -128
- package/template/components/layout/button/drop.module.css +0 -229
- package/template/components/layout/button/drop.tsx +0 -516
- package/template/components/layout/hero/v1/heroSlideshow.module.css +0 -107
- package/template/components/layout/hero/v1/heroSlideshow.tsx +0 -233
- package/template/components/layout/modals/accessibilityModal/AccessibilityModal.module.css +0 -126
- package/template/components/layout/modals/accessibilityModal/AccessibilityModal.tsx +0 -290
- package/template/components/layout/modals/alert/Alert.tsx +0 -188
- package/template/components/layout/navigation/MioCard.module.css +0 -88
- package/template/components/layout/navigation/MioCard.tsx +0 -266
- package/template/components/tools/ProgressiveBlur.tsx +0 -73
- package/template/components/tools/icon/SocialIcons.tsx +0 -88
- package/template/components/tools/icon/icon.module.css +0 -0
- package/template/components/tools/icon/icon.tsx +0 -125
- package/template/components/tools/react-bits/ClickSpark.tsx +0 -177
- package/template/components/tools/react-bits/FadeContent.tsx +0 -90
- package/template/components/tools/react-bits/GlareHover.tsx +0 -73
- package/template/components/tools/react-bits/GradientText/GradientText.css +0 -70
- package/template/components/tools/react-bits/GradientText/GradientText.tsx +0 -44
- package/template/components/tools/react-bits/Magnet.tsx +0 -118
- package/template/components/tools/react-bits/Stepper/Stepper.css +0 -134
- package/template/components/tools/react-bits/Stepper/Stepper.tsx +0 -412
|
@@ -0,0 +1,953 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { AnimatePresence, motion, MotionConfig } from "framer-motion";
|
|
3
|
+
import React, {
|
|
4
|
+
createContext,
|
|
5
|
+
useCallback,
|
|
6
|
+
useContext,
|
|
7
|
+
useEffect,
|
|
8
|
+
useId,
|
|
9
|
+
useMemo,
|
|
10
|
+
useRef,
|
|
11
|
+
useState,
|
|
12
|
+
} from "react";
|
|
13
|
+
import { createPortal } from "react-dom";
|
|
14
|
+
import { useBodyScrollLock } from "@/components/tools/useBodyScrollLock";
|
|
15
|
+
import Button, { type ButtonVariant } from "@/components/(buttons)/button";
|
|
16
|
+
import Icon from "@/components/tools/label/Icon";
|
|
17
|
+
import Label from "@/components/tools/label/Label";
|
|
18
|
+
import Shape from "@/components/tools/shape/Shape";
|
|
19
|
+
import { useReduceMotion } from "@/components/tools/ReduceMotionContext";
|
|
20
|
+
import CustomScrollbar from "@/components/tools/CustomScrollbar";
|
|
21
|
+
import { ShapeInput } from "@/components/tools/shape/ShapePaths";
|
|
22
|
+
import ScrollShadow from "@/components/(layout)/scrollShadow/ScrollShadow";
|
|
23
|
+
|
|
24
|
+
export type DrawerVariant =
|
|
25
|
+
| "default"
|
|
26
|
+
| "primary"
|
|
27
|
+
| "secondary"
|
|
28
|
+
| "tertiary"
|
|
29
|
+
| "success"
|
|
30
|
+
| "error";
|
|
31
|
+
export type DrawerSize = "xs" | "sm" | "md" | "lg" | "cover";
|
|
32
|
+
// The edge the panel slides in from and stays flush against.
|
|
33
|
+
export type DrawerPlacement = "left" | "right" | "top" | "bottom";
|
|
34
|
+
|
|
35
|
+
interface DrawerProps {
|
|
36
|
+
isPresented: boolean;
|
|
37
|
+
onOpenChange: (open: boolean) => void;
|
|
38
|
+
isDismissable?: boolean;
|
|
39
|
+
isKeyboardDismissDisabled?: boolean;
|
|
40
|
+
role?: "dialog" | "alertdialog";
|
|
41
|
+
size?: DrawerSize;
|
|
42
|
+
placement?: DrawerPlacement;
|
|
43
|
+
// Tints the backdrop (gradient facing the edge this drawer opens from) and
|
|
44
|
+
// forces the Footer's cancel/confirm button styles (outlined /
|
|
45
|
+
// {variant}-bold) regardless of whatever variant props those buttons carry
|
|
46
|
+
// on the call site — same contract as Modal's variant.
|
|
47
|
+
variant?: DrawerVariant;
|
|
48
|
+
// Insets the panel FLOATING_MARGIN away from the viewport on all four
|
|
49
|
+
// sides and rounds all four corners instead of only the two facing
|
|
50
|
+
// inward. @default false
|
|
51
|
+
floating?: boolean;
|
|
52
|
+
// Same idea as ToastProvider's maxVisibleToasts — once a nesting chain
|
|
53
|
+
// goes deeper than this, the drawers past the frontmost N stop being
|
|
54
|
+
// rendered visible/interactive (their own children keep rendering
|
|
55
|
+
// normally, so a still-deeper drawer under the cap stays open and
|
|
56
|
+
// frontmost). Only meaningful on the root of a chain — every nested
|
|
57
|
+
// Drawer picks up the root's value automatically, so set it there.
|
|
58
|
+
// @default 3
|
|
59
|
+
maxVisibleDrawers?: number;
|
|
60
|
+
style?: React.CSSProperties;
|
|
61
|
+
children?: React.ReactNode;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// Button has no success-bold or default-bold — those two fall back to
|
|
65
|
+
// primary-bold.
|
|
66
|
+
const CONFIRM_BUTTON_VARIANT: Record<DrawerVariant, ButtonVariant> = {
|
|
67
|
+
default: "primary-bold",
|
|
68
|
+
primary: "primary-bold",
|
|
69
|
+
secondary: "secondary-bold",
|
|
70
|
+
tertiary: "tertiary-bold",
|
|
71
|
+
success: "primary-bold",
|
|
72
|
+
error: "error-bold",
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
// Spread before setting `variant` on a cloned Button — Button's own boolean
|
|
76
|
+
// shorthand props (primary, error, outlined, ...) take priority over
|
|
77
|
+
// `variant` internally, so any boolean the call site set has to be cleared
|
|
78
|
+
// first or the forced variant silently loses.
|
|
79
|
+
const RESET_BUTTON_BOOLEAN_VARIANTS: Partial<
|
|
80
|
+
React.ComponentProps<typeof Button>
|
|
81
|
+
> = {
|
|
82
|
+
primary: undefined,
|
|
83
|
+
"primary-bold": undefined,
|
|
84
|
+
secondary: undefined,
|
|
85
|
+
"secondary-bold": undefined,
|
|
86
|
+
tertiary: undefined,
|
|
87
|
+
"tertiary-bold": undefined,
|
|
88
|
+
error: undefined,
|
|
89
|
+
"error-bold": undefined,
|
|
90
|
+
outlined: undefined,
|
|
91
|
+
text: undefined,
|
|
92
|
+
link: undefined,
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
const FOCUSABLE_SELECTOR =
|
|
96
|
+
'a[href], button:not([disabled]), textarea:not([disabled]), input:not([disabled]), select:not([disabled]), [tabindex]:not([tabindex="-1"])';
|
|
97
|
+
|
|
98
|
+
function getFocusable(container: HTMLElement): HTMLElement[] {
|
|
99
|
+
return Array.from(
|
|
100
|
+
container.querySelectorAll<HTMLElement>(FOCUSABLE_SELECTOR),
|
|
101
|
+
).filter((el) => el.offsetParent !== null);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function iconColors(variant: DrawerVariant = "default") {
|
|
105
|
+
if (variant === "default") {
|
|
106
|
+
return {
|
|
107
|
+
bg: "var(--md-sys-color-surface-container-high)",
|
|
108
|
+
fg: "var(--md-sys-color-on-surface-variant)",
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
return {
|
|
112
|
+
bg: `var(--md-sys-color-${variant}-container)`,
|
|
113
|
+
fg: `var(--md-sys-color-on-${variant}-container)`,
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
const SIZE_DIMENSIONS: Record<
|
|
118
|
+
Exclude<DrawerSize, "cover">,
|
|
119
|
+
[number, number]
|
|
120
|
+
> = {
|
|
121
|
+
xs: [280, 360],
|
|
122
|
+
sm: [360, 420],
|
|
123
|
+
md: [400, 520],
|
|
124
|
+
lg: [480, 680],
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
// `axis` is whichever dimension the placement's edge actually constrains —
|
|
128
|
+
// width for a left/right panel, height for a top/bottom one. The other
|
|
129
|
+
// dimension always fills the full viewport edge-to-edge (see surfaceStyle).
|
|
130
|
+
//
|
|
131
|
+
// Both bounds are capped against the viewport via min() rather than used as
|
|
132
|
+
// bare pixel values — SIZE_DIMENSIONS is a fixed, desktop-oriented pixel
|
|
133
|
+
// scale with no awareness of how narrow the actual screen is, so on
|
|
134
|
+
// mobile/tablet an "md" (or even "xs") drawer's own minWidth could
|
|
135
|
+
// otherwise force it wider than the viewport, overflowing off-screen
|
|
136
|
+
// instead of shrinking to fit. Floating additionally loses FLOATING_MARGIN
|
|
137
|
+
// off BOTH sides of this axis (see FLOATING_MARGIN_STYLE — it's uniform on
|
|
138
|
+
// all four sides now), so the subtraction is doubled.
|
|
139
|
+
function sizeStyle(
|
|
140
|
+
size: DrawerSize,
|
|
141
|
+
axis: "width" | "height",
|
|
142
|
+
floating: boolean,
|
|
143
|
+
): React.CSSProperties {
|
|
144
|
+
const viewportUnit = axis === "width" ? "100dvw" : "100dvh";
|
|
145
|
+
const cap = floating
|
|
146
|
+
? `calc(${viewportUnit} - ${FLOATING_MARGIN * 2}px)`
|
|
147
|
+
: viewportUnit;
|
|
148
|
+
if (size === "cover") {
|
|
149
|
+
const coverValue = `min(94${axis === "width" ? "vw" : "dvh"}, ${cap})`;
|
|
150
|
+
return axis === "width"
|
|
151
|
+
? { width: coverValue, maxWidth: coverValue }
|
|
152
|
+
: { height: coverValue, maxHeight: coverValue };
|
|
153
|
+
}
|
|
154
|
+
const [min, max] = SIZE_DIMENSIONS[size];
|
|
155
|
+
return axis === "width"
|
|
156
|
+
? { minWidth: `min(${min}px, ${cap})`, maxWidth: `min(${max}px, ${cap})` }
|
|
157
|
+
: {
|
|
158
|
+
minHeight: `min(${min}px, ${cap})`,
|
|
159
|
+
maxHeight: `min(${max}px, ${cap})`,
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
// Rounded only on the two corners facing into the viewport — the edge
|
|
164
|
+
// touching the viewport boundary stays square, same visual language as a
|
|
165
|
+
// panel that's actually attached to that edge. Floating rounds all four —
|
|
166
|
+
// nothing is flush against anything anymore.
|
|
167
|
+
const CORNER_RADIUS: Record<DrawerPlacement, React.CSSProperties> = {
|
|
168
|
+
right: {
|
|
169
|
+
borderTopLeftRadius: "var(--borderRadius-l)",
|
|
170
|
+
borderBottomLeftRadius: "var(--borderRadius-l)",
|
|
171
|
+
},
|
|
172
|
+
left: {
|
|
173
|
+
borderTopRightRadius: "var(--borderRadius-l)",
|
|
174
|
+
borderBottomRightRadius: "var(--borderRadius-l)",
|
|
175
|
+
},
|
|
176
|
+
top: {
|
|
177
|
+
borderBottomLeftRadius: "var(--borderRadius-l)",
|
|
178
|
+
borderBottomRightRadius: "var(--borderRadius-l)",
|
|
179
|
+
},
|
|
180
|
+
bottom: {
|
|
181
|
+
borderTopLeftRadius: "var(--borderRadius-l)",
|
|
182
|
+
borderTopRightRadius: "var(--borderRadius-l)",
|
|
183
|
+
},
|
|
184
|
+
};
|
|
185
|
+
|
|
186
|
+
const FLOATING_RADIUS: React.CSSProperties = {
|
|
187
|
+
borderRadius: "var(--borderRadius-l)",
|
|
188
|
+
};
|
|
189
|
+
|
|
190
|
+
const FLOATING_MARGIN = 12;
|
|
191
|
+
|
|
192
|
+
// Uniform on all four sides regardless of placement — simpler than trying
|
|
193
|
+
// to reason per-side about which edge is "the one size sizes against", and
|
|
194
|
+
// avoids getting a direction backwards there.
|
|
195
|
+
const FLOATING_MARGIN_STYLE: React.CSSProperties = {
|
|
196
|
+
margin: FLOATING_MARGIN,
|
|
197
|
+
};
|
|
198
|
+
|
|
199
|
+
function isSidewaysPlacement(placement: DrawerPlacement) {
|
|
200
|
+
return placement === "left" || placement === "right";
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
function surfaceStyle(
|
|
204
|
+
placement: DrawerPlacement,
|
|
205
|
+
size: DrawerSize,
|
|
206
|
+
floating: boolean,
|
|
207
|
+
): React.CSSProperties {
|
|
208
|
+
const sideways = isSidewaysPlacement(placement);
|
|
209
|
+
return {
|
|
210
|
+
position: "relative",
|
|
211
|
+
display: "flex",
|
|
212
|
+
flexDirection: "column",
|
|
213
|
+
overflow: "hidden",
|
|
214
|
+
zIndex: 100002,
|
|
215
|
+
boxShadow:
|
|
216
|
+
"0 0 0 1px var(--globalUIBorderCard), 0 1px 3px rgba(0, 0, 0, 0.1), 0 2px 6px rgba(0, 0, 0, 0.1)",
|
|
217
|
+
backgroundColor: "var(--md-sys-color-surface-container-low)",
|
|
218
|
+
// Flush: pin the cross axis to the full viewport dimension explicitly.
|
|
219
|
+
// Floating: leave it to flexbox's own stretch (the overlay's
|
|
220
|
+
// align-items: stretch already does this) minus the margin below —
|
|
221
|
+
// that's what actually produces the inset, not a fixed dvh/dvw size.
|
|
222
|
+
// (This branch can only ever run with floating === false — the ternary
|
|
223
|
+
// that used to live inside it checking `floating` again was dead code.)
|
|
224
|
+
...(!floating &&
|
|
225
|
+
(sideways
|
|
226
|
+
? { height: "100dvh", maxHeight: "100dvh" }
|
|
227
|
+
: { width: "100dvw", maxWidth: "100dvw" })),
|
|
228
|
+
...sizeStyle(size, sideways ? "width" : "height", floating),
|
|
229
|
+
...(floating
|
|
230
|
+
? { ...FLOATING_MARGIN_STYLE, ...FLOATING_RADIUS }
|
|
231
|
+
: CORNER_RADIUS[placement]),
|
|
232
|
+
};
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
function overlayFlexStyle(placement: DrawerPlacement): React.CSSProperties {
|
|
236
|
+
const sideways = isSidewaysPlacement(placement);
|
|
237
|
+
return {
|
|
238
|
+
flexDirection: sideways ? "row" : "column",
|
|
239
|
+
alignItems: "stretch",
|
|
240
|
+
justifyContent:
|
|
241
|
+
placement === "left" || placement === "top" ? "flex-start" : "flex-end",
|
|
242
|
+
};
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
const headerBaseStyle: React.CSSProperties = {
|
|
246
|
+
flexShrink: 0,
|
|
247
|
+
width: "100%",
|
|
248
|
+
display: "flex",
|
|
249
|
+
alignItems: "center",
|
|
250
|
+
justifyContent: "flex-start",
|
|
251
|
+
gap: 0,
|
|
252
|
+
padding: "12px",
|
|
253
|
+
background: "var(--md-sys-color-surface-container-low)",
|
|
254
|
+
};
|
|
255
|
+
|
|
256
|
+
const footerBaseStyle: React.CSSProperties = {
|
|
257
|
+
flexShrink: 0,
|
|
258
|
+
display: "flex",
|
|
259
|
+
alignItems: "center",
|
|
260
|
+
gap: 12,
|
|
261
|
+
justifyContent: "space-between",
|
|
262
|
+
width: "100%",
|
|
263
|
+
padding: "12px",
|
|
264
|
+
background: "var(--md-sys-color-surface-container-low)",
|
|
265
|
+
};
|
|
266
|
+
|
|
267
|
+
// No padding on the scroll root itself — see the identical note on Modal's
|
|
268
|
+
// contentStyle. The shadow overlays are direct children of this element and
|
|
269
|
+
// need to span its full width/height to sit flush against the drawer edges;
|
|
270
|
+
// padding lives on contentInnerStyle instead, one level in.
|
|
271
|
+
const contentStyle: React.CSSProperties = {
|
|
272
|
+
flex: "1 1 auto",
|
|
273
|
+
minHeight: 0,
|
|
274
|
+
padding: 12,
|
|
275
|
+
width: "100%",
|
|
276
|
+
};
|
|
277
|
+
|
|
278
|
+
const contentInnerStyle: React.CSSProperties = {
|
|
279
|
+
display: "flex",
|
|
280
|
+
alignItems: "flex-start",
|
|
281
|
+
justifyContent: "space-between",
|
|
282
|
+
flexDirection: "column",
|
|
283
|
+
borderRadius: "var(--borderRadius-md)",
|
|
284
|
+
gap: 24,
|
|
285
|
+
width: "100%",
|
|
286
|
+
minHeight: "100%",
|
|
287
|
+
backgroundColor: "var(--md-sys-color-surface-container-lowest)",
|
|
288
|
+
color: "var(--md-sys-color-on-surface-variant)",
|
|
289
|
+
padding: "12px",
|
|
290
|
+
};
|
|
291
|
+
|
|
292
|
+
interface DrawerContextValue {
|
|
293
|
+
close: () => void;
|
|
294
|
+
scrollRef: React.RefObject<HTMLDivElement | null>;
|
|
295
|
+
scrolledFromTop: boolean;
|
|
296
|
+
canScrollDown: boolean;
|
|
297
|
+
scrollByPage: () => void;
|
|
298
|
+
variant: DrawerVariant;
|
|
299
|
+
titleId: string;
|
|
300
|
+
descriptionId: string;
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
const DrawerContext = createContext<DrawerContextValue | null>(null);
|
|
304
|
+
|
|
305
|
+
function useDrawerContext(component: string) {
|
|
306
|
+
const ctx = useContext(DrawerContext);
|
|
307
|
+
if (!ctx) {
|
|
308
|
+
throw new Error(`<Drawer.${component}> must be rendered inside <Drawer>`);
|
|
309
|
+
}
|
|
310
|
+
return ctx;
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
// Lets a Drawer opened from inside another Drawer's content notice its
|
|
314
|
+
// parent and nudge it back (scale + push toward its own rounded corners)
|
|
315
|
+
// while it's on top — see the nested-drawers example on the homepage.
|
|
316
|
+
// Deliberately separate from DrawerContext: this one is about the
|
|
317
|
+
// relationship between two Drawer instances, not props Header/Content/Footer
|
|
318
|
+
// need from their own Drawer.
|
|
319
|
+
//
|
|
320
|
+
// `depth` isn't just "is a child open" — it's how many levels of open
|
|
321
|
+
// descendants sit in front of the reporting child (itself included), so a
|
|
322
|
+
// chain of N nested drawers pushes each ancestor back by a multiple of the
|
|
323
|
+
// peek amount, not all to the same spot. Keyed by id (not a plain count)
|
|
324
|
+
// so multiple children reporting at once — e.g. two nested drawers opened
|
|
325
|
+
// side by side from the same parent — resolve to the deepest one instead of
|
|
326
|
+
// clobbering each other.
|
|
327
|
+
interface DrawerNestingContextValue {
|
|
328
|
+
reportDepth: (id: string, depth: number) => void;
|
|
329
|
+
clearDepth: (id: string) => void;
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
const DrawerNestingContext = createContext<DrawerNestingContextValue | null>(
|
|
333
|
+
null,
|
|
334
|
+
);
|
|
335
|
+
|
|
336
|
+
// One shared backdrop per nesting chain, not one per Drawer — otherwise two
|
|
337
|
+
// open drawers stack two independent blurs. Only the root Drawer (the one
|
|
338
|
+
// with no DrawerNestingContext ancestor) actually paints a backdrop and
|
|
339
|
+
// provides this; every nested Drawer just registers its variant/placement
|
|
340
|
+
// into it while open instead of painting its own, and any Drawer nested more
|
|
341
|
+
// than one level deep still reaches the same root provider directly — React
|
|
342
|
+
// context doesn't need each intermediate level to relay it.
|
|
343
|
+
interface DrawerBackdropDescriptor {
|
|
344
|
+
variant: DrawerVariant;
|
|
345
|
+
placement: DrawerPlacement;
|
|
346
|
+
}
|
|
347
|
+
interface DrawerBackdropContextValue {
|
|
348
|
+
register: (id: string, descriptor: DrawerBackdropDescriptor) => void;
|
|
349
|
+
unregister: (id: string) => void;
|
|
350
|
+
// The root's own maxVisibleDrawers prop, carried down the same way — every
|
|
351
|
+
// nested Drawer reads it from here rather than having its own opinion, so
|
|
352
|
+
// the whole chain agrees on one cap.
|
|
353
|
+
maxVisibleDrawers: number;
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
const DrawerBackdropContext = createContext<DrawerBackdropContextValue | null>(
|
|
357
|
+
null,
|
|
358
|
+
);
|
|
359
|
+
|
|
360
|
+
// vaul's own drawer easing — a sharp deceleration that suits a panel
|
|
361
|
+
// physically sliding to a stop against the viewport edge, distinct from
|
|
362
|
+
// Modal's bouncier scale-in.
|
|
363
|
+
const SLIDE_EASE = [0.32, 0.72, 0, 1] as const;
|
|
364
|
+
|
|
365
|
+
// Matches --borderRadius-l (calc(24px * 1.5) in theme.css) — used as the
|
|
366
|
+
// per-level peek offset when a nested drawer is open in front of this one,
|
|
367
|
+
// so the amount an ancestor is pushed back is exactly enough to clear its
|
|
368
|
+
// own rounded corner into view, no more. Hardcoded rather than read via
|
|
369
|
+
// getComputedStyle since it's just a fixed peek amount, not something that
|
|
370
|
+
// needs to track theme changes live.
|
|
371
|
+
const NESTED_PUSH_PX = 16;
|
|
372
|
+
|
|
373
|
+
// Same sign convention as `offscreen` below: left/top slide in from the
|
|
374
|
+
// negative direction, so pushing an ancestor further in that same direction
|
|
375
|
+
// (positive) is what reveals its trailing edge past a narrower/nested child
|
|
376
|
+
// stacked in front of it; right/bottom is the mirror image.
|
|
377
|
+
const PUSH_DIRECTION: Record<DrawerPlacement, 1 | -1> = {
|
|
378
|
+
left: 1,
|
|
379
|
+
top: 1,
|
|
380
|
+
right: -1,
|
|
381
|
+
bottom: -1,
|
|
382
|
+
};
|
|
383
|
+
|
|
384
|
+
function Drawer({
|
|
385
|
+
isPresented,
|
|
386
|
+
onOpenChange,
|
|
387
|
+
isDismissable = false,
|
|
388
|
+
isKeyboardDismissDisabled = false,
|
|
389
|
+
role = "dialog",
|
|
390
|
+
size = "md",
|
|
391
|
+
placement = "right",
|
|
392
|
+
variant = "default",
|
|
393
|
+
floating = false,
|
|
394
|
+
maxVisibleDrawers = 3,
|
|
395
|
+
style,
|
|
396
|
+
children,
|
|
397
|
+
}: DrawerProps) {
|
|
398
|
+
const rm = useReduceMotion();
|
|
399
|
+
useBodyScrollLock(isPresented);
|
|
400
|
+
const [mounted, setMounted] = useState(false);
|
|
401
|
+
// Portal-safety: defer createPortal until after client hydration, which
|
|
402
|
+
// can only be detected post-mount.
|
|
403
|
+
// eslint-disable-next-line react-hooks/set-state-in-effect
|
|
404
|
+
useEffect(() => setMounted(true), []);
|
|
405
|
+
|
|
406
|
+
const titleId = useId();
|
|
407
|
+
const descriptionId = useId();
|
|
408
|
+
|
|
409
|
+
const close = useCallback(() => onOpenChange(false), [onOpenChange]);
|
|
410
|
+
|
|
411
|
+
const surfaceRef = useRef<HTMLDivElement>(null);
|
|
412
|
+
const previouslyFocused = useRef<HTMLElement | null>(null);
|
|
413
|
+
|
|
414
|
+
const handleOverlayClick = useCallback(
|
|
415
|
+
(e: React.MouseEvent) => {
|
|
416
|
+
if (e.target !== e.currentTarget) return;
|
|
417
|
+
if (isDismissable) close();
|
|
418
|
+
},
|
|
419
|
+
[isDismissable, close],
|
|
420
|
+
);
|
|
421
|
+
|
|
422
|
+
const scrollRef = useRef<HTMLDivElement>(null);
|
|
423
|
+
const [canScrollDown, setCanScrollDown] = useState(false);
|
|
424
|
+
const [scrolledFromTop, setScrolledFromTop] = useState(false);
|
|
425
|
+
|
|
426
|
+
const checkScroll = useCallback(() => {
|
|
427
|
+
const el = scrollRef.current;
|
|
428
|
+
if (!el) return;
|
|
429
|
+
setCanScrollDown(el.scrollHeight - el.scrollTop - el.clientHeight > 1);
|
|
430
|
+
setScrolledFromTop(el.scrollTop > 0);
|
|
431
|
+
}, []);
|
|
432
|
+
|
|
433
|
+
useEffect(() => {
|
|
434
|
+
const el = scrollRef.current;
|
|
435
|
+
if (!el || !isPresented) return;
|
|
436
|
+
checkScroll();
|
|
437
|
+
el.addEventListener("scroll", checkScroll, { passive: true });
|
|
438
|
+
window.addEventListener("resize", checkScroll);
|
|
439
|
+
return () => {
|
|
440
|
+
el.removeEventListener("scroll", checkScroll);
|
|
441
|
+
window.removeEventListener("resize", checkScroll);
|
|
442
|
+
};
|
|
443
|
+
}, [checkScroll, isPresented]);
|
|
444
|
+
|
|
445
|
+
// One "page" short of the full viewport height, so the next screen always
|
|
446
|
+
// overlaps the last line or two of what was already visible.
|
|
447
|
+
const scrollByPage = useCallback(() => {
|
|
448
|
+
const el = scrollRef.current;
|
|
449
|
+
if (!el) return;
|
|
450
|
+
el.scrollBy({ top: el.clientHeight - 24, behavior: "smooth" });
|
|
451
|
+
}, []);
|
|
452
|
+
|
|
453
|
+
// Nested drawers: collect how deep the currently-open descendant chain
|
|
454
|
+
// extends below THIS drawer (0 if none open), keyed by each direct
|
|
455
|
+
// child's own id so simultaneous children resolve to the deepest one
|
|
456
|
+
// instead of clobbering each other.
|
|
457
|
+
const [childDepths, setChildDepths] = useState<Record<string, number>>({});
|
|
458
|
+
const reportChildDepth = useCallback((id: string, depth: number) => {
|
|
459
|
+
setChildDepths((prev) =>
|
|
460
|
+
prev[id] === depth ? prev : { ...prev, [id]: depth },
|
|
461
|
+
);
|
|
462
|
+
}, []);
|
|
463
|
+
const clearChildDepth = useCallback((id: string) => {
|
|
464
|
+
setChildDepths((prev) => {
|
|
465
|
+
if (!(id in prev)) return prev;
|
|
466
|
+
const next = { ...prev };
|
|
467
|
+
delete next[id];
|
|
468
|
+
return next;
|
|
469
|
+
});
|
|
470
|
+
}, []);
|
|
471
|
+
const nestingContextValue = useMemo<DrawerNestingContextValue>(
|
|
472
|
+
() => ({ reportDepth: reportChildDepth, clearDepth: clearChildDepth }),
|
|
473
|
+
[reportChildDepth, clearChildDepth],
|
|
474
|
+
);
|
|
475
|
+
// How many descendants below me are currently open — this is the push
|
|
476
|
+
// multiplier for my OWN surface transform, see PUSH_DIRECTION below.
|
|
477
|
+
const openDescendantDepth = Object.values(childDepths).reduce(
|
|
478
|
+
(max, d) => Math.max(max, d),
|
|
479
|
+
0,
|
|
480
|
+
);
|
|
481
|
+
|
|
482
|
+
// If this Drawer was itself opened from inside another Drawer's content,
|
|
483
|
+
// report my own total depth (myself + whatever's open below me) up to
|
|
484
|
+
// that parent so it can push back by the right multiple in turn. Absence
|
|
485
|
+
// of a parent is also how a Drawer knows it's the root of its own nesting
|
|
486
|
+
// chain (see isRoot below).
|
|
487
|
+
const parentNesting = useContext(DrawerNestingContext);
|
|
488
|
+
const nestingId = useId();
|
|
489
|
+
const myOpenDepth = isPresented ? 1 + openDescendantDepth : 0;
|
|
490
|
+
useEffect(() => {
|
|
491
|
+
if (!parentNesting || myOpenDepth <= 0) return;
|
|
492
|
+
parentNesting.reportDepth(nestingId, myOpenDepth);
|
|
493
|
+
return () => parentNesting.clearDepth(nestingId);
|
|
494
|
+
}, [parentNesting, nestingId, myOpenDepth]);
|
|
495
|
+
|
|
496
|
+
// Only the root of a nesting chain paints the shared backdrop; it stacks
|
|
497
|
+
// every currently-open descendant's variant/placement (pushed by their own
|
|
498
|
+
// registration effect below) and paints whichever registered last — i.e.
|
|
499
|
+
// the frontmost open drawer — falling back to its own variant/placement
|
|
500
|
+
// once the stack empties back out.
|
|
501
|
+
const isRoot = !parentNesting;
|
|
502
|
+
const [backdropStack, setBackdropStack] = useState<
|
|
503
|
+
Array<DrawerBackdropDescriptor & { id: string }>
|
|
504
|
+
>([]);
|
|
505
|
+
const registerBackdrop = useCallback(
|
|
506
|
+
(id: string, descriptor: DrawerBackdropDescriptor) => {
|
|
507
|
+
setBackdropStack((prev) => [
|
|
508
|
+
...prev.filter((d) => d.id !== id),
|
|
509
|
+
{ id, ...descriptor },
|
|
510
|
+
]);
|
|
511
|
+
},
|
|
512
|
+
[],
|
|
513
|
+
);
|
|
514
|
+
const unregisterBackdrop = useCallback((id: string) => {
|
|
515
|
+
setBackdropStack((prev) => prev.filter((d) => d.id !== id));
|
|
516
|
+
}, []);
|
|
517
|
+
const backdropContextValue = useMemo<DrawerBackdropContextValue>(
|
|
518
|
+
() => ({
|
|
519
|
+
register: registerBackdrop,
|
|
520
|
+
unregister: unregisterBackdrop,
|
|
521
|
+
maxVisibleDrawers,
|
|
522
|
+
}),
|
|
523
|
+
[registerBackdrop, unregisterBackdrop, maxVisibleDrawers],
|
|
524
|
+
);
|
|
525
|
+
|
|
526
|
+
// Non-root: register this drawer's own variant/placement into the root's
|
|
527
|
+
// shared stack for as long as it's open, instead of painting a backdrop.
|
|
528
|
+
const backdropCtx = useContext(DrawerBackdropContext);
|
|
529
|
+
const backdropId = useId();
|
|
530
|
+
useEffect(() => {
|
|
531
|
+
if (isRoot || !backdropCtx || !isPresented) return;
|
|
532
|
+
backdropCtx.register(backdropId, { variant, placement });
|
|
533
|
+
return () => backdropCtx.unregister(backdropId);
|
|
534
|
+
}, [isRoot, backdropCtx, isPresented, backdropId, variant, placement]);
|
|
535
|
+
|
|
536
|
+
// The whole chain shares one cap — root uses its own prop, every nested
|
|
537
|
+
// Drawer reads the same value back from the root's context instead of
|
|
538
|
+
// having its own opinion. openDescendantDepth already IS "how many open
|
|
539
|
+
// drawers are stacked in front of me" (0 = frontmost), so the cap is just
|
|
540
|
+
// "am I further back than that many."
|
|
541
|
+
const effectiveMaxVisible = isRoot
|
|
542
|
+
? maxVisibleDrawers
|
|
543
|
+
: (backdropCtx?.maxVisibleDrawers ?? maxVisibleDrawers);
|
|
544
|
+
const isBeyondVisibleCap = openDescendantDepth >= effectiveMaxVisible;
|
|
545
|
+
|
|
546
|
+
// Focus enters the drawer on open and returns to whatever triggered it on
|
|
547
|
+
// close — skipped while beyond the visible cap, since there's nothing on
|
|
548
|
+
// screen to focus (or trap Tab/Escape inside, below) until it re-enters
|
|
549
|
+
// the frontmost N.
|
|
550
|
+
useEffect(() => {
|
|
551
|
+
if (!isPresented || isBeyondVisibleCap) return;
|
|
552
|
+
previouslyFocused.current = document.activeElement as HTMLElement | null;
|
|
553
|
+
const raf = requestAnimationFrame(() => {
|
|
554
|
+
const el = surfaceRef.current;
|
|
555
|
+
if (!el) return;
|
|
556
|
+
const focusable = getFocusable(el);
|
|
557
|
+
(focusable[0] ?? el).focus();
|
|
558
|
+
});
|
|
559
|
+
return () => {
|
|
560
|
+
cancelAnimationFrame(raf);
|
|
561
|
+
previouslyFocused.current?.focus?.();
|
|
562
|
+
};
|
|
563
|
+
}, [isPresented, isBeyondVisibleCap]);
|
|
564
|
+
|
|
565
|
+
// Escape dismisses (unless disabled); Tab is trapped inside the drawer.
|
|
566
|
+
useEffect(() => {
|
|
567
|
+
if (!isPresented || isBeyondVisibleCap) return;
|
|
568
|
+
const handleKeyDown = (e: KeyboardEvent) => {
|
|
569
|
+
if (e.key === "Escape") {
|
|
570
|
+
if (!isKeyboardDismissDisabled) {
|
|
571
|
+
e.preventDefault();
|
|
572
|
+
close();
|
|
573
|
+
}
|
|
574
|
+
return;
|
|
575
|
+
}
|
|
576
|
+
if (e.key !== "Tab") return;
|
|
577
|
+
const el = surfaceRef.current;
|
|
578
|
+
if (!el) return;
|
|
579
|
+
const focusable = getFocusable(el);
|
|
580
|
+
if (focusable.length === 0) {
|
|
581
|
+
e.preventDefault();
|
|
582
|
+
return;
|
|
583
|
+
}
|
|
584
|
+
const first = focusable[0];
|
|
585
|
+
const last = focusable[focusable.length - 1];
|
|
586
|
+
const active = document.activeElement;
|
|
587
|
+
if (e.shiftKey) {
|
|
588
|
+
if (active === first || !el.contains(active)) {
|
|
589
|
+
e.preventDefault();
|
|
590
|
+
last.focus();
|
|
591
|
+
}
|
|
592
|
+
} else if (active === last || !el.contains(active)) {
|
|
593
|
+
e.preventDefault();
|
|
594
|
+
first.focus();
|
|
595
|
+
}
|
|
596
|
+
};
|
|
597
|
+
window.addEventListener("keydown", handleKeyDown);
|
|
598
|
+
return () => window.removeEventListener("keydown", handleKeyDown);
|
|
599
|
+
}, [isPresented, isBeyondVisibleCap, isKeyboardDismissDisabled, close]);
|
|
600
|
+
|
|
601
|
+
const overlayStyle = useMemo<React.CSSProperties>(() => {
|
|
602
|
+
const base: React.CSSProperties = {
|
|
603
|
+
position: "fixed",
|
|
604
|
+
top: 0,
|
|
605
|
+
left: 0,
|
|
606
|
+
width: "100%",
|
|
607
|
+
height: "100%",
|
|
608
|
+
display: "flex",
|
|
609
|
+
...overlayFlexStyle(placement),
|
|
610
|
+
zIndex: 100000,
|
|
611
|
+
};
|
|
612
|
+
// Non-root drawers are just a transparent click-catcher for their own
|
|
613
|
+
// isDismissable/positioning — the root's backdrop already covers the
|
|
614
|
+
// whole viewport, painting a second one would double the blur.
|
|
615
|
+
if (!isRoot) return base;
|
|
616
|
+
const active: DrawerBackdropDescriptor = backdropStack[
|
|
617
|
+
backdropStack.length - 1
|
|
618
|
+
] ?? { variant, placement };
|
|
619
|
+
return {
|
|
620
|
+
...base,
|
|
621
|
+
background:
|
|
622
|
+
active.variant === "default"
|
|
623
|
+
? "rgba(0, 0, 0, 0.4)"
|
|
624
|
+
: `linear-gradient(to ${active.placement}, rgba(0, 0, 0, 0.4), color-mix(in srgb, var(--md-sys-color-${active.variant}-container) 50%, transparent))`,
|
|
625
|
+
backdropFilter: "blur(12px)",
|
|
626
|
+
WebkitBackdropFilter: "blur(12px)",
|
|
627
|
+
};
|
|
628
|
+
}, [placement, isRoot, backdropStack, variant]);
|
|
629
|
+
|
|
630
|
+
const contextValue = useMemo<DrawerContextValue>(
|
|
631
|
+
() => ({
|
|
632
|
+
close,
|
|
633
|
+
scrollRef,
|
|
634
|
+
scrolledFromTop,
|
|
635
|
+
canScrollDown,
|
|
636
|
+
scrollByPage,
|
|
637
|
+
variant,
|
|
638
|
+
titleId,
|
|
639
|
+
descriptionId,
|
|
640
|
+
}),
|
|
641
|
+
[
|
|
642
|
+
close,
|
|
643
|
+
scrollRef,
|
|
644
|
+
scrolledFromTop,
|
|
645
|
+
canScrollDown,
|
|
646
|
+
scrollByPage,
|
|
647
|
+
variant,
|
|
648
|
+
titleId,
|
|
649
|
+
descriptionId,
|
|
650
|
+
],
|
|
651
|
+
);
|
|
652
|
+
const childArray = React.Children.toArray(children);
|
|
653
|
+
const headerChild = childArray.find(
|
|
654
|
+
(child) => React.isValidElement(child) && child.type === DrawerHeader,
|
|
655
|
+
);
|
|
656
|
+
const footerChild = childArray.find(
|
|
657
|
+
(child) => React.isValidElement(child) && child.type === DrawerFooter,
|
|
658
|
+
);
|
|
659
|
+
const middleChildren = childArray.filter(
|
|
660
|
+
(child) =>
|
|
661
|
+
!(
|
|
662
|
+
React.isValidElement(child) &&
|
|
663
|
+
(child.type === DrawerHeader || child.type === DrawerFooter)
|
|
664
|
+
),
|
|
665
|
+
);
|
|
666
|
+
|
|
667
|
+
const axis: "x" | "y" = isSidewaysPlacement(placement) ? "x" : "y";
|
|
668
|
+
const offscreen =
|
|
669
|
+
placement === "left" || placement === "top" ? "-100%" : "100%";
|
|
670
|
+
|
|
671
|
+
const panelBody = (
|
|
672
|
+
<DrawerNestingContext.Provider value={nestingContextValue}>
|
|
673
|
+
<DrawerContext.Provider value={contextValue}>
|
|
674
|
+
{headerChild}
|
|
675
|
+
<div
|
|
676
|
+
style={{
|
|
677
|
+
position: "relative",
|
|
678
|
+
display: "flex",
|
|
679
|
+
flexDirection: "column",
|
|
680
|
+
flex: "1 1 auto",
|
|
681
|
+
minHeight: 0,
|
|
682
|
+
width: "100%",
|
|
683
|
+
}}
|
|
684
|
+
>
|
|
685
|
+
{middleChildren}
|
|
686
|
+
<CustomScrollbar targetRef={scrollRef} />
|
|
687
|
+
</div>
|
|
688
|
+
{footerChild}
|
|
689
|
+
</DrawerContext.Provider>
|
|
690
|
+
</DrawerNestingContext.Provider>
|
|
691
|
+
);
|
|
692
|
+
|
|
693
|
+
if (!mounted) return null;
|
|
694
|
+
return createPortal(
|
|
695
|
+
<MotionConfig reducedMotion={rm ? "always" : "never"}>
|
|
696
|
+
<AnimatePresence>
|
|
697
|
+
{isPresented && (
|
|
698
|
+
<motion.div
|
|
699
|
+
initial={{ opacity: 0 }}
|
|
700
|
+
animate={{ opacity: 1 }}
|
|
701
|
+
exit={{ opacity: 0 }}
|
|
702
|
+
style={overlayStyle}
|
|
703
|
+
onClick={handleOverlayClick}
|
|
704
|
+
>
|
|
705
|
+
<motion.div
|
|
706
|
+
ref={surfaceRef}
|
|
707
|
+
role={role}
|
|
708
|
+
aria-modal="true"
|
|
709
|
+
aria-labelledby={titleId}
|
|
710
|
+
aria-describedby={descriptionId}
|
|
711
|
+
aria-hidden={isBeyondVisibleCap || undefined}
|
|
712
|
+
tabIndex={-1}
|
|
713
|
+
initial={{ [axis]: offscreen }}
|
|
714
|
+
animate={{
|
|
715
|
+
// Beyond the cap: park at the cap edge's push amount rather
|
|
716
|
+
// than continuing to march back per extra level — it's
|
|
717
|
+
// invisible either way, no reason to keep translating it.
|
|
718
|
+
[axis]:
|
|
719
|
+
PUSH_DIRECTION[placement] *
|
|
720
|
+
Math.min(openDescendantDepth, effectiveMaxVisible) *
|
|
721
|
+
NESTED_PUSH_PX,
|
|
722
|
+
scale: 1 - Math.min(openDescendantDepth, 4) * 0.03,
|
|
723
|
+
opacity: isBeyondVisibleCap
|
|
724
|
+
? 0
|
|
725
|
+
: 1 - Math.min(openDescendantDepth, 4) * 0.06,
|
|
726
|
+
}}
|
|
727
|
+
exit={{ [axis]: offscreen }}
|
|
728
|
+
transition={{ duration: 0.35, ease: SLIDE_EASE }}
|
|
729
|
+
style={{
|
|
730
|
+
...surfaceStyle(placement, size, floating),
|
|
731
|
+
...style,
|
|
732
|
+
pointerEvents: isBeyondVisibleCap ? "none" : undefined,
|
|
733
|
+
}}
|
|
734
|
+
className="a11y-modal-surface"
|
|
735
|
+
>
|
|
736
|
+
{/* Only the root of a nesting chain provides the shared
|
|
737
|
+
backdrop registry — a nested Drawer just consumes the
|
|
738
|
+
same provider from its ancestor untouched, no relay
|
|
739
|
+
needed at each level. */}
|
|
740
|
+
{isRoot ? (
|
|
741
|
+
<DrawerBackdropContext.Provider value={backdropContextValue}>
|
|
742
|
+
{panelBody}
|
|
743
|
+
</DrawerBackdropContext.Provider>
|
|
744
|
+
) : (
|
|
745
|
+
panelBody
|
|
746
|
+
)}
|
|
747
|
+
</motion.div>
|
|
748
|
+
</motion.div>
|
|
749
|
+
)}
|
|
750
|
+
</AnimatePresence>
|
|
751
|
+
</MotionConfig>,
|
|
752
|
+
document.body,
|
|
753
|
+
);
|
|
754
|
+
}
|
|
755
|
+
|
|
756
|
+
type DrawerHeaderProps = {
|
|
757
|
+
title: string;
|
|
758
|
+
withIcon?: {
|
|
759
|
+
systemName: string;
|
|
760
|
+
variant?: DrawerVariant;
|
|
761
|
+
shapePath: ShapeInput;
|
|
762
|
+
};
|
|
763
|
+
closeTrigger?: boolean;
|
|
764
|
+
style?: React.CSSProperties;
|
|
765
|
+
};
|
|
766
|
+
|
|
767
|
+
function DrawerHeader({
|
|
768
|
+
title,
|
|
769
|
+
withIcon,
|
|
770
|
+
closeTrigger,
|
|
771
|
+
style,
|
|
772
|
+
}: DrawerHeaderProps) {
|
|
773
|
+
const { scrolledFromTop, titleId, close, variant } =
|
|
774
|
+
useDrawerContext("Header");
|
|
775
|
+
// Falls back to the Drawer's own variant when withIcon doesn't specify
|
|
776
|
+
// one of its own — otherwise iconColors' internal "default" fallback
|
|
777
|
+
// would silently ignore whatever variant the Drawer itself is tinted
|
|
778
|
+
// with, and the icon would stay neutral gray no matter what.
|
|
779
|
+
const colors = withIcon ? iconColors(withIcon.variant ?? variant) : null;
|
|
780
|
+
return (
|
|
781
|
+
<div
|
|
782
|
+
style={{
|
|
783
|
+
...headerBaseStyle,
|
|
784
|
+
borderBottom: `1px solid ${scrolledFromTop ? "var(--globalUIBorder)" : "transparent"}`,
|
|
785
|
+
transition: "border-color var(--transition-expressive-color)",
|
|
786
|
+
...style,
|
|
787
|
+
}}
|
|
788
|
+
>
|
|
789
|
+
{withIcon && colors && (
|
|
790
|
+
<Shape
|
|
791
|
+
systemName={withIcon.systemName}
|
|
792
|
+
shapePath={withIcon.shapePath}
|
|
793
|
+
background={colors.bg}
|
|
794
|
+
color={colors.fg}
|
|
795
|
+
size="sm"
|
|
796
|
+
isHovered={false}
|
|
797
|
+
interactive={false}
|
|
798
|
+
/>
|
|
799
|
+
)}
|
|
800
|
+
<h4
|
|
801
|
+
id={titleId}
|
|
802
|
+
style={{
|
|
803
|
+
flex: 1,
|
|
804
|
+
margin: 0,
|
|
805
|
+
padding: "0 12px",
|
|
806
|
+
fontWeight: 500,
|
|
807
|
+
color:
|
|
808
|
+
variant === "default"
|
|
809
|
+
? "var(--md-sys-color-on-surface)"
|
|
810
|
+
: `var(--md-sys-color-on-${variant}-container)`,
|
|
811
|
+
}}
|
|
812
|
+
>
|
|
813
|
+
{title}
|
|
814
|
+
</h4>
|
|
815
|
+
{closeTrigger && (
|
|
816
|
+
<Button
|
|
817
|
+
text
|
|
818
|
+
onClick={close}
|
|
819
|
+
aria-label="Close"
|
|
820
|
+
style={{
|
|
821
|
+
marginLeft: "auto",
|
|
822
|
+
minWidth: 44,
|
|
823
|
+
color: "var(--md-sys-color-secondary)",
|
|
824
|
+
}}
|
|
825
|
+
>
|
|
826
|
+
<Icon systemName="close" />
|
|
827
|
+
</Button>
|
|
828
|
+
)}
|
|
829
|
+
</div>
|
|
830
|
+
);
|
|
831
|
+
}
|
|
832
|
+
|
|
833
|
+
type DrawerContentProps = {
|
|
834
|
+
children?: React.ReactNode;
|
|
835
|
+
style?: React.CSSProperties;
|
|
836
|
+
};
|
|
837
|
+
|
|
838
|
+
function DrawerContent({ children, style }: DrawerContentProps) {
|
|
839
|
+
const { descriptionId, scrollRef } = useDrawerContext("Content");
|
|
840
|
+
return (
|
|
841
|
+
<ScrollShadow ref={scrollRef} variant="low" style={contentStyle}>
|
|
842
|
+
<div id={descriptionId} style={{ ...contentInnerStyle, ...style }}>
|
|
843
|
+
{children}
|
|
844
|
+
</div>
|
|
845
|
+
</ScrollShadow>
|
|
846
|
+
);
|
|
847
|
+
}
|
|
848
|
+
|
|
849
|
+
type DrawerFooterProps = {
|
|
850
|
+
children?: React.ReactNode;
|
|
851
|
+
style?: React.CSSProperties;
|
|
852
|
+
};
|
|
853
|
+
|
|
854
|
+
// Same contract as Modal.Footer — scans children for plain <Button
|
|
855
|
+
// value="cancel"> / <Button value="confirm">. Cancel is always forced to
|
|
856
|
+
// "outlined" and always closes after your own onClick runs. Confirm is
|
|
857
|
+
// always forced to {variant}-bold and only gets intercepted while there's
|
|
858
|
+
// more to scroll (paging down instead of firing your handler); once
|
|
859
|
+
// scrolled to the end your onClick runs unmodified. data-continue-label
|
|
860
|
+
// swaps its label while that's active.
|
|
861
|
+
function DrawerFooter({ children, style }: DrawerFooterProps) {
|
|
862
|
+
const { canScrollDown, scrollByPage, close, variant } =
|
|
863
|
+
useDrawerContext("Footer");
|
|
864
|
+
|
|
865
|
+
const processedChildren = React.Children.map(children, (child) => {
|
|
866
|
+
if (!React.isValidElement<React.ComponentProps<typeof Button>>(child)) {
|
|
867
|
+
return child;
|
|
868
|
+
}
|
|
869
|
+
|
|
870
|
+
if (child.props.value === "cancel") {
|
|
871
|
+
return React.cloneElement(child, {
|
|
872
|
+
...RESET_BUTTON_BOOLEAN_VARIANTS,
|
|
873
|
+
variant: "outlined",
|
|
874
|
+
onClick: (e: React.MouseEvent<HTMLButtonElement>) => {
|
|
875
|
+
child.props.onClick?.(e);
|
|
876
|
+
close();
|
|
877
|
+
},
|
|
878
|
+
});
|
|
879
|
+
}
|
|
880
|
+
|
|
881
|
+
if (child.props.value === "confirm") {
|
|
882
|
+
const confirmVariant = CONFIRM_BUTTON_VARIANT[variant];
|
|
883
|
+
if (!canScrollDown) {
|
|
884
|
+
return React.cloneElement(child, {
|
|
885
|
+
...RESET_BUTTON_BOOLEAN_VARIANTS,
|
|
886
|
+
variant: confirmVariant,
|
|
887
|
+
});
|
|
888
|
+
}
|
|
889
|
+
const continueLabel = (child.props as Record<string, unknown>)[
|
|
890
|
+
"data-continue-label"
|
|
891
|
+
] as string | undefined;
|
|
892
|
+
return React.cloneElement(child, {
|
|
893
|
+
...RESET_BUTTON_BOOLEAN_VARIANTS,
|
|
894
|
+
variant: confirmVariant,
|
|
895
|
+
onClick: () => scrollByPage(),
|
|
896
|
+
children: continueLabel ? (
|
|
897
|
+
<Label label={continueLabel} />
|
|
898
|
+
) : (
|
|
899
|
+
child.props.children
|
|
900
|
+
),
|
|
901
|
+
});
|
|
902
|
+
}
|
|
903
|
+
|
|
904
|
+
return child;
|
|
905
|
+
});
|
|
906
|
+
|
|
907
|
+
return (
|
|
908
|
+
<div
|
|
909
|
+
style={{
|
|
910
|
+
...footerBaseStyle,
|
|
911
|
+
borderTop: `1px solid ${canScrollDown ? "var(--globalUIBorder)" : "transparent"}`,
|
|
912
|
+
transition: "border-color var(--transition-expressive-color)",
|
|
913
|
+
...style,
|
|
914
|
+
}}
|
|
915
|
+
>
|
|
916
|
+
{processedChildren}
|
|
917
|
+
</div>
|
|
918
|
+
);
|
|
919
|
+
}
|
|
920
|
+
|
|
921
|
+
export default Object.assign(Drawer, {
|
|
922
|
+
Header: DrawerHeader,
|
|
923
|
+
Content: DrawerContent,
|
|
924
|
+
Footer: DrawerFooter,
|
|
925
|
+
});
|
|
926
|
+
|
|
927
|
+
/* Example usage:
|
|
928
|
+
|
|
929
|
+
<Drawer isPresented={open} onOpenChange={setOpen} placement="right" size="sm">
|
|
930
|
+
<Drawer.Header title="Notifications" closeTrigger />
|
|
931
|
+
<Drawer.Content>
|
|
932
|
+
<p>Panel content.</p>
|
|
933
|
+
</Drawer.Content>
|
|
934
|
+
<Drawer.Footer>
|
|
935
|
+
<Button value="cancel" text><Label label="Close" /></Button>
|
|
936
|
+
</Drawer.Footer>
|
|
937
|
+
</Drawer>
|
|
938
|
+
|
|
939
|
+
// Nested — open this second Drawer from a button rendered inside the first
|
|
940
|
+
// Drawer's own children; the parent automatically scales/dims behind it
|
|
941
|
+
// while it's open, no extra wiring needed.
|
|
942
|
+
<Drawer isPresented={outerOpen} onOpenChange={setOuterOpen} placement="left">
|
|
943
|
+
<Drawer.Header title="Outer" closeTrigger />
|
|
944
|
+
<Drawer.Content>
|
|
945
|
+
<Button onClick={() => setInnerOpen(true)}><Label label="Open nested drawer" /></Button>
|
|
946
|
+
<Drawer isPresented={innerOpen} onOpenChange={setInnerOpen} placement="left" size="sm">
|
|
947
|
+
<Drawer.Header title="Inner" closeTrigger />
|
|
948
|
+
<Drawer.Content><p>Nested content.</p></Drawer.Content>
|
|
949
|
+
</Drawer>
|
|
950
|
+
</Drawer.Content>
|
|
951
|
+
</Drawer>
|
|
952
|
+
|
|
953
|
+
*/
|