|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import type { |
| 4 | + AccordionMultipleProps, |
| 5 | + AccordionSingleProps, |
| 6 | +} from '@radix-ui/react-accordion'; |
| 7 | +import * as AccordionPrimitive from '@radix-ui/react-accordion'; |
| 8 | +import { Check, ChevronRight, Link as LinkIcon } from 'lucide-react'; |
| 9 | +import { |
| 10 | + type ComponentPropsWithoutRef, |
| 11 | + forwardRef, |
| 12 | + type ReactNode, |
| 13 | + useEffect, |
| 14 | + useRef, |
| 15 | + useState, |
| 16 | +} from 'react'; |
| 17 | +import { cn } from '../lib/cn'; |
| 18 | +import { useCopyButton } from 'fumadocs-ui/utils/use-copy-button'; |
| 19 | +import { buttonVariants } from './ui/button'; |
| 20 | +import { mergeRefs } from '../lib/merge-refs'; |
| 21 | + |
| 22 | +export const Accordions = forwardRef< |
| 23 | + HTMLDivElement, |
| 24 | + | Omit<AccordionSingleProps, 'value' | 'onValueChange'> |
| 25 | + | Omit<AccordionMultipleProps, 'value' | 'onValueChange'> |
| 26 | +>(({ type = 'single', className, defaultValue, ...props }, ref) => { |
| 27 | + const rootRef = useRef<HTMLDivElement>(null); |
| 28 | + const composedRef = mergeRefs(ref, rootRef); |
| 29 | + const [value, setValue] = useState<string | string[]>(() => |
| 30 | + type === 'single' ? (defaultValue ?? '') : (defaultValue ?? []), |
| 31 | + ); |
| 32 | + |
| 33 | + useEffect(() => { |
| 34 | + const id = window.location.hash.substring(1); |
| 35 | + const element = rootRef.current; |
| 36 | + if (!element || id.length === 0) return; |
| 37 | + |
| 38 | + const selected = document.getElementById(id); |
| 39 | + if (!selected || !element.contains(selected)) return; |
| 40 | + const value = selected.getAttribute('data-accordion-value'); |
| 41 | + |
| 42 | + if (value) |
| 43 | + setValue((prev) => (typeof prev === 'string' ? value : [value, ...prev])); |
| 44 | + }, []); |
| 45 | + |
| 46 | + return ( |
| 47 | + // @ts-expect-error -- Multiple types |
| 48 | + <AccordionPrimitive.Root |
| 49 | + type={type} |
| 50 | + ref={composedRef} |
| 51 | + value={value} |
| 52 | + onValueChange={setValue} |
| 53 | + collapsible={type === 'single' ? true : undefined} |
| 54 | + className={cn( |
| 55 | + 'divide-y divide-fd-border overflow-hidden rounded-lg border bg-fd-card', |
| 56 | + className, |
| 57 | + )} |
| 58 | + {...props} |
| 59 | + /> |
| 60 | + ); |
| 61 | +}); |
| 62 | + |
| 63 | +Accordions.displayName = 'Accordions'; |
| 64 | + |
| 65 | +export const Accordion = forwardRef< |
| 66 | + HTMLDivElement, |
| 67 | + Omit< |
| 68 | + ComponentPropsWithoutRef<typeof AccordionPrimitive.Item>, |
| 69 | + 'value' | 'title' |
| 70 | + > & { |
| 71 | + title: string | ReactNode; |
| 72 | + value?: string; |
| 73 | + } |
| 74 | +>( |
| 75 | + ( |
| 76 | + { title, className, id, value = String(title), children, ...props }, |
| 77 | + ref, |
| 78 | + ) => { |
| 79 | + return ( |
| 80 | + <AccordionPrimitive.Item |
| 81 | + ref={ref} |
| 82 | + value={value} |
| 83 | + className={cn('scroll-m-24', className)} |
| 84 | + {...props} |
| 85 | + > |
| 86 | + <AccordionPrimitive.Header |
| 87 | + id={id} |
| 88 | + data-accordion-value={value} |
| 89 | + className="not-prose flex flex-row items-center text-fd-card-foreground font-medium has-focus-visible:bg-fd-accent" |
| 90 | + > |
| 91 | + <AccordionPrimitive.Trigger className="group flex flex-1 items-center gap-2 px-3 py-2.5 text-start focus-visible:outline-none"> |
| 92 | + <ChevronRight className="size-4 shrink-0 text-fd-muted-foreground transition-transform duration-200 group-data-[state=open]:rotate-90" /> |
| 93 | + {title} |
| 94 | + </AccordionPrimitive.Trigger> |
| 95 | + {id ? <CopyButton id={id} /> : null} |
| 96 | + </AccordionPrimitive.Header> |
| 97 | + <AccordionPrimitive.Content className="overflow-hidden data-[state=closed]:animate-fd-accordion-up data-[state=open]:animate-fd-accordion-down"> |
| 98 | + <div className="px-4 pb-2 text-[0.9375rem] prose-no-margin"> |
| 99 | + {children} |
| 100 | + </div> |
| 101 | + </AccordionPrimitive.Content> |
| 102 | + </AccordionPrimitive.Item> |
| 103 | + ); |
| 104 | + }, |
| 105 | +); |
| 106 | + |
| 107 | +function CopyButton({ id }: { id: string }) { |
| 108 | + const [checked, onClick] = useCopyButton(() => { |
| 109 | + const url = new URL(window.location.href); |
| 110 | + url.hash = id; |
| 111 | + |
| 112 | + return navigator.clipboard.writeText(url.toString()); |
| 113 | + }); |
| 114 | + |
| 115 | + return ( |
| 116 | + <button |
| 117 | + type="button" |
| 118 | + aria-label="Copy Link" |
| 119 | + className={cn( |
| 120 | + buttonVariants({ |
| 121 | + color: 'ghost', |
| 122 | + className: 'text-fd-muted-foreground me-2', |
| 123 | + }), |
| 124 | + )} |
| 125 | + onClick={onClick} |
| 126 | + > |
| 127 | + {checked ? ( |
| 128 | + <Check className="size-3.5" /> |
| 129 | + ) : ( |
| 130 | + <LinkIcon className="size-3.5" /> |
| 131 | + )} |
| 132 | + </button> |
| 133 | + ); |
| 134 | +} |
| 135 | + |
| 136 | +Accordion.displayName = 'Accordion'; |
0 commit comments