Skip to content

armgit / vaul

Public Imported from GitHub
0

A drawer component for React.

Clean up ts errors (#75)

Emil Kowalski1e2954323fd4
diff --git a/src/context.ts b/src/context.ts
index c52368c..ce83387 100644
--- a/src/context.ts
+++ b/src/context.ts
@@ -14,12 +14,12 @@ interface DrawerContextValue {
isOpen: boolean;
setIsAnimating: (o: boolean) => void;
keyboardIsOpen: React.MutableRefObject<boolean>;
- experimentalSafariThemeAnimation: boolean;
+ experimentalSafariThemeAnimation?: boolean;
snapPointsOffset: number[] | null;
- snapPoints: (number | string)[] | null;
+ snapPoints?: (number | string)[] | null;
modal: boolean;
shouldFade: boolean;
- activeSnapPoint: number | string | null;
+ activeSnapPoint?: number | string | null;
setActiveSnapPoint: (o: number | string | null) => void;
}
diff --git a/src/index.tsx b/src/index.tsx
index bc0bc01..4cc8624 100644
--- a/src/index.tsx
+++ b/src/index.tsx
@@ -501,7 +501,7 @@ function Root({
nestedOpenChangeTimer.current = setTimeout(() => {
set(drawerRef.current, {
transition: 'none',
- transform: `translateY(${getTranslateY(drawerRef.current)}px)`,
+ transform: `translateY(${getTranslateY(drawerRef.current as HTMLElement)}px)`,
});
}, 500);
}
diff --git a/src/use-position-fixed.ts b/src/use-position-fixed.ts
index 1381814..5c2dfa7 100644
--- a/src/use-position-fixed.ts
+++ b/src/use-position-fixed.ts
@@ -1,7 +1,7 @@
import React from 'react';
import { isIOS } from './use-prevent-scroll';
-let previousBodyPosition = null;
+let previousBodyPosition: Record<string, string> | null = null;
export function usePositionFixed({ isOpen, modal }: { isOpen: boolean; modal: boolean }) {
const scrollPos = React.useRef(0);
diff --git a/src/use-safari-theme-color.ts b/src/use-safari-theme-color.ts
index 67c506d..502960c 100644
--- a/src/use-safari-theme-color.ts
+++ b/src/use-safari-theme-color.ts
@@ -56,10 +56,10 @@ function interpolateColors(color1: number[], color2: number[], steps: number, li
return interpolatedColorArray;
}
export function useSafariThemeColor(
- drawer: React.MutableRefObject<HTMLDivElement>,
- overlay: React.MutableRefObject<HTMLDivElement>,
+ drawer: React.RefObject<HTMLDivElement>,
+ overlay: React.RefObject<HTMLDivElement>,
isOpen: boolean,
- shouldAnimate: boolean,
+ shouldAnimate?: boolean,
) {
const [backgroundColor, setBackgroundColor] = React.useState<RGB | null>([255, 255, 255]);
const [overlayColor, setOverlayColor] = React.useState<RGB | null>([153, 153, 153]);
@@ -127,7 +127,7 @@ export function useSafariThemeColor(
// dividing by 10 will give us a total time of 500.
const index = Math.floor(elapsed / 10);
- if (overlay.current && colors && !releaseExit) {
+ if (overlay.current && colors && !releaseExit && drawer.current) {
if (
drawer.current.style.transform === 'translateY(0px)' &&
drawer.current.getAttribute('vaul-clicked-outside') !== 'true'
diff --git a/src/use-snap-points.ts b/src/use-snap-points.ts
index a622a28..381d946 100644
--- a/src/use-snap-points.ts
+++ b/src/use-snap-points.ts
@@ -12,9 +12,9 @@ export function useSnapPoints({
fadeFromIndex,
}: {
activeSnapPointProp?: number | string | null;
- setActiveSnapPointProp?(snapPoint: number | null): void;
+ setActiveSnapPointProp?(snapPoint: number | null | string): void;
snapPoints?: (number | string)[];
- fadeFromIndex: number;
+ fadeFromIndex?: number;
drawerRef: React.RefObject<HTMLDivElement>;
overlayRef: React.RefObject<HTMLDivElement>;
}) {
@@ -30,7 +30,8 @@ export function useSnapPoints({
);
const shouldFade =
- (snapPoints && snapPoints.length > 0 && snapPoints[fadeFromIndex] === activeSnapPoint) || !snapPoints;
+ (snapPoints && snapPoints.length > 0 && fadeFromIndex && snapPoints[fadeFromIndex] === activeSnapPoint) ||
+ !snapPoints;
const activeSnapPointIndex = React.useMemo(
() => snapPoints?.findIndex((snapPoint) => snapPoint === activeSnapPoint) ?? null,
@@ -49,9 +50,12 @@ export function useSnapPoints({
}
const height = isPx ? snapPointAsNumber : hasWindow ? snapPoint * window.innerHeight : 0;
+ if (hasWindow) {
+ return window.innerHeight - height;
+ }
- return hasWindow && window.innerHeight - height;
- }) ?? null,
+ return height;
+ }) ?? [],
[snapPoints],
);
@@ -108,7 +112,9 @@ export function useSnapPoints({
closeDrawer: () => void;
velocity: number;
}) {
- if (typeof activeSnapPointOffset !== 'number') return;
+
+ if (typeof activeSnapPointOffset !== 'number' || fadeFromIndex === undefined) return;
+
const currentPosition = activeSnapPointOffset - draggedDistance;
const isOverlaySnapPoint = activeSnapPointIndex === fadeFromIndex - 1;
const isFirst = activeSnapPointIndex === 0;
@@ -146,6 +152,8 @@ export function useSnapPoints({
closeDrawer();
}
+ if (activeSnapPointIndex === null) return;
+
snapToPoint(snapPointsOffset[activeSnapPointIndex + dragDirection]);
return;
}
@@ -154,6 +162,7 @@ export function useSnapPoints({
}
function onDrag({ draggedDistance }: { draggedDistance: number }) {
+ if (activeSnapPointOffset === null) return;
const newYValue = activeSnapPointOffset - draggedDistance;
if (newYValue < snapPointsOffset[snapPointsOffset.length - 1]) {
@@ -170,7 +179,8 @@ export function useSnapPoints({
}
function getPercentageDragged(absDraggedDistance: number, isDraggingDown: boolean) {
- if (!snapPoints || !activeSnapPointIndex || !snapPointsOffset) return null;
+ if (!snapPoints || !activeSnapPointIndex || !snapPointsOffset || fadeFromIndex === undefined) return null;
+
// If this is true we are dragging to a snap point that is supposed to have an overlay
const isOverlaySnapPoint = activeSnapPointIndex === fadeFromIndex - 1;
const isOverlaySnapPointOrHigher = activeSnapPointIndex >= fadeFromIndex;