doccupine 0.0.133 → 0.0.135
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +50 -10
- package/dist/index.d.ts +1 -1
- package/dist/index.js +478 -45
- package/dist/lib/config-manager.d.ts +8 -1
- package/dist/lib/config-manager.js +98 -4
- package/dist/lib/metadata.d.ts +5 -0
- package/dist/lib/metadata.js +15 -4
- package/dist/lib/openapi-types.d.ts +120 -0
- package/dist/lib/openapi-types.js +14 -0
- package/dist/lib/openapi.d.ts +52 -0
- package/dist/lib/openapi.js +603 -0
- package/dist/lib/rss.d.ts +24 -0
- package/dist/lib/rss.js +137 -0
- package/dist/lib/structures.js +50 -4
- package/dist/lib/types.d.ts +22 -0
- package/dist/lib/utils.d.ts +8 -0
- package/dist/lib/utils.js +14 -0
- package/dist/templates/app/api/playground/route.d.ts +1 -0
- package/dist/templates/app/api/playground/route.js +199 -0
- package/dist/templates/app/rssRoute.d.ts +11 -0
- package/dist/templates/app/rssRoute.js +30 -0
- package/dist/templates/components/Docs.d.ts +1 -1
- package/dist/templates/components/Docs.js +15 -3
- package/dist/templates/components/MDXComponents.d.ts +1 -1
- package/dist/templates/components/MDXComponents.js +29 -0
- package/dist/templates/components/SideBar.d.ts +1 -1
- package/dist/templates/components/SideBar.js +13 -0
- package/dist/templates/components/layout/Accordion.d.ts +1 -1
- package/dist/templates/components/layout/Accordion.js +3 -2
- package/dist/templates/components/layout/ActionBar.d.ts +1 -1
- package/dist/templates/components/layout/ActionBar.js +65 -27
- package/dist/templates/components/layout/ApiPlayground.d.ts +1 -0
- package/dist/templates/components/layout/ApiPlayground.js +1075 -0
- package/dist/templates/components/layout/ApiPlaygroundDemo.d.ts +1 -0
- package/dist/templates/components/layout/ApiPlaygroundDemo.js +81 -0
- package/dist/templates/components/layout/Badge.d.ts +1 -0
- package/dist/templates/components/layout/Badge.js +455 -0
- package/dist/templates/components/layout/Code.d.ts +1 -1
- package/dist/templates/components/layout/Code.js +10 -22
- package/dist/templates/components/layout/CopyButton.d.ts +1 -0
- package/dist/templates/components/layout/CopyButton.js +68 -0
- package/dist/templates/components/layout/DocsComponents.d.ts +1 -1
- package/dist/templates/components/layout/DocsComponents.js +14 -0
- package/dist/templates/components/layout/Mermaid.d.ts +1 -1
- package/dist/templates/components/layout/Mermaid.js +21 -3
- package/dist/templates/components/layout/Prompt.d.ts +1 -0
- package/dist/templates/components/layout/Prompt.js +149 -0
- package/dist/templates/components/layout/SharedStyles.d.ts +1 -1
- package/dist/templates/components/layout/SharedStyles.js +0 -4
- package/dist/templates/components/layout/Steps.d.ts +1 -1
- package/dist/templates/components/layout/Steps.js +0 -1
- package/dist/templates/components/layout/Tooltip.d.ts +1 -0
- package/dist/templates/components/layout/Tooltip.js +222 -0
- package/dist/templates/components/layout/Tree.d.ts +1 -0
- package/dist/templates/components/layout/Tree.js +322 -0
- package/dist/templates/components/layout/TreeData.d.ts +1 -0
- package/dist/templates/components/layout/TreeData.js +131 -0
- package/dist/templates/components/layout/Update.d.ts +1 -1
- package/dist/templates/components/layout/Update.js +5 -1
- package/dist/templates/lib/rss.d.ts +1 -0
- package/dist/templates/lib/rss.js +78 -0
- package/dist/templates/mdx/accordion.mdx.d.ts +1 -1
- package/dist/templates/mdx/accordion.mdx.js +19 -1
- package/dist/templates/mdx/ai-assistant.mdx.d.ts +1 -1
- package/dist/templates/mdx/ai-assistant.mdx.js +3 -2
- package/dist/templates/mdx/analytics.mdx.d.ts +1 -1
- package/dist/templates/mdx/analytics.mdx.js +2 -2
- package/dist/templates/mdx/api-playground.mdx.d.ts +1 -0
- package/dist/templates/mdx/api-playground.mdx.js +158 -0
- package/dist/templates/mdx/authentication.mdx.d.ts +1 -1
- package/dist/templates/mdx/authentication.mdx.js +3 -3
- package/dist/templates/mdx/badges.mdx.d.ts +1 -0
- package/dist/templates/mdx/badges.mdx.js +237 -0
- package/dist/templates/mdx/buttons.mdx.d.ts +1 -1
- package/dist/templates/mdx/buttons.mdx.js +1 -1
- package/dist/templates/mdx/callouts.mdx.d.ts +1 -1
- package/dist/templates/mdx/callouts.mdx.js +1 -1
- package/dist/templates/mdx/cards.mdx.d.ts +1 -1
- package/dist/templates/mdx/cards.mdx.js +1 -1
- package/dist/templates/mdx/color-swatches.mdx.d.ts +1 -1
- package/dist/templates/mdx/color-swatches.mdx.js +1 -1
- package/dist/templates/mdx/columns.mdx.d.ts +1 -1
- package/dist/templates/mdx/columns.mdx.js +1 -1
- package/dist/templates/mdx/commands.mdx.d.ts +1 -1
- package/dist/templates/mdx/commands.mdx.js +3 -2
- package/dist/templates/mdx/components.mdx.d.ts +1 -1
- package/dist/templates/mdx/components.mdx.js +25 -10
- package/dist/templates/mdx/deployment-and-hosting.mdx.d.ts +1 -1
- package/dist/templates/mdx/deployment-and-hosting.mdx.js +4 -3
- package/dist/templates/mdx/fields.mdx.d.ts +1 -1
- package/dist/templates/mdx/fields.mdx.js +1 -1
- package/dist/templates/mdx/fonts.mdx.d.ts +1 -1
- package/dist/templates/mdx/fonts.mdx.js +2 -2
- package/dist/templates/mdx/footer-links.mdx.d.ts +1 -1
- package/dist/templates/mdx/footer-links.mdx.js +1 -1
- package/dist/templates/mdx/frames.mdx.d.ts +1 -1
- package/dist/templates/mdx/frames.mdx.js +1 -1
- package/dist/templates/mdx/globals.mdx.d.ts +1 -1
- package/dist/templates/mdx/globals.mdx.js +1 -1
- package/dist/templates/mdx/icons.mdx.d.ts +1 -1
- package/dist/templates/mdx/icons.mdx.js +1 -1
- package/dist/templates/mdx/image-and-embeds.mdx.d.ts +1 -1
- package/dist/templates/mdx/image-and-embeds.mdx.js +1 -1
- package/dist/templates/mdx/images-and-embeds.mdx.d.ts +1 -0
- package/dist/templates/mdx/images-and-embeds.mdx.js +118 -0
- package/dist/templates/mdx/media-and-assets.mdx.d.ts +1 -1
- package/dist/templates/mdx/media-and-assets.mdx.js +2 -2
- package/dist/templates/mdx/mermaid.mdx.d.ts +1 -1
- package/dist/templates/mdx/mermaid.mdx.js +1 -1
- package/dist/templates/mdx/model-context-protocol.mdx.d.ts +1 -1
- package/dist/templates/mdx/model-context-protocol.mdx.js +2 -2
- package/dist/templates/mdx/navigation.mdx.d.ts +1 -1
- package/dist/templates/mdx/navigation.mdx.js +27 -12
- package/dist/templates/mdx/platform/ai-assistant.mdx.d.ts +1 -1
- package/dist/templates/mdx/platform/ai-assistant.mdx.js +4 -3
- package/dist/templates/mdx/platform/analytics.mdx.d.ts +1 -1
- package/dist/templates/mdx/platform/analytics.mdx.js +2 -2
- package/dist/templates/mdx/platform/api-playground.mdx.d.ts +1 -0
- package/dist/templates/mdx/platform/api-playground.mdx.js +62 -0
- package/dist/templates/mdx/platform/billing.mdx.d.ts +1 -1
- package/dist/templates/mdx/platform/billing.mdx.js +1 -1
- package/dist/templates/mdx/platform/build-and-deploy.mdx.d.ts +1 -1
- package/dist/templates/mdx/platform/build-and-deploy.mdx.js +1 -1
- package/dist/templates/mdx/platform/creating-a-project.mdx.d.ts +1 -1
- package/dist/templates/mdx/platform/creating-a-project.mdx.js +12 -2
- package/dist/templates/mdx/platform/custom-domains.mdx.d.ts +1 -1
- package/dist/templates/mdx/platform/custom-domains.mdx.js +1 -1
- package/dist/templates/mdx/platform/fonts-settings.mdx.d.ts +1 -1
- package/dist/templates/mdx/platform/fonts-settings.mdx.js +1 -1
- package/dist/templates/mdx/platform/footer-links.mdx.d.ts +1 -0
- package/dist/templates/mdx/platform/{external-links.mdx.js → footer-links.mdx.js} +5 -5
- package/dist/templates/mdx/platform/import-from-github.mdx.d.ts +1 -0
- package/dist/templates/mdx/platform/import-from-github.mdx.js +91 -0
- package/dist/templates/mdx/platform/mcp.mdx.d.ts +1 -1
- package/dist/templates/mdx/platform/mcp.mdx.js +3 -3
- package/dist/templates/mdx/platform/project-settings.mdx.d.ts +1 -1
- package/dist/templates/mdx/platform/project-settings.mdx.js +1 -1
- package/dist/templates/mdx/platform/self-updating-docs.mdx.d.ts +1 -0
- package/dist/templates/mdx/platform/self-updating-docs.mdx.js +113 -0
- package/dist/templates/mdx/platform/team-members.mdx.d.ts +1 -1
- package/dist/templates/mdx/platform/team-members.mdx.js +1 -1
- package/dist/templates/mdx/prompt.mdx.d.ts +1 -0
- package/dist/templates/mdx/prompt.mdx.js +69 -0
- package/dist/templates/mdx/sections.mdx.d.ts +1 -1
- package/dist/templates/mdx/sections.mdx.js +1 -1
- package/dist/templates/mdx/steps.mdx.d.ts +1 -1
- package/dist/templates/mdx/steps.mdx.js +1 -1
- package/dist/templates/mdx/tabs.mdx.d.ts +1 -1
- package/dist/templates/mdx/tabs.mdx.js +1 -1
- package/dist/templates/mdx/theme.mdx.d.ts +1 -1
- package/dist/templates/mdx/theme.mdx.js +1 -1
- package/dist/templates/mdx/tooltips.mdx.d.ts +1 -0
- package/dist/templates/mdx/tooltips.mdx.js +57 -0
- package/dist/templates/mdx/tree.mdx.d.ts +1 -0
- package/dist/templates/mdx/tree.mdx.js +158 -0
- package/dist/templates/mdx/update.mdx.d.ts +1 -1
- package/dist/templates/mdx/update.mdx.js +42 -2
- package/dist/templates/mdx/what-is-doccupine.mdx.d.ts +1 -1
- package/dist/templates/mdx/what-is-doccupine.mdx.js +1 -1
- package/dist/templates/package.js +7 -7
- package/dist/templates/services/openapi/playgroundAllowlistStub.d.ts +1 -0
- package/dist/templates/services/openapi/playgroundAllowlistStub.js +4 -0
- package/dist/templates/types/openapi.d.ts +1 -0
- package/dist/templates/types/openapi.js +93 -0
- package/dist/templates/utils/apiSnippets.d.ts +1 -0
- package/dist/templates/utils/apiSnippets.js +94 -0
- package/dist/templates/utils/orderNavItems.d.ts +1 -1
- package/dist/templates/utils/orderNavItems.js +3 -0
- package/dist/templates/utils/playgroundAllowlist.d.ts +1 -0
- package/dist/templates/utils/playgroundAllowlist.js +70 -0
- package/dist/templates/utils/ssrfGuard.d.ts +1 -0
- package/dist/templates/utils/ssrfGuard.js +217 -0
- package/package.json +4 -3
- package/dist/templates/mdx/platform/external-links.mdx.d.ts +0 -1
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
export const tooltipTemplate = `"use client";
|
|
2
|
+
import { useEffect, useId, useLayoutEffect, useRef, useState } from "react";
|
|
3
|
+
import Link from "next/link";
|
|
4
|
+
import styled, { css } from "styled-components";
|
|
5
|
+
import { styledSmall } from "cherry-styled-components";
|
|
6
|
+
import { Theme } from "@/app/theme";
|
|
7
|
+
|
|
8
|
+
// Layout cannot be measured during SSR; fall back to useEffect there so
|
|
9
|
+
// prerendering stays warning-free while the browser still measures before
|
|
10
|
+
// paint.
|
|
11
|
+
const useIsomorphicLayoutEffect =
|
|
12
|
+
typeof window === "undefined" ? useEffect : useLayoutEffect;
|
|
13
|
+
|
|
14
|
+
const VIEWPORT_MARGIN = 8;
|
|
15
|
+
|
|
16
|
+
const StyledTooltip = styled.span\`
|
|
17
|
+
position: relative;
|
|
18
|
+
display: inline-block;
|
|
19
|
+
\`;
|
|
20
|
+
|
|
21
|
+
const StyledTooltipTrigger = styled.span<{ theme: Theme }>\`
|
|
22
|
+
cursor: help;
|
|
23
|
+
text-decoration: underline dotted 1.5px;
|
|
24
|
+
text-decoration-color: \${({ theme }) => theme.colors.gray};
|
|
25
|
+
text-underline-offset: 4px;
|
|
26
|
+
border-radius: \${({ theme }) => theme.spacing.radius.xs};
|
|
27
|
+
|
|
28
|
+
&:focus-visible {
|
|
29
|
+
outline: none;
|
|
30
|
+
box-shadow: 0 0 0 2px \${({ theme }) => theme.colors.primaryLight};
|
|
31
|
+
}
|
|
32
|
+
\`;
|
|
33
|
+
|
|
34
|
+
// The gap between trigger and bubble is padding on the positioner (not a
|
|
35
|
+
// margin) so the pointer never crosses empty space on its way to the CTA
|
|
36
|
+
// link, which would fire mouseleave and close the tooltip early.
|
|
37
|
+
const StyledTooltipBubble = styled.span<{
|
|
38
|
+
theme: Theme;
|
|
39
|
+
$isOpen: boolean;
|
|
40
|
+
$placement: "top" | "bottom";
|
|
41
|
+
$shift: number;
|
|
42
|
+
}>\`
|
|
43
|
+
position: absolute;
|
|
44
|
+
left: 50%;
|
|
45
|
+
transform: translateX(calc(-50% + \${({ $shift }) => $shift}px));
|
|
46
|
+
width: max-content;
|
|
47
|
+
max-width: min(260px, calc(100vw - 16px));
|
|
48
|
+
z-index: 10;
|
|
49
|
+
opacity: 0;
|
|
50
|
+
visibility: hidden;
|
|
51
|
+
transition:
|
|
52
|
+
opacity 0.2s ease,
|
|
53
|
+
visibility 0.2s ease;
|
|
54
|
+
|
|
55
|
+
\${({ $placement }) =>
|
|
56
|
+
$placement === "top"
|
|
57
|
+
? css\`
|
|
58
|
+
bottom: 100%;
|
|
59
|
+
padding-bottom: 8px;
|
|
60
|
+
\`
|
|
61
|
+
: css\`
|
|
62
|
+
top: 100%;
|
|
63
|
+
padding-top: 8px;
|
|
64
|
+
\`}
|
|
65
|
+
|
|
66
|
+
\${({ $isOpen }) =>
|
|
67
|
+
$isOpen &&
|
|
68
|
+
css\`
|
|
69
|
+
opacity: 1;
|
|
70
|
+
visibility: visible;
|
|
71
|
+
\`}
|
|
72
|
+
\`;
|
|
73
|
+
|
|
74
|
+
const StyledTooltipContent = styled.span<{ theme: Theme }>\`
|
|
75
|
+
display: block;
|
|
76
|
+
background: \${({ theme }) => theme.colors.light};
|
|
77
|
+
border: solid 1px \${({ theme }) => theme.colors.grayLight};
|
|
78
|
+
border-radius: \${({ theme }) => theme.spacing.radius.lg};
|
|
79
|
+
padding: 10px 14px;
|
|
80
|
+
\${({ theme }) => styledSmall(theme)};
|
|
81
|
+
color: \${({ theme }) => theme.colors.grayDark};
|
|
82
|
+
text-align: left;
|
|
83
|
+
\`;
|
|
84
|
+
|
|
85
|
+
const StyledTooltipHeadline = styled.span<{ theme: Theme }>\`
|
|
86
|
+
display: block;
|
|
87
|
+
font-weight: 700;
|
|
88
|
+
color: \${({ theme }) => theme.colors.dark};
|
|
89
|
+
\`;
|
|
90
|
+
|
|
91
|
+
const StyledTooltipCta = styled.a<{ theme: Theme }>\`
|
|
92
|
+
display: block;
|
|
93
|
+
margin-top: 5px;
|
|
94
|
+
font-weight: 700;
|
|
95
|
+
color: \${({ theme }) => theme.colors.primary};
|
|
96
|
+
text-decoration: none;
|
|
97
|
+
transition: color 0.3s ease;
|
|
98
|
+
|
|
99
|
+
&:hover {
|
|
100
|
+
color: \${({ theme }) => theme.colors.primaryDark};
|
|
101
|
+
}
|
|
102
|
+
\`;
|
|
103
|
+
|
|
104
|
+
interface TooltipProps {
|
|
105
|
+
children: React.ReactNode;
|
|
106
|
+
tip: string;
|
|
107
|
+
headline?: string;
|
|
108
|
+
cta?: string;
|
|
109
|
+
href?: string;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function Tooltip({ children, tip, headline, cta, href }: TooltipProps) {
|
|
113
|
+
const [isOpen, setIsOpen] = useState(false);
|
|
114
|
+
const [placement, setPlacement] = useState<"top" | "bottom">("top");
|
|
115
|
+
const [shift, setShift] = useState(0);
|
|
116
|
+
const wrapperRef = useRef<HTMLSpanElement>(null);
|
|
117
|
+
const bubbleRef = useRef<HTMLSpanElement>(null);
|
|
118
|
+
const tooltipId = useId();
|
|
119
|
+
|
|
120
|
+
// Keep the bubble inside the viewport: shift it horizontally when the
|
|
121
|
+
// trigger sits near a screen edge, and flip it below the trigger when
|
|
122
|
+
// there is no room above. The hidden bubble still has layout, so it can
|
|
123
|
+
// be measured the moment it opens.
|
|
124
|
+
useIsomorphicLayoutEffect(() => {
|
|
125
|
+
if (!isOpen) return;
|
|
126
|
+
const wrapper = wrapperRef.current;
|
|
127
|
+
const bubble = bubbleRef.current;
|
|
128
|
+
if (!wrapper || !bubble) return;
|
|
129
|
+
const triggerRect = wrapper.getBoundingClientRect();
|
|
130
|
+
const bubbleRect = bubble.getBoundingClientRect();
|
|
131
|
+
setPlacement(
|
|
132
|
+
triggerRect.top - bubbleRect.height - VIEWPORT_MARGIN < 0
|
|
133
|
+
? "bottom"
|
|
134
|
+
: "top",
|
|
135
|
+
);
|
|
136
|
+
const center = triggerRect.left + triggerRect.width / 2;
|
|
137
|
+
const half = bubbleRect.width / 2;
|
|
138
|
+
if (center - half < VIEWPORT_MARGIN) {
|
|
139
|
+
setShift(VIEWPORT_MARGIN - (center - half));
|
|
140
|
+
} else if (center + half > window.innerWidth - VIEWPORT_MARGIN) {
|
|
141
|
+
setShift(window.innerWidth - VIEWPORT_MARGIN - (center + half));
|
|
142
|
+
} else {
|
|
143
|
+
setShift(0);
|
|
144
|
+
}
|
|
145
|
+
}, [isOpen]);
|
|
146
|
+
|
|
147
|
+
// Touch devices have no hover and fire blur unreliably, so a tap anywhere
|
|
148
|
+
// outside must close the tooltip explicitly.
|
|
149
|
+
useEffect(() => {
|
|
150
|
+
if (!isOpen) return;
|
|
151
|
+
const closeOnOutsidePointer = (event: PointerEvent) => {
|
|
152
|
+
const wrapper = wrapperRef.current;
|
|
153
|
+
if (
|
|
154
|
+
wrapper &&
|
|
155
|
+
event.target instanceof Node &&
|
|
156
|
+
!wrapper.contains(event.target)
|
|
157
|
+
) {
|
|
158
|
+
setIsOpen(false);
|
|
159
|
+
}
|
|
160
|
+
};
|
|
161
|
+
document.addEventListener("pointerdown", closeOnOutsidePointer);
|
|
162
|
+
return () =>
|
|
163
|
+
document.removeEventListener("pointerdown", closeOnOutsidePointer);
|
|
164
|
+
}, [isOpen]);
|
|
165
|
+
|
|
166
|
+
return (
|
|
167
|
+
<StyledTooltip
|
|
168
|
+
ref={wrapperRef}
|
|
169
|
+
onMouseEnter={() => setIsOpen(true)}
|
|
170
|
+
onMouseLeave={() => setIsOpen(false)}
|
|
171
|
+
onFocus={() => setIsOpen(true)}
|
|
172
|
+
onBlur={(e) =>
|
|
173
|
+
!e.currentTarget.contains(e.relatedTarget) && setIsOpen(false)
|
|
174
|
+
}
|
|
175
|
+
onKeyDown={(e) => e.key === "Escape" && setIsOpen(false)}
|
|
176
|
+
>
|
|
177
|
+
{/* Click opens but never toggles closed: on touch, a tap can fire
|
|
178
|
+
focus and click in the same gesture, and a toggle would close the
|
|
179
|
+
tooltip the instant it opened. */}
|
|
180
|
+
<StyledTooltipTrigger
|
|
181
|
+
tabIndex={0}
|
|
182
|
+
aria-describedby={tooltipId}
|
|
183
|
+
onClick={() => setIsOpen(true)}
|
|
184
|
+
>
|
|
185
|
+
{children}
|
|
186
|
+
</StyledTooltipTrigger>
|
|
187
|
+
<StyledTooltipBubble
|
|
188
|
+
ref={bubbleRef}
|
|
189
|
+
id={tooltipId}
|
|
190
|
+
role="tooltip"
|
|
191
|
+
$isOpen={isOpen}
|
|
192
|
+
$placement={placement}
|
|
193
|
+
$shift={shift}
|
|
194
|
+
>
|
|
195
|
+
<StyledTooltipContent>
|
|
196
|
+
{headline && (
|
|
197
|
+
<StyledTooltipHeadline>{headline}</StyledTooltipHeadline>
|
|
198
|
+
)}
|
|
199
|
+
{tip}
|
|
200
|
+
{cta &&
|
|
201
|
+
href &&
|
|
202
|
+
(href.startsWith("/") ? (
|
|
203
|
+
<StyledTooltipCta as={Link} href={href}>
|
|
204
|
+
{cta}
|
|
205
|
+
</StyledTooltipCta>
|
|
206
|
+
) : (
|
|
207
|
+
<StyledTooltipCta
|
|
208
|
+
href={href}
|
|
209
|
+
target="_blank"
|
|
210
|
+
rel="noopener noreferrer"
|
|
211
|
+
>
|
|
212
|
+
{cta}
|
|
213
|
+
</StyledTooltipCta>
|
|
214
|
+
))}
|
|
215
|
+
</StyledTooltipContent>
|
|
216
|
+
</StyledTooltipBubble>
|
|
217
|
+
</StyledTooltip>
|
|
218
|
+
);
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
export { Tooltip };
|
|
222
|
+
`;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const treeTemplate = "\"use client\";\nimport { useCallback, useEffect, useMemo, useRef, useState } from \"react\";\nimport styled, { css } from \"styled-components\";\nimport { Theme } from \"@/app/theme\";\nimport { Icon } from \"@/components/layout/Icon\";\nimport { thinScrollbar } from \"@/components/layout/SharedStyled\";\nimport type { TreeNode } from \"@/components/layout/TreeData\";\n\nconst TYPE_AHEAD_RESET_MS = 600;\n\ninterface FlatItem {\n node: TreeNode;\n parentId: string | null;\n}\n\n// The prose container styles every descendant \"ul li\" (bullet dots, padding,\n// min-height) at (0,1,2)+ specificity, so the tree's own lists and items use\n// && to reach (0,2,0) and win.\nconst StyledTree = styled.ul<{ theme: Theme }>`\n && {\n background: ${({ theme }) => theme.colors.light};\n border: solid 1px ${({ theme }) => theme.colors.grayLight};\n border-radius: ${({ theme }) => theme.spacing.radius.lg};\n padding: 12px;\n margin: 0;\n width: 100%;\n list-style: none;\n overflow-x: auto;\n ${thinScrollbar};\n }\n`;\n\nconst StyledTreeGroup = styled.ul<{ theme: Theme }>`\n && {\n list-style: none;\n margin: 0 0 0 13px;\n padding: 0 0 0 9px;\n border-left: solid 1px ${({ theme }) => theme.colors.grayLight};\n }\n`;\n\nconst StyledTreeRow = styled.span<{ theme: Theme; $isInteractive: boolean }>`\n display: flex;\n align-items: center;\n gap: 6px;\n padding: 4px 6px;\n border-radius: ${({ theme }) => theme.spacing.radius.xs};\n transition: background 0.2s ease;\n\n ${({ $isInteractive, theme }) =>\n $isInteractive &&\n css`\n cursor: pointer;\n\n &:hover {\n background: ${theme.colors.grayLight};\n }\n `}\n`;\n\nconst StyledTreeItem = styled.li<{ theme: Theme }>`\n && {\n display: block;\n padding: 0;\n margin: 0;\n min-height: 0;\n outline: none;\n }\n\n &&::before {\n content: none;\n }\n\n &:focus-visible > ${StyledTreeRow} {\n box-shadow: 0 0 0 2px ${({ theme }) => theme.colors.primaryLight};\n }\n`;\n\nconst StyledTreeChevron = styled.span<{ theme: Theme; $isOpen: boolean }>`\n display: inline-flex;\n align-items: center;\n justify-content: center;\n flex-shrink: 0;\n width: 16px;\n height: 16px;\n color: ${({ theme }) => theme.colors.gray};\n transition: transform 0.2s ease;\n\n ${({ $isOpen }) =>\n $isOpen &&\n css`\n transform: rotate(90deg);\n `}\n`;\n\nconst StyledTreeSpacer = styled.span`\n flex-shrink: 0;\n width: 16px;\n height: 16px;\n`;\n\nconst StyledTreeIcon = styled.span<{ theme: Theme }>`\n display: inline-flex;\n align-items: center;\n justify-content: center;\n flex-shrink: 0;\n color: ${({ theme }) => theme.colors.gray};\n`;\n\nconst StyledTreeName = styled.span<{ theme: Theme; $isFolder: boolean }>`\n font-family: ${({ theme }) => theme.fonts.mono};\n font-size: 14px;\n line-height: 1.6;\n white-space: nowrap;\n color: ${({ theme, $isFolder }) =>\n $isFolder ? theme.colors.dark : theme.colors.grayDark};\n`;\n\ninterface TreeProps {\n nodes: TreeNode[];\n}\n\n// Renders the TreeNode data that the Tree compound in MDXComponents parses\n// out of either authoring syntax. Receiving plain data (never elements)\n// keeps the server/client boundary trivially serializable.\nfunction Tree({ nodes }: TreeProps) {\n // Open state is stored as overrides on top of each folder's defaultOpen so\n // nodes parsed after a re-render keep their authored state until toggled.\n const [overrides, setOverrides] = useState<Record<string, boolean>>({});\n const [focusedId, setFocusedId] = useState<string | null>(null);\n const itemRefs = useRef<Map<string, HTMLLIElement>>(new Map());\n const typeAheadBuffer = useRef(\"\");\n const typeAheadTimer = useRef<ReturnType<typeof setTimeout> | null>(null);\n\n useEffect(\n () => () => {\n if (typeAheadTimer.current) clearTimeout(typeAheadTimer.current);\n },\n [],\n );\n\n const isOpen = useCallback(\n (node: TreeNode) => overrides[node.id] ?? node.defaultOpen,\n [overrides],\n );\n\n // The flattened list of currently visible items drives keyboard\n // navigation in document order.\n const visible = useMemo(() => {\n const out: FlatItem[] = [];\n const walk = (list: TreeNode[], parentId: string | null) => {\n list.forEach((node) => {\n out.push({ node, parentId });\n if (node.isFolder && isOpen(node)) {\n walk(node.children, node.id);\n }\n });\n };\n walk(nodes, null);\n return out;\n }, [nodes, isOpen]);\n\n // Roving tabindex: exactly one visible item is tabbable. Falls back to the\n // first item when focus points at a node hidden by a collapsed ancestor.\n const activeId = useMemo(() => {\n if (focusedId && visible.some((item) => item.node.id === focusedId)) {\n return focusedId;\n }\n return visible.length > 0 ? visible[0].node.id : null;\n }, [focusedId, visible]);\n\n const setOpen = (node: TreeNode, open: boolean) => {\n if (!node.isFolder || !node.openable) return;\n setOverrides((prev) => ({ ...prev, [node.id]: open }));\n };\n\n const focusItem = (id: string) => {\n setFocusedId(id);\n itemRefs.current.get(id)?.focus();\n };\n\n const handleKeyDown = (event: React.KeyboardEvent<HTMLUListElement>) => {\n if (event.ctrlKey || event.metaKey || event.altKey) return;\n const index = visible.findIndex((item) => item.node.id === activeId);\n if (index < 0) return;\n const current = visible[index];\n const node = current.node;\n const key = event.key;\n\n if (key === \"ArrowDown\") {\n event.preventDefault();\n if (index + 1 < visible.length) focusItem(visible[index + 1].node.id);\n } else if (key === \"ArrowUp\") {\n event.preventDefault();\n if (index > 0) focusItem(visible[index - 1].node.id);\n } else if (key === \"ArrowRight\") {\n event.preventDefault();\n if (!node.isFolder) return;\n if (!isOpen(node)) setOpen(node, true);\n else if (node.children.length > 0) focusItem(node.children[0].id);\n } else if (key === \"ArrowLeft\") {\n event.preventDefault();\n if (node.isFolder && isOpen(node) && node.openable) {\n setOpen(node, false);\n } else if (current.parentId) {\n focusItem(current.parentId);\n }\n } else if (key === \"Home\") {\n event.preventDefault();\n focusItem(visible[0].node.id);\n } else if (key === \"End\") {\n event.preventDefault();\n focusItem(visible[visible.length - 1].node.id);\n } else if (key === \"Enter\" || key === \" \") {\n event.preventDefault();\n setOpen(node, !isOpen(node));\n } else if (key === \"*\") {\n event.preventDefault();\n setOverrides((prev) => {\n const next = { ...prev };\n visible.forEach((item) => {\n if (\n item.parentId === current.parentId &&\n item.node.isFolder &&\n item.node.openable\n ) {\n next[item.node.id] = true;\n }\n });\n return next;\n });\n } else if (key.length === 1 && key !== \" \") {\n // Type-ahead: buffered characters jump to the next item whose name\n // starts with what was typed.\n if (typeAheadTimer.current) clearTimeout(typeAheadTimer.current);\n typeAheadBuffer.current += key.toLowerCase();\n typeAheadTimer.current = setTimeout(() => {\n typeAheadBuffer.current = \"\";\n }, TYPE_AHEAD_RESET_MS);\n const query = typeAheadBuffer.current;\n const start = query.length === 1 ? index + 1 : index;\n for (let offset = 0; offset < visible.length; offset++) {\n const item = visible[(start + offset) % visible.length];\n if (item.node.name.toLowerCase().startsWith(query)) {\n focusItem(item.node.id);\n break;\n }\n }\n }\n };\n\n const renderItems = (list: TreeNode[], level: number) =>\n list.map((node, index) => {\n const open = node.isFolder && isOpen(node);\n const isInteractive = node.isFolder && node.openable;\n return (\n <StyledTreeItem\n key={node.id}\n role=\"treeitem\"\n aria-level={level}\n aria-setsize={list.length}\n aria-posinset={index + 1}\n aria-expanded={node.isFolder ? open : undefined}\n tabIndex={node.id === activeId ? 0 : -1}\n ref={(element) => {\n if (element) itemRefs.current.set(node.id, element);\n else itemRefs.current.delete(node.id);\n }}\n onFocus={(event) => {\n // Focus events bubble through nested treeitems; without this a\n // child's focus would also mark every ancestor as focused.\n event.stopPropagation();\n setFocusedId(node.id);\n }}\n >\n {/* Safari does not reliably focus the item on click, so focus is\n set explicitly before toggling. */}\n <StyledTreeRow\n $isInteractive={isInteractive}\n onClick={(event) => {\n event.stopPropagation();\n focusItem(node.id);\n if (isInteractive) setOpen(node, !open);\n }}\n >\n {isInteractive ? (\n <StyledTreeChevron $isOpen={open} aria-hidden>\n <Icon name=\"ChevronRight\" size={14} />\n </StyledTreeChevron>\n ) : (\n <StyledTreeSpacer aria-hidden />\n )}\n <StyledTreeIcon aria-hidden>\n <Icon\n name={node.isFolder ? (open ? \"FolderOpen\" : \"Folder\") : \"File\"}\n size={16}\n />\n </StyledTreeIcon>\n <StyledTreeName $isFolder={node.isFolder}>\n {node.name}\n </StyledTreeName>\n </StyledTreeRow>\n {node.isFolder && open && node.children.length > 0 && (\n <StyledTreeGroup role=\"group\">\n {renderItems(node.children, level + 1)}\n </StyledTreeGroup>\n )}\n </StyledTreeItem>\n );\n });\n\n if (nodes.length === 0) return null;\n\n return (\n <StyledTree role=\"tree\" aria-label=\"File tree\" onKeyDown={handleKeyDown}>\n {renderItems(nodes, 1)}\n </StyledTree>\n );\n}\n\nexport { Tree };\n";
|
|
@@ -0,0 +1,322 @@
|
|
|
1
|
+
export const treeTemplate = `"use client";
|
|
2
|
+
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
|
3
|
+
import styled, { css } from "styled-components";
|
|
4
|
+
import { Theme } from "@/app/theme";
|
|
5
|
+
import { Icon } from "@/components/layout/Icon";
|
|
6
|
+
import { thinScrollbar } from "@/components/layout/SharedStyled";
|
|
7
|
+
import type { TreeNode } from "@/components/layout/TreeData";
|
|
8
|
+
|
|
9
|
+
const TYPE_AHEAD_RESET_MS = 600;
|
|
10
|
+
|
|
11
|
+
interface FlatItem {
|
|
12
|
+
node: TreeNode;
|
|
13
|
+
parentId: string | null;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
// The prose container styles every descendant "ul li" (bullet dots, padding,
|
|
17
|
+
// min-height) at (0,1,2)+ specificity, so the tree's own lists and items use
|
|
18
|
+
// && to reach (0,2,0) and win.
|
|
19
|
+
const StyledTree = styled.ul<{ theme: Theme }>\`
|
|
20
|
+
&& {
|
|
21
|
+
background: \${({ theme }) => theme.colors.light};
|
|
22
|
+
border: solid 1px \${({ theme }) => theme.colors.grayLight};
|
|
23
|
+
border-radius: \${({ theme }) => theme.spacing.radius.lg};
|
|
24
|
+
padding: 12px;
|
|
25
|
+
margin: 0;
|
|
26
|
+
width: 100%;
|
|
27
|
+
list-style: none;
|
|
28
|
+
overflow-x: auto;
|
|
29
|
+
\${thinScrollbar};
|
|
30
|
+
}
|
|
31
|
+
\`;
|
|
32
|
+
|
|
33
|
+
const StyledTreeGroup = styled.ul<{ theme: Theme }>\`
|
|
34
|
+
&& {
|
|
35
|
+
list-style: none;
|
|
36
|
+
margin: 0 0 0 13px;
|
|
37
|
+
padding: 0 0 0 9px;
|
|
38
|
+
border-left: solid 1px \${({ theme }) => theme.colors.grayLight};
|
|
39
|
+
}
|
|
40
|
+
\`;
|
|
41
|
+
|
|
42
|
+
const StyledTreeRow = styled.span<{ theme: Theme; $isInteractive: boolean }>\`
|
|
43
|
+
display: flex;
|
|
44
|
+
align-items: center;
|
|
45
|
+
gap: 6px;
|
|
46
|
+
padding: 4px 6px;
|
|
47
|
+
border-radius: \${({ theme }) => theme.spacing.radius.xs};
|
|
48
|
+
transition: background 0.2s ease;
|
|
49
|
+
|
|
50
|
+
\${({ $isInteractive, theme }) =>
|
|
51
|
+
$isInteractive &&
|
|
52
|
+
css\`
|
|
53
|
+
cursor: pointer;
|
|
54
|
+
|
|
55
|
+
&:hover {
|
|
56
|
+
background: \${theme.colors.grayLight};
|
|
57
|
+
}
|
|
58
|
+
\`}
|
|
59
|
+
\`;
|
|
60
|
+
|
|
61
|
+
const StyledTreeItem = styled.li<{ theme: Theme }>\`
|
|
62
|
+
&& {
|
|
63
|
+
display: block;
|
|
64
|
+
padding: 0;
|
|
65
|
+
margin: 0;
|
|
66
|
+
min-height: 0;
|
|
67
|
+
outline: none;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
&&::before {
|
|
71
|
+
content: none;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
&:focus-visible > \${StyledTreeRow} {
|
|
75
|
+
box-shadow: 0 0 0 2px \${({ theme }) => theme.colors.primaryLight};
|
|
76
|
+
}
|
|
77
|
+
\`;
|
|
78
|
+
|
|
79
|
+
const StyledTreeChevron = styled.span<{ theme: Theme; $isOpen: boolean }>\`
|
|
80
|
+
display: inline-flex;
|
|
81
|
+
align-items: center;
|
|
82
|
+
justify-content: center;
|
|
83
|
+
flex-shrink: 0;
|
|
84
|
+
width: 16px;
|
|
85
|
+
height: 16px;
|
|
86
|
+
color: \${({ theme }) => theme.colors.gray};
|
|
87
|
+
transition: transform 0.2s ease;
|
|
88
|
+
|
|
89
|
+
\${({ $isOpen }) =>
|
|
90
|
+
$isOpen &&
|
|
91
|
+
css\`
|
|
92
|
+
transform: rotate(90deg);
|
|
93
|
+
\`}
|
|
94
|
+
\`;
|
|
95
|
+
|
|
96
|
+
const StyledTreeSpacer = styled.span\`
|
|
97
|
+
flex-shrink: 0;
|
|
98
|
+
width: 16px;
|
|
99
|
+
height: 16px;
|
|
100
|
+
\`;
|
|
101
|
+
|
|
102
|
+
const StyledTreeIcon = styled.span<{ theme: Theme }>\`
|
|
103
|
+
display: inline-flex;
|
|
104
|
+
align-items: center;
|
|
105
|
+
justify-content: center;
|
|
106
|
+
flex-shrink: 0;
|
|
107
|
+
color: \${({ theme }) => theme.colors.gray};
|
|
108
|
+
\`;
|
|
109
|
+
|
|
110
|
+
const StyledTreeName = styled.span<{ theme: Theme; $isFolder: boolean }>\`
|
|
111
|
+
font-family: \${({ theme }) => theme.fonts.mono};
|
|
112
|
+
font-size: 14px;
|
|
113
|
+
line-height: 1.6;
|
|
114
|
+
white-space: nowrap;
|
|
115
|
+
color: \${({ theme, $isFolder }) =>
|
|
116
|
+
$isFolder ? theme.colors.dark : theme.colors.grayDark};
|
|
117
|
+
\`;
|
|
118
|
+
|
|
119
|
+
interface TreeProps {
|
|
120
|
+
nodes: TreeNode[];
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// Renders the TreeNode data that the Tree compound in MDXComponents parses
|
|
124
|
+
// out of either authoring syntax. Receiving plain data (never elements)
|
|
125
|
+
// keeps the server/client boundary trivially serializable.
|
|
126
|
+
function Tree({ nodes }: TreeProps) {
|
|
127
|
+
// Open state is stored as overrides on top of each folder's defaultOpen so
|
|
128
|
+
// nodes parsed after a re-render keep their authored state until toggled.
|
|
129
|
+
const [overrides, setOverrides] = useState<Record<string, boolean>>({});
|
|
130
|
+
const [focusedId, setFocusedId] = useState<string | null>(null);
|
|
131
|
+
const itemRefs = useRef<Map<string, HTMLLIElement>>(new Map());
|
|
132
|
+
const typeAheadBuffer = useRef("");
|
|
133
|
+
const typeAheadTimer = useRef<ReturnType<typeof setTimeout> | null>(null);
|
|
134
|
+
|
|
135
|
+
useEffect(
|
|
136
|
+
() => () => {
|
|
137
|
+
if (typeAheadTimer.current) clearTimeout(typeAheadTimer.current);
|
|
138
|
+
},
|
|
139
|
+
[],
|
|
140
|
+
);
|
|
141
|
+
|
|
142
|
+
const isOpen = useCallback(
|
|
143
|
+
(node: TreeNode) => overrides[node.id] ?? node.defaultOpen,
|
|
144
|
+
[overrides],
|
|
145
|
+
);
|
|
146
|
+
|
|
147
|
+
// The flattened list of currently visible items drives keyboard
|
|
148
|
+
// navigation in document order.
|
|
149
|
+
const visible = useMemo(() => {
|
|
150
|
+
const out: FlatItem[] = [];
|
|
151
|
+
const walk = (list: TreeNode[], parentId: string | null) => {
|
|
152
|
+
list.forEach((node) => {
|
|
153
|
+
out.push({ node, parentId });
|
|
154
|
+
if (node.isFolder && isOpen(node)) {
|
|
155
|
+
walk(node.children, node.id);
|
|
156
|
+
}
|
|
157
|
+
});
|
|
158
|
+
};
|
|
159
|
+
walk(nodes, null);
|
|
160
|
+
return out;
|
|
161
|
+
}, [nodes, isOpen]);
|
|
162
|
+
|
|
163
|
+
// Roving tabindex: exactly one visible item is tabbable. Falls back to the
|
|
164
|
+
// first item when focus points at a node hidden by a collapsed ancestor.
|
|
165
|
+
const activeId = useMemo(() => {
|
|
166
|
+
if (focusedId && visible.some((item) => item.node.id === focusedId)) {
|
|
167
|
+
return focusedId;
|
|
168
|
+
}
|
|
169
|
+
return visible.length > 0 ? visible[0].node.id : null;
|
|
170
|
+
}, [focusedId, visible]);
|
|
171
|
+
|
|
172
|
+
const setOpen = (node: TreeNode, open: boolean) => {
|
|
173
|
+
if (!node.isFolder || !node.openable) return;
|
|
174
|
+
setOverrides((prev) => ({ ...prev, [node.id]: open }));
|
|
175
|
+
};
|
|
176
|
+
|
|
177
|
+
const focusItem = (id: string) => {
|
|
178
|
+
setFocusedId(id);
|
|
179
|
+
itemRefs.current.get(id)?.focus();
|
|
180
|
+
};
|
|
181
|
+
|
|
182
|
+
const handleKeyDown = (event: React.KeyboardEvent<HTMLUListElement>) => {
|
|
183
|
+
if (event.ctrlKey || event.metaKey || event.altKey) return;
|
|
184
|
+
const index = visible.findIndex((item) => item.node.id === activeId);
|
|
185
|
+
if (index < 0) return;
|
|
186
|
+
const current = visible[index];
|
|
187
|
+
const node = current.node;
|
|
188
|
+
const key = event.key;
|
|
189
|
+
|
|
190
|
+
if (key === "ArrowDown") {
|
|
191
|
+
event.preventDefault();
|
|
192
|
+
if (index + 1 < visible.length) focusItem(visible[index + 1].node.id);
|
|
193
|
+
} else if (key === "ArrowUp") {
|
|
194
|
+
event.preventDefault();
|
|
195
|
+
if (index > 0) focusItem(visible[index - 1].node.id);
|
|
196
|
+
} else if (key === "ArrowRight") {
|
|
197
|
+
event.preventDefault();
|
|
198
|
+
if (!node.isFolder) return;
|
|
199
|
+
if (!isOpen(node)) setOpen(node, true);
|
|
200
|
+
else if (node.children.length > 0) focusItem(node.children[0].id);
|
|
201
|
+
} else if (key === "ArrowLeft") {
|
|
202
|
+
event.preventDefault();
|
|
203
|
+
if (node.isFolder && isOpen(node) && node.openable) {
|
|
204
|
+
setOpen(node, false);
|
|
205
|
+
} else if (current.parentId) {
|
|
206
|
+
focusItem(current.parentId);
|
|
207
|
+
}
|
|
208
|
+
} else if (key === "Home") {
|
|
209
|
+
event.preventDefault();
|
|
210
|
+
focusItem(visible[0].node.id);
|
|
211
|
+
} else if (key === "End") {
|
|
212
|
+
event.preventDefault();
|
|
213
|
+
focusItem(visible[visible.length - 1].node.id);
|
|
214
|
+
} else if (key === "Enter" || key === " ") {
|
|
215
|
+
event.preventDefault();
|
|
216
|
+
setOpen(node, !isOpen(node));
|
|
217
|
+
} else if (key === "*") {
|
|
218
|
+
event.preventDefault();
|
|
219
|
+
setOverrides((prev) => {
|
|
220
|
+
const next = { ...prev };
|
|
221
|
+
visible.forEach((item) => {
|
|
222
|
+
if (
|
|
223
|
+
item.parentId === current.parentId &&
|
|
224
|
+
item.node.isFolder &&
|
|
225
|
+
item.node.openable
|
|
226
|
+
) {
|
|
227
|
+
next[item.node.id] = true;
|
|
228
|
+
}
|
|
229
|
+
});
|
|
230
|
+
return next;
|
|
231
|
+
});
|
|
232
|
+
} else if (key.length === 1 && key !== " ") {
|
|
233
|
+
// Type-ahead: buffered characters jump to the next item whose name
|
|
234
|
+
// starts with what was typed.
|
|
235
|
+
if (typeAheadTimer.current) clearTimeout(typeAheadTimer.current);
|
|
236
|
+
typeAheadBuffer.current += key.toLowerCase();
|
|
237
|
+
typeAheadTimer.current = setTimeout(() => {
|
|
238
|
+
typeAheadBuffer.current = "";
|
|
239
|
+
}, TYPE_AHEAD_RESET_MS);
|
|
240
|
+
const query = typeAheadBuffer.current;
|
|
241
|
+
const start = query.length === 1 ? index + 1 : index;
|
|
242
|
+
for (let offset = 0; offset < visible.length; offset++) {
|
|
243
|
+
const item = visible[(start + offset) % visible.length];
|
|
244
|
+
if (item.node.name.toLowerCase().startsWith(query)) {
|
|
245
|
+
focusItem(item.node.id);
|
|
246
|
+
break;
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
};
|
|
251
|
+
|
|
252
|
+
const renderItems = (list: TreeNode[], level: number) =>
|
|
253
|
+
list.map((node, index) => {
|
|
254
|
+
const open = node.isFolder && isOpen(node);
|
|
255
|
+
const isInteractive = node.isFolder && node.openable;
|
|
256
|
+
return (
|
|
257
|
+
<StyledTreeItem
|
|
258
|
+
key={node.id}
|
|
259
|
+
role="treeitem"
|
|
260
|
+
aria-level={level}
|
|
261
|
+
aria-setsize={list.length}
|
|
262
|
+
aria-posinset={index + 1}
|
|
263
|
+
aria-expanded={node.isFolder ? open : undefined}
|
|
264
|
+
tabIndex={node.id === activeId ? 0 : -1}
|
|
265
|
+
ref={(element) => {
|
|
266
|
+
if (element) itemRefs.current.set(node.id, element);
|
|
267
|
+
else itemRefs.current.delete(node.id);
|
|
268
|
+
}}
|
|
269
|
+
onFocus={(event) => {
|
|
270
|
+
// Focus events bubble through nested treeitems; without this a
|
|
271
|
+
// child's focus would also mark every ancestor as focused.
|
|
272
|
+
event.stopPropagation();
|
|
273
|
+
setFocusedId(node.id);
|
|
274
|
+
}}
|
|
275
|
+
>
|
|
276
|
+
{/* Safari does not reliably focus the item on click, so focus is
|
|
277
|
+
set explicitly before toggling. */}
|
|
278
|
+
<StyledTreeRow
|
|
279
|
+
$isInteractive={isInteractive}
|
|
280
|
+
onClick={(event) => {
|
|
281
|
+
event.stopPropagation();
|
|
282
|
+
focusItem(node.id);
|
|
283
|
+
if (isInteractive) setOpen(node, !open);
|
|
284
|
+
}}
|
|
285
|
+
>
|
|
286
|
+
{isInteractive ? (
|
|
287
|
+
<StyledTreeChevron $isOpen={open} aria-hidden>
|
|
288
|
+
<Icon name="ChevronRight" size={14} />
|
|
289
|
+
</StyledTreeChevron>
|
|
290
|
+
) : (
|
|
291
|
+
<StyledTreeSpacer aria-hidden />
|
|
292
|
+
)}
|
|
293
|
+
<StyledTreeIcon aria-hidden>
|
|
294
|
+
<Icon
|
|
295
|
+
name={node.isFolder ? (open ? "FolderOpen" : "Folder") : "File"}
|
|
296
|
+
size={16}
|
|
297
|
+
/>
|
|
298
|
+
</StyledTreeIcon>
|
|
299
|
+
<StyledTreeName $isFolder={node.isFolder}>
|
|
300
|
+
{node.name}
|
|
301
|
+
</StyledTreeName>
|
|
302
|
+
</StyledTreeRow>
|
|
303
|
+
{node.isFolder && open && node.children.length > 0 && (
|
|
304
|
+
<StyledTreeGroup role="group">
|
|
305
|
+
{renderItems(node.children, level + 1)}
|
|
306
|
+
</StyledTreeGroup>
|
|
307
|
+
)}
|
|
308
|
+
</StyledTreeItem>
|
|
309
|
+
);
|
|
310
|
+
});
|
|
311
|
+
|
|
312
|
+
if (nodes.length === 0) return null;
|
|
313
|
+
|
|
314
|
+
return (
|
|
315
|
+
<StyledTree role="tree" aria-label="File tree" onKeyDown={handleKeyDown}>
|
|
316
|
+
{renderItems(nodes, 1)}
|
|
317
|
+
</StyledTree>
|
|
318
|
+
);
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
export { Tree };
|
|
322
|
+
`;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const treeDataTemplate = "import React from \"react\";\n\nexport interface TreeNode {\n id: string;\n name: string;\n isFolder: boolean;\n defaultOpen: boolean;\n openable: boolean;\n children: TreeNode[];\n}\n\nexport interface TreeFolderProps {\n name: string;\n defaultOpen?: boolean;\n openable?: boolean;\n children?: React.ReactNode;\n}\n\nexport interface TreeFileProps {\n name: string;\n}\n\n// Tree.Folder and Tree.File are declarative markers: the Tree compound in\n// MDXComponents parses them into TreeNode data before anything renders, so\n// they never render on their own. This module must stay free of\n// \"use client\" - parsing runs where MDX creates the elements (server\n// components included), and only there is comparing an element's type\n// against these markers guaranteed to work. Client modules would turn the\n// markers into reference proxies whose identity does not survive the\n// server boundary.\nexport function TreeFolder(_props: TreeFolderProps) {\n return null;\n}\n\nexport function TreeFile(_props: TreeFileProps) {\n return null;\n}\n\nfunction extractText(children: React.ReactNode): string {\n if (children == null || typeof children === \"boolean\") return \"\";\n if (typeof children === \"string\" || typeof children === \"number\") {\n return String(children);\n }\n if (Array.isArray(children)) return children.map(extractText).join(\"\");\n if (React.isValidElement(children)) {\n const element = children as React.ReactElement<{\n children?: React.ReactNode;\n }>;\n return extractText(element.props.children);\n }\n return \"\";\n}\n\ntype ListElement = React.ReactElement<{ children?: React.ReactNode }>;\n\nfunction isListElement(child: React.ReactNode): child is ListElement {\n return (\n React.isValidElement(child) && (child.type === \"ul\" || child.type === \"ol\")\n );\n}\n\n// Markdown list syntax: a trailing slash or nested items mark a folder, and\n// folders that contain items expand by default. Names strip inline\n// formatting because only the text content is extracted. Ids derive from\n// position and name so open state survives re-renders.\nfunction parseListItems(list: ListElement, parentId: string): TreeNode[] {\n const nodes: TreeNode[] = [];\n React.Children.forEach(list.props.children, (item) => {\n if (!React.isValidElement(item) || item.type !== \"li\") return;\n const li = item as React.ReactElement<{ children?: React.ReactNode }>;\n const labelParts: React.ReactNode[] = [];\n const nestedLists: ListElement[] = [];\n React.Children.forEach(li.props.children, (child) => {\n if (isListElement(child)) nestedLists.push(child);\n else labelParts.push(child);\n });\n const raw = extractText(labelParts).trim();\n const name = raw.endsWith(\"/\") ? raw.slice(0, -1) : raw;\n if (!name) return;\n const id = parentId + \"/\" + nodes.length + \"-\" + name;\n nodes.push({\n id,\n name,\n isFolder: raw.endsWith(\"/\") || nestedLists.length > 0,\n defaultOpen: nestedLists.length > 0,\n openable: true,\n children: nestedLists.flatMap((nested) => parseListItems(nested, id)),\n });\n });\n return nodes;\n}\n\n// Both authoring syntaxes (Tree.Folder/Tree.File elements and markdown\n// lists) normalize into the same node model, so they can be mixed freely.\nexport function parseTreeChildren(\n children: React.ReactNode,\n parentId: string,\n): TreeNode[] {\n const nodes: TreeNode[] = [];\n React.Children.forEach(children, (child) => {\n if (!React.isValidElement(child)) return;\n if (child.type === TreeFolder) {\n const props = child.props as TreeFolderProps;\n if (!props.name) return;\n const id = parentId + \"/\" + nodes.length + \"-\" + props.name;\n nodes.push({\n id,\n name: props.name,\n isFolder: true,\n defaultOpen: props.defaultOpen === true,\n openable: props.openable !== false,\n children: parseTreeChildren(props.children, id),\n });\n } else if (child.type === TreeFile) {\n const props = child.props as TreeFileProps;\n if (!props.name) return;\n nodes.push({\n id: parentId + \"/\" + nodes.length + \"-\" + props.name,\n name: props.name,\n isFolder: false,\n defaultOpen: false,\n openable: false,\n children: [],\n });\n } else if (isListElement(child)) {\n nodes.push(...parseListItems(child, parentId + \"/\" + nodes.length));\n }\n });\n return nodes;\n}\n";
|