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 filter list

Tim Wilsonb12603b284b6
diff --git a/apps/web/ui/analytics/toggle.tsx b/apps/web/ui/analytics/toggle.tsx
index 39a62f5038..57d53c738f 100644
--- a/apps/web/ui/analytics/toggle.tsx
+++ b/apps/web/ui/analytics/toggle.tsx
@@ -123,7 +123,7 @@ export default function Toggle() {
>
<div
className={cn(
- "mx-auto flex w-full max-w-screen-xl flex-col gap-2 space-y-3 px-2.5 px-2.5 md:space-y-0 lg:px-20",
+ "mx-auto flex w-full max-w-screen-xl flex-col gap-2 px-2.5 px-2.5 lg:px-20",
{
"md:h-10": key,
},
@@ -250,6 +250,21 @@ export default function Toggle() {
</div>
</div>
</div>
+ <div
+ className={cn(
+ "transition-[height] duration-[300ms]",
+ activeFilters.length ? "h-6" : "h-0",
+ )}
+ />
+ <Filter.List
+ filters={filters}
+ activeFilters={activeFilters}
+ onRemove={(key) =>
+ queryParams({
+ del: key,
+ })
+ }
+ />
</div>
</div>
);
diff --git a/packages/ui/src/animated-size-container.tsx b/packages/ui/src/animated-size-container.tsx
index 6a15e45d67..561c771b1a 100644
--- a/packages/ui/src/animated-size-container.tsx
+++ b/packages/ui/src/animated-size-container.tsx
@@ -48,7 +48,10 @@ const AnimatedSizeContainer = forwardRef<
}}
transition={transition ?? { type: "spring", duration: 0.3 }}
>
- <div ref={containerRef} className="h-max w-max">
+ <div
+ ref={containerRef}
+ className={cn(height && "h-max", width && "w-max")}
+ >
{children}
</div>
</motion.div>
diff --git a/packages/ui/src/filter/filter-list.tsx b/packages/ui/src/filter/filter-list.tsx
new file mode 100644
index 0000000000..e6373faadb
--- /dev/null
+++ b/packages/ui/src/filter/filter-list.tsx
@@ -0,0 +1,100 @@
+import { cn } from "@dub/utils";
+import { AnimatePresence, motion } from "framer-motion";
+import { X } from "lucide-react";
+import { ReactNode, isValidElement } from "react";
+import { AnimatedSizeContainer } from "../animated-size-container";
+import { Filter, FilterOption } from "./types";
+
+type FilterListProps = {
+ filters: Filter[];
+ activeFilters?: {
+ key: Filter["key"];
+ value: FilterOption["value"];
+ }[];
+ onRemove: (key: string) => void;
+ className?: string;
+};
+
+export function FilterList({
+ filters,
+ activeFilters,
+ onRemove,
+ className,
+}: FilterListProps) {
+ return (
+ <AnimatedSizeContainer
+ height
+ className="w-full"
+ transition={{ type: "tween", duration: 0.3 }}
+ >
+ <div className={cn("flex flex-wrap gap-x-4 gap-y-2", className)}>
+ <AnimatePresence>
+ {activeFilters?.map(({ key, value }) => {
+ const filter = filters.find((f) => f.key === key);
+ if (!filter) {
+ throw new Error(
+ "Filter.List received an activeFilter without a corresponding filter",
+ );
+ }
+
+ const option = filter.options?.find((o) => o.value === value);
+
+ return (
+ <motion.div
+ key={key}
+ initial={{ opacity: 0, y: 4 }}
+ animate={{ opacity: 1, y: 0 }}
+ className="flex items-center divide-x rounded-md border border-gray-200 bg-white text-sm text-black"
+ >
+ {/* Filter */}
+ <div className="flex items-center gap-2.5 px-3 py-2">
+ <span className="shrink-0 text-gray-600">
+ {isReactNode(filter.icon) ? (
+ filter.icon
+ ) : (
+ <filter.icon className="h-4 w-4" />
+ )}
+ </span>
+ {filter.label}
+ </div>
+
+ {/* is */}
+ <div className="px-3 py-2 text-gray-500">is</div>
+
+ {/* Option */}
+ <div className="flex items-center gap-2.5 px-3 py-2">
+ {option ? (
+ <>
+ <span className="shrink-0 text-gray-600">
+ {isReactNode(option.icon) ? (
+ option.icon
+ ) : (
+ <option.icon className="h-4 w-4" />
+ )}
+ </span>
+ {option.label}
+ </>
+ ) : (
+ <div className="h-5 w-12 animate-pulse rounded-md bg-gray-200" />
+ )}
+ </div>
+
+ {/* Remove */}
+ <button
+ type="button"
+ className="h-full rounded-r-md p-2 text-gray-500 ring-inset ring-gray-500 hover:bg-gray-100 hover:text-gray-800 focus:outline-none focus-visible:ring-1"
+ onClick={() => onRemove(key)}
+ >
+ <X className="h-3.5 w-3.5" />
+ </button>
+ </motion.div>
+ );
+ })}
+ </AnimatePresence>
+ </div>
+ </AnimatedSizeContainer>
+ );
+}
+
+const isReactNode = (element: any): element is ReactNode =>
+ isValidElement(element);
diff --git a/packages/ui/src/filter/filter-select.tsx b/packages/ui/src/filter/filter-select.tsx
new file mode 100644
index 0000000000..84b185b002
--- /dev/null
+++ b/packages/ui/src/filter/filter-select.tsx
@@ -0,0 +1,243 @@
+import { cn } from "@dub/utils";
+import { Command } from "cmdk";
+import { Check, ChevronDown, ListFilter } from "lucide-react";
+import {
+ CSSProperties,
+ ReactNode,
+ isValidElement,
+ useCallback,
+ useEffect,
+ useRef,
+ useState,
+} from "react";
+import { AnimatedSizeContainer } from "../animated-size-container";
+import { LoadingSpinner } from "../icons";
+import { Popover } from "../popover";
+import { Filter, FilterOption } from "./types";
+
+type FilterSelectProps = {
+ filters: Filter[];
+ onSelect: (key: string, value: string) => void;
+ onRemove: (key: string) => void;
+ activeFilters?: {
+ key: Filter["key"];
+ value: FilterOption["value"];
+ }[];
+ children?: ReactNode;
+ className?: string;
+};
+
+export function FilterSelect({
+ filters,
+ onSelect,
+ onRemove,
+ activeFilters,
+ children,
+ className,
+}: FilterSelectProps) {
+ // Track main list container/dimensions to maintain size for loading spinner
+ const mainListContainer = useRef<HTMLDivElement>(null);
+ const mainListDimensions = useRef<{
+ width: number;
+ height: number;
+ }>();
+
+ const [isOpen, setIsOpen] = useState(false);
+ const [search, setSearch] = useState("");
+ const [selectedFilterKey, setSelectedFilterKey] = useState<
+ Filter["key"] | null
+ >(null);
+
+ const reset = useCallback(() => {
+ setSearch("");
+ setSelectedFilterKey(null);
+ }, []);
+
+ // Reset state when closed
+ useEffect(() => {
+ if (!isOpen) reset();
+ }, [isOpen]);
+
+ // The currently selected filter to display options for
+ const selectedFilter = selectedFilterKey
+ ? filters.find(({ key }) => key === selectedFilterKey)
+ : null;
+
+ const openFilter = useCallback((key: Filter["key"]) => {
+ if (mainListContainer.current) {
+ mainListDimensions.current = {
+ width: mainListContainer.current.scrollWidth,
+ height: mainListContainer.current.scrollHeight,
+ };
+ }
+
+ setSearch("");
+ setSelectedFilterKey(key);
+ }, []);
+
+ const selectOption = useCallback(
+ (value: FilterOption["value"]) => {
+ if (!selectedFilter) return;
+
+ activeFilters?.find(({ key }) => key === selectedFilterKey)?.value ===
+ value
+ ? onRemove(selectedFilter.key)
+ : onSelect(selectedFilter.key, value);
+
+ setIsOpen(false);
+ },
+ [activeFilters, selectedFilter],
+ );
+
+ return (
+ <Popover
+ openPopover={isOpen}
+ setOpenPopover={setIsOpen}
+ onEscapeKeyDown={(e) => {
+ if (selectedFilterKey) e.preventDefault();
+ }}
+ content={
+ <AnimatedSizeContainer width height>
+ <Command>
+ <Command.Input
+ size={1}
+ className="w-full rounded-t-lg border-0 border-b border-gray-200 px-4 py-3 text-sm ring-0 placeholder:text-gray-400 focus:border-gray-200 focus:ring-0"
+ placeholder={`${selectedFilter?.label || "Filter"}...`}
+ value={search}
+ onValueChange={setSearch}
+ onKeyDown={(e) => {
+ if (e.key === "Escape" || (e.key === "Backspace" && !search)) {
+ e.preventDefault();
+ selectedFilterKey ? reset() : setIsOpen(false);
+ }
+ }}
+ />
+ <div
+ className="scrollbar-hide max-h-[50vh] overflow-y-scroll p-2"
+ ref={mainListContainer}
+ >
+ {!selectedFilter ? (
+ <Command.List className="flex min-w-[150px] flex-col gap-1">
+ {filters.map((filter) => (
+ <FilterButton
+ {...filter}
+ key={filter.key}
+ // highlighted={filter.key === highlightedFilterKey}
+ onSelect={() => openFilter(filter.key)}
+ />
+ ))}
+ <NoMatches />
+ </Command.List>
+ ) : (
+ <Command.List className="flex min-w-[100px] flex-col gap-1">
+ {selectedFilter.options ? (
+ <>
+ {selectedFilter.options?.map((option) => {
+ const isSelected =
+ activeFilters?.find(
+ ({ key }) => key === selectedFilterKey,
+ )?.value === option.value;
+
+ return (
+ <FilterButton
+ {...option}
+ key={option.value}
+ right={
+ isSelected ? (
+ <Check className="h-4 w-4" />
+ ) : (
+ option.right
+ )
+ }
+ onSelect={() => selectOption(option.value)}
+ />
+ );
+ })}
+ <NoMatches />
+ </>
+ ) : (
+ <Command.Loading>
+ <div
+ className="flex items-center justify-center px-2 py-4"
+ style={mainListDimensions.current}
+ >
+ <LoadingSpinner />
+ </div>
+ </Command.Loading>
+ )}
+ </Command.List>
+ )}
+ </div>
+ </Command>
+ </AnimatedSizeContainer>
+ }
+ >
+ <button
+ type="button"
+ className={cn(
+ "group flex h-10 cursor-pointer appearance-none items-center gap-x-2 truncate rounded-md border px-3 outline-none transition-all sm:text-sm",
+ "border-gray-200 bg-white text-gray-900 placeholder-gray-400 transition-all",
+ "focus-visible:border-gray-500 data-[state=open]:border-gray-500 data-[state=open]:ring-4 data-[state=open]:ring-gray-200",
+ className,
+ )}
+ >
+ <ListFilter className="h-4 w-4 shrink-0" />
+ <span className="flex-1 overflow-hidden text-ellipsis whitespace-nowrap text-left text-gray-900">
+ {children ?? "Filter"}
+ </span>
+ <div className="ml-1">
+ {activeFilters?.length ? (
+ <div className="flex h-4 w-4 items-center justify-center rounded-full bg-black text-[0.625rem] text-white">
+ {activeFilters.length}
+ </div>
+ ) : (
+ <ChevronDown
+ className={`h-4 w-4 flex-shrink-0 text-gray-400 transition-transform duration-75 group-data-[state=open]:rotate-180`}
+ />
+ )}
+ </div>
+ </button>
+ </Popover>
+ );
+}
+
+function FilterButton({
+ icon: Icon,
+ label,
+ right,
+ onSelect,
+}: (Filter | FilterOption) & {
+ right?: ReactNode;
+ onSelect: () => void;
+}) {
+ return (
+ <Command.Item
+ className={cn(
+ "flex cursor-pointer items-center gap-4 whitespace-nowrap rounded-md px-3 py-2.5 text-left text-sm",
+ "data-[selected=true]:bg-gray-100",
+ )}
+ onSelect={onSelect}
+ value={label}
+ >
+ <span className="shrink-0 text-gray-600">
+ {isReactNode(Icon) ? Icon : <Icon className="h-4 w-4" />}
+ </span>
+ {label}
+ <div className="ml-1 flex shrink-0 grow justify-end text-gray-500">
+ {right}
+ </div>
+ </Command.Item>
+ );
+}
+
+const NoMatches = ({ style }: { style?: CSSProperties }) => (
+ <Command.Empty
+ className="my-1 text-center text-sm text-gray-400"
+ style={style}
+ >
+ No matches
+ </Command.Empty>
+);
+
+const isReactNode = (element: any): element is ReactNode =>
+ isValidElement(element);
diff --git a/packages/ui/src/filter/index.ts b/packages/ui/src/filter/index.ts
new file mode 100644
index 0000000000..d407cd63c9
--- /dev/null
+++ b/packages/ui/src/filter/index.ts
@@ -0,0 +1,6 @@
+import { FilterList } from "./filter-list";
+import { FilterSelect } from "./filter-select";
+
+const Filter = { Select: FilterSelect, List: FilterList };
+
+export { Filter };
diff --git a/packages/ui/src/filter/types.ts b/packages/ui/src/filter/types.ts
new file mode 100644
index 0000000000..2136758f8d
--- /dev/null
+++ b/packages/ui/src/filter/types.ts
@@ -0,0 +1,16 @@
+import { LucideIcon } from "lucide-react";
+import { ReactNode } from "react";
+
+export type Filter = {
+ key: string;
+ icon: LucideIcon | ReactNode;
+ label: string;
+ options: FilterOption[] | null;
+};
+
+export type FilterOption = {
+ value: any;
+ icon: LucideIcon | ReactNode;
+ label: string;
+ right?: ReactNode;
+};