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.

Add QR code modal

Tim Wilsonc753ea8bb37f
diff --git a/apps/web/ui/modals/add-edit-link-modal-new/index.tsx b/apps/web/ui/modals/add-edit-link-modal-new/index.tsx
index 4ae550d530..f4cc3c0f6e 100644
--- a/apps/web/ui/modals/add-edit-link-modal-new/index.tsx
+++ b/apps/web/ui/modals/add-edit-link-modal-new/index.tsx
@@ -52,6 +52,9 @@ import { useMetatags } from "./use-metatags";
import { UTMBuilder } from "./utm-builder";
export const LinkModalContext = createContext<{
+ workspaceId?: string;
+ workspacePlan?: string;
+ workspaceLogo?: string;
generatingMetatags: boolean;
}>({ generatingMetatags: false });
@@ -88,7 +91,7 @@ function AddEditLinkModalInner({
const { slug } = params;
const searchParams = useSearchParams();
const { queryParams } = useRouterStuff();
- const { id: workspaceId, nextPlan } = useWorkspace();
+ const { id: workspaceId, plan, nextPlan, logo } = useWorkspace();
const {
control,
@@ -200,7 +203,14 @@ function AddEditLinkModalInner({
});
}}
>
- <LinkModalContext.Provider value={{ generatingMetatags }}>
+ <LinkModalContext.Provider
+ value={{
+ workspaceId,
+ workspacePlan: plan,
+ workspaceLogo: logo ?? undefined,
+ generatingMetatags,
+ }}
+ >
<form
onSubmit={handleSubmit(async (data) => {
// @ts-ignore – exclude extra attributes from `data` object before sending to API
diff --git a/apps/web/ui/modals/add-edit-link-modal-new/og-modal.tsx b/apps/web/ui/modals/add-edit-link-modal-new/og-modal.tsx
index 077cae45f6..8216c2217d 100644
--- a/apps/web/ui/modals/add-edit-link-modal-new/og-modal.tsx
+++ b/apps/web/ui/modals/add-edit-link-modal-new/og-modal.tsx
@@ -215,7 +215,7 @@ function OGModalInner({
}
side="right"
>
- <kbd className="flex size-6 cursor-default items-center justify-center rounded-md border border-gray-200 text-xs text-gray-950">
+ <kbd className="flex size-6 cursor-default items-center justify-center rounded-md border border-gray-200 font-sans text-xs text-gray-950">
D
</kbd>
</Tooltip>
diff --git a/apps/web/ui/modals/add-edit-link-modal-new/qr-code-design-modal.tsx b/apps/web/ui/modals/add-edit-link-modal-new/qr-code-design-modal.tsx
new file mode 100644
index 0000000000..3c1a013775
--- /dev/null
+++ b/apps/web/ui/modals/add-edit-link-modal-new/qr-code-design-modal.tsx
@@ -0,0 +1,248 @@
+import { QRCode } from "@/ui/shared/qr-code";
+import {
+ Button,
+ InfoTooltip,
+ Modal,
+ ShimmerDots,
+ SimpleTooltipContent,
+ Switch,
+ Tooltip,
+ useMediaQuery,
+} from "@dub/ui";
+import { Check2 } from "@dub/ui/src/icons";
+import { cn } from "@dub/utils";
+import { AnimatePresence, motion } from "framer-motion";
+import {
+ Dispatch,
+ SetStateAction,
+ useCallback,
+ useId,
+ useMemo,
+ useState,
+} from "react";
+import { QRCodeDesign } from "./qr-code-preview";
+
+const DEFAULT_COLORS = [
+ "#000000",
+ "#BDC2C6",
+ "#C73E33",
+ "#DF6547",
+ "#F4B3D7",
+ "#F6CF54",
+ "#49A065",
+ "#AE49BF",
+ "#68B6E9",
+ "#2146B7",
+];
+
+type QRCodeDesignModalProps = {
+ url?: string;
+ logo: string;
+ data: QRCodeDesign;
+ setData: Dispatch<SetStateAction<QRCodeDesign>>;
+};
+
+function QRCodeDesignModal(
+ props: {
+ showQRCodeDesignModal: boolean;
+ setShowQRCodeDesignModal: Dispatch<SetStateAction<boolean>>;
+ } & QRCodeDesignModalProps,
+) {
+ return props.showQRCodeDesignModal ? (
+ <QRCodeDesignModalInner {...props} />
+ ) : null;
+}
+
+function QRCodeDesignModalInner({
+ showQRCodeDesignModal,
+ setShowQRCodeDesignModal,
+ url,
+ logo,
+ data: dataParent,
+ setData: setDataParent,
+}: {
+ showQRCodeDesignModal: boolean;
+ setShowQRCodeDesignModal: Dispatch<SetStateAction<boolean>>;
+} & QRCodeDesignModalProps) {
+ const id = useId();
+ const { isMobile } = useMediaQuery();
+
+ const [data, setData] = useState(dataParent);
+
+ return (
+ <Modal
+ showModal={showQRCodeDesignModal}
+ setShowModal={setShowQRCodeDesignModal}
+ className="max-w-[500px]"
+ >
+ <form
+ className="flex flex-col gap-6 p-4"
+ onSubmit={(e) => {
+ e.preventDefault();
+ e.stopPropagation();
+ setShowQRCodeDesignModal(false);
+
+ setDataParent(data);
+ }}
+ >
+ <div className="flex items-center justify-between">
+ <h3 className="text-lg font-medium">Link Design</h3>
+ <div className="max-md:hidden">
+ <Tooltip
+ content={
+ <div className="px-2 py-1 text-xs text-gray-700">
+ Press <strong className="font-medium text-gray-950">Q</strong>{" "}
+ to open this quickly
+ </div>
+ }
+ side="right"
+ >
+ <kbd className="flex size-6 cursor-default items-center justify-center rounded-md border border-gray-200 font-sans text-xs text-gray-950">
+ Q
+ </kbd>
+ </Tooltip>
+ </div>
+ </div>
+
+ <div>
+ <div className="flex items-center gap-2">
+ <span className="text-sm font-medium text-gray-700">
+ QR Code Preview
+ </span>
+ <InfoTooltip
+ content={
+ <SimpleTooltipContent
+ title="Customize your QR code to fit your brand."
+ cta="Learn more."
+ href="https://dub.co/help/article/custom-qr-codes"
+ />
+ }
+ />
+ </div>
+ <div className="relative mt-2 flex h-44 items-center justify-center overflow-hidden rounded-md border border-gray-300">
+ {!isMobile && (
+ <ShimmerDots className="opacity-30 [mask-image:radial-gradient(40%_80%,transparent_50%,black)]" />
+ )}
+ {url && (
+ <AnimatePresence mode="wait">
+ <motion.div
+ key={data.fgColor + data.showLogo}
+ initial={{ filter: "blur(2px)", opacity: 0.4 }}
+ animate={{ filter: "blur(0px)", opacity: 1 }}
+ exit={{ filter: "blur(2px)", opacity: 0.4 }}
+ transition={{ duration: 0.1 }}
+ className="relative flex size-full items-center justify-center"
+ >
+ <QRCode
+ url={url}
+ fgColor={data.fgColor}
+ showLogo={data.showLogo}
+ logo={logo}
+ scale={1}
+ />
+ </motion.div>
+ </AnimatePresence>
+ )}
+ </div>
+ </div>
+
+ {/* Logo toggle */}
+ <div className="flex items-center justify-between">
+ <div className="flex items-center gap-2">
+ <label
+ className="text-sm font-medium text-gray-700"
+ htmlFor={`${id}-show-logo`}
+ >
+ Logo
+ </label>
+ <InfoTooltip
+ content={
+ <SimpleTooltipContent
+ title="Display your logo in the center of the QR code."
+ cta="Learn more."
+ href="https://dub.co/help/article/custom-qr-codes"
+ />
+ }
+ />
+ </div>
+ <Switch
+ id={`${id}-show-logo`}
+ checked={data.showLogo}
+ fn={(checked) => {
+ setData((d) => ({ ...d, showLogo: checked }));
+ }}
+ />
+ </div>
+
+ {/* Color selector */}
+ <div>
+ <span className="block text-sm font-medium text-gray-700">
+ QR Code Color
+ </span>
+ <div className="mt-3 flex flex-wrap justify-between gap-2">
+ {DEFAULT_COLORS.map((color) => {
+ const isSelected = data.fgColor === color;
+ return (
+ <button
+ key={color}
+ type="button"
+ aria-pressed={isSelected}
+ onClick={() => setData((d) => ({ ...d, fgColor: color }))}
+ className={cn(
+ "flex size-8 items-center justify-center rounded-full transition-all",
+ isSelected
+ ? "ring-1 ring-black ring-offset-4"
+ : "ring-black/10 hover:ring-4",
+ )}
+ style={{ backgroundColor: color }}
+ >
+ {isSelected && <Check2 className="size-4 text-white" />}
+ </button>
+ );
+ })}
+ </div>
+ </div>
+
+ <div className="flex items-center justify-end gap-2">
+ <Button
+ type="button"
+ variant="secondary"
+ text="Cancel"
+ className="h-9 w-fit"
+ onClick={() => {
+ setShowQRCodeDesignModal(false);
+ }}
+ />
+ <Button
+ type="submit"
+ variant="primary"
+ text="Save changes"
+ className="h-9 w-fit"
+ />
+ </div>
+ </form>
+ </Modal>
+ );
+}
+
+export function useQRCodeDesignModal(props: QRCodeDesignModalProps) {
+ const [showQRCodeDesignModal, setShowQRCodeDesignModal] = useState(false);
+
+ const QRCodeDesignModalCallback = useCallback(() => {
+ return (
+ <QRCodeDesignModal
+ showQRCodeDesignModal={showQRCodeDesignModal}
+ setShowQRCodeDesignModal={setShowQRCodeDesignModal}
+ {...props}
+ />
+ );
+ }, [showQRCodeDesignModal, setShowQRCodeDesignModal]);
+
+ return useMemo(
+ () => ({
+ setShowQRCodeDesignModal,
+ QRCodeDesignModal: QRCodeDesignModalCallback,
+ }),
+ [setShowQRCodeDesignModal, QRCodeDesignModalCallback],
+ );
+}
diff --git a/apps/web/ui/modals/add-edit-link-modal-new/qr-code-preview.tsx b/apps/web/ui/modals/add-edit-link-modal-new/qr-code-preview.tsx
index 7861ec5c1f..3709395e9a 100644
--- a/apps/web/ui/modals/add-edit-link-modal-new/qr-code-preview.tsx
+++ b/apps/web/ui/modals/add-edit-link-modal-new/qr-code-preview.tsx
@@ -1,61 +1,63 @@
-import { QRCodeSVG } from "@/lib/qr";
-import useWorkspace from "@/lib/swr/use-workspace";
-import { Button, ShimmerDots, useMediaQuery } from "@dub/ui";
-import { Pen2, QRCode } from "@dub/ui/src/icons";
+import { QRCode } from "@/ui/shared/qr-code";
+import {
+ Button,
+ ShimmerDots,
+ useKeyboardShortcut,
+ useLocalStorage,
+ useMediaQuery,
+} from "@dub/ui";
+import { Pen2, QRCode as QRCodeIcon } from "@dub/ui/src/icons";
import { linkConstructor } from "@dub/utils";
import { AnimatePresence, motion } from "framer-motion";
-import { useMemo, useState } from "react";
+import { useContext, useMemo } from "react";
import { useFormContext } from "react-hook-form";
-import { toast } from "sonner";
import { useDebounce } from "use-debounce";
-import { LinkFormData } from ".";
+import { LinkFormData, LinkModalContext } from ".";
+import { useQRCodeDesignModal } from "./qr-code-design-modal";
+
+export type QRCodeDesign = {
+ fgColor: string;
+ showLogo: boolean;
+};
export function QRCodePreview() {
const { isMobile } = useMediaQuery();
- const { logo, plan } = useWorkspace();
+ const { workspaceLogo, workspacePlan } = useContext(LinkModalContext);
const { watch } = useFormContext<LinkFormData>();
const { key: rawKey, domain: rawDomain } = watch();
const [key] = useDebounce(rawKey, 500);
const [domain] = useDebounce(rawDomain, 500);
- const [fgColor] = useState("#000000");
- const [showLogo] = useState(true);
+ const [data, setData] = useLocalStorage<QRCodeDesign>("qr-code-design", {
+ fgColor: "#000000",
+ showLogo: true,
+ });
- const qrData = useMemo(
- () =>
- key && domain
- ? {
- value: linkConstructor({
- key: key,
- domain: domain,
- searchParams: {
- qr: "1",
- },
- }),
- bgColor: "#ffffff",
- fgColor,
- size: 1024,
- level: "Q", // QR Code error correction level: https://blog.qrstuff.com/general/qr-code-error-correction
- includeMargin: false,
- ...(showLogo && {
- imageSettings: {
- src:
- logo && plan !== "free"
- ? logo
- : "https://assets.dub.co/logo.png",
- height: 256,
- width: 256,
- excavate: true,
- },
- }),
- }
- : null,
- [key, domain, showLogo],
- );
+ const shortLinkUrl = useMemo(() => {
+ return key && domain ? linkConstructor({ key, domain }) : undefined;
+ }, [key, domain]);
+
+ const logo =
+ workspaceLogo && workspacePlan !== "free"
+ ? workspaceLogo
+ : "https://assets.dub.co/logo.png";
+
+ const { QRCodeDesignModal, setShowQRCodeDesignModal } = useQRCodeDesignModal({
+ data,
+ setData,
+ url: shortLinkUrl,
+ logo,
+ });
+
+ useKeyboardShortcut("q", () => setShowQRCodeDesignModal(true), {
+ modal: true,
+ enabled: Boolean(shortLinkUrl),
+ });
return (
<div>
+ <QRCodeDesignModal />
<div className="flex items-center justify-between">
<h2 className="text-sm font-medium text-gray-700">QR Code</h2>
<Button
@@ -63,7 +65,7 @@ export function QRCodePreview() {
variant="outline"
icon={<Pen2 className="mx-px size-4" />}
className="h-7 w-fit px-1"
- onClick={() => toast.info("WIP")}
+ onClick={() => setShowQRCodeDesignModal(true)}
disabledTooltip={
key && domain ? undefined : "Enter a short link to customize"
}
@@ -73,40 +75,28 @@ export function QRCodePreview() {
{!isMobile && (
<ShimmerDots className="opacity-30 [mask-image:radial-gradient(40%_80%,transparent_50%,black)]" />
)}
- {qrData ? (
+ {shortLinkUrl ? (
<AnimatePresence mode="wait">
<motion.div
- key={qrData.value}
+ key={shortLinkUrl}
initial={{ filter: "blur(2px)", opacity: 0.4 }}
animate={{ filter: "blur(0px)", opacity: 1 }}
exit={{ filter: "blur(2px)", opacity: 0.4 }}
transition={{ duration: 0.1 }}
className="relative flex size-full items-center justify-center"
>
- <QRCodeSVG
- value={qrData.value}
- size={qrData.size / 16}
- bgColor={qrData.bgColor}
- fgColor={qrData.fgColor}
- level={qrData.level}
- includeMargin={false}
- {...(qrData.imageSettings && {
- imageSettings: {
- ...qrData.imageSettings,
- height: qrData.imageSettings
- ? qrData.imageSettings.height / 16
- : 0,
- width: qrData.imageSettings
- ? qrData.imageSettings.width / 16
- : 0,
- },
- })}
+ <QRCode
+ url={shortLinkUrl}
+ fgColor={data.fgColor}
+ showLogo={data.showLogo}
+ logo={logo}
+ scale={0.5}
/>
</motion.div>
</AnimatePresence>
) : (
<div className="flex size-full flex-col items-center justify-center gap-2">
- <QRCode className="size-5 text-gray-700" />
+ <QRCodeIcon className="size-5 text-gray-700" />
<p className="max-w-32 text-center text-xs text-gray-700">
Enter a short link to generate a preview
</p>
diff --git a/apps/web/ui/shared/qr-code.tsx b/apps/web/ui/shared/qr-code.tsx
new file mode 100644
index 0000000000..d5916e8a4c
--- /dev/null
+++ b/apps/web/ui/shared/qr-code.tsx
@@ -0,0 +1,58 @@
+import { QRCodeSVG } from "@/lib/qr";
+import { useMemo } from "react";
+
+export function QRCode({
+ url,
+ fgColor,
+ showLogo,
+ logo,
+ scale = 1,
+}: {
+ url: string;
+ fgColor?: string;
+ showLogo?: boolean;
+ logo?: string;
+ scale?: number;
+}) {
+ const qrData = useMemo(
+ () => ({
+ value: url,
+ bgColor: "#ffffff",
+ fgColor,
+ size: 1024,
+ level: "Q", // QR Code error correction level: https://blog.qrstuff.com/general/qr-code-error-correction
+ includeMargin: false,
+ ...(showLogo && {
+ imageSettings: {
+ src: logo || "https://assets.dub.co/logo.png",
+ height: 256,
+ width: 256,
+ excavate: true,
+ },
+ }),
+ }),
+ [url, fgColor, showLogo, logo],
+ );
+
+ return (
+ <QRCodeSVG
+ value={qrData.value}
+ size={(qrData.size / 8) * scale}
+ bgColor={qrData.bgColor}
+ fgColor={qrData.fgColor}
+ level={qrData.level}
+ includeMargin={false}
+ {...(qrData.imageSettings && {
+ imageSettings: {
+ ...qrData.imageSettings,
+ height: qrData.imageSettings
+ ? (qrData.imageSettings.height / 8) * scale
+ : 0,
+ width: qrData.imageSettings
+ ? (qrData.imageSettings.width / 8) * scale
+ : 0,
+ },
+ })}
+ />
+ );
+}
diff --git a/packages/ui/src/switch.tsx b/packages/ui/src/switch.tsx
index c8b115f6ca..9a79d07a17 100644
--- a/packages/ui/src/switch.tsx
+++ b/packages/ui/src/switch.tsx
@@ -7,6 +7,7 @@ import { Tooltip } from "./tooltip";
export function Switch({
fn,
+ id,
trackDimensions,
thumbDimensions,
thumbTranslate,
@@ -16,6 +17,7 @@ export function Switch({
disabledTooltip,
}: {
fn?: Dispatch<SetStateAction<boolean>> | ((checked: boolean) => void);
+ id?: string;
trackDimensions?: string;
thumbDimensions?: string;
thumbTranslate?: string;
@@ -32,6 +34,7 @@ export function Switch({
<SwitchPrimitive.Root
checked={loading ? false : checked}
name="switch"
+ id={id}
{...(fn && { onCheckedChange: fn })}
disabled={switchDisabled}
className={cn(