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.

WIP accordion blocks

Tim Wilson247e557909a3
diff --git a/apps/web/ui/partners/design/edit-list.tsx b/apps/web/ui/partners/design/edit-list.tsx
index 02e18c4b23..acf479bf11 100644
--- a/apps/web/ui/partners/design/edit-list.tsx
+++ b/apps/web/ui/partners/design/edit-list.tsx
@@ -1,7 +1,133 @@
-"use client"
+"use client";
+
+import { Button, GripDotsVertical, Plus2, Trash } from "@dub/ui";
+import { cn } from "@dub/utils";
+import { motion, Reorder, useDragControls } from "framer-motion";
+import {
+ createContext,
+ Dispatch,
+ ReactNode,
+ SetStateAction,
+ useContext,
+ useState,
+} from "react";
+
+const EditListContext = createContext<{
+ expandedValue: string | null;
+ setExpandedValue: Dispatch<SetStateAction<string | null>>;
+}>({
+ expandedValue: null,
+ setExpandedValue: () => {},
+});
+
+export function EditList({
+ values,
+ onReorder,
+ onAdd,
+ addButtonLabel = "Add item",
+ className,
+ children,
+}: {
+ values: string[];
+ onReorder: (newValues: string[]) => void;
+ onAdd: () => string | void;
+ addButtonLabel?: string;
+ className?: string;
+ children: ReactNode;
+}) {
+ const [expandedValue, setExpandedValue] = useState<string | null>(
+ values.length === 1 ? values[0] : null,
+ );
-export function EditList() {
return (
+ <div className={cn("flex flex-col gap-2", className)}>
+ <Reorder.Group
+ axis="y"
+ values={values}
+ onReorder={onReorder}
+ className="flex flex-col gap-2"
+ >
+ <EditListContext.Provider value={{ expandedValue, setExpandedValue }}>
+ {children}
+ </EditListContext.Provider>
+ </Reorder.Group>
+ <Button
+ onClick={() => {
+ const result = onAdd();
+ if (result) setExpandedValue(result);
+ }}
+ variant="secondary"
+ className="mt-2"
+ icon={<Plus2 className="size-4" />}
+ text={addButtonLabel}
+ />
+ </div>
+ );
+}
- )
-}
\ No newline at end of file
+export function EditListItem({
+ value,
+ title,
+ children,
+ onRemove,
+}: {
+ value: string;
+ title: ReactNode;
+ children: ReactNode;
+ onRemove?: () => void;
+}) {
+ const { expandedValue, setExpandedValue } = useContext(EditListContext);
+ const controls = useDragControls();
+
+ return (
+ <Reorder.Item
+ value={value}
+ dragListener={false}
+ dragControls={controls}
+ className="relative overflow-hidden rounded-md border border-neutral-200 bg-neutral-50"
+ >
+ <div className="relative">
+ <div
+ onPointerDown={(e) => controls.start(e)}
+ className="absolute inset-y-0 left-0 flex cursor-grab items-center px-2"
+ data-handle
+ >
+ <GripDotsVertical className="size-4 text-neutral-800" />
+ </div>
+ <button
+ type="button"
+ onClick={() =>
+ setExpandedValue((ev) => (ev === value ? null : value))
+ }
+ className="flex w-full select-none items-center gap-2 px-2 py-2.5 transition-colors hover:bg-neutral-100"
+ >
+ <div className="truncate px-6 text-sm font-semibold text-neutral-800">
+ {title}
+ </div>
+ </button>
+ {onRemove && (
+ <button
+ type="button"
+ className="absolute inset-y-0 right-0 px-2.5 text-neutral-400 transition-colors hover:bg-neutral-100 hover:text-neutral-600"
+ onClick={onRemove}
+ title="Remove item"
+ >
+ <Trash className="size-3.5" />
+ </button>
+ )}
+ </div>
+ <motion.div
+ animate={{
+ height: expandedValue === value ? "auto" : 0,
+ overflow: "hidden",
+ }}
+ transition={{
+ duration: 0.2,
+ }}
+ initial={false}
+ >
+ <div className="border-t border-neutral-200 p-5">{children}</div>
+ </motion.div>
+ </Reorder.Item>
+ );
+}
diff --git a/apps/web/ui/partners/design/modals/accordion-block-modal.tsx b/apps/web/ui/partners/design/modals/accordion-block-modal.tsx
index 22aab613dc..77cf675dbb 100644
--- a/apps/web/ui/partners/design/modals/accordion-block-modal.tsx
+++ b/apps/web/ui/partners/design/modals/accordion-block-modal.tsx
@@ -1,10 +1,20 @@
"use client";
import { programLanderAccordionBlockSchema } from "@/lib/zod/schemas/program-lander";
-import { Button, Modal, useMediaQuery } from "@dub/ui";
-import { Dispatch, SetStateAction, useId } from "react";
-import { useFieldArray, useForm } from "react-hook-form";
+import { Button, Modal, useMediaQuery, useScrollProgress } from "@dub/ui";
+import { Dispatch, SetStateAction, useId, useRef } from "react";
+import {
+ FieldArrayWithId,
+ FormProvider,
+ useFieldArray,
+ UseFieldArrayRemove,
+ useForm,
+ useFormContext,
+ useWatch,
+} from "react-hook-form";
+import { v4 as uuid } from "uuid";
import { z } from "zod";
+import { EditList, EditListItem } from "../edit-list";
type AccordionBlockData = z.infer<
typeof programLanderAccordionBlockSchema
@@ -19,7 +29,11 @@ type AccordionBlockModalProps = {
export function AccordionBlockModal(props: AccordionBlockModalProps) {
return (
- <Modal showModal={props.showModal} setShowModal={props.setShowModal}>
+ <Modal
+ showModal={props.showModal}
+ setShowModal={props.setShowModal}
+ className=""
+ >
<AccordionBlockModalInner {...props} />
</Modal>
);
@@ -32,19 +46,32 @@ function AccordionBlockModalInner({
}: AccordionBlockModalProps) {
const id = useId();
const { isMobile } = useMediaQuery();
- const { handleSubmit, register, control } = useForm<AccordionBlockData>({
- defaultValues,
+ const form = useForm<AccordionBlockData>({
+ defaultValues: defaultValues ?? {
+ title: "",
+ items: [
+ {
+ id: uuid(),
+ title: "Item 1",
+ content: "",
+ },
+ ],
+ },
});
- const { fields, append, prepend, remove, swap, move, insert } = useFieldArray(
- {
- control,
- name: "items",
- },
- );
+ const { handleSubmit, register, control } = form;
+
+ const { fields, append, remove, move } = useFieldArray({
+ control,
+ keyName: "rhfId",
+ name: "items",
+ });
+
+ const scrollRef = useRef<HTMLDivElement>(null);
+ const { scrollProgress, updateScrollProgress } = useScrollProgress(scrollRef);
return (
- <>
+ <FormProvider {...form}>
<div className="p-4 pt-3">
<h3 className="text-base font-semibold leading-6 text-neutral-800">
Add Accordion
@@ -79,25 +106,41 @@ function AccordionBlockModalInner({
</div>
</div>
- <div>
- {fields.map((field, index) => (
- <div key={field.id}>
- <input {...register(`items.${index}.title`)} />
- <input {...register(`items.${index}.content`)} />
- </div>
- ))}
- <button
- type="button"
- onClick={() =>
+ <div
+ ref={scrollRef}
+ onScroll={updateScrollProgress}
+ className="scrollbar-hide relative max-h-[calc(100vh-300px)] overflow-y-auto"
+ >
+ <EditList
+ values={fields.map(({ id }) => id)}
+ onAdd={() => {
+ const id = uuid();
append({
- id: crypto.randomUUID(),
- title: "title",
- content: "content",
- })
- }
+ id,
+ title: `Item ${fields.length + 1}`,
+ content: "",
+ });
+
+ return id;
+ }}
+ onReorder={() => {}}
>
- add
- </button>
+ {fields.map((field, index) => (
+ <AccordionItem
+ key={field.id}
+ field={field}
+ totalFields={fields.length}
+ remove={remove}
+ index={index}
+ />
+ ))}
+ </EditList>
+
+ {/* Bottom scroll fade */}
+ <div
+ className="pointer-events-none absolute bottom-0 left-0 hidden h-16 w-full bg-gradient-to-t from-white sm:block"
+ style={{ opacity: 1 - Math.pow(scrollProgress, 2) }}
+ />
</div>
<div className="flex items-center justify-end gap-2">
@@ -116,7 +159,77 @@ function AccordionBlockModalInner({
</div>
</form>
</div>
- </>
+ </FormProvider>
+ );
+}
+
+function AccordionItem({
+ field,
+ totalFields,
+ remove,
+ index,
+}: {
+ field: FieldArrayWithId<AccordionBlockData, "items", "rhfId">;
+ totalFields: number;
+ remove: UseFieldArrayRemove;
+ index: number;
+}) {
+ const id = useId();
+ const { register, control } = useFormContext<AccordionBlockData>();
+
+ const title = useWatch({
+ control,
+ name: `items.${index}.title`,
+ });
+
+ return (
+ <EditListItem
+ key={field.id}
+ value={field.id}
+ title={title || "Item"}
+ onRemove={totalFields > 1 ? () => remove(index) : undefined}
+ >
+ <div className="flex flex-col gap-6">
+ {/* Title */}
+ <div>
+ <label
+ htmlFor={`${id}-${field.id}-title`}
+ className="flex items-center gap-2 text-sm font-medium text-neutral-700"
+ >
+ Title
+ </label>
+ <div className="mt-2 rounded-md shadow-sm">
+ <input
+ id={`${id}-${field.id}-title`}
+ type="text"
+ placeholder="Title"
+ className="block w-full rounded-md border-neutral-300 text-neutral-900 placeholder-neutral-400 focus:border-neutral-500 focus:outline-none focus:ring-neutral-500 sm:text-sm"
+ {...register(`items.${index}.title`)}
+ />
+ </div>
+ </div>
+
+ {/* Content */}
+ <div>
+ <label
+ htmlFor={`${id}-${field.id}-content`}
+ className="flex items-center gap-2 text-sm font-medium text-neutral-700"
+ >
+ Content
+ </label>
+ <div className="mt-2 rounded-md shadow-sm">
+ <textarea
+ id={`${id}-${field.id}-content`}
+ rows={3}
+ maxLength={1000}
+ placeholder="Start typing"
+ className="block max-h-32 min-h-16 w-full rounded-md border-neutral-300 text-neutral-900 placeholder-neutral-400 focus:border-neutral-500 focus:outline-none focus:ring-neutral-500 sm:text-sm"
+ {...register(`items.${index}.content`)}
+ />
+ </div>
+ </div>
+ </div>
+ </EditListItem>
);
}
diff --git a/packages/ui/src/icons/nucleo/grip-dots-vertical.tsx b/packages/ui/src/icons/nucleo/grip-dots-vertical.tsx
new file mode 100644
index 0000000000..9502e3ac2f
--- /dev/null
+++ b/packages/ui/src/icons/nucleo/grip-dots-vertical.tsx
@@ -0,0 +1,76 @@
+import { SVGProps } from "react";
+
+export function GripDotsVertical(props: SVGProps<SVGSVGElement>) {
+ return (
+ <svg
+ height="18"
+ width="18"
+ viewBox="0 0 18 18"
+ xmlns="http://www.w3.org/2000/svg"
+ {...props}
+ >
+ <g fill="currentColor">
+ <circle
+ cx="6.75"
+ cy="9"
+ fill="currentColor"
+ r=".5"
+ stroke="currentColor"
+ strokeLinecap="round"
+ strokeLinejoin="round"
+ strokeWidth="1.5"
+ />
+ <circle
+ cx="6.75"
+ cy="3.75"
+ fill="currentColor"
+ r=".5"
+ stroke="currentColor"
+ strokeLinecap="round"
+ strokeLinejoin="round"
+ strokeWidth="1.5"
+ />
+ <circle
+ cx="6.75"
+ cy="14.25"
+ fill="currentColor"
+ r=".5"
+ stroke="currentColor"
+ strokeLinecap="round"
+ strokeLinejoin="round"
+ strokeWidth="1.5"
+ />
+ <circle
+ cx="11.25"
+ cy="9"
+ fill="currentColor"
+ r=".5"
+ stroke="currentColor"
+ strokeLinecap="round"
+ strokeLinejoin="round"
+ strokeWidth="1.5"
+ />
+ <circle
+ cx="11.25"
+ cy="3.75"
+ fill="currentColor"
+ r=".5"
+ stroke="currentColor"
+ strokeLinecap="round"
+ strokeLinejoin="round"
+ strokeWidth="1.5"
+ />
+ <circle
+ cx="11.25"
+ cy="14.25"
+ fill="currentColor"
+ r=".5"
+ stroke="currentColor"
+ strokeLinecap="round"
+ strokeLinejoin="round"
+ strokeWidth="1.5"
+ />
+ </g>
+ </svg>
+ );
+}
diff --git a/packages/ui/src/icons/nucleo/index.ts b/packages/ui/src/icons/nucleo/index.ts
index febeb861f0..330975a310 100644
--- a/packages/ui/src/icons/nucleo/index.ts
+++ b/packages/ui/src/icons/nucleo/index.ts
@@ -114,6 +114,7 @@ export * from "./greek-temple";
export * from "./grid";
export * from "./grid-layout-rows";
export * from "./grid-plus";
+export * from "./grip-dots-vertical";
export * from "./headset";
export * from "./headset-fill";
export * from "./heart-fill";