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.

Use `Combobox` for `FolderDropdown`

Tim Wilson356cb83457e1
diff --git a/apps/web/ui/folders/folder-dropdown.tsx b/apps/web/ui/folders/folder-dropdown.tsx
index ac2b8a92d3..4f645a6d24 100644
--- a/apps/web/ui/folders/folder-dropdown.tsx
+++ b/apps/web/ui/folders/folder-dropdown.tsx
@@ -3,16 +3,17 @@ import { getPlanCapabilities } from "@/lib/plan-capabilities";
import useFolders from "@/lib/swr/use-folders";
import useWorkspace from "@/lib/swr/use-workspace";
import { FolderSummary } from "@/lib/types";
-import { Button, Popover, Tick, TooltipContent } from "@dub/ui";
+import { Button, Combobox, TooltipContent } from "@dub/ui";
import { cn } from "@dub/utils";
import { ChevronsUpDown } from "lucide-react";
import Link from "next/link";
import { useRouter, useSearchParams } from "next/navigation";
-import { useEffect, useState } from "react";
+import { useEffect, useMemo, useState } from "react";
import { useAddFolderModal } from "../modals/add-folder-modal";
import { FolderIcon } from "./folder-icon";
interface FolderDropdownProps {
+ variant?: "inline" | "input";
onFolderSelect?: (folder: FolderSummary) => void;
hideViewAll?: boolean;
hideFolderIcon?: boolean;
@@ -23,6 +24,7 @@ interface FolderDropdownProps {
}
export const FolderDropdown = ({
+ variant = "inline",
onFolderSelect,
hideViewAll = false,
hideFolderIcon = false,
@@ -66,151 +68,149 @@ export const FolderDropdown = ({
}
}, [folderId, folders]);
+ const { canAddFolder } = getPlanCapabilities(plan);
+
+ const folderOptions = useMemo(() => {
+ const allFolders = [unsortedLinks, ...(folders || [])];
+ return [
+ ...allFolders.map((folder) => ({
+ value: folder.id,
+ label: folder.name,
+ icon: <FolderIcon className="mr-1" folder={folder} shape="square" />,
+ meta: folder,
+ })),
+ {
+ value: "create",
+ label: "Create new folder",
+ icon: (
+ <FolderIcon
+ className="mr-1"
+ folder={{ id: "new", accessLevel: null }}
+ shape="square"
+ />
+ ),
+ disabledTooltip: !canAddFolder ? (
+ <TooltipContent
+ title="You can only use Link Folders on a Pro plan and above. Upgrade to Pro to continue."
+ cta="Upgrade to Pro"
+ href={`/${slug}/upgrade`}
+ />
+ ) : undefined,
+ },
+ ];
+ }, [folders, canAddFolder, slug]);
+
+ const selectedOption = useMemo(() => {
+ if (!selectedFolder) return null;
+ return {
+ value: selectedFolder.id,
+ label: selectedFolder.name,
+ icon: (
+ <FolderIcon className="mr-1" folder={selectedFolder} shape="square" />
+ ),
+ meta: selectedFolder,
+ };
+ }, [selectedFolder]);
+
if (folderId && loading) {
return <FolderSwitcherPlaceholder />;
}
- const { canAddFolder } = getPlanCapabilities(plan);
-
- const As = onFolderSelect ? "button" : Link;
-
return (
<>
<AddFolderModal />
- <Popover
- content={
- <div className="relative mt-1 max-h-80 w-full space-y-0.5 overflow-auto rounded-md bg-white p-2 text-base sm:w-60 sm:text-sm sm:shadow-lg md:w-80">
- {!hideViewAll && (
- <div className="flex items-center justify-between px-2 pb-1">
- <p className="text-xs font-medium text-neutral-500">Folders</p>
- <Link
- href={`/${slug}/settings/library/folders`}
- onClick={() => setOpenPopover(false)}
- className="rounded-md border border-neutral-200 px-2 py-1 text-xs transition-colors hover:bg-neutral-100"
- >
- View All
- </Link>
- </div>
- )}
-
- {[unsortedLinks, ...(folders || [])].map((folder) => {
- return (
- <As
- key={folder.id}
- href={
- onFolderSelect
- ? "#"
- : `/${slug}${folder.id === "unsorted" ? "" : `?folderId=${folder.id}`}`
- }
- className={cn(
- "relative flex w-full items-center gap-x-2 rounded-md px-2 py-1.5 transition-all duration-75 hover:bg-neutral-100 active:bg-neutral-200",
- {
- "font-medium": selectedFolder?.id === folder.id,
- },
- )}
- onClick={() => {
- setOpenPopover(false);
- setSelectedFolder(folder);
- onFolderSelect?.(folder);
- }}
- >
- <FolderIcon folder={folder} shape="square" />
- <span
- className={`flex items-center justify-start gap-1.5 text-sm sm:max-w-36 md:max-w-52 ${
- selectedFolder?.id === folder.id
- ? "font-medium"
- : "font-normal"
- }`}
- >
- <span className="truncate">{folder.name}</span>
-
- {folder.id === "unsorted" && (
- <div className="rounded bg-neutral-100 p-1">
- <div className="text-xs font-normal text-black">
- Unsorted
- </div>
- </div>
- )}
- </span>
-
- {selectedFolder?.id === folder.id && (
- <span className="absolute inset-y-0 right-0 flex items-center pr-3 text-black">
- <Tick className="size-5" aria-hidden="true" />
- </span>
- )}
- </As>
- );
- })}
-
- {loading &&
- Array.from({ length: 3 }).map((_, index) => (
- <FolderItemPlaceholder key={index} />
- ))}
-
+ <Combobox
+ selected={selectedOption}
+ setSelected={(option) => {
+ if (option?.value === "create") {
+ setShowAddFolderModal(true);
+ return;
+ }
+
+ const folder = option?.meta;
+ if (folder) {
+ setSelectedFolder(folder);
+ onFolderSelect
+ ? onFolderSelect(folder)
+ : router.push(
+ `/${slug}${folder.id === "unsorted" ? "" : `?folderId=${folder.id}`}`,
+ );
+ }
+ }}
+ inputRight={
+ hideViewAll ? undefined : (
+ <Link
+ href={`/${slug}/settings/library/folders`}
+ onClick={() => setOpenPopover(false)}
+ className="rounded-md border border-neutral-200 px-2 py-1 text-xs transition-colors hover:bg-neutral-100"
+ >
+ View All
+ </Link>
+ )
+ }
+ options={loading ? undefined : folderOptions}
+ icon={
+ !(selectedFolder?.id === "unsorted" && hideFolderIcon) &&
+ selectedFolder ? (
+ <FolderIcon
+ folder={selectedFolder}
+ shape="square"
+ className="hidden md:block"
+ />
+ ) : undefined
+ }
+ optionRight={(option) =>
+ option.value === "unsorted" ? (
+ <div className="rounded bg-neutral-100 p-1">
+ <div className="text-xs font-normal text-black">Unsorted</div>
+ </div>
+ ) : undefined
+ }
+ caret={
+ <ChevronsUpDown className="ml-2 size-4 shrink-0 text-neutral-400" />
+ }
+ buttonProps={{
+ className: cn(
+ "group flex items-center gap-2 rounded-lg px-2 py-1 w-fit",
+ variant === "inline" && "border-none !ring-0",
+ "transition-colors hover:bg-neutral-100 active:bg-neutral-200 data-[state=open]:bg-neutral-100",
+ buttonClassName,
+ ),
+ textWrapperClassName: cn(
+ "min-w-0 truncate text-left text-xl font-semibold leading-7 text-neutral-900 md:text-2xl",
+ buttonTextClassName,
+ ),
+ }}
+ optionClassName="md:min-w-[250px]"
+ searchPlaceholder="Search folders..."
+ emptyState={
+ <div className="flex w-full flex-col items-center gap-2 py-4">
+ No folders found
<Button
- key="add-folder"
- variant="outline"
- icon={
- <FolderIcon
- folder={{ id: "new", accessLevel: null }}
- shape="square"
- />
- }
- text="Create new folder"
- className="justify-start px-2"
onClick={() => {
setOpenPopover(false);
setShowAddFolderModal(true);
}}
+ variant="primary"
+ className="h-7 w-fit px-2"
disabledTooltip={
- !canAddFolder && (
+ !canAddFolder ? (
<TooltipContent
title="You can only use Link Folders on a Pro plan and above. Upgrade to Pro to continue."
cta="Upgrade to Pro"
href={`/${slug}/upgrade`}
/>
- )
+ ) : undefined
}
+ text="Create folder"
/>
</div>
}
- openPopover={openPopover}
- setOpenPopover={setOpenPopover}
- align="start"
- popoverContentClassName="-ml-1"
- onWheel={(e) => {
- // Allows scrolling to work when the popover's in a modal
- e.stopPropagation();
- }}
+ open={openPopover}
+ onOpenChange={setOpenPopover}
>
- <button
- className={cn(
- "group flex max-w-40 items-center gap-2 rounded-lg px-2 py-1 md:max-w-fit",
- "transition-colors hover:bg-neutral-100 active:bg-neutral-200 data-[state=open]:bg-neutral-100",
- buttonClassName,
- )}
- >
- {!(selectedFolder?.id === "unsorted" && hideFolderIcon) &&
- selectedFolder && (
- <FolderIcon
- folder={selectedFolder}
- shape="square"
- className="hidden md:block"
- />
- )}
-
- <h1
- className={cn(
- "min-w-0 truncate text-left text-xl font-semibold leading-7 text-neutral-900 md:text-2xl",
- buttonTextClassName,
- )}
- >
- {selectedFolder ? selectedFolder.name : "Links"}
- </h1>
-
- <ChevronsUpDown className="ml-auto size-4 shrink-0 text-neutral-400" />
- </button>
- </Popover>
+ {selectedFolder ? selectedFolder.name : "Links"}
+ </Combobox>
</>
);
};
diff --git a/apps/web/ui/folders/move-link-form.tsx b/apps/web/ui/folders/move-link-form.tsx
index 3d980d51bb..5759b725a5 100644
--- a/apps/web/ui/folders/move-link-form.tsx
+++ b/apps/web/ui/folders/move-link-form.tsx
@@ -87,6 +87,7 @@ export const MoveLinkForm = ({
</label>
<div className="mt-1">
<FolderDropdown
+ variant="input"
hideViewAll={true}
disableAutoRedirect={true}
onFolderSelect={(folder) => {
diff --git a/packages/ui/src/combobox/index.tsx b/packages/ui/src/combobox/index.tsx
index 2b0a34a8ec..5384bf608b 100644
--- a/packages/ui/src/combobox/index.tsx
+++ b/packages/ui/src/combobox/index.tsx
@@ -24,11 +24,13 @@ import {
Plus,
} from "../icons";
import { Popover, PopoverProps } from "../popover";
+import { Tooltip } from "../tooltip";
export type ComboboxOption<TMeta = any> = {
- label: string;
+ label: string | ReactNode;
value: string;
icon?: Icon | ReactNode;
+ disabledTooltip?: ReactNode;
meta?: TMeta;
};
@@ -52,12 +54,13 @@ export type ComboboxProps<
onCreate?: (search: string) => Promise<boolean>;
buttonProps?: ButtonProps;
shortcutHint?: string;
- caret?: boolean;
+ caret?: boolean | ReactNode;
side?: PopoverProps["side"];
open?: boolean;
onOpenChange?: (open: boolean) => void;
onSearchChange?: (search: string) => void;
shouldFilter?: boolean;
+ inputRight?: ReactNode;
inputClassName?: string;
optionRight?: (option: ComboboxOption) => ReactNode;
optionClassName?: string;
@@ -90,6 +93,7 @@ export function Combobox({
onOpenChange,
onSearchChange,
shouldFilter = true,
+ inputRight,
inputClassName,
optionRight,
optionClassName,
@@ -215,6 +219,7 @@ export function Combobox({
}
}}
/>
+ {inputRight && <div className="mr-2">{inputRight}</div>}
{shortcutHint && (
<kbd className="mr-2 hidden shrink-0 rounded border border-neutral-200 bg-neutral-100 px-2 py-0.5 text-xs font-light text-neutral-500 md:block">
{shortcutHint}
@@ -306,11 +311,14 @@ export function Combobox({
selected.map((option) => option.label).join(", ") ||
placeholder}
</div>
- {caret && (
- <ChevronDown
- className={`ml-1 size-4 shrink-0 text-neutral-400 transition-transform duration-75 group-data-[state=open]:rotate-180`}
- />
- )}
+ {caret &&
+ (caret === true ? (
+ <ChevronDown
+ className={`ml-1 size-4 shrink-0 text-neutral-400 transition-transform duration-75 group-data-[state=open]:rotate-180`}
+ />
+ ) : (
+ caret
+ ))}
</>
}
icon={
@@ -366,44 +374,61 @@ function Option({
className?: string;
}) {
return (
- <Command.Item
- className={cn(
- "flex cursor-pointer items-center gap-3 whitespace-nowrap rounded-md px-3 py-2 text-left text-sm",
- "data-[selected=true]:bg-neutral-100",
- className,
- )}
- onSelect={onSelect}
- value={option.label + option?.value}
- >
- {multiple && (
- <div className="shrink-0 text-neutral-600">
- {selected ? (
- <CheckboxCheckedFill className="size-4 text-neutral-600" />
- ) : (
- <CheckboxUnchecked className="size-4 text-neutral-400" />
- )}
- </div>
- )}
- <div className="flex min-w-0 grow items-center gap-1">
- {option.icon && (
- <span className="shrink-0 text-neutral-600">
- {isReactNode(option.icon) ? (
- option.icon
+ <DisabledTooltip disabledTooltip={option.disabledTooltip}>
+ <Command.Item
+ className={cn(
+ "flex cursor-pointer items-center gap-3 whitespace-nowrap rounded-md px-3 py-2 text-left text-sm",
+ "data-[selected=true]:bg-neutral-100",
+ Boolean(option.disabledTooltip) && "cursor-not-allowed opacity-50",
+ className,
+ )}
+ disabled={!!option.disabledTooltip}
+ onSelect={onSelect}
+ value={option.label + option?.value}
+ >
+ {multiple && (
+ <div className="shrink-0 text-neutral-600">
+ {selected ? (
+ <CheckboxCheckedFill className="size-4 text-neutral-600" />
) : (
- <option.icon className="h-4 w-4" />
+ <CheckboxUnchecked className="size-4 text-neutral-400" />
)}
- </span>
+ </div>
)}
- <span className="grow truncate">{option.label}</span>
- </div>
- {right}
- {!multiple && selected && (
- <Check2 className="size-4 shrink-0 text-neutral-600" />
- )}
- </Command.Item>
+ <div className="flex min-w-0 grow items-center gap-1">
+ {option.icon && (
+ <span className="shrink-0 text-neutral-600">
+ {isReactNode(option.icon) ? (
+ option.icon
+ ) : (
+ <option.icon className="h-4 w-4" />
+ )}
+ </span>
+ )}
+ <span className="grow truncate">{option.label}</span>
+ </div>
+ {right}
+ {!multiple && selected && (
+ <Check2 className="size-4 shrink-0 text-neutral-600" />
+ )}
+ </Command.Item>
+ </DisabledTooltip>
);
}
+const DisabledTooltip = ({
+ children,
+ disabledTooltip,
+}: PropsWithChildren<{ disabledTooltip: ReactNode }>) => {
+ return disabledTooltip ? (
+ <Tooltip content={disabledTooltip}>
+ <div>{children}</div>
+ </Tooltip>
+ ) : (
+ children
+ );
+};
+
const isReactNode = (element: any): element is ReactNode =>
isValidElement(element);