@bonprix-ds/react-bottom-sheet 1.3.0 → 1.3.2
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/dist/src/BottomSheetRoot.d.ts +1 -1
- package/dist/src/BottomSheetRoot.js +6 -7
- package/dist/src/BottomSheetRoot.js.map +1 -1
- package/dist/src/useKeepScrollPositionOnLeave.d.ts +2 -0
- package/dist/src/useKeepScrollPositionOnLeave.js +23 -0
- package/dist/src/useKeepScrollPositionOnLeave.js.map +1 -0
- package/package.json +7 -7
- package/dist/src/useKeepScrollPositionOnPageHide.d.ts +0 -2
- package/dist/src/useKeepScrollPositionOnPageHide.js +0 -25
- package/dist/src/useKeepScrollPositionOnPageHide.js.map +0 -1
|
@@ -62,7 +62,7 @@ export interface SheetContentProps {
|
|
|
62
62
|
export interface SheetButtonBaseProps {
|
|
63
63
|
/** The label of the button. */
|
|
64
64
|
children: string;
|
|
65
|
-
/** If used, an icon will be additionally shown. The value is either a name of one icon from our icon set or an inline `<svg>` element. */
|
|
65
|
+
/** If used, an icon will be additionally shown. The value is either an icon component from `bonprix-design-system/icons`, such as `<CartIcon />`, a name of one icon from our icon set or an inline `<svg>` element. */
|
|
66
66
|
icon?: IconOrElement;
|
|
67
67
|
/** The position of the icon relative to the content. */
|
|
68
68
|
iconPosition?: "start" | "end";
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
import e from "./BottomSheetContext.js";
|
|
3
3
|
import t from "./useConsumeBottomSheet.js";
|
|
4
4
|
import n from "./useCreateBottomSheet.js";
|
|
5
|
-
import r from "./
|
|
5
|
+
import r from "./useKeepScrollPositionOnLeave.js";
|
|
6
6
|
import { useEffect as i, useRef as a } from "react";
|
|
7
7
|
import * as o from "@radix-ui/react-dialog";
|
|
8
8
|
import { Sheet as s } from "react-modal-sheet";
|
|
@@ -39,12 +39,10 @@ var x = c({
|
|
|
39
39
|
} }
|
|
40
40
|
}), S = ({ children: t, open: c, isOpen: l, onClose: u, onBack: d, zIndex: f = 9999, ref: p, closeButtonLabel: S = "Schließen", backButtonLabel: C = "Zurück" }) => {
|
|
41
41
|
l !== void 0 && v("is-open-prop");
|
|
42
|
-
let w = _(), { root: T, container: E, dragHandleContainer: D, dragHandle: O, content: k, contentWrapper: A, backdrop: j } = x(), { isShown: M, scrollRef: N, scrollPosition: P } = n({ open: c ?? l ?? !1 }), F = P === "middle" || P === "bottom", { innerOpen: I } = g(M);
|
|
43
|
-
r(I);
|
|
44
|
-
let L = a(null), { returnFocus: R } = h(I, L);
|
|
42
|
+
let w = _(), { root: T, container: E, dragHandleContainer: D, dragHandle: O, content: k, contentWrapper: A, backdrop: j } = x(), { isShown: M, scrollRef: N, scrollPosition: P } = n({ open: c ?? l ?? !1 }), F = P === "middle" || P === "bottom", { innerOpen: I } = g(M), L = r(I), R = a(null), { returnFocus: z } = h(I, R);
|
|
45
43
|
return i(() => {
|
|
46
|
-
I ||
|
|
47
|
-
}, [I,
|
|
44
|
+
I || z();
|
|
45
|
+
}, [I, z]), /* @__PURE__ */ y(o.Root, {
|
|
48
46
|
open: I,
|
|
49
47
|
onOpenChange: (e) => {
|
|
50
48
|
e || u();
|
|
@@ -56,6 +54,7 @@ var x = c({
|
|
|
56
54
|
detent: "content",
|
|
57
55
|
"data-bpds": "bottom-sheet",
|
|
58
56
|
prefersReducedMotion: w,
|
|
57
|
+
disableScrollLocking: L,
|
|
59
58
|
style: {
|
|
60
59
|
pointerEvents: void 0,
|
|
61
60
|
visibility: I ? "visible" : "hidden",
|
|
@@ -63,7 +62,7 @@ var x = c({
|
|
|
63
62
|
},
|
|
64
63
|
avoidKeyboard: !1,
|
|
65
64
|
children: [/* @__PURE__ */ y(o.Content, {
|
|
66
|
-
ref:
|
|
65
|
+
ref: R,
|
|
67
66
|
forceMount: !0,
|
|
68
67
|
asChild: !0,
|
|
69
68
|
"aria-modal": !0,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"BottomSheetRoot.js","names":["BottomSheetContext","useConsumeBottomSheet","useCreateBottomSheet","useKeepScrollPositionOnPageHide","DialogPrimitive","Button","IconButton","generateClasses","tv","backdrop","BottomSheetRoot","isOpen","warnDeprecated","prefersReducedMotion","usePrefersReducedMotion","open","disableDrag","scrollPosition","useInnerOpen","isShown","innerOpen","contentRef","useRef","useFocusReturn","useEffect","returnFocus","nextOpen","onClose","Sheet","root","event","container","dragHandleContainer","dragHandle","ZIndexContext","zIndex","content","contentWrapper","Title","Level","header","titleAndIcons","onBack","ArrowLeftIcon","backButtonLabel","CloserIcon","closeButtonLabel","subline","Content","setScrollRef","scrollArea","disableHorizontalPadding","ButtonSection","PrimaryButton","props","Component","component","otherProps","handleClick","onClick","SecondaryButton"],"sources":["../../src/BottomSheetRoot.tsx"],"sourcesContent":["\"use client\";\n\nimport * as DialogPrimitive from \"@radix-ui/react-dialog\";\nimport React, { useEffect, useRef, type ComponentProps, type ElementType, type ReactNode, type RefObject } from \"react\";\nimport { Sheet } from \"react-modal-sheet\";\nimport { tv } from \"tailwind-variants\";\n\nimport { backdrop } from \"@bonprix-ds/internal-components\";\nimport Button from \"@bonprix-ds/react-button\";\nimport { type IconOrElement } from \"@bonprix-ds/react-icon\";\nimport IconButton from \"@bonprix-ds/react-icon-button\";\nimport { ArrowLeftIcon, CloserIcon } from \"@bonprix-ds/react-icon/icons\";\nimport { TitleLevel, useFocusReturn, useInnerOpen, usePrefersReducedMotion, ZIndexContext } from \"@bonprix-ds/utils\";\nimport { warnDeprecated } from \"@bonprix-ds/utils/deprecation\";\n\nimport BottomSheetContext from \"./BottomSheetContext\";\nimport useConsumeBottomSheet from \"./useConsumeBottomSheet\";\nimport useCreateBottomSheet from \"./useCreateBottomSheet\";\nimport useKeepScrollPositionOnPageHide from \"./useKeepScrollPositionOnPageHide\";\n\nimport \"./BottomSheet.css\";\n\nconst generateClasses = tv({\n slots: {\n root: \"preflight block\",\n container: \"max-h-[calc(100%-4rem)] rounded-none bg-clr-bg-modal shadow-none outline-none\",\n header: \"px-4 py-3\",\n titleAndIcons: \"flex touch-pan-down items-center gap-2\",\n contentWrapper: \"flex w-full flex-col\",\n content: [\n \"typography-body1 text-clr-primary\",\n // scroller must be a flexbox to fix scrollbar\n // scroller needs some padding to not cut off the focus-ring\n \"*:-mx-0.5 *:flex *:px-0.5\",\n // scroller must set min-h-0 in order to make scrolling work inside our flex context\n \"*:min-h-0\",\n ],\n dragHandleContainer: \"flex h-6 shrink-0 items-center justify-center\",\n dragHandle: \"h-1 w-8 rounded-full bg-clr-on-bg-low\",\n backdrop: backdrop(),\n scrollArea: \"scroll-area relative\",\n },\n variants: {\n disableHorizontalPadding: {\n false: { scrollArea: \"px-4\" },\n true: \"\",\n },\n },\n});\n\ninterface SheetProps {\n /** All of the subcomponents should be placed inside this property. */\n children: ReactNode;\n /** Callback function, that is triggered when the sheet is attempted to be closed.\n *\n * It will be called in following cases:\n * - Click on backdrop\n * - Press of the `Escape` key\n * - Swipe down to close\n * - Click on \"close\" icon button in the upper-right corner\n * - Click on any button (PrimaryButton or SecondaryButton)\n *\n * Should be used in conjunction with `open`.\n *\n * Once the sheet has closed, focus returns to the element that had it before the sheet opened. */\n onClose: () => void;\n /** Callback to execute when clicking back icon. */\n onBack?: () => void;\n /** The z-index of the sheet.. */\n zIndex?: number;\n /** The accessible name of the close button. */\n closeButtonLabel?: string;\n /** The accessible name of the back button. */\n backButtonLabel?: string;\n}\n\ntype OpenProps = SheetProps & {\n /** Toggles the visibility of the sheet. Should be used in conjunction with the `onClose` property. */\n open: boolean;\n /** @deprecated [is-open-prop] Use `open` instead. */\n isOpen?: never;\n};\n\ntype DeprecatedOpenProps = SheetProps & {\n open?: never;\n /**\n * Toggles the visibility of the sheet. Should be used in conjunction with the `onClose` property.\n * @deprecated [is-open-prop] Use `open` instead.\n */\n isOpen: boolean;\n};\n\nexport interface SheetTitleProps {\n /** The text content. */\n children: string;\n /** An optional subline that is displayed right below the title. */\n subline?: string;\n /** The heading level for the title. */\n level?: TitleLevel;\n}\n\nexport interface SheetButtonSectionProps {\n /** Primary and secondary buttons should be placed inside this section. */\n children: ReactNode;\n}\n\nexport interface SheetContentProps {\n /** The content of the content section. */\n children: ReactNode;\n /** If `true`, the horizontal padding around the content will be removed. */\n disableHorizontalPadding?: boolean;\n}\n\nexport interface SheetButtonBaseProps {\n /** The label of the button. */\n children: string;\n /** If used, an icon will be additionally shown. The value is either a name of one icon from our icon set or an inline `<svg>` element. */\n icon?: IconOrElement;\n /** The position of the icon relative to the content. */\n iconPosition?: \"start\" | \"end\";\n}\n\nexport type SheetButtonProps<C extends ElementType> = SheetButtonBaseProps & {\n /** The semantic component to render the Button. Preferrably `\"button\"`. Native props will be passed to this component. */\n component?: C;\n} & Omit<ComponentProps<C>, keyof SheetButtonBaseProps | \"component\">;\n\ntype SheetPrimaryButtonProps<C extends ElementType> = SheetButtonProps<C> & {\n /** If `true`, a loading spinner will be shown instead of the content. */\n loading?: boolean;\n};\n\nexport type Props = (DeprecatedOpenProps | OpenProps) & {\n /** A reference to the root element of the sheet. */\n ref?: RefObject<HTMLDivElement>;\n};\n\nconst BottomSheetRoot = ({\n children,\n open,\n isOpen,\n onClose,\n onBack,\n zIndex = 9999,\n ref,\n closeButtonLabel = \"Schließen\",\n backButtonLabel = \"Zurück\",\n}: Props) => {\n if (isOpen !== undefined) warnDeprecated(\"is-open-prop\");\n const prefersReducedMotion = usePrefersReducedMotion();\n const { root, container, dragHandleContainer, dragHandle, content, contentWrapper, backdrop } = generateClasses();\n const { isShown, scrollRef, scrollPosition } = useCreateBottomSheet({ open: open ?? isOpen ?? false });\n\n // When \"middle\" or \"bottom\", dragging is disabled to allow for regular scrolling.\n // When at \"top\", we need to enable drag to close. When `undefined`, the element is not scrollable and can be dragged, too.\n const disableDrag = scrollPosition === \"middle\" || scrollPosition === \"bottom\";\n\n const { innerOpen } = useInnerOpen(isShown);\n useKeepScrollPositionOnPageHide(innerOpen);\n\n const contentRef = useRef<HTMLDivElement>(null);\n const { returnFocus } = useFocusReturn(innerOpen, contentRef);\n\n // Radix's `onCloseAutoFocus` waits until react-modal-sheet has animated the sheet away under `forceMount` - and never\n // fires where that animation does not settle. Closing is the reliable signal.\n useEffect(() => {\n if (!innerOpen) returnFocus();\n }, [innerOpen, returnFocus]);\n\n const focusSheet = (event: Event) => {\n event.preventDefault();\n (event.target as HTMLElement).focus();\n };\n\n const handleOpenChange = (nextOpen: boolean) => {\n if (!nextOpen) onClose();\n };\n\n return (\n <DialogPrimitive.Root open={innerOpen} onOpenChange={handleOpenChange}>\n <Sheet\n isOpen={innerOpen}\n onClose={onClose}\n className={root()}\n detent=\"content\"\n data-bpds=\"bottom-sheet\"\n prefersReducedMotion={prefersReducedMotion}\n style={{\n // We remove `pointer-events` handling from the library because it interfers with a lot of things like opened Selects that wouldn't close etc.\n pointerEvents: undefined,\n // When removing react-modal-sheet's pointerEvents handling, we need to adjust visibility because we can no longer rely on `pointer-events: none` being set on the wrapper that spans the whole page even when closed.\n visibility: innerOpen ? \"visible\" : \"hidden\",\n zIndex: zIndex,\n }}\n avoidKeyboard={false}\n >\n {/* `forceMount` leaves the mounting to the sheet, whose state machine keeps the container around for the closing animation. */}\n <DialogPrimitive.Content\n ref={contentRef}\n forceMount\n asChild\n aria-modal\n aria-describedby={undefined}\n tabIndex={-1}\n onOpenAutoFocus={focusSheet}\n >\n <Sheet.Container className={container()} ref={ref}>\n <Sheet.Header unstyled className={dragHandleContainer()}>\n <Sheet.DragIndicator unstyled className={dragHandle()} />\n </Sheet.Header>\n <ZIndexContext.Provider value={zIndex}>\n <BottomSheetContext.Provider\n value={{ scrollRef, scrollPosition, onClose, onBack, disableDrag, closeButtonLabel, backButtonLabel }}\n >\n <Sheet.Content\n className={content()}\n // disable default scrolling\n disableScroll\n // Dragging is still managed by `Sheet.Content` so we control it here\n disableDrag={disableDrag}\n >\n <div\n className={contentWrapper()}\n // Apply keyboard height as padding bottom to keep content above the keyboard\n style={{ paddingBottom: \"max(var(--keyboard-inset-height, 0rem), var(--app-safe-area-inset-bottom, 1rem))\" }}\n >\n {children}\n </div>\n </Sheet.Content>\n </BottomSheetContext.Provider>\n </ZIndexContext.Provider>\n </Sheet.Container>\n </DialogPrimitive.Content>\n {/* TODO: Deactivate the animation, when the user prefers reduced motion: https://github.com/Temzasse/react-modal-sheet/issues/227 */}\n {/* The backdrop carries no handler of its own: Radix dismisses the dialog on a press outside of its content. */}\n <Sheet.Backdrop className={backdrop()} />\n </Sheet>\n </DialogPrimitive.Root>\n );\n};\n\nexport default BottomSheetRoot;\n\nexport const Title = ({ children, subline, level: Level = \"h3\" }: SheetTitleProps) => {\n const { onBack, onClose, closeButtonLabel, backButtonLabel } = useConsumeBottomSheet();\n const { header, titleAndIcons } = generateClasses();\n return (\n <div className={header()}>\n <div className={titleAndIcons()}>\n {onBack && <IconButton onClick={onBack} icon={<ArrowLeftIcon />} label={backButtonLabel} variant=\"tertiary\" size=\"small\" />}\n <DialogPrimitive.Title asChild>\n <div className=\"grow\">\n <Level className=\"typography-bonprix-h6 my-0 line-clamp-2 text-start text-clr-on-bg-high\">{children}</Level>\n </div>\n </DialogPrimitive.Title>\n <IconButton onClick={onClose} icon={<CloserIcon />} label={closeButtonLabel} variant=\"tertiary\" size=\"small\" />\n </div>\n {subline && <p className=\"typography-body1 my-0 text-start text-clr-on-bg-high\">{subline}</p>}\n </div>\n );\n};\n\nexport const Content = ({ children, disableHorizontalPadding = false }: SheetContentProps) => {\n const { scrollRef: setScrollRef, disableDrag } = useConsumeBottomSheet();\n const { scrollArea } = generateClasses({ disableHorizontalPadding });\n return (\n <div\n ref={setScrollRef}\n className={scrollArea()}\n // react-modal-sheet writes this same property imperatively, but only once it has measured the element as\n // scrollable - and it bails before doing so on every open where that measurement is not ready yet, which\n // leaves the browser free to claim the downward swipe and cancel the drag. Setting it here closes that gap.\n style={{ touchAction: disableDrag ? \"auto\" : \"pan-down\" }}\n data-bpds-disable-horizontal-padding={disableHorizontalPadding}\n tabIndex={0}\n >\n {children}\n </div>\n );\n};\n\nexport const ButtonSection = ({ children }: SheetButtonSectionProps) => {\n return <div className=\"button-section flex w-full shrink-0 touch-pan-down flex-col gap-3 px-4 pt-4\">{children}</div>;\n};\n\nexport const PrimaryButton = <C extends ElementType>({\n children,\n icon,\n iconPosition,\n loading,\n component = \"button\" as C,\n ...props\n}: SheetPrimaryButtonProps<C>) => {\n const Component: ElementType = component;\n const { onClick, ...otherProps } = props;\n const { onClose } = useConsumeBottomSheet();\n const handleClick = (event: React.MouseEvent) => {\n // oxlint-disable-next-line @typescript-eslint/no-unsafe-call\n onClick?.(event);\n onClose();\n };\n return (\n <Button\n component={Component}\n {...otherProps}\n variant=\"primary\"\n onClick={handleClick}\n icon={icon}\n iconPosition={iconPosition}\n size=\"large\"\n loading={loading}\n >\n {children}\n </Button>\n );\n};\n\nexport const SecondaryButton = <C extends ElementType>({\n children,\n icon,\n iconPosition,\n component = \"button\" as C,\n ...props\n}: SheetButtonProps<C>) => {\n const Component: ElementType = component;\n const { onClose } = useConsumeBottomSheet();\n return (\n <Button component={Component} {...props} variant=\"secondary\" onClick={onClose} icon={icon} iconPosition={iconPosition} size=\"large\">\n {children}\n </Button>\n );\n};\n\nBottomSheetRoot.displayName = \"BottomSheet\";\n"],"mappings":";;;;;;;;;;;;;;;;;;AAsBA,IAAMO,IAAkBC,EAAG;CACzB,OAAO;EACL,MAAM;EACN,WAAW;EACX,QAAQ;EACR,eAAe;EACf,gBAAgB;EAChB,SAAS;GACP;GAGA;GAEA;EACF;EACA,qBAAqB;EACrB,YAAY;EACZ,UAAUC,EAAS;EACnB,YAAY;CACd;CACA,UAAU,EACR,0BAA0B;EACxB,OAAO,EAAE,YAAY,OAAO;EAC5B,MAAM;CACR,EACF;AACF,CAAC,GAyFKC,KAAmB,EACvB,aACA,SACA,WACA,YACA,WACA,YAAS,MACT,QACA,sBAAmB,aACnB,qBAAkB,eACP;CACX,AAAIC,MAAW,KAAA,KAAWC,EAAe,cAAc;CACvD,IAAMC,IAAuBC,EAAwB,G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OAAD;EACE,KAAK0C;EACL,WAAWC,EAAW;EAItB,OAAO,EAAE,aAAalC,IAAc,SAAS,WAAW;EACxD,wCAAsCmC;EACtC,UAAU;EAET;CACE,CAAA;AAET,GAEaC,KAAiB,EAAE,kBACvB,kBAAC,OAAD;CAAK,WAAU;CAA+E;AAAc,CAAA,GAGxGC,KAAwC,EACnD,aACA,SACA,iBACA,YACA,eAAY,UACZ,GAAGC,QAC6B;CAChC,IAAMC,IAAyBC,GACzB,EAAE,YAAS,GAAGC,MAAeH,GAC7B,EAAE,eAAYrD,EAAsB,GACpCyD,KAAe,MAA4B;EAG/C,AADAC,IAAU7B,CAAK,GACfH,EAAQ;CACV;CACA,OACE,kBAACtB,GAAD;EACE,WAAWkD;EACX,GAAIE;EACJ,SAAQ;EACR,SAASC;EACH;EACQ;EACd,MAAK;EACI;EAER;CACK,CAAA;AAEZ,GAEaE,KAA0C,EACrD,aACA,SACA,iBACA,eAAY,UACZ,GAAGN,QACsB;CACzB,IAAMC,IAAyBC,GACzB,EAAE,eAAYvD,EAAsB;CAC1C,OACE,kBAACI,GAAD;EAAQ,WAAWkD;EAAW,GAAID;EAAO,SAAQ;EAAY,SAAS3B;EAAe;EAAoB;EAAc,MAAK;EACzH;CACK,CAAA;AAEZ;AAEA,EAAgB,cAAc"}
|
|
1
|
+
{"version":3,"file":"BottomSheetRoot.js","names":["BottomSheetContext","useConsumeBottomSheet","useCreateBottomSheet","useKeepScrollPositionOnLeave","DialogPrimitive","Button","IconButton","generateClasses","tv","backdrop","BottomSheetRoot","isOpen","warnDeprecated","prefersReducedMotion","usePrefersReducedMotion","open","disableDrag","scrollPosition","useInnerOpen","isShown","disableScrollLocking","innerOpen","contentRef","useRef","useFocusReturn","useEffect","returnFocus","nextOpen","onClose","Sheet","root","event","container","dragHandleContainer","dragHandle","ZIndexContext","zIndex","content","contentWrapper","Title","Level","header","titleAndIcons","onBack","ArrowLeftIcon","backButtonLabel","CloserIcon","closeButtonLabel","subline","Content","setScrollRef","scrollArea","disableHorizontalPadding","ButtonSection","PrimaryButton","props","Component","component","otherProps","handleClick","onClick","SecondaryButton"],"sources":["../../src/BottomSheetRoot.tsx"],"sourcesContent":["\"use client\";\n\nimport * as DialogPrimitive from \"@radix-ui/react-dialog\";\nimport React, { type ComponentProps, type ElementType, MouseEvent, type ReactNode, type RefObject, useEffect, useRef } from \"react\";\nimport { Sheet } from \"react-modal-sheet\";\nimport { tv } from \"tailwind-variants\";\n\nimport { backdrop } from \"@bonprix-ds/internal-components\";\nimport Button from \"@bonprix-ds/react-button\";\nimport { type IconOrElement } from \"@bonprix-ds/react-icon\";\nimport IconButton from \"@bonprix-ds/react-icon-button\";\nimport { ArrowLeftIcon, CloserIcon } from \"@bonprix-ds/react-icon/icons\";\nimport { TitleLevel, useFocusReturn, useInnerOpen, usePrefersReducedMotion, ZIndexContext } from \"@bonprix-ds/utils\";\nimport { warnDeprecated } from \"@bonprix-ds/utils/deprecation\";\n\nimport BottomSheetContext from \"./BottomSheetContext\";\nimport useConsumeBottomSheet from \"./useConsumeBottomSheet\";\nimport useCreateBottomSheet from \"./useCreateBottomSheet\";\nimport useKeepScrollPositionOnLeave from \"./useKeepScrollPositionOnLeave\";\n\nimport \"./BottomSheet.css\";\n\nconst generateClasses = tv({\n slots: {\n root: \"preflight block\",\n container: \"max-h-[calc(100%-4rem)] rounded-none bg-clr-bg-modal shadow-none outline-none\",\n header: \"px-4 py-3\",\n titleAndIcons: \"flex touch-pan-down items-center gap-2\",\n contentWrapper: \"flex w-full flex-col\",\n content: [\n \"typography-body1 text-clr-primary\",\n // scroller must be a flexbox to fix scrollbar\n // scroller needs some padding to not cut off the focus-ring\n \"*:-mx-0.5 *:flex *:px-0.5\",\n // scroller must set min-h-0 in order to make scrolling work inside our flex context\n \"*:min-h-0\",\n ],\n dragHandleContainer: \"flex h-6 shrink-0 items-center justify-center\",\n dragHandle: \"h-1 w-8 rounded-full bg-clr-on-bg-low\",\n backdrop: backdrop(),\n scrollArea: \"scroll-area relative\",\n },\n variants: {\n disableHorizontalPadding: {\n false: { scrollArea: \"px-4\" },\n true: \"\",\n },\n },\n});\n\ninterface SheetProps {\n /** All of the subcomponents should be placed inside this property. */\n children: ReactNode;\n /** Callback function, that is triggered when the sheet is attempted to be closed.\n *\n * It will be called in following cases:\n * - Click on backdrop\n * - Press of the `Escape` key\n * - Swipe down to close\n * - Click on \"close\" icon button in the upper-right corner\n * - Click on any button (PrimaryButton or SecondaryButton)\n *\n * Should be used in conjunction with `open`.\n *\n * Once the sheet has closed, focus returns to the element that had it before the sheet opened. */\n onClose: () => void;\n /** Callback to execute when clicking back icon. */\n onBack?: () => void;\n /** The z-index of the sheet.. */\n zIndex?: number;\n /** The accessible name of the close button. */\n closeButtonLabel?: string;\n /** The accessible name of the back button. */\n backButtonLabel?: string;\n}\n\ntype OpenProps = SheetProps & {\n /** Toggles the visibility of the sheet. Should be used in conjunction with the `onClose` property. */\n open: boolean;\n /** @deprecated [is-open-prop] Use `open` instead. */\n isOpen?: never;\n};\n\ntype DeprecatedOpenProps = SheetProps & {\n open?: never;\n /**\n * Toggles the visibility of the sheet. Should be used in conjunction with the `onClose` property.\n * @deprecated [is-open-prop] Use `open` instead.\n */\n isOpen: boolean;\n};\n\nexport interface SheetTitleProps {\n /** The text content. */\n children: string;\n /** An optional subline that is displayed right below the title. */\n subline?: string;\n /** The heading level for the title. */\n level?: TitleLevel;\n}\n\nexport interface SheetButtonSectionProps {\n /** Primary and secondary buttons should be placed inside this section. */\n children: ReactNode;\n}\n\nexport interface SheetContentProps {\n /** The content of the content section. */\n children: ReactNode;\n /** If `true`, the horizontal padding around the content will be removed. */\n disableHorizontalPadding?: boolean;\n}\n\nexport interface SheetButtonBaseProps {\n /** The label of the button. */\n children: string;\n /** If used, an icon will be additionally shown. The value is either an icon component from `bonprix-design-system/icons`, such as `<CartIcon />`, a name of one icon from our icon set or an inline `<svg>` element. */\n icon?: IconOrElement;\n /** The position of the icon relative to the content. */\n iconPosition?: \"start\" | \"end\";\n}\n\nexport type SheetButtonProps<C extends ElementType> = SheetButtonBaseProps & {\n /** The semantic component to render the Button. Preferrably `\"button\"`. Native props will be passed to this component. */\n component?: C;\n} & Omit<ComponentProps<C>, keyof SheetButtonBaseProps | \"component\">;\n\ntype SheetPrimaryButtonProps<C extends ElementType> = SheetButtonProps<C> & {\n /** If `true`, a loading spinner will be shown instead of the content. */\n loading?: boolean;\n};\n\nexport type Props = (DeprecatedOpenProps | OpenProps) & {\n /** A reference to the root element of the sheet. */\n ref?: RefObject<HTMLDivElement>;\n};\n\nconst BottomSheetRoot = ({\n children,\n open,\n isOpen,\n onClose,\n onBack,\n zIndex = 9999,\n ref,\n closeButtonLabel = \"Schließen\",\n backButtonLabel = \"Zurück\",\n}: Props) => {\n if (isOpen !== undefined) warnDeprecated(\"is-open-prop\");\n const prefersReducedMotion = usePrefersReducedMotion();\n const { root, container, dragHandleContainer, dragHandle, content, contentWrapper, backdrop } = generateClasses();\n const { isShown, scrollRef, scrollPosition } = useCreateBottomSheet({ open: open ?? isOpen ?? false });\n\n // When \"middle\" or \"bottom\", dragging is disabled to allow for regular scrolling.\n // When at \"top\", we need to enable drag to close. When `undefined`, the element is not scrollable and can be dragged, too.\n const disableDrag = scrollPosition === \"middle\" || scrollPosition === \"bottom\";\n\n const { innerOpen } = useInnerOpen(isShown);\n const disableScrollLocking = useKeepScrollPositionOnLeave(innerOpen);\n\n const contentRef = useRef<HTMLDivElement>(null);\n const { returnFocus } = useFocusReturn(innerOpen, contentRef);\n\n // Radix's `onCloseAutoFocus` waits until react-modal-sheet has animated the sheet away under `forceMount` - and never\n // fires where that animation does not settle. Closing is the reliable signal.\n useEffect(() => {\n if (!innerOpen) returnFocus();\n }, [innerOpen, returnFocus]);\n\n const focusSheet = (event: Event) => {\n event.preventDefault();\n (event.target as HTMLElement).focus();\n };\n\n const handleOpenChange = (nextOpen: boolean) => {\n if (!nextOpen) onClose();\n };\n\n return (\n <DialogPrimitive.Root open={innerOpen} onOpenChange={handleOpenChange}>\n <Sheet\n isOpen={innerOpen}\n onClose={onClose}\n className={root()}\n detent=\"content\"\n data-bpds=\"bottom-sheet\"\n prefersReducedMotion={prefersReducedMotion}\n disableScrollLocking={disableScrollLocking}\n style={{\n // We remove `pointer-events` handling from the library because it interfers with a lot of things like opened Selects that wouldn't close etc.\n pointerEvents: undefined,\n // When removing react-modal-sheet's pointerEvents handling, we need to adjust visibility because we can no longer rely on `pointer-events: none` being set on the wrapper that spans the whole page even when closed.\n visibility: innerOpen ? \"visible\" : \"hidden\",\n zIndex: zIndex,\n }}\n avoidKeyboard={false}\n >\n {/* `forceMount` leaves the mounting to the sheet, whose state machine keeps the container around for the closing animation. */}\n <DialogPrimitive.Content\n ref={contentRef}\n forceMount\n asChild\n aria-modal\n aria-describedby={undefined}\n tabIndex={-1}\n onOpenAutoFocus={focusSheet}\n >\n <Sheet.Container className={container()} ref={ref}>\n <Sheet.Header unstyled className={dragHandleContainer()}>\n <Sheet.DragIndicator unstyled className={dragHandle()} />\n </Sheet.Header>\n <ZIndexContext.Provider value={zIndex}>\n <BottomSheetContext.Provider\n value={{ scrollRef, scrollPosition, onClose, onBack, disableDrag, closeButtonLabel, backButtonLabel }}\n >\n <Sheet.Content\n className={content()}\n // disable default scrolling\n disableScroll\n // Dragging is still managed by `Sheet.Content` so we control it here\n disableDrag={disableDrag}\n >\n <div\n className={contentWrapper()}\n // Apply keyboard height as padding bottom to keep content above the keyboard\n style={{ paddingBottom: \"max(var(--keyboard-inset-height, 0rem), var(--app-safe-area-inset-bottom, 1rem))\" }}\n >\n {children}\n </div>\n </Sheet.Content>\n </BottomSheetContext.Provider>\n </ZIndexContext.Provider>\n </Sheet.Container>\n </DialogPrimitive.Content>\n {/* TODO: Deactivate the animation, when the user prefers reduced motion: https://github.com/Temzasse/react-modal-sheet/issues/227 */}\n {/* The backdrop carries no handler of its own: Radix dismisses the dialog on a press outside of its content. */}\n <Sheet.Backdrop className={backdrop()} />\n </Sheet>\n </DialogPrimitive.Root>\n );\n};\n\nexport default BottomSheetRoot;\n\nexport const Title = ({ children, subline, level: Level = \"h3\" }: SheetTitleProps) => {\n const { onBack, onClose, closeButtonLabel, backButtonLabel } = useConsumeBottomSheet();\n const { header, titleAndIcons } = generateClasses();\n return (\n <div className={header()}>\n <div className={titleAndIcons()}>\n {onBack && <IconButton onClick={onBack} icon={<ArrowLeftIcon />} label={backButtonLabel} variant=\"tertiary\" size=\"small\" />}\n <DialogPrimitive.Title asChild>\n <div className=\"grow\">\n <Level className=\"typography-bonprix-h6 my-0 line-clamp-2 text-start text-clr-on-bg-high\">{children}</Level>\n </div>\n </DialogPrimitive.Title>\n <IconButton onClick={onClose} icon={<CloserIcon />} label={closeButtonLabel} variant=\"tertiary\" size=\"small\" />\n </div>\n {subline && <p className=\"typography-body1 my-0 text-start text-clr-on-bg-high\">{subline}</p>}\n </div>\n );\n};\n\nexport const Content = ({ children, disableHorizontalPadding = false }: SheetContentProps) => {\n const { scrollRef: setScrollRef, disableDrag } = useConsumeBottomSheet();\n const { scrollArea } = generateClasses({ disableHorizontalPadding });\n return (\n <div\n ref={setScrollRef}\n className={scrollArea()}\n // react-modal-sheet writes this same property imperatively, but only once it has measured the element as\n // scrollable - and it bails before doing so on every open where that measurement is not ready yet, which\n // leaves the browser free to claim the downward swipe and cancel the drag. Setting it here closes that gap.\n style={{ touchAction: disableDrag ? \"auto\" : \"pan-down\" }}\n data-bpds-disable-horizontal-padding={disableHorizontalPadding}\n tabIndex={0}\n >\n {children}\n </div>\n );\n};\n\nexport const ButtonSection = ({ children }: SheetButtonSectionProps) => {\n return <div className=\"button-section flex w-full shrink-0 touch-pan-down flex-col gap-3 px-4 pt-4\">{children}</div>;\n};\n\nexport const PrimaryButton = <C extends ElementType>({\n children,\n icon,\n iconPosition,\n loading,\n component = \"button\" as C,\n ...props\n}: SheetPrimaryButtonProps<C>) => {\n const Component: ElementType = component;\n const { onClick, ...otherProps } = props;\n const { onClose } = useConsumeBottomSheet();\n const handleClick = (event: MouseEvent) => {\n // oxlint-disable-next-line @typescript-eslint/no-unsafe-call\n onClick?.(event);\n onClose();\n };\n return (\n <Button\n component={Component}\n {...otherProps}\n variant=\"primary\"\n onClick={handleClick}\n icon={icon}\n iconPosition={iconPosition}\n size=\"large\"\n loading={loading}\n >\n {children}\n </Button>\n );\n};\n\nexport const SecondaryButton = <C extends ElementType>({\n children,\n icon,\n iconPosition,\n component = \"button\" as C,\n ...props\n}: SheetButtonProps<C>) => {\n const Component: ElementType = component;\n const { onClose } = useConsumeBottomSheet();\n return (\n <Button component={Component} {...props} variant=\"secondary\" onClick={onClose} icon={icon} iconPosition={iconPosition} size=\"large\">\n {children}\n </Button>\n );\n};\n\nBottomSheetRoot.displayName = \"BottomSheet\";\n"],"mappings":";;;;;;;;;;;;;;;;;;AAsBA,IAAMO,IAAkBC,EAAG;CACzB,OAAO;EACL,MAAM;EACN,WAAW;EACX,QAAQ;EACR,eAAe;EACf,gBAAgB;EAChB,SAAS;GACP;GAGA;GAEA;EACF;EACA,qBAAqB;EACrB,YAAY;EACZ,UAAUC,EAAS;EACnB,YAAY;CACd;CACA,UAAU,EACR,0BAA0B;EACxB,OAAO,EAAE,YAAY,OAAO;EAC5B,MAAM;CACR,EACF;AACF,CAAC,GAyFKC,KAAmB,EACvB,aACA,SACA,WACA,YACA,WACA,YAAS,MACT,QACA,sBAAmB,aACnB,qBAAkB,eACP;CACX,AAAIC,MAAW,KAAA,KAAWC,EAAe,cAAc;CACvD,IAAMC,IAAuBC,EAAwB,GAC/C,EAAE,SAAM,cAAW,wBAAqB,eAAY,YAAS,mBAAgB,gBAAaP,EAAgB,GAC1G,EAAE,YAAS,cAAW,sBAAmBL,EAAqB,EAAE,MAAMa,KAAQJ,KAAU,GAAM,CAAC,GAI/FK,IAAcC,MAAmB,YAAYA,MAAmB,UAEhE,EAAE,iBAAcC,EAAaC,CAAO,GACpCC,IAAuBjB,EAA6BkB,CAAS,GAE7DC,IAAaC,EAAuB,IAAI,GACxC,EAAE,mB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C,GACzB,EAAE,YAAS,GAAGC,MAAeH,GAC7B,EAAE,eAAYtD,EAAsB,GACpC0D,KAAe,MAAsB;EAGzC,AADAC,IAAU7B,CAAK,GACfH,EAAQ;CACV;CACA,OACE,kBAACvB,GAAD;EACE,WAAWmD;EACX,GAAIE;EACJ,SAAQ;EACR,SAASC;EACH;EACQ;EACd,MAAK;EACI;EAER;CACK,CAAA;AAEZ,GAEaE,KAA0C,EACrD,aACA,SACA,iBACA,eAAY,UACZ,GAAGN,QACsB;CACzB,IAAMC,IAAyBC,GACzB,EAAE,eAAYxD,EAAsB;CAC1C,OACE,kBAACI,GAAD;EAAQ,WAAWmD;EAAW,GAAID;EAAO,SAAQ;EAAY,SAAS3B;EAAe;EAAoB;EAAc,MAAK;EACzH;CACK,CAAA;AAEZ;AAEA,EAAgB,cAAc"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { useEffect as e, useState as t } from "react";
|
|
2
|
+
import { flushSync as n } from "react-dom";
|
|
3
|
+
// src/useKeepScrollPositionOnLeave.ts
|
|
4
|
+
var r = (r) => {
|
|
5
|
+
let [i, a] = t(!1);
|
|
6
|
+
return e(() => {
|
|
7
|
+
if (!r) return;
|
|
8
|
+
let e = () => n(() => a(!0)), t = () => a(!1), i = ({ navigationType: e, destination: t }) => {
|
|
9
|
+
if (!(e === "push" && t.sameDocument) || new URL(t.url).pathname === window.location.pathname) return;
|
|
10
|
+
let n = -parseFloat(document.body.style.marginTop);
|
|
11
|
+
n > 0 && (document.body.style.marginTop = "", window.scrollTo({
|
|
12
|
+
top: n,
|
|
13
|
+
behavior: "instant"
|
|
14
|
+
}));
|
|
15
|
+
};
|
|
16
|
+
return window.addEventListener("pagehide", e), window.addEventListener("pageshow", t), window.navigation?.addEventListener("navigate", i), () => {
|
|
17
|
+
window.removeEventListener("pagehide", e), window.removeEventListener("pageshow", t), window.navigation?.removeEventListener("navigate", i);
|
|
18
|
+
};
|
|
19
|
+
}, [r]), i;
|
|
20
|
+
};
|
|
21
|
+
export { r as default };
|
|
22
|
+
|
|
23
|
+
//# sourceMappingURL=useKeepScrollPositionOnLeave.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useKeepScrollPositionOnLeave.js","names":["useKeepScrollPositionOnLeave","isPageHidden","setIsPageHidden","useState","useEffect","isOpen","hide","flushSync","show","takeShiftOffBeforePush","navigationType","destination","scrollY"],"sources":["../../src/useKeepScrollPositionOnLeave.ts"],"sourcesContent":["import { useEffect, useState } from \"react\";\nimport { flushSync } from \"react-dom\";\n\n// On iOS, react-modal-sheet locks the page by scrolling it to the top and shifting `body` up by a negative\n// `margin-top`. The browser saves a page's scroll position as the page is left, so a page that is left in that state\n// is saved at the top, and going back would land there. A page is left in one of two ways:\n//\n// - The browser loads another page, which fires `pagehide`. While the page is hidden, the returned flag turns the\n// lock off through `disableScrollLocking`, so that react-modal-sheet undoes it itself. `flushSync`, because a\n// hidden page may never run another task.\n// - A client-side router pushes a history entry in the same document, which fires `navigate`. Next.js pushes from\n// inside a React commit, where the sheet can neither re-render nor schedule an update, so the shift is taken off by\n// hand, and react-modal-sheet takes off the rest once the sheet closes. A push that keeps the path, as for a sheet\n// that closes on back, leaves the lock on. So does a native link, which fires `navigate` long before `pagehide`:\n// the page stays locked until it is hidden.\n//\n// The shift is read back from `body`'s inline `margin-top`, which is why react-modal-sheet's version is pinned:\n// \"react-modal-sheet still shifts the page on iOS the way useKeepScrollPositionOnLeave reads it\" in\n// BottomSheet.browser.test.tsx fails as soon as an update changes that.\nconst useKeepScrollPositionOnLeave = (isOpen: boolean) => {\n const [isPageHidden, setIsPageHidden] = useState(false);\n\n useEffect(() => {\n if (!isOpen) return;\n\n const hide = () => flushSync(() => setIsPageHidden(true));\n const show = () => setIsPageHidden(false);\n\n const takeShiftOffBeforePush = ({ navigationType, destination }: NavigateEvent) => {\n const isClientSidePush = navigationType === \"push\" && destination.sameDocument;\n if (!isClientSidePush || new URL(destination.url).pathname === window.location.pathname) return;\n\n const scrollY = -parseFloat(document.body.style.marginTop);\n if (!(scrollY > 0)) return;\n\n document.body.style.marginTop = \"\";\n window.scrollTo({ top: scrollY, behavior: \"instant\" });\n };\n\n window.addEventListener(\"pagehide\", hide);\n window.addEventListener(\"pageshow\", show);\n window.navigation?.addEventListener(\"navigate\", takeShiftOffBeforePush);\n\n return () => {\n window.removeEventListener(\"pagehide\", hide);\n window.removeEventListener(\"pageshow\", show);\n window.navigation?.removeEventListener(\"navigate\", takeShiftOffBeforePush);\n };\n }, [isOpen]);\n\n return isPageHidden;\n};\n\nexport default useKeepScrollPositionOnLeave;\n"],"mappings":";;;AAmBA,IAAMA,KAAgC,MAAoB;CACxD,IAAM,CAACC,GAAcC,KAAmBC,EAAS,EAAK;CA8BtD,OA5BAC,QAAgB;EACd,IAAI,CAACC,GAAQ;EAEb,IAAMC,UAAaC,QAAgBL,EAAgB,EAAI,CAAC,GAClDM,UAAaN,EAAgB,EAAK,GAElCO,KAA0B,EAAE,mBAAgB,qBAAiC;GAEjF,IAAI,EADqBC,MAAmB,UAAUC,EAAY,iBACzC,IAAI,IAAIA,EAAY,GAAG,CAAC,CAAC,aAAa,OAAO,SAAS,UAAU;GAEzF,IAAMC,IAAU,CAAC,WAAW,SAAS,KAAK,MAAM,SAAS;GACzD,AAAMA,IAAU,MAEhB,SAAS,KAAK,MAAM,YAAY,IAChC,OAAO,SAAS;IAAE,KAAKA;IAAS,UAAU;GAAU,CAAC;EACvD;EAMA,OAJA,OAAO,iBAAiB,YAAYN,CAAI,GACxC,OAAO,iBAAiB,YAAYE,CAAI,GACxC,OAAO,YAAY,iBAAiB,YAAYC,CAAsB,SAEzD;GAGX,AAFA,OAAO,oBAAoB,YAAYH,CAAI,GAC3C,OAAO,oBAAoB,YAAYE,CAAI,GAC3C,OAAO,YAAY,oBAAoB,YAAYC,CAAsB;EAC3E;CACF,GAAG,CAACJ,CAAM,CAAC,GAEJJ;AACT"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bonprix-ds/react-bottom-sheet",
|
|
3
|
-
"version": "1.3.
|
|
3
|
+
"version": "1.3.2",
|
|
4
4
|
"description": "BottomSheet component, implemented in React, for the Bonprix Design System.",
|
|
5
5
|
"keywords": [],
|
|
6
6
|
"license": "UNLICENSED",
|
|
@@ -41,10 +41,10 @@
|
|
|
41
41
|
"provenance": true
|
|
42
42
|
},
|
|
43
43
|
"dependencies": {
|
|
44
|
-
"@bonprix-ds/internal-components": "0.2.
|
|
45
|
-
"@bonprix-ds/react-button": "1.1.
|
|
46
|
-
"@bonprix-ds/react-icon": "1.
|
|
47
|
-
"@bonprix-ds/react-icon-button": "1.2.
|
|
44
|
+
"@bonprix-ds/internal-components": "0.2.7",
|
|
45
|
+
"@bonprix-ds/react-button": "1.1.7",
|
|
46
|
+
"@bonprix-ds/react-icon": "1.4.0",
|
|
47
|
+
"@bonprix-ds/react-icon-button": "1.2.7",
|
|
48
48
|
"@bonprix-ds/utils": "0.6.0",
|
|
49
49
|
"@radix-ui/react-dialog": "1.3.1",
|
|
50
50
|
"react-modal-sheet": "5.6.0"
|
|
@@ -54,8 +54,8 @@
|
|
|
54
54
|
"@testing-library/dom": "10.4.2",
|
|
55
55
|
"@testing-library/react": "16.3.3",
|
|
56
56
|
"@types/react": "19.3.0",
|
|
57
|
-
"vite": "8.3.
|
|
58
|
-
"vitest": "5.0
|
|
57
|
+
"vite": "8.3.8",
|
|
58
|
+
"vitest": "5.1.0"
|
|
59
59
|
},
|
|
60
60
|
"peerDependencies": {
|
|
61
61
|
"react": "^19",
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import { useEffect as e } from "react";
|
|
2
|
-
// src/useKeepScrollPositionOnPageHide.ts
|
|
3
|
-
var t = (e) => e.stopImmediatePropagation(), n = (n) => {
|
|
4
|
-
e(() => {
|
|
5
|
-
if (!n) return;
|
|
6
|
-
let e, r = () => {
|
|
7
|
-
let n = -parseFloat(document.body.style.marginTop);
|
|
8
|
-
n > 0 && (e = n, window.addEventListener("scroll", t, { capture: !0 }), document.body.style.marginTop = "", document.documentElement.style.overflow = "", window.scrollTo({
|
|
9
|
-
top: n,
|
|
10
|
-
behavior: "instant"
|
|
11
|
-
}));
|
|
12
|
-
}, i = (n) => {
|
|
13
|
-
n.persisted && e !== void 0 && (window.removeEventListener("scroll", t, { capture: !0 }), document.documentElement.style.overflow = "hidden", document.body.style.marginTop = `-${e}px`, window.scrollTo({
|
|
14
|
-
top: 0,
|
|
15
|
-
behavior: "instant"
|
|
16
|
-
}), e = void 0);
|
|
17
|
-
};
|
|
18
|
-
return window.addEventListener("pagehide", r), window.addEventListener("pageshow", i), () => {
|
|
19
|
-
window.removeEventListener("pagehide", r), window.removeEventListener("pageshow", i), window.removeEventListener("scroll", t, { capture: !0 });
|
|
20
|
-
};
|
|
21
|
-
}, [n]);
|
|
22
|
-
};
|
|
23
|
-
export { n as default };
|
|
24
|
-
|
|
25
|
-
//# sourceMappingURL=useKeepScrollPositionOnPageHide.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"useKeepScrollPositionOnPageHide.js","names":["keepScrollEventsFromTheLock","event","useKeepScrollPositionOnPageHide","isEnabled","lockedScrollY","unlock","scrollY","lockAgain"],"sources":["../../src/useKeepScrollPositionOnPageHide.ts"],"sourcesContent":["import { useEffect } from \"react\";\n\n// The page's scroll event targets `document`, so a capturing listener on `window` runs before react-modal-sheet's\n// own listener on `window`, which scrolls the page back to the top on every scroll while the lock is on. Stopping\n// the event there withholds it from every scroll listener on the page, not just the lock's - which is harmless only\n// because it is in place solely between `pagehide` and `pageshow`, while nothing on the page can be seen.\nconst keepScrollEventsFromTheLock = (event: Event) => event.stopImmediatePropagation();\n\n// On iOS, react-modal-sheet locks the page by scrolling it to the top and shifting `body` up by a negative\n// `margin-top`. A page that is left in that state is saved with its scroll position at the top, so going back\n// would land there. The lock is lifted while the page is hidden, and put back if it returns from the bfcache.\n// The shift is read back from `body`'s inline `margin-top`, which only the lock sets while the sheet is open.\n//\n// All of this mirrors react-modal-sheet's private `preventScrollMobileSafari`, which is why its version is pinned.\n// \"react-modal-sheet still locks the page on iOS the way this hook undoes it\" in BottomSheet.browser.test.tsx runs\n// against the real library and fails as soon as an update changes the lock - check this hook before bumping it.\nconst useKeepScrollPositionOnPageHide = (isEnabled: boolean) => {\n useEffect(() => {\n if (!isEnabled) return;\n\n let lockedScrollY: number | undefined;\n\n const unlock = () => {\n const scrollY = -parseFloat(document.body.style.marginTop);\n if (!(scrollY > 0)) return;\n\n lockedScrollY = scrollY;\n window.addEventListener(\"scroll\", keepScrollEventsFromTheLock, { capture: true });\n document.body.style.marginTop = \"\";\n document.documentElement.style.overflow = \"\";\n window.scrollTo({ top: scrollY, behavior: \"instant\" });\n };\n\n const lockAgain = (event: PageTransitionEvent) => {\n if (!event.persisted || lockedScrollY === undefined) return;\n\n window.removeEventListener(\"scroll\", keepScrollEventsFromTheLock, { capture: true });\n document.documentElement.style.overflow = \"hidden\";\n document.body.style.marginTop = `-${lockedScrollY}px`;\n window.scrollTo({ top: 0, behavior: \"instant\" });\n lockedScrollY = undefined;\n };\n\n window.addEventListener(\"pagehide\", unlock);\n window.addEventListener(\"pageshow\", lockAgain);\n\n return () => {\n window.removeEventListener(\"pagehide\", unlock);\n window.removeEventListener(\"pageshow\", lockAgain);\n window.removeEventListener(\"scroll\", keepScrollEventsFromTheLock, { capture: true });\n };\n }, [isEnabled]);\n};\n\nexport default useKeepScrollPositionOnPageHide;\n"],"mappings":";;AAMA,IAAMA,KAA+B,MAAiBC,EAAM,yBAAyB,GAU/EC,KAAmC,MAAuB;CAC9D,QAAgB;EACd,IAAI,CAACC,GAAW;EAEhB,IAAIC,GAEEC,UAAe;GACnB,IAAMC,IAAU,CAAC,WAAW,SAAS,KAAK,MAAM,SAAS;GACzD,AAAMA,IAAU,MAEhB,IAAgBA,GAChB,OAAO,iBAAiB,UAAUN,GAA6B,EAAE,SAAS,GAAK,CAAC,GAChF,SAAS,KAAK,MAAM,YAAY,IAChC,SAAS,gBAAgB,MAAM,WAAW,IAC1C,OAAO,SAAS;IAAE,KAAKM;IAAS,UAAU;GAAU,CAAC;EACvD,GAEMC,KAAa,MAA+B;GAChD,AAAKN,EAAM,aAAaG,MAAkB,KAAA,MAE1C,OAAO,oBAAoB,UAAUJ,GAA6B,EAAE,SAAS,GAAK,CAAC,GACnF,SAAS,gBAAgB,MAAM,WAAW,UAC1C,SAAS,KAAK,MAAM,YAAY,IAAII,EAAc,KAClD,OAAO,SAAS;IAAE,KAAK;IAAG,UAAU;GAAU,CAAC,GAC/C,IAAgB,KAAA;EAClB;EAKA,OAHA,OAAO,iBAAiB,YAAYC,CAAM,GAC1C,OAAO,iBAAiB,YAAYE,CAAS,SAEhC;GAGX,AAFA,OAAO,oBAAoB,YAAYF,CAAM,GAC7C,OAAO,oBAAoB,YAAYE,CAAS,GAChD,OAAO,oBAAoB,UAAUP,GAA6B,EAAE,SAAS,GAAK,CAAC;EACrF;CACF,GAAG,CAACG,CAAS,CAAC;AAChB"}
|