Skip to content

Commit

Permalink
fix: typescript strict on to prevent null calls (#477)
Browse files Browse the repository at this point in the history
* fix: typescript strict on to prevent null calls

* fix: restore original code

* fix: restore original code

* fix: onRelease event nullable
  • Loading branch information
maiconcarraro authored Oct 13, 2024
1 parent 2d12d85 commit 1a8d000
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 16 deletions.
4 changes: 2 additions & 2 deletions src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ interface DrawerContextValue {
drawerRef: React.RefObject<HTMLDivElement>;
overlayRef: React.RefObject<HTMLDivElement>;
onPress: (event: React.PointerEvent<HTMLDivElement>) => void;
onRelease: (event: React.PointerEvent<HTMLDivElement>) => void;
onRelease: (event: React.PointerEvent<HTMLDivElement> | null) => void;
onDrag: (event: React.PointerEvent<HTMLDivElement>) => void;
onNestedDrag: (event: React.PointerEvent<HTMLDivElement>, percentageDragged: number) => void;
onNestedOpenChange: (o: boolean) => void;
Expand All @@ -23,7 +23,7 @@ interface DrawerContextValue {
closeDrawer: () => void;
openProp?: boolean;
onOpenChange?: (o: boolean) => void;
direction?: DrawerDirection;
direction: DrawerDirection;
shouldScaleBackground: boolean;
setBackgroundColorOnScale: boolean;
noBodyStyles: boolean;
Expand Down
30 changes: 20 additions & 10 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ export function Root({
modal = true,
onClose,
nested,
noBodyStyles,
noBodyStyles = false,
direction = 'bottom',
defaultOpen = false,
disablePreventScroll = true,
Expand Down Expand Up @@ -247,7 +247,7 @@ export function Root({
const { restorePositionSetting } = usePositionFixed({
isOpen,
modal,
nested,
nested: nested ?? false,
hasBeenOpened,
preventScrollRestoration,
noBodyStyles,
Expand Down Expand Up @@ -307,7 +307,11 @@ export function Root({
}

// Disallow dragging if drawer was scrolled within `scrollLockTimeout`
if (date.getTime() - lastTimeDragPrevented.current?.getTime() < scrollLockTimeout && swipeAmount === 0) {
if (
lastTimeDragPrevented.current &&
date.getTime() - lastTimeDragPrevented.current.getTime() < scrollLockTimeout &&
swipeAmount === 0
) {
lastTimeDragPrevented.current = date;
return false;
}
Expand Down Expand Up @@ -581,7 +585,7 @@ export function Root({
dragEndTime.current = new Date();
}

function onRelease(event: React.PointerEvent<HTMLDivElement>) {
function onRelease(event: React.PointerEvent<HTMLDivElement> | null) {
if (!isDragging || !drawerRef.current) return;

drawerRef.current.classList.remove(DRAG_CLASS);
Expand All @@ -590,7 +594,7 @@ export function Root({
dragEndTime.current = new Date();
const swipeAmount = getTranslate(drawerRef.current, direction);

if (!shouldDrag(event.target, false) || !swipeAmount || Number.isNaN(swipeAmount)) return;
if (!event || !shouldDrag(event.target, false) || !swipeAmount || Number.isNaN(swipeAmount)) return;

if (dragStartTime.current === null) return;

Expand Down Expand Up @@ -790,9 +794,11 @@ export const Overlay = React.forwardRef<HTMLDivElement, React.ComponentPropsWith
return null;
}

const onMouseUp = React.useCallback((event: React.PointerEvent<HTMLDivElement>) => onRelease(event), [onRelease]);

return (
<DialogPrimitive.Overlay
onMouseUp={onRelease}
onMouseUp={onMouseUp}
ref={composedRef}
data-vaul-overlay=""
data-vaul-snap-points={isOpen && hasSnapPoints ? 'true' : 'false'}
Expand Down Expand Up @@ -867,7 +873,7 @@ export const Content = React.forwardRef<HTMLDivElement, ContentProps>(function (
}
}, []);

function handleOnPointerUp(event: React.PointerEvent<HTMLDivElement>) {
function handleOnPointerUp(event: React.PointerEvent<HTMLDivElement> | null) {
pointerStartRef.current = null;
wasBeyondThePointRef.current = false;
onRelease(event);
Expand Down Expand Up @@ -1005,8 +1011,10 @@ export const Handle = React.forwardRef<HTMLDivElement, HandleProps>(function (
// Make sure to clear the timeout id if the user releases the handle before the cancel timeout
handleCancelInteraction();

if ((!snapPoints || snapPoints.length === 0) && dismissible) {
closeDrawer();
if (!snapPoints || snapPoints.length === 0) {
if (!dismissible) {
closeDrawer();
}
return;
}

Expand All @@ -1031,7 +1039,9 @@ export const Handle = React.forwardRef<HTMLDivElement, HandleProps>(function (
}

function handleCancelInteraction() {
window.clearTimeout(closeTimeoutIdRef.current);
if (closeTimeoutIdRef.current) {
window.clearTimeout(closeTimeoutIdRef.current);
}
shouldCancelInteractionRef.current = false;
}

Expand Down
6 changes: 5 additions & 1 deletion src/use-prevent-scroll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,11 +267,15 @@ function preventScrollMobileSafari() {
}

// Sets a CSS property on an element, and returns a function to revert it to the previous value.
function setStyle(element: HTMLElement, style: string, value: string) {
function setStyle(element: HTMLElement, style: keyof React.CSSProperties, value: string) {
// https://github.com/microsoft/TypeScript/issues/17827#issuecomment-391663310
// @ts-ignore
let cur = element.style[style];
// @ts-ignore
element.style[style] = value;

return () => {
// @ts-ignore
element.style[style] = cur;
};
}
Expand Down
5 changes: 3 additions & 2 deletions src/use-snap-points.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export function useSnapPoints({
);

const activeSnapPointIndex = React.useMemo(
() => snapPoints?.findIndex((snapPoint) => snapPoint === activeSnapPoint),
() => snapPoints?.findIndex((snapPoint) => snapPoint === activeSnapPoint) ?? null,
[snapPoints, activeSnapPoint],
);

Expand Down Expand Up @@ -126,6 +126,7 @@ export function useSnapPoints({
if (
snapPointsOffset &&
newSnapPointIndex !== snapPointsOffset.length - 1 &&
fadeFromIndex !== undefined &&
newSnapPointIndex !== fadeFromIndex &&
newSnapPointIndex < fadeFromIndex
) {
Expand Down Expand Up @@ -205,7 +206,7 @@ export function useSnapPoints({
const dragDirection = hasDraggedUp ? 1 : -1; // 1 = up, -1 = down

// Don't do anything if we swipe upwards while being on the last snap point
if (dragDirection > 0 && isLastSnapPoint) {
if (dragDirection > 0 && isLastSnapPoint && snapPoints) {
snapToPoint(snapPointsOffset[snapPoints.length - 1]);
return;
}
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"target": "es2018",
"moduleResolution": "node",
"esModuleInterop": true,
"lib": ["es2015", "dom"]
"lib": ["es2015", "dom"],
"strict": true
},
"include": ["src"],
}

0 comments on commit 1a8d000

Please sign in to comment.