Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

FoldedSettingsSection.tsx

93 lines3.6 KB

1import { SettingsGroup } from "./SettingsGroup";
2import { ChevronRightIcon } from "lucide-react";
3import { type ReactNode, useState } from "react";
4​
5import { cn } from "~/lib/utils";
6import { Collapsible, CollapsiblePanel, CollapsibleTrigger } from "../ui/collapsible";
7import { useSettingsSearchTarget, useSettingsSearchTargetId } from "./settingsLayout";
8​
9export function FoldedSettingsSection({
10 id,
11 title,
12 summary,
13 control,
14 headerPlacement = "inside",
15 children,
16}: {
17 readonly id: string;
18 readonly title: ReactNode;
19 readonly summary?: string | null;
20 readonly control?: ReactNode;
21 readonly headerPlacement?: "inside" | "outside";
22 readonly children: ReactNode;
23}) {
24 const [open, setOpen] = useState(false);
25 const searchTargetId = useSettingsSearchTargetId();
26 const targetRef = useSettingsSearchTarget<HTMLElement>(id);
27 // A search jump lands inside the fold, so open it before the scroll runs.
28 const [openedForTarget, setOpenedForTarget] = useState<string | null>(null);
29 if (searchTargetId === id && openedForTarget !== id) {
30 setOpenedForTarget(id);
31 if (!open) setOpen(true);
32 }
33​
34 if (headerPlacement === "outside") {
35 return (
36 <section id={id} ref={targetRef} tabIndex={-1} className="outline-none">
37 <Collapsible open={open} onOpenChange={setOpen}>
38 <div className="space-y-2.5">
39 <div
40 data-settings-scroll-target
41 className="flex min-h-7 items-start justify-between gap-4 px-3 sm:px-4"
42 >
43 <h2>
44 <CollapsibleTrigger className="flex min-h-7 items-center gap-2 rounded-md text-sm font-normal text-foreground/70 outline-none focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-ring">
45 {title}
46 <ChevronRightIcon
47 aria-hidden
48 className={cn(
49 "size-4 shrink-0 transition-transform duration-150 motion-reduce:transition-none",
50 open && "rotate-90",
51 )}
52 />
53 </CollapsibleTrigger>
54 </h2>
55 {control}
56 </div>
57 <CollapsiblePanel>
58 <SettingsGroup>{children}</SettingsGroup>
59 </CollapsiblePanel>
60 </div>
61 </Collapsible>
62 </section>
63 );
64 }
65​
66 return (
67 <section id={id} ref={targetRef} tabIndex={-1} className="outline-none">
68 <Collapsible open={open} onOpenChange={setOpen} render={<SettingsGroup divided={false} />}>
69 <div className="flex items-center gap-4 px-3 sm:px-4">
70 <CollapsibleTrigger className="flex min-h-11 min-w-0 flex-1 items-center gap-2 py-2 text-left outline-none focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-ring rounded-md">
71 <ChevronRightIcon
72 aria-hidden
73 className={cn(
74 "size-4 shrink-0 text-muted-foreground transition-transform duration-150 motion-reduce:transition-none",
75 open && "rotate-90",
76 )}
77 />
78 <span className="shrink-0 text-sm font-medium">{title}</span>
79 {summary ? (
80 <span className="min-w-0 truncate text-xs text-muted-foreground">{summary}</span>
81 ) : null}
82 </CollapsibleTrigger>
83 {control ? <div className="flex shrink-0 items-center">{control}</div> : null}
84 </div>
85 <CollapsiblePanel>
86 <div className="border-t border-border/50 [&>*+*]:border-t [&>*+*]:border-border/50">
87 {children}
88 </div>
89 </CollapsiblePanel>
90 </Collapsible>
91 </section>
92 );