forked from jordanlambrecht/tracker-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCollapsibleCard.tsx
More file actions
67 lines (59 loc) · 1.96 KB
/
Copy pathCollapsibleCard.tsx
File metadata and controls
67 lines (59 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// src/components/ui/CollapsibleCard.tsx
"use client"
import { type ReactNode, useRef, useState } from "react"
import { Card } from "@/components/ui/Card"
import { ChevronToggle } from "@/components/ui/ChevronToggle"
interface CollapsibleCardProps {
header: ReactNode
subheader?: ReactNode
trailing?: ReactNode
children: ReactNode
expanded?: boolean
onToggle?: () => void
defaultExpanded?: boolean
}
export type { CollapsibleCardProps }
export function CollapsibleCard({
header,
subheader,
trailing,
children,
expanded: controlledExpanded,
onToggle,
defaultExpanded = false,
}: CollapsibleCardProps) {
const [internalExpanded, setInternalExpanded] = useState(defaultExpanded)
const isExpanded = controlledExpanded ?? internalExpanded
const bodyRef = useRef<HTMLDivElement>(null)
function handleToggle() {
if (onToggle) {
onToggle()
} else {
setInternalExpanded((e) => !e)
}
}
return (
<Card elevation="raised" className="flex flex-col gap-0 p-0 overflow-hidden">
<button
type="button"
onClick={handleToggle}
aria-expanded={isExpanded}
className="flex items-center gap-3 px-5 py-4 w-full text-left cursor-pointer hover:bg-overlay active:nm-inset-sm active:scale-[0.99] transition-colors duration-100"
>
<div className="flex-1 min-w-0 flex items-center gap-3">{header}</div>
{trailing && <span className="text-xs font-mono text-tertiary shrink-0">{trailing}</span>}
<ChevronToggle expanded={isExpanded} variant="flip" className="text-tertiary text-sm" />
</button>
{subheader}
<div
ref={bodyRef}
className="grid transition-[grid-template-rows] duration-200 ease-in-out"
style={{ gridTemplateRows: isExpanded ? "1fr" : "0fr" }}
>
<div className="overflow-hidden">
<div className="px-5 pb-5 flex flex-col gap-5 border-t border-border">{children}</div>
</div>
</div>
</Card>
)
}