The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Added color picker for QR code foreground color, hide logo toggle
Steven Tey7b65692d04e6
diff --git a/components/app/modals/link-qr-modal.tsx b/components/app/modals/link-qr-modal.tsxindex 16f21c217a..33d5b95143 100644--- a/components/app/modals/link-qr-modal.tsx+++ b/components/app/modals/link-qr-modal.tsx@@ -6,14 +6,17 @@ import {useRef,useState,} from "react";-import BlurImage from "@/components/shared/blur-image";-import { Download, Logo } from "@/components/shared/icons";+import { Download, Logo, ChevronRight } from "@/components/shared/icons";import Modal from "@/components/shared/modal";import { getQRAsCanvas, getQRAsSVGDataUri, QRCodeSVG } from "@/lib/qr";import useProject from "@/lib/swr/use-project";-import { LinkProps } from "@/lib/types";+import { SimpleLinkProps } from "@/lib/types";import { linkConstructor } from "@/lib/utils";+import useUsage from "@/lib/swr/use-usage";+import { HexColorPicker, HexColorInput } from "react-colorful";+import Tooltip, { TooltipContent } from "@/components/shared/tooltip";+import Switch from "@/components/shared/switch";function LinkQRModalHelper({showLinkQRModal,@@ -22,7 +25,7 @@ function LinkQRModalHelper({}: {showLinkQRModal: boolean;setShowLinkQRModal: Dispatch<SetStateAction<boolean>>;- props: LinkProps;+ props: SimpleLinkProps;}) {const anchorRef = useRef<HTMLAnchorElement>();const { project: { domain, logo } = {} } = useProject();@@ -39,7 +42,14 @@ function LinkQRModalHelper({() => linkConstructor({ key: props.key, domain }),[props, domain]);- const qrLogoUrl = useMemo(() => logo || new URL('/static/logo.svg', location.href).href, []);+ const qrLogoUrl = useMemo(() => {+ if (logo) return logo;+ if (process.env.NEXT_PUBLIC_VERCEL_ENV) {+ return "https://dub.sh/static/logo.svg";+ } else {+ return "http://localhost:3000/static/logo.svg";+ }+ }, [logo]);function download(url: string, extension: string) {if (!anchorRef.current) return;@@ -48,22 +58,26 @@ function LinkQRModalHelper({anchorRef.current.click();}+ const [showLogo, setShowLogo] = useState(true);const [qrData, setQrData] = useState({+ value: qrDestUrl,bgColor: "#ffffff",fgColor: "#000000",size: 1024,- level: "Q",- imageSettings: {- src: qrLogoUrl,- height: 256,- width: 256,- excavate: true,- },+ level: "Q", // QR Code error correction level: https://blog.qrstuff.com/general/qr-code-error-correction+ ...(showLogo && {+ imageSettings: {+ src: qrLogoUrl,+ height: 256,+ width: 256,+ excavate: true,+ },+ }),});return (<Modal showModal={showLinkQRModal} setShowModal={setShowLinkQRModal}>- <div className="inline-block w-full sm:max-w-md overflow-hidden align-middle transition-all transform bg-white sm:border sm:border-gray-200 shadow-xl sm:rounded-2xl">+ <div className="inline-block w-full sm:max-w-md align-middle transition-all transform bg-white sm:border sm:border-gray-200 shadow-xl sm:rounded-2xl"><div className="flex flex-col justify-center items-center space-y-3 sm:px-16 px-4 pt-8 py-4 border-b border-gray-200">{avatarUrl ? (<BlurImage@@ -79,63 +93,51 @@ function LinkQRModalHelper({<h3 className="font-medium text-lg">Download QR Code</h3></div>- <div className="flex flex-col space-y-8 text-left bg-gray-50 sm:px-16 px-4 py-8">+ <div className="flex flex-col space-y-6 text-left bg-gray-50 py-6 sm:rounded-b-2xl"><div className="p-4 rounded-lg bg-white mx-auto border-2 border-gray-200"><QRCodeSVG- value={qrDestUrl}- size={128}+ value={qrData.value}+ size={qrData.size / 8}bgColor={qrData.bgColor}fgColor={qrData.fgColor}- level="Q"+ level={qrData.level}includeMargin={false}- imageSettings={{- src: qrLogoUrl,- height: 36,- width: 36,- excavate: true- }}+ imageSettings={+ showLogo && {+ ...qrData.imageSettings,+ height: qrData.imageSettings.height / 8,+ width: qrData.imageSettings.width / 8,+ }+ }/></div>- <div className="flex gap-2">+ <AdvancedSettings+ qrData={qrData}+ setQrData={setQrData}+ setShowLogo={setShowLogo}+ />++ <div className="flex gap-2 sm:px-16 px-4"><button- onClick={() =>- download(- getQRAsSVGDataUri({- value: qrDestUrl,- ...qrData,- }),- "svg"- )+ onClick={async () =>+ download(await getQRAsSVGDataUri(qrData), "svg")}className="py-1.5 px-5 bg-black hover:bg-white rounded-md border border-black text-sm text-white hover:text-black transition-all w-full flex items-center gap-2 justify-center"><Download /> SVG</button><button- onClick={async () =>- download(- await getQRAsCanvas(- { value: qrDestUrl, ...qrData },- "image/png"- ),- "png"- )+ onClick={async () =>+ download(await getQRAsCanvas(qrData, "image/png"), "png")}className="py-1.5 px-5 bg-black hover:bg-white rounded-md border border-black text-sm text-white hover:text-black transition-all w-full flex items-center gap-2 justify-center"><Download /> PNG</button><button- onClick={- async () =>- download(- await getQRAsCanvas(- { value: qrDestUrl, ...qrData },- "image/jpeg"- ),- "jpg"- )+ onClick={async () =>+ download(await getQRAsCanvas(qrData, "image/jpeg"), "jpg")}className="py-1.5 px-5 bg-black hover:bg-white rounded-md border border-black text-sm text-white hover:text-black transition-all w-full flex items-center gap-2 justify-center">@@ -144,14 +146,157 @@ function LinkQRModalHelper({</div>{/* This will be used to prompt downloads. */}- <a className="hidden" download={`${props.key}-qrcode.svg`} ref={anchorRef} />+ <a+ className="hidden"+ download={`${props.key}-qrcode.svg`}+ ref={anchorRef}+ /></div></div></Modal>);}-export function useLinkQRModal({ props }: { props: LinkProps }) {+function AdvancedSettings({ qrData, setQrData, setShowLogo }) {+ const { plan } = useUsage();+ const [expanded, setExpanded] = useState(false);++ return (+ <div>+ <div className="sm:px-16 px-4">+ <button+ type="button"+ className="flex items-center"+ onClick={() => setExpanded(!expanded)}+ >+ <ChevronRight+ className={`h-5 w-5 text-gray-600 ${+ expanded ? "rotate-90" : ""+ } transition-all`}+ />+ <p className="text-gray-600 text-sm">Advanced options</p>+ </button>+ </div>+ {expanded && (+ <div className="mt-4 grid gap-5 bg-white border-t border-b border-gray-200 sm:px-16 px-4 py-8">+ <div>+ <label+ htmlFor="logo-toggle"+ className="block text-sm font-medium text-gray-700"+ >+ Logo+ </label>+ {plan === "Free" ? (+ <Tooltip+ content={+ <TooltipContent+ title="Upgrade to Pro to use remove Dub branding."+ cta="Upgrade to Pro"+ ctaLink="/settings"+ />+ }+ >+ <div className="flex mt-1 space-x-2 items-center cursor-not-allowed">+ <Switch+ trackDimensions="h-6 w-12"+ thumbDimensions="w-5 h-5"+ thumbTranslate="translate-x-6"+ setState={setShowLogo}+ disabled={true}+ />+ <p className="text-gray-600 text-sm">Show Dub.sh Logo</p>+ </div>+ </Tooltip>+ ) : (+ <div className="flex mt-1 space-x-2 items-center">+ <Switch+ trackDimensions="h-6 w-12"+ thumbDimensions="w-5 h-5"+ thumbTranslate="translate-x-6"+ setState={setShowLogo}+ />+ <p className="text-gray-600 text-sm">Show Dub.sh Logo</p>+ </div>+ )}+ </div>+ <div>+ <label+ htmlFor="color"+ className="block text-sm font-medium text-gray-700"+ >+ Foreground Color+ </label>+ {plan === "Free" ? (+ <Tooltip+ content={+ <TooltipContent+ title="Upgrade to Pro to use custom colors."+ cta="Upgrade to Pro"+ ctaLink="/settings"+ />+ }+ >+ <div className="relative flex mt-1 rounded-md shadow-sm h-9 w-48">+ <div+ className="rounded-l-md w-12 h-full border cursor-not-allowed"+ style={{+ backgroundColor: qrData.fgColor,+ borderColor: qrData.fgColor,+ }}+ />+ <div className="flex items-center cursor-not-allowed border border-l-0 border-gray-300 bg-gray-50 text-gray-400 pl-3 w-full rounded-r-md sm:text-sm">+ {qrData.fgColor}+ </div>+ </div>+ </Tooltip>+ ) : (+ <div className="relative flex mt-1 rounded-md shadow-sm h-9 w-48">+ <Tooltip+ content={+ <div className="max-w-xs flex flex-col text-center items-center space-y-3 p-5">+ <HexColorPicker+ color={qrData.fgColor}+ onChange={(color) =>+ setQrData({+ ...qrData,+ fgColor: color,+ })+ }+ />+ </div>+ }+ >+ <button+ className="rounded-l-md w-12 h-full border"+ style={{+ backgroundColor: qrData.fgColor,+ borderColor: qrData.fgColor,+ }}+ />+ </Tooltip>+ <HexColorInput+ id="color"+ name="color"+ color={qrData.fgColor}+ onChange={(color) =>+ setQrData({+ ...qrData,+ fgColor: color,+ })+ }+ prefixed+ className="border border-l-0 border-gray-300 text-gray-900 placeholder-gray-300 focus:border-black focus:ring-black pl-3 block w-full rounded-r-md focus:outline-none sm:text-sm"+ />+ </div>+ )}+ </div>+ </div>+ )}+ </div>+ );+}++export function useLinkQRModal({ props }: { props: SimpleLinkProps }) {const [showLinkQRModal, setShowLinkQRModal] = useState(false);const LinkQRModal = useCallback(() => {diff --git a/components/home/link-card.tsx b/components/home/link-card.tsxindex 1afca29a0d..be9280c033 100644--- a/components/home/link-card.tsx+++ b/components/home/link-card.tsx@@ -1,7 +1,7 @@import { useCallback, useEffect, useMemo, useRef, useState } from "react";import BlurImage from "@/components/shared/blur-image";import CopyButton from "@/components/shared/copy-button";-import { Chart, LoadingDots } from "@/components/shared/icons";+import { Chart, LoadingDots, QR } from "@/components/shared/icons";import useSWR from "swr";import { fetcher, nFormatter, linkConstructor } from "@/lib/utils";import Link from "next/link";@@ -10,6 +10,7 @@ import { FRAMER_MOTION_LIST_ITEM_VARIANTS } from "@/lib/constants";import { SimpleLinkProps } from "@/lib/types";import toast from "react-hot-toast";import { useDebouncedCallback } from "use-debounce";+import { useLinkQRModal } from "@/components/app/modals/link-qr-modal";export default function LinkCard({_key: key,@@ -95,8 +96,16 @@ export default function LinkCard({return () => unsubscribeX();});+ const { setShowLinkQRModal, LinkQRModal } = useLinkQRModal({+ props: {+ key,+ url,+ },+ });+return (<motion.li variants={FRAMER_MOTION_LIST_ITEM_VARIANTS}>+ <LinkQRModal /><motion.divanimate={controls}drag="x"@@ -127,6 +136,13 @@ export default function LinkCard({{linkConstructor({ key, pretty: true })}</a><CopyButton url={linkConstructor({ key })} />+ <button+ onClick={() => setShowLinkQRModal(true)}+ className="group p-1.5 rounded-full bg-gray-100 hover:bg-blue-100 hover:scale-105 active:scale-95 transition-all duration-75"+ >+ <span className="sr-only">Copy</span>+ <QR className="text-gray-700 group-hover:text-blue-800 transition-all" />+ </button><Linkhref={{ pathname: "/", query: { key } }}as={`/stats/${encodeURI(key)}`}diff --git a/components/shared/icons/index.tsx b/components/shared/icons/index.tsxindex a6b071bfff..1544dfee43 100644--- a/components/shared/icons/index.tsx+++ b/components/shared/icons/index.tsx@@ -1,3 +1,5 @@+// Most of these icons are gotten from Vercel's open-sourced icons library: https://vercel.com/design/icons+export { default as LoadingDots } from "./loading-dots";export { default as LoadingCircle } from "./loading-circle";export { default as ExpandingArrow } from "./expanding-arrow";diff --git a/components/shared/switch.tsx b/components/shared/switch.tsxindex cdf1304313..95ff075cbc 100644--- a/components/shared/switch.tsx+++ b/components/shared/switch.tsx@@ -3,26 +3,36 @@ import cx from "classnames";import { Dispatch, SetStateAction } from "react";const Switch = ({+ trackDimensions = "h-4 w-8",+ thumbDimensions = "h-3 w-3",+ thumbTranslate = "translate-x-4",+ disabled = false,setState,}: {+ trackDimensions?: string;+ thumbDimensions?: string;+ thumbTranslate?: string;+ disabled?: boolean;setState: Dispatch<SetStateAction<boolean>>;}) => {return (<SwitchPrimitive.RootdefaultChecked={true}onCheckedChange={(checked) => setState(checked)}+ disabled={disabled}className={cx(- "radix-state-checked:bg-blue-500",- "radix-state-unchecked:bg-gray-200 dark:radix-state-unchecked:bg-gray-800",- "relative inline-flex h-4 w-8 flex-shrink-0 cursor-pointer rounded-full border-2 border-transparent transition-colors duration-200 ease-in-out",+ disabled+ ? "cursor-not-allowed bg-gray-200"+ : "cursor-pointer radix-state-checked:bg-blue-500 radix-state-unchecked:bg-gray-200",+ `relative inline-flex ${trackDimensions} flex-shrink-0 rounded-full border-2 border-transparent transition-colors duration-200 ease-in-out`,"focus:outline-none focus-visible:ring focus-visible:ring-blue-500 focus-visible:ring-opacity-75")}><SwitchPrimitive.ThumbclassName={cx(- "radix-state-checked:translate-x-4",+ `radix-state-checked:${thumbTranslate}`,"radix-state-unchecked:translate-x-0",- "pointer-events-none inline-block h-3 w-3 transform rounded-full bg-white shadow-lg ring-0 transition duration-200 ease-in-out"+ `pointer-events-none ${thumbDimensions} transform ${thumbTranslate} rounded-full bg-white shadow-lg ring-0 transition duration-200 ease-in-out`)}/></SwitchPrimitive.Root>diff --git a/package.json b/package.jsonindex 51643ca769..2c50391084 100644--- a/package.json+++ b/package.json@@ -45,6 +45,7 @@"npm": "^8.19.0","prisma": "^4.3.1","react": "^18.1.0",+ "react-colorful": "^5.6.1","react-dom": "18.1.0","react-hot-toast": "^2.4.0","react-spring": "^9.5.4",diff --git a/yarn.lock b/yarn.lockindex 5ca0586b77..b782c2b974 100644--- a/yarn.lock+++ b/yarn.lock@@ -4872,6 +4872,11 @@ raw-body@^2.2.0:iconv-lite "0.4.24"unpipe "1.0.0"+react-colorful@^5.6.1:+ version "5.6.1"+ resolved "https://registry.yarnpkg.com/react-colorful/-/react-colorful-5.6.1.tgz#7dc2aed2d7c72fac89694e834d179e32f3da563b"+ integrity sha512-1exovf0uGTGyq5mXQT0zgQ80uvj2PCwvF8zY1RN9/vbJVSjSo3fsB/4L3ObbF7u70NduSiK4xu4Y6q1MHoUGEw==+version "18.1.0"resolved "https://registry.npmjs.org/react-dom/-/react-dom-18.1.0.tgz"