|
1
|
"use client" |
|
2
|
|
|
3
|
import React, { Suspense, cloneElement, useRef, useState } from "react" |
|
4
|
import { Loading } from "@/components/Loading" |
|
5
|
import clsx from "clsx" |
|
6
|
import { DetailsSummary, DetailsSummaryProps } from "./Summary" |
|
7
|
import { useCollapsible } from "../../hooks/use-collapsible" |
|
8
|
|
|
9
|
export type DetailsProps = { |
|
10
|
openInitial?: boolean |
|
11
|
summaryContent?: React.ReactNode |
|
12
|
summaryElm?: React.ReactNode |
|
13
|
heightAnimation?: boolean |
|
14
|
} & React.HTMLAttributes<HTMLDetailsElement> |
|
15
|
|
|
16
|
export const Details = ({ |
|
17
|
openInitial = false, |
|
18
|
summaryContent, |
|
19
|
summaryElm, |
|
20
|
children, |
|
21
|
heightAnimation = false, |
|
22
|
...props |
|
23
|
}: DetailsProps) => { |
|
24
|
const [open, setOpen] = useState(openInitial) |
|
25
|
const ref = useRef<HTMLDetailsElement>(null) |
|
26
|
const childrenWrapperRef = useRef<HTMLDivElement>(null) |
|
27
|
const { getCollapsibleElms, setCollapsed } = useCollapsible({ |
|
28
|
initialValue: !openInitial, |
|
29
|
heightAnimation, |
|
30
|
onClose: () => setOpen(false), |
|
31
|
childrenRef: childrenWrapperRef, |
|
32
|
}) |
|
33
|
|
|
34
|
const handleToggle = (e: React.MouseEvent<HTMLElement>) => { |
|
35
|
const targetElm = e.target as HTMLElement |
|
36
|
if (targetElm.tagName.toLowerCase() === "a") { |
|
37
|
window.location.href = |
|
38
|
targetElm.getAttribute("href") || window.location.href |
|
39
|
return |
|
40
|
} |
|
41
|
if (targetElm.tagName.toLowerCase() === "code") { |
|
42
|
return |
|
43
|
} |
|
44
|
if (open) { |
|
45
|
setCollapsed(true) |
|
46
|
} else { |
|
47
|
setOpen(true) |
|
48
|
setCollapsed(false) |
|
49
|
} |
|
50
|
} |
|
51
|
|
|
52
|
return ( |
|
53
|
<details |
|
54
|
{...props} |
|
55
|
ref={ref} |
|
56
|
open={open} |
|
57
|
onClick={(event) => { |
|
58
|
event.preventDefault() |
|
59
|
}} |
|
60
|
onToggle={(event) => { |
|
61
|
// this is to avoid event propagation |
|
62
|
// when details are nested, which is a bug |
|
63
|
// in react. Learn more here: |
|
64
|
// https://github.com/facebook/react/issues/22718 |
|
65
|
event.stopPropagation() |
|
66
|
}} |
|
67
|
className={clsx( |
|
68
|
"border-medusa-border-base border-y border-solid border-x-0", |
|
69
|
"overflow-hidden [&>summary]:relative", |
|
70
|
props.className |
|
71
|
)} |
|
72
|
> |
|
73
|
{summaryContent && ( |
|
74
|
<DetailsSummary |
|
75
|
open={open} |
|
76
|
onClick={handleToggle} |
|
77
|
className="cursor-pointer" |
|
78
|
title={summaryContent} |
|
79
|
/> |
|
80
|
)} |
|
81
|
{summaryElm && |
|
82
|
cloneElement<DetailsSummaryProps>( |
|
83
|
summaryElm as React.ReactElement< |
|
84
|
DetailsSummaryProps, |
|
85
|
React.FunctionComponent<DetailsSummaryProps> |
|
86
|
>, |
|
87
|
{ |
|
88
|
open, |
|
89
|
onClick: handleToggle, |
|
90
|
} |
|
91
|
)} |
|
92
|
{getCollapsibleElms( |
|
93
|
<Suspense fallback={<Loading className="!mb-docs_2 !mt-0" />}> |
|
94
|
<div ref={childrenWrapperRef}>{children}</div> |
|
95
|
</Suspense> |
|
96
|
)} |
|
97
|
</details> |
|
98
|
) |
|
99
|
} |