forked from webpack/webpack.js.org
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomponents.jsx
More file actions
85 lines (71 loc) · 2.27 KB
/
components.jsx
File metadata and controls
85 lines (71 loc) · 2.27 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import PropTypes from "prop-types";
import { isValidElement, useState } from "react";
import Popover from "react-tiny-popover";
const DEFAULT_CHILDREN_SIZE = 4;
const isFirstChild = (child) => typeof child === "string" && child !== " ";
const removeSpaces = (child) => (isFirstChild(child) ? child.trim() : child);
const addLink = (child, i, url) =>
isFirstChild(child) ? (
<a href={url} key={i}>
{child}
</a>
) : (
child
);
const Card = ({ body }) => (
// Applied .shadow > .markdown styles: max-height and overflow
<div className="markdown max-h-[48vh] overflow-auto">
{/* Combined .inline and .shadow pre.inline styles:
display: block, margin: 0, padding: 0 with right-padding override
*/}
<pre className="block m-0 p-0 pr-[15px]">
<code>{body}</code>
</pre>
</div>
);
Card.propTypes = {
body: PropTypes.node,
};
export function Details({ url, myChilds }) {
const [open, setOpen] = useState(false);
// Find the index of </default>
const closeDefaultTagIndex = myChilds.findIndex((child) => {
if (isValidElement(child)) {
return (
child.props.className.includes("tag") &&
child.props.children.length === DEFAULT_CHILDREN_SIZE
);
}
return false;
});
const content = [...myChilds];
// Summary is the part of the snippet that would be shown in the code snippet,
// to get it we need to cut the <default></default> enclosing tags
const summary = content
.splice(2, closeDefaultTagIndex - 3)
.map(removeSpaces)
.map((child, i) => addLink(child, i, url));
content.splice(0, DEFAULT_CHILDREN_SIZE); // Remove <default></default> information
return (
<Popover
isOpen={open}
position={["right", "top"]}
padding={0}
onClickOutside={() => setOpen(false)}
// Replaced .shadow with Tailwind equivalents, including the custom rgba box-shadow
containerClassName="overflow-visible rounded shadow-[-1px_1px_10px_0_rgba(255,255,255,0.44)]"
content={<Card body={content} />}
>
<span
className="code-details-summary-span"
onClick={() => setOpen((prev) => !prev)}
>
{summary}
</span>
</Popover>
);
}
Details.propTypes = {
url: PropTypes.string,
myChilds: PropTypes.arrayOf(PropTypes.node),
};