A drawer component for React.
Implement free mode, snap points fine-tuning (#69)
Emil Kowalski4c80527c6fb8
diff --git a/README.md b/README.mdindex 0450721..f4f4be3 100644--- a/README.md+++ b/README.md@@ -49,9 +49,11 @@ Can be controlled with the `value` and `onOpenChange` props. Can be opened by deAdditional 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`.### Triggerdiff --git a/src/context.ts b/src/context.tsindex ed4267c..4f8a6a9 100644--- a/src/context.ts+++ b/src/context.ts@@ -17,6 +17,7 @@ interface DrawerContextValue {experimentalSafariThemeAnimation: boolean;snapPointHeights: number[] | null;snapPoints: number[] | null;+ modal: boolean;shouldFade: boolean;activeSnapPoint: number | null;setActiveSnapPoint: (o: number | null) => void;diff --git a/src/index.tsx b/src/index.tsxindex 37e27dd..c28f1bb 100644--- a/src/index.tsx+++ b/src/index.tsx@@ -49,6 +49,7 @@ type DialogProps = {onDrag?(event: React.PointerEvent<HTMLDivElement>, percentageDragged: number): void;onRelease?(event: React.PointerEvent<HTMLDivElement>, open: boolean): void;experimentalSafariThemeAnimation?: boolean;+ modal?: boolean;} & (WithFadeFromProps | WithoutFadeFromProps);function Root({@@ -67,6 +68,7 @@ function Root({fadeFromIndex = snapPoints?.length - 1,activeSnapPoint: activeSnapPointProp,setActiveSnapPoint: setActiveSnapPointProp,+ modal = true,}: DialogProps) {const [isOpen = false, setIsOpen] = useControllableState({prop: openProp,@@ -109,10 +111,10 @@ function Root({});usePreventScroll({- isDisabled: !isOpen || isDragging || isAnimating,+ isDisabled: !isOpen || isDragging || isAnimating || !modal,});- usePositionFixed({ isOpen });+ usePositionFixed({ isOpen, modal });function getScale() {return (window.innerWidth - WINDOW_TOP_OFFSET) / window.innerWidth;@@ -120,12 +122,8 @@ function Root({function onPress(event: React.PointerEvent<HTMLDivElement>) {if (!dismissible) return;- if (- (drawerRef.current && !drawerRef.current.contains(event.target as Node)) ||- (event.target as HTMLElement).tagName === 'BUTTON'- )- return;+ if (drawerRef.current && !drawerRef.current.contains(event.target as Node)) return;setIsDragging(true);dragStartTime.current = new Date();@@ -232,6 +230,7 @@ function Root({if (shouldFade || activeSnapPointIndex === fadeFromIndex - 1) {changeThemeColorOnDrag(percentageDragged);onDragProp?.(event, percentageDragged);+set(overlayRef.current,{@@ -373,7 +372,7 @@ function Root({}function onRelease(event: React.PointerEvent<HTMLDivElement>) {- if ((event.target as HTMLElement).tagName === 'BUTTON' || !isDragging) return;+ if (!isDragging) return;setIsDragging(false);dragEndTime.current = new Date();const swipeAmount = getTranslateY(drawerRef.current);@@ -510,6 +509,7 @@ function Root({}setIsOpen(o);}}+ modal={modal}><DrawerContext.Providervalue={{@@ -530,6 +530,7 @@ function Root({onNestedRelease,keyboardIsOpen,setIsAnimating,+ modal,snapPointHeights,experimentalSafariThemeAnimation,}}@@ -541,7 +542,7 @@ function Root({}const Overlay = React.forwardRef<HTMLDivElement, React.ComponentPropsWithoutRef<typeof DialogPrimitive.Overlay>>(- function ({ children, ...rest }, ref) {+ function ({ children, style, ...rest }, ref) {const { overlayRef, snapPoints, onRelease, experimentalSafariThemeAnimation, shouldFade, isOpen } =useDrawerContext();const composedRef = useComposedRefs(ref, overlayRef);@@ -584,6 +585,7 @@ const Content = React.forwardRef<HTMLDivElement, ContentProps>(function (snapPointHeights,setActiveSnapPoint,snapPoints,+ modal,} = useDrawerContext();const composedRef = useComposedRefs(ref, drawerRef);const animationEndTimer = React.useRef<NodeJS.Timeout>(null);diff --git a/src/use-position-fixed.ts b/src/use-position-fixed.tsindex 7a51cc1..1381814 100644--- a/src/use-position-fixed.ts+++ b/src/use-position-fixed.ts@@ -3,7 +3,7 @@ import { isIOS } from './use-prevent-scroll';let previousBodyPosition = null;-export function usePositionFixed({ isOpen }: { isOpen: boolean }) {+export function usePositionFixed({ isOpen, modal }: { isOpen: boolean; modal: boolean }) {const scrollPos = React.useRef(0);function setPositionFixed() {@@ -77,6 +77,12 @@ export function usePositionFixed({ isOpen }: { isOpen: boolean }) {// This is needed to force Safari toolbar to show **before** the drawer starts animating to prevent a gnarly shift from happeningif (isOpen) {setPositionFixed();++ if (!modal) {+ setTimeout(() => {+ restorePositionSetting();+ }, 500);+ }} else {restorePositionSetting();}diff --git a/src/use-snap-points.ts b/src/use-snap-points.tsindex 3fc44ed..f8d5561 100644--- a/src/use-snap-points.ts+++ b/src/use-snap-points.ts@@ -107,7 +107,12 @@ export function useSnapPoints({return;}- if (velocity > VELOCITY_THRESHOLD) {+ // Find the closest snap point to the current position+ const closestSnapPoint = snapPointHeights?.reduce((prev, curr) => {+ return Math.abs(curr - currentPosition) < Math.abs(prev - currentPosition) ? curr : prev;+ });++ if (velocity > VELOCITY_THRESHOLD && Math.abs(draggedDistance) < window.innerHeight * 0.4) {// -1 = down, 1 = up, might need a better nameconst dragDirection = draggedDistance > 0 ? 1 : -1;// Don't do anything if we swipe upwards while being on the last snap point@@ -121,11 +126,6 @@ export function useSnapPoints({return;}- // Find the closest snap point to the current position- const closestSnapPoint = snapPointHeights?.reduce((prev, curr) => {- return Math.abs(curr - currentPosition) < Math.abs(prev - currentPosition) ? curr : prev;- });-snapToPoint(closestSnapPoint);}