Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

refactor modals to use Vaul

Steven Teyaafecb7a6a06
diff --git a/app/ui/modal.tsx b/app/ui/modal.tsx
index ec26e13f2d..0c807714b1 100644
--- a/app/ui/modal.tsx
+++ b/app/ui/modal.tsx
@@ -5,6 +5,8 @@ import { useRouter } from "next/navigation";
import FocusTrap from "focus-trap-react";
import { AnimatePresence, motion, useAnimation } from "framer-motion";
import { cn } from "#/lib/utils";
+import { Drawer } from "vaul";
+import useWindowSize from "#/lib/hooks/use-window-size";
export default function Modal({
children,
@@ -22,7 +24,6 @@ export default function Modal({
disableDefaultHide?: boolean;
}) {
const router = useRouter();
- const mobileModalRef = useRef<HTMLDivElement | null>(null);
const closeModal = ({ dragged }: { dragged?: boolean } = {}) => {
if (disableDefaultHide && !dragged) {
@@ -51,26 +52,29 @@ export default function Modal({
return () => document.removeEventListener("keydown", onKeyDown);
}, [showModal, closeModal]);
- const controls = useAnimation();
- const transitionProps = { type: "spring", stiffness: 500, damping: 30 };
- useEffect(() => {
- controls.start({
- y: 0,
- transition: transitionProps,
- });
- }, []);
+ const { isMobile } = useWindowSize();
- async function handleDragEnd(_, info) {
- const offset = info.offset.y;
- const velocity = info.velocity.y;
- const height = mobileModalRef.current?.getBoundingClientRect().height || 0;
- if (offset > height / 2 || velocity > 800) {
- await controls.start({ y: "100%", transition: transitionProps });
- closeModal({ dragged: true });
- } else {
- controls.start({ y: 0, transition: transitionProps });
- }
- }
+ if (isMobile)
+ return (
+ <Drawer.Root
+ open={showModal}
+ onOpenChange={(open) => {
+ if (!open) {
+ closeModal({ dragged: true });
+ }
+ }}
+ shouldScaleBackground
+ >
+ <Drawer.Overlay className="fixed inset-0 z-30 bg-gray-100 bg-opacity-10 backdrop-blur" />
+ <Drawer.Portal>
+ <Drawer.Content className="fixed bottom-0 left-0 right-0 z-40 mt-24 rounded-t-[10px] border-t border-gray-200 bg-white">
+ <div className="mx-auto my-3 h-1 w-12 rounded-full bg-gray-300" />
+ {children}
+ </Drawer.Content>
+ <Drawer.Overlay />
+ </Drawer.Portal>
+ </Drawer.Root>
+ );
return (
<AnimatePresence>
@@ -90,26 +94,6 @@ export default function Modal({
}}
>
<div className="absolute">
- <motion.div
- ref={mobileModalRef}
- key="mobile-modal"
- className="rounded-t-4xl group fixed inset-x-0 bottom-0 z-40 max-h-[80vh] w-screen cursor-grab overflow-scroll bg-white active:cursor-grabbing md:hidden"
- initial={{ y: "100%" }}
- animate={controls}
- exit={{ y: "100%" }}
- transition={transitionProps}
- drag="y"
- dragDirectionLock
- onDragEnd={handleDragEnd}
- dragElastic={{ top: 0, bottom: 1 }}
- dragConstraints={{ top: 0, bottom: 0 }}
- >
- <div className="rounded-t-4xl sticky top-0 z-20 flex h-7 w-full items-center justify-center border-t border-gray-200">
- <div className="-mr-1 h-1 w-6 rounded-full bg-gray-300 transition-all group-active:rotate-12" />
- <div className="h-1 w-6 rounded-full bg-gray-300 transition-all group-active:-rotate-12" />
- </div>
- {children}
- </motion.div>
<motion.dialog
key="desktop-modal"
className={cn(
diff --git a/app/ui/popover.tsx b/app/ui/popover.tsx
index e7361fb9e7..8ff5e51e43 100644
--- a/app/ui/popover.tsx
+++ b/app/ui/popover.tsx
@@ -1,9 +1,9 @@
"use client";
import { useParams } from "next/navigation";
-import { ReactNode, useEffect, useRef } from "react";
+import { ReactNode, useEffect } from "react";
import * as PopoverPrimitive from "@radix-ui/react-popover";
-import { AnimatePresence, motion, useAnimation } from "framer-motion";
+import { Drawer } from "vaul";
import useWindowSize from "#/lib/hooks/use-window-size";
export default function Popover({
@@ -20,90 +20,43 @@ export default function Popover({
setOpenPopover;
}) {
const { slug } = useParams() as { slug?: string };
-
- const mobileTooltipRef = useRef<HTMLDivElement | null>(null);
- const controls = useAnimation();
- const transitionProps = { type: "spring", stiffness: 500, damping: 30 };
-
- async function handleDragEnd(_, info) {
- const offset = info.offset.y;
- const velocity = info.velocity.y;
- const height =
- mobileTooltipRef.current?.getBoundingClientRect().height || 0;
- if (offset > height / 2 || velocity > 800) {
- await controls.start({ y: "100%", transition: transitionProps });
- setOpenPopover(false);
- } else {
- controls.start({ y: 0, transition: transitionProps });
- }
- }
- const { width } = useWindowSize();
+ const { isMobile } = useWindowSize();
// workaround to make popover close when route changes on desktop
useEffect(() => {
setOpenPopover(false);
}, [slug]);
- return (
- <>
+ return isMobile ? (
+ <Drawer.Root
+ open={openPopover}
+ onOpenChange={setOpenPopover}
+ shouldScaleBackground
+ >
<div className="sm:hidden">{children}</div>
- <AnimatePresence>
- {openPopover && (
- <>
- <motion.div
- ref={mobileTooltipRef}
- key="mobile-tooltip"
- className="group fixed inset-x-0 bottom-0 z-40 w-screen cursor-grab active:cursor-grabbing sm:hidden"
- initial={{ y: "100%" }}
- animate={{
- y: openPopover ? 0 : "100%",
- transition: transitionProps,
- }}
- exit={{ y: "100%" }}
- transition={transitionProps}
- drag="y"
- dragDirectionLock
- onDragEnd={handleDragEnd}
- dragElastic={{ top: 0, bottom: 1 }}
- dragConstraints={{ top: 0, bottom: 0 }}
- >
- <div
- className={`rounded-t-4xl -mb-1 flex h-7 w-full items-center justify-center border-t border-gray-200 bg-white`}
- >
- <div className="-mr-1 h-1 w-6 rounded-full bg-gray-300 transition-all group-active:rotate-12" />
- <div className="h-1 w-6 rounded-full bg-gray-300 transition-all group-active:-rotate-12" />
- </div>
- <div className="flex min-h-[150px] w-full items-center justify-center overflow-hidden bg-white pb-8 align-middle shadow-xl">
- {content}
- </div>
- </motion.div>
- <motion.div
- key="mobile-tooltip-backdrop"
- className="fixed inset-0 z-30 bg-gray-100 bg-opacity-10 backdrop-blur sm:hidden"
- initial={{ opacity: 0 }}
- animate={{ opacity: 1 }}
- exit={{ opacity: 0 }}
- onClick={() => setOpenPopover(false)}
- />
- </>
- )}
- </AnimatePresence>
- <PopoverPrimitive.Root
- // workaround to make popover work on mobile (without this it'll automatically close on click without changing route)
- open={width > 640 && openPopover}
- onOpenChange={width > 640 && setOpenPopover}
+ <Drawer.Overlay className="fixed inset-0 z-30 bg-gray-100 bg-opacity-10 backdrop-blur" />
+ <Drawer.Portal>
+ <Drawer.Content className="fixed bottom-0 left-0 right-0 z-40 mt-24 rounded-t-[10px] border-t border-gray-200 bg-white">
+ <div className="mx-auto my-3 h-1 w-12 rounded-full bg-gray-300" />
+ <div className="flex min-h-[150px] w-full items-center justify-center overflow-hidden bg-white pb-8 align-middle shadow-xl">
+ {content}
+ </div>
+ </Drawer.Content>
+ <Drawer.Overlay />
+ </Drawer.Portal>
+ </Drawer.Root>
+ ) : (
+ <PopoverPrimitive.Root open={openPopover} onOpenChange={setOpenPopover}>
+ <PopoverPrimitive.Trigger className="hidden sm:inline-flex" asChild>
+ {children}
+ </PopoverPrimitive.Trigger>
+ <PopoverPrimitive.Content
+ sideOffset={4}
+ align={align}
+ className="z-20 hidden animate-slide-up-fade items-center rounded-md border border-gray-200 bg-white drop-shadow-lg sm:block"
>
- <PopoverPrimitive.Trigger className="hidden sm:inline-flex" asChild>
- {children}
- </PopoverPrimitive.Trigger>
- <PopoverPrimitive.Content
- sideOffset={4}
- align={align}
- className="z-20 hidden animate-slide-up-fade items-center rounded-md border border-gray-200 bg-white drop-shadow-lg sm:block"
- >
- {content}
- </PopoverPrimitive.Content>
- </PopoverPrimitive.Root>
- </>
+ {content}
+ </PopoverPrimitive.Content>
+ </PopoverPrimitive.Root>
);
}
diff --git a/components/app/links/link-filters.tsx b/components/app/links/link-filters.tsx
index a288450cba..98801633e3 100644
--- a/components/app/links/link-filters.tsx
+++ b/components/app/links/link-filters.tsx
@@ -470,7 +470,6 @@ const TagPopover = ({ tag, count }: { tag: TagProps; count: number }) => {
</div>
}
align="end"
- desktopOnly
openPopover={openPopover}
setOpenPopover={setOpenPopover}
>
diff --git a/components/app/modals/add-edit-link-modal/index.tsx b/components/app/modals/add-edit-link-modal/index.tsx
index 27f09c4db1..406c9c2260 100644
--- a/components/app/modals/add-edit-link-modal/index.tsx
+++ b/components/app/modals/add-edit-link-modal/index.tsx
@@ -273,6 +273,7 @@ function AddEditLinkModal({
setShowModal={setShowAddEditLinkModal}
className="max-w-screen-lg"
disableDefaultHide={homepageDemo ? false : true}
+ {...(welcomeFlow && { onClose: () => router.back() })}
>
<div className="relative grid max-h-[min(906px,_90vh)] w-full divide-x divide-gray-100 overflow-auto scrollbar-hide md:grid-cols-2">
{!welcomeFlow && !homepageDemo && (
diff --git a/components/app/modals/add-project-modal.tsx b/components/app/modals/add-project-modal.tsx
index 96a85a3734..d87084715b 100644
--- a/components/app/modals/add-project-modal.tsx
+++ b/components/app/modals/add-project-modal.tsx
@@ -86,6 +86,7 @@ function AddProjectModalHelper({
showModal={showAddProjectModal}
setShowModal={setShowAddProjectModal}
disableDefaultHide={welcomeFlow}
+ {...(welcomeFlow && { onClose: () => router.back() })}
>
<div className="flex flex-col items-center justify-center space-y-3 border-b border-gray-200 px-4 py-4 pt-8 sm:px-16">
<Logo />
diff --git a/components/app/modals/upgrade-plan-modal.tsx b/components/app/modals/upgrade-plan-modal.tsx
index d291a76d20..c4c28326af 100644
--- a/components/app/modals/upgrade-plan-modal.tsx
+++ b/components/app/modals/upgrade-plan-modal.tsx
@@ -55,6 +55,7 @@ function UpgradePlanModal({
setShowModal={setShowUpgradePlanModal}
className="max-w-lg"
disableDefaultHide={welcomeFlow}
+ {...(welcomeFlow && { onClose: () => router.back() })}
>
<motion.div
variants={{
diff --git a/components/shared/popover.tsx b/components/shared/popover.tsx
index 19b32541ea..82e42eb814 100644
--- a/components/shared/popover.tsx
+++ b/components/shared/popover.tsx
@@ -1,120 +1,60 @@
import { useRouter } from "next/router";
import { ReactNode, useEffect, useRef } from "react";
import * as PopoverPrimitive from "@radix-ui/react-popover";
-import { AnimatePresence, motion, useAnimation } from "framer-motion";
-import { cn } from "#/lib/utils";
+import { Drawer } from "vaul";
+import useWindowSize from "#/lib/hooks/use-window-size";
export default function Popover({
children,
content,
align = "center",
- desktopOnly,
openPopover,
setOpenPopover,
}: {
children: ReactNode;
content: ReactNode | string;
align?: "center" | "start" | "end";
- desktopOnly?: boolean;
openPopover: boolean;
setOpenPopover: (open: boolean) => void;
}) {
const router = useRouter();
-
- const mobileTooltipRef = useRef<HTMLDivElement | null>(null);
- const controls = useAnimation();
- const transitionProps = { type: "spring", stiffness: 500, damping: 30 };
-
- async function handleDragEnd(_, info) {
- const offset = info.offset.y;
- const velocity = info.velocity.y;
- const height =
- mobileTooltipRef.current?.getBoundingClientRect().height || 0;
- if (offset > height / 2 || velocity > 800) {
- await controls.start({ y: "100%", transition: transitionProps });
- setOpenPopover(false);
- } else {
- controls.start({ y: 0, transition: transitionProps });
- }
- }
+ const { isMobile } = useWindowSize();
// workaround to make popover close when route changes on desktop
useEffect(() => {
setOpenPopover(false);
}, [router.query.slug]);
- return (
- <>
- {!desktopOnly && (
- <>
- <div className="sm:hidden">{children}</div>
- <AnimatePresence>
- {openPopover && (
- <>
- <motion.div
- ref={mobileTooltipRef}
- key="mobile-tooltip"
- className="group fixed inset-x-0 bottom-0 left-0 z-40 w-screen cursor-grab active:cursor-grabbing sm:hidden"
- initial={{ y: "100%" }}
- animate={{
- y: openPopover ? 0 : "100%",
- transition: transitionProps,
- }}
- exit={{ y: "100%" }}
- transition={transitionProps}
- drag="y"
- dragDirectionLock
- onDragEnd={handleDragEnd}
- dragElastic={{ top: 0, bottom: 1 }}
- dragConstraints={{ top: 0, bottom: 0 }}
- >
- <div
- className={`rounded-t-4xl -mb-1 flex h-7 w-full items-center justify-center border-t border-gray-200 bg-white`}
- >
- <div className="-mr-1 h-1 w-6 rounded-full bg-gray-300 transition-all group-active:rotate-12" />
- <div className="h-1 w-6 rounded-full bg-gray-300 transition-all group-active:-rotate-12" />
- </div>
- <div className="flex min-h-[150px] w-full items-center justify-center overflow-hidden bg-white pb-8 align-middle shadow-xl">
- {content}
- </div>
- </motion.div>
- <motion.div
- key="mobile-tooltip-backdrop"
- className="fixed inset-0 z-30 bg-gray-100 bg-opacity-10 backdrop-blur sm:hidden"
- initial={{ opacity: 0 }}
- animate={{ opacity: 1 }}
- exit={{ opacity: 0 }}
- onClick={() => setOpenPopover(false)}
- />
- </>
- )}
- </AnimatePresence>
- </>
- )}
- <PopoverPrimitive.Root open={openPopover} onOpenChange={setOpenPopover}>
- <PopoverPrimitive.Trigger
- {...(!desktopOnly && {
- className: "hidden sm:inline-flex",
- })}
- asChild
- >
- {children}
- </PopoverPrimitive.Trigger>
- <PopoverPrimitive.Portal>
- <PopoverPrimitive.Content
- sideOffset={4}
- align={align}
- className={cn(
- "z-40 animate-slide-up-fade items-center rounded-md border border-gray-200 bg-white drop-shadow-lg",
- {
- "hidden sm:block": !desktopOnly,
- },
- )}
- >
+ return isMobile ? (
+ <Drawer.Root
+ open={openPopover}
+ onOpenChange={setOpenPopover}
+ shouldScaleBackground
+ >
+ <div className="sm:hidden">{children}</div>
+ <Drawer.Overlay className="fixed inset-0 z-30 bg-gray-100 bg-opacity-10 backdrop-blur" />
+ <Drawer.Portal>
+ <Drawer.Content className="fixed bottom-0 left-0 right-0 z-40 mt-24 rounded-t-[10px] border-t border-gray-200 bg-white">
+ <div className="mx-auto my-3 h-1 w-12 rounded-full bg-gray-300" />
+ <div className="flex min-h-[150px] w-full items-center justify-center overflow-hidden bg-white pb-8 align-middle shadow-xl">
{content}
- </PopoverPrimitive.Content>
- </PopoverPrimitive.Portal>
- </PopoverPrimitive.Root>
- </>
+ </div>
+ </Drawer.Content>
+ <Drawer.Overlay />
+ </Drawer.Portal>
+ </Drawer.Root>
+ ) : (
+ <PopoverPrimitive.Root open={openPopover} onOpenChange={setOpenPopover}>
+ <PopoverPrimitive.Trigger className="hidden sm:inline-flex" asChild>
+ {children}
+ </PopoverPrimitive.Trigger>
+ <PopoverPrimitive.Content
+ sideOffset={4}
+ align={align}
+ className="z-20 hidden animate-slide-up-fade items-center rounded-md border border-gray-200 bg-white drop-shadow-lg sm:block"
+ >
+ {content}
+ </PopoverPrimitive.Content>
+ </PopoverPrimitive.Root>
);
}
diff --git a/lib/hooks/use-window-size.ts b/lib/hooks/use-window-size.ts
index 2f6e0a0945..e6b87c08ac 100644
--- a/lib/hooks/use-window-size.ts
+++ b/lib/hooks/use-window-size.ts
@@ -1,14 +1,18 @@
-import { useState, useEffect } from "react";
-// Define general type for useWindowSize hook, which includes width and height
-interface Size {
+import { useState, useEffect, useMemo } from "react";
+
+// Hook
+export default function useWindowSize(): {
width: number;
height: number;
-}
-// Hook
-export default function useWindowSize(): Size {
+ isMobile: boolean;
+ isDesktop: boolean;
+} {
// Initialize state with undefined width/height so server and client renders match
// Learn more here: https://joshwcomeau.com/react/the-perils-of-rehydration/
- const [windowSize, setWindowSize] = useState<Size>({
+ const [windowSize, setWindowSize] = useState<{
+ width: number;
+ height: number;
+ }>({
width: 0,
height: 0,
});
@@ -28,5 +32,17 @@ export default function useWindowSize(): Size {
// Remove event listener on cleanup
return () => window.removeEventListener("resize", handleResize);
}, []); // Empty array ensures that effect is only run on mount
- return windowSize;
+
+ // Memoize the result of the calculations using useMemo
+ const memoizedWindowSize = useMemo(() => {
+ return {
+ ...windowSize,
+ isMobile:
+ typeof windowSize?.width === "number" && windowSize?.width < 768,
+ isDesktop:
+ typeof windowSize?.width === "number" && windowSize?.width >= 768,
+ };
+ }, [windowSize]);
+
+ return memoizedWindowSize;
}
diff --git a/package.json b/package.json
index 9043566c4a..3536383f41 100644
--- a/package.json
+++ b/package.json
@@ -85,7 +85,8 @@
"tailwind-scrollbar-hide": "^1.1.7",
"tailwindcss-radix": "^2.8.0",
"topojson-client": "^3.1.0",
- "use-debounce": "^8.0.4"
+ "use-debounce": "^8.0.4",
+ "vaul": "^0.0.6"
},
"devDependencies": {
"@trivago/prettier-plugin-sort-imports": "^4.0.0",
diff --git a/yarn.lock b/yarn.lock
index db7a1962df..d5c925dfae 100644
--- a/yarn.lock
+++ b/yarn.lock
@@ -1595,6 +1595,27 @@
aria-hidden "^1.1.1"
react-remove-scroll "2.5.4"
+"@radix-ui/react-dialog@^1.0.4":
+ version "1.0.4"
+ resolved "https://registry.yarnpkg.com/@radix-ui/react-dialog/-/react-dialog-1.0.4.tgz#06bce6c16bb93eb36d7a8589e665a20f4c1c52c1"
+ integrity sha512-hJtRy/jPULGQZceSAP2Re6/4NpKo8im6V8P2hUqZsdFiSL8l35kYsw3qbRI6Ay5mQd2+wlLqje770eq+RJ3yZg==
+ dependencies:
+ "@babel/runtime" "^7.13.10"
+ "@radix-ui/primitive" "1.0.1"
+ "@radix-ui/react-compose-refs" "1.0.1"
+ "@radix-ui/react-context" "1.0.1"
+ "@radix-ui/react-dismissable-layer" "1.0.4"
+ "@radix-ui/react-focus-guards" "1.0.1"
+ "@radix-ui/react-focus-scope" "1.0.3"
+ "@radix-ui/react-id" "1.0.1"
+ "@radix-ui/react-portal" "1.0.3"
+ "@radix-ui/react-presence" "1.0.1"
+ "@radix-ui/react-primitive" "1.0.3"
+ "@radix-ui/react-slot" "1.0.2"
+ "@radix-ui/react-use-controllable-state" "1.0.1"
+ aria-hidden "^1.1.1"
+ react-remove-scroll "2.5.5"
+
"@radix-ui/[email protected]":
version "1.0.0"
resolved "https://registry.npmjs.org/@radix-ui/react-direction/-/react-direction-1.0.0.tgz"
@@ -1645,6 +1666,13 @@
dependencies:
"@babel/runtime" "^7.13.10"
+"@radix-ui/[email protected]":
+ version "1.0.1"
+ resolved "https://registry.yarnpkg.com/@radix-ui/react-focus-guards/-/react-focus-guards-1.0.1.tgz#1ea7e32092216b946397866199d892f71f7f98ad"
+ integrity sha512-Rect2dWbQ8waGzhMavsIbmSVCgYxkXLxxR3ZvCX79JOglzdEy4JXMb98lq4hPxUbLr77nP0UOGf4rcMU+s1pUA==
+ dependencies:
+ "@babel/runtime" "^7.13.10"
+
"@radix-ui/[email protected]":
version "1.0.0"
resolved "https://registry.npmjs.org/@radix-ui/react-focus-scope/-/react-focus-scope-1.0.0.tgz"
@@ -1665,6 +1693,16 @@
"@radix-ui/react-primitive" "1.0.2"
"@radix-ui/react-use-callback-ref" "1.0.0"
+"@radix-ui/[email protected]":
+ version "1.0.3"
+ resolved "https://registry.yarnpkg.com/@radix-ui/react-focus-scope/-/react-focus-scope-1.0.3.tgz#9c2e8d4ed1189a1d419ee61edd5c1828726472f9"
+ integrity sha512-upXdPfqI4islj2CslyfUBNlaJCPybbqRHAi1KER7Isel9Q2AtSJ0zRBZv8mWQiFXD2nyAJ4BhC3yXgZ6kMBSrQ==
+ dependencies:
+ "@babel/runtime" "^7.13.10"
+ "@radix-ui/react-compose-refs" "1.0.1"
+ "@radix-ui/react-primitive" "1.0.3"
+ "@radix-ui/react-use-callback-ref" "1.0.1"
+
"@radix-ui/[email protected]":
version "1.0.0"
resolved "https://registry.npmjs.org/@radix-ui/react-id/-/react-id-1.0.0.tgz"
@@ -8515,6 +8553,13 @@ validate-npm-package-name@^5.0.0:
dependencies:
builtins "^5.0.0"
+vaul@^0.0.6:
+ version "0.0.6"
+ resolved "https://registry.yarnpkg.com/vaul/-/vaul-0.0.6.tgz#7b9a0b38bcf4637a462062a10419f5f9857a5904"
+ integrity sha512-YTI1wkukcgw8EKrZ4cgnj/NDXue0Y6vicGkix1iu4j2oNX2Wcdr9FXdPlvXxhzBdDfEl+dIMu8zjgri3UIawNg==
+ dependencies:
+ "@radix-ui/react-dialog" "^1.0.4"
+
vfile-location@^4.0.0:
version "4.1.0"
resolved "https://registry.npmjs.org/vfile-location/-/vfile-location-4.1.0.tgz"