Skip to content

armgit / vaul

Public Imported from GitHub
0

A drawer component for React.

Allow pixel values for snap points (#71)

Emil Kowalski6c2b797c90b2
diff --git a/README.md b/README.md
index f4f4be3..e73e7f2 100644
--- a/README.md
+++ b/README.md
@@ -49,8 +49,8 @@ Can be controlled with the `value` and `onOpenChange` props. Can be opened by de
Additional props:
-`closeThreshold`: Number between 0 and 1 that determines when the drawer should be closed. Example: ` closeThreshold`` of 0.5 would close the drawer if the user swiped for 50% of the height of the drawer or more.
- `scrollLockTimeout`: Duration for which the drawer is not draggable after scrolling content inside of the drawer. Defaults to 500ms
+`closeThreshold`: Number between 0 and 1 that determines when the drawer should be closed. Example: ` closeThreshold` of 0.5 would close the drawer if the user swiped for 50% of the height of the drawer or more.
+`scrollLockTimeout`: Duration for which the drawer is not draggable after scrolling content inside of the drawer. Defaults to 500ms
`snapPoints`: Array of numbers from 0 to 100 that corresponds to % of the screen a given snap point should take up. Should go from least visible. Example `[0.2, 0.5, 0.8]`.
`fadeFromIndex`: Index of a snapPoint from which the overlay fade should be applied. Defaults to the last snap point.
`modal`: When `false`it allows to interact with elements outside of the drawer without closing it. Defaults to`true`.
diff --git a/src/context.ts b/src/context.ts
index 4f8a6a9..129d233 100644
--- a/src/context.ts
+++ b/src/context.ts
@@ -16,11 +16,11 @@ interface DrawerContextValue {
keyboardIsOpen: React.MutableRefObject<boolean>;
experimentalSafariThemeAnimation: boolean;
snapPointHeights: number[] | null;
- snapPoints: number[] | null;
+ snapPoints: (number | string)[] | null;
modal: boolean;
shouldFade: boolean;
- activeSnapPoint: number | null;
- setActiveSnapPoint: (o: number | null) => void;
+ activeSnapPoint: number | string | null;
+ setActiveSnapPoint: (o: number | string | null) => void;
}
export const DrawerContext = React.createContext<DrawerContextValue | undefined>(undefined);
diff --git a/src/index.tsx b/src/index.tsx
index 131e600..32a85ec 100644
--- a/src/index.tsx
+++ b/src/index.tsx
@@ -26,18 +26,18 @@ const NESTED_DISPLACEMENT = 16;
const WINDOW_TOP_OFFSET = 26;
type WithFadeFromProps = {
- snapPoints: number[];
+ snapPoints: (number | string)[];
fadeFromIndex: number;
};
type WithoutFadeFromProps = {
- snapPoints?: number[];
+ snapPoints?: (number | string)[];
fadeFromIndex?: never;
};
type DialogProps = {
- activeSnapPoint?: number | null;
- setActiveSnapPoint?(snapPoint: number | null): void;
+ activeSnapPoint?: number | string | null;
+ setActiveSnapPoint?(snapPoint: number | string | null): void;
children?: React.ReactNode;
open?: boolean;
defaultOpen?: boolean;
diff --git a/src/use-snap-points.ts b/src/use-snap-points.ts
index cbf3913..83d0786 100644
--- a/src/use-snap-points.ts
+++ b/src/use-snap-points.ts
@@ -11,9 +11,9 @@ export function useSnapPoints({
overlayRef,
fadeFromIndex,
}: {
- activeSnapPointProp?: number | null;
+ activeSnapPointProp?: number | string | null;
setActiveSnapPointProp?(snapPoint: number | null): void;
- snapPoints?: number[];
+ snapPoints?: (number | string)[];
fadeFromIndex: number;
drawerRef: React.RefObject<HTMLDivElement>;
overlayRef: React.RefObject<HTMLDivElement>;
@@ -39,7 +39,19 @@ export function useSnapPoints({
);
const snapPointHeights = React.useMemo(
- () => snapPoints?.map((snapPoint) => hasWindow && window.innerHeight - snapPoint * window.innerHeight) ?? null,
+ () =>
+ snapPoints?.map((snapPoint) => {
+ const isPx = typeof snapPoint === 'string';
+ let snapPointAsNumber = 0;
+
+ if (isPx) {
+ snapPointAsNumber = parseInt(snapPoint, 10);
+ }
+
+ const height = isPx ? snapPointAsNumber : snapPoint * window.innerHeight;
+
+ return hasWindow && window.innerHeight - height;
+ }) ?? null,
[snapPoints],
);