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 Unsplash option to preview image editing

Tim Wilsoncc9e949e2e59
diff --git a/apps/web/ui/modals/add-edit-link-modal/og-section.tsx b/apps/web/ui/modals/add-edit-link-modal/og-section.tsx
index f2bf834a77..901909c77c 100644
--- a/apps/web/ui/modals/add-edit-link-modal/og-section.tsx
+++ b/apps/web/ui/modals/add-edit-link-modal/og-section.tsx
@@ -209,7 +209,9 @@ export default function OGSection({
<Popover
content={
<UnsplashSearch
- setData={setData}
+ onImageSelected={(image) =>
+ setData((prev) => ({ ...prev, image }))
+ }
setOpenPopover={handleSet}
/>
}
diff --git a/apps/web/ui/modals/add-edit-link-modal/preview.tsx b/apps/web/ui/modals/add-edit-link-modal/preview.tsx
index 7d8f9c83a2..8d3bcb6697 100644
--- a/apps/web/ui/modals/add-edit-link-modal/preview.tsx
+++ b/apps/web/ui/modals/add-edit-link-modal/preview.tsx
@@ -7,13 +7,17 @@ import {
Photo,
Popover,
Twitter,
+ Unsplash,
+ useResizeObserver,
} from "@dub/ui";
import { Button } from "@dub/ui/src/button";
import { getDomainWithoutWWW, resizeImage } from "@dub/utils";
+import { AnimatePresence, motion } from "framer-motion";
import { Edit2, Link2, Upload } from "lucide-react";
import {
ChangeEvent,
Dispatch,
+ RefObject,
SetStateAction,
useCallback,
useMemo,
@@ -22,6 +26,7 @@ import {
} from "react";
import { useDebounce } from "use-debounce";
import { usePromptModal } from "../prompt-modal";
+import UnsplashSearch from "./unsplash-search";
export default function Preview({
data,
@@ -259,25 +264,12 @@ const ImagePreview = ({
<Popover
align="end"
content={
- <div className="grid w-full gap-px p-2">
- <Button
- text="Upload image"
- variant="outline"
- icon={<Upload className="h-4 w-4" />}
- className="h-9 justify-start px-2 font-medium disabled:border-none disabled:bg-transparent"
- onClick={() => {
- inputFileRef.current?.click();
- setOpenPopover(false);
- }}
- />
- <Button
- text="Use image from URL"
- variant="outline"
- icon={<Link2 className="h-4 w-4" />}
- className="h-9 justify-start px-2 font-medium"
- onClick={() => setShowPromptModal(true)}
- />
- </div>
+ <ImagePreviewPopoverContent
+ onImageChange={onImageChange}
+ inputFileRef={inputFileRef}
+ setOpenPopover={setOpenPopover}
+ setShowPromptModal={setShowPromptModal}
+ />
}
openPopover={openPopover}
setOpenPopover={setOpenPopover}
@@ -302,3 +294,71 @@ const ImagePreview = ({
</>
);
};
+
+const ImagePreviewPopoverContent = ({
+ onImageChange,
+ inputFileRef,
+ setOpenPopover,
+ setShowPromptModal,
+}: {
+ onImageChange: (image: string) => void;
+ inputFileRef: RefObject<HTMLInputElement>;
+ setOpenPopover: Dispatch<SetStateAction<boolean>>;
+ setShowPromptModal: Dispatch<SetStateAction<boolean>>;
+}) => {
+ const contentWrapperRef = useRef<HTMLDivElement>(null);
+ const resizeObserverEntry = useResizeObserver(contentWrapperRef);
+
+ const [state, setState] = useState<"default" | "unsplash">("default");
+
+ return (
+ <motion.div
+ className="relative overflow-hidden"
+ animate={{
+ width: resizeObserverEntry?.borderBoxSize[0].inlineSize ?? "auto",
+ height: resizeObserverEntry?.borderBoxSize[0].blockSize ?? "auto",
+ }}
+ transition={{ type: "spring", duration: 0.3, bounce: 0.15 }}
+ >
+ <div ref={contentWrapperRef} className="inline-block">
+ <AnimatePresence>
+ {state === "unsplash" && (
+ <UnsplashSearch
+ onImageSelected={onImageChange}
+ setOpenPopover={setOpenPopover}
+ />
+ )}
+
+ {state === "default" && (
+ <div className="grid gap-px p-2">
+ <Button
+ text="Upload image"
+ variant="outline"
+ icon={<Upload className="h-4 w-4" />}
+ className="h-9 justify-start px-2 font-medium disabled:border-none disabled:bg-transparent"
+ onClick={() => {
+ inputFileRef.current?.click();
+ setOpenPopover(false);
+ }}
+ />
+ <Button
+ text="Use image from URL"
+ variant="outline"
+ icon={<Link2 className="h-4 w-4" />}
+ className="h-9 justify-start px-2 font-medium"
+ onClick={() => setShowPromptModal(true)}
+ />
+ <Button
+ text="Use image from Unsplash"
+ variant="outline"
+ icon={<Unsplash className="h-4 w-4 p-0.5" />}
+ className="h-9 justify-start px-2 font-medium"
+ onClick={() => setState("unsplash")}
+ />
+ </div>
+ )}
+ </AnimatePresence>
+ </div>
+ </motion.div>
+ );
+};
diff --git a/apps/web/ui/modals/add-edit-link-modal/unsplash-search.tsx b/apps/web/ui/modals/add-edit-link-modal/unsplash-search.tsx
index 2bd064646f..9543041e8e 100644
--- a/apps/web/ui/modals/add-edit-link-modal/unsplash-search.tsx
+++ b/apps/web/ui/modals/add-edit-link-modal/unsplash-search.tsx
@@ -1,4 +1,3 @@
-import { LinkProps } from "@/lib/types";
import { LoadingSpinner, useMediaQuery } from "@dub/ui";
import { fetcher } from "@dub/utils";
import { Dispatch, SetStateAction, useState } from "react";
@@ -8,10 +7,10 @@ import { Basic } from "unsplash-js/dist/methods/photos/types";
import { useDebounce } from "use-debounce";
export default function UnsplashSearch({
- setData,
+ onImageSelected,
setOpenPopover,
}: {
- setData: Dispatch<SetStateAction<LinkProps>>;
+ onImageSelected: (image: string) => void;
setOpenPopover: Dispatch<SetStateAction<boolean>>;
}) {
const [search, setSearch] = useState("");
@@ -30,7 +29,11 @@ export default function UnsplashSearch({
const { isMobile } = useMediaQuery();
return (
- <div className="h-[24rem] w-full overflow-auto p-3 md:w-[24rem]">
+ <div
+ className="h-[24rem] w-full overflow-auto p-3 md:w-[24rem]"
+ // Fixes a Webkit issue where elements outside of the visible area are still interactable
+ style={{ WebkitClipPath: "inset(0 0 0 0)" }}
+ >
<div className="relative mt-1 rounded-md shadow-sm">
<input
type="text"
@@ -52,10 +55,7 @@ export default function UnsplashSearch({
key={photo.id}
type="button"
onClick={() => {
- setData((prev) => ({
- ...prev,
- image: photo.urls.regular,
- }));
+ onImageSelected(photo.urls.regular);
setOpenPopover(false);
fetch("/api/unsplash/download", {
method: "POST",