doccupine 0.0.140 → 0.0.142
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 +14 -7
- package/dist/cli.d.ts +1 -0
- package/dist/cli.js +191 -0
- package/dist/index.d.ts +3 -7
- package/dist/index.js +5 -1894
- package/dist/lib/config-manager.d.ts +1 -0
- package/dist/lib/config-manager.js +63 -3
- package/dist/lib/generated-artifacts.d.ts +26 -0
- package/dist/lib/generated-artifacts.js +147 -0
- package/dist/lib/layout.js +30 -30
- package/dist/lib/metadata.js +58 -30
- package/dist/lib/openapi-types.d.ts +2 -2
- package/dist/lib/openapi.d.ts +6 -3
- package/dist/lib/openapi.js +330 -50
- package/dist/lib/output-safety.d.ts +15 -0
- package/dist/lib/output-safety.js +428 -0
- package/dist/lib/structures.js +11 -0
- package/dist/lib/utils.d.ts +10 -5
- package/dist/lib/utils.js +108 -5
- package/dist/mdx-to-nextjs-generator.d.ts +166 -0
- package/dist/mdx-to-nextjs-generator.js +2229 -0
- package/dist/templates/app/api/gate/route.d.ts +1 -1
- package/dist/templates/app/api/gate/route.js +18 -6
- package/dist/templates/app/api/mcp/route.d.ts +1 -1
- package/dist/templates/app/api/mcp/route.js +181 -111
- package/dist/templates/app/api/playground/route.d.ts +1 -1
- package/dist/templates/app/api/playground/route.js +45 -13
- package/dist/templates/app/api/rag/route.d.ts +1 -1
- package/dist/templates/app/api/rag/route.js +72 -20
- package/dist/templates/app/api/search/route.d.ts +1 -1
- package/dist/templates/app/api/search/route.js +5 -0
- package/dist/templates/app/robots.d.ts +1 -1
- package/dist/templates/app/robots.js +1 -26
- package/dist/templates/app/theme.d.ts +1 -1
- package/dist/templates/app/theme.js +31 -19
- package/dist/templates/components/Chat.d.ts +1 -1
- package/dist/templates/components/Chat.js +342 -34
- package/dist/templates/components/SearchDocs.d.ts +1 -1
- package/dist/templates/components/SearchDocs.js +63 -11
- package/dist/templates/components/SearchModalContent.d.ts +1 -1
- package/dist/templates/components/SearchModalContent.js +191 -49
- package/dist/templates/components/SideBar.d.ts +1 -1
- package/dist/templates/components/SideBar.js +8 -2
- package/dist/templates/components/layout/Accordion.d.ts +1 -1
- package/dist/templates/components/layout/Accordion.js +26 -3
- package/dist/templates/components/layout/Badge.d.ts +1 -1
- package/dist/templates/components/layout/Badge.js +1 -1
- package/dist/templates/components/layout/Button.d.ts +1 -1
- package/dist/templates/components/layout/Button.js +4 -4
- package/dist/templates/components/layout/Callout.d.ts +1 -1
- package/dist/templates/components/layout/Callout.js +6 -6
- package/dist/templates/components/layout/Card.d.ts +1 -1
- package/dist/templates/components/layout/Card.js +9 -4
- package/dist/templates/components/layout/CherryThemeProvider.d.ts +1 -1
- package/dist/templates/components/layout/CherryThemeProvider.js +63 -12
- package/dist/templates/components/layout/Code.d.ts +1 -1
- package/dist/templates/components/layout/Code.js +5 -5
- package/dist/templates/components/layout/CopyButton.d.ts +1 -1
- package/dist/templates/components/layout/CopyButton.js +1 -1
- package/dist/templates/components/layout/DemoTheme.d.ts +1 -1
- package/dist/templates/components/layout/DemoTheme.js +8 -28
- package/dist/templates/components/layout/DocsComponents.d.ts +1 -1
- package/dist/templates/components/layout/DocsComponents.js +57 -6
- package/dist/templates/components/layout/GlobalStyles.d.ts +1 -1
- package/dist/templates/components/layout/GlobalStyles.js +15 -9
- package/dist/templates/components/layout/Prompt.d.ts +1 -1
- package/dist/templates/components/layout/Prompt.js +4 -7
- package/dist/templates/components/layout/Steps.d.ts +1 -1
- package/dist/templates/components/layout/Steps.js +7 -1
- package/dist/templates/components/layout/Tabs.d.ts +1 -1
- package/dist/templates/components/layout/Tabs.js +108 -19
- package/dist/templates/env.example.d.ts +1 -1
- package/dist/templates/env.example.js +11 -0
- package/dist/templates/eslint.config.d.ts +1 -1
- package/dist/templates/eslint.config.js +20 -0
- package/dist/templates/gitignore.d.ts +1 -1
- package/dist/templates/gitignore.js +4 -3
- package/dist/templates/lib/access.d.ts +1 -0
- package/dist/templates/lib/access.js +41 -0
- package/dist/templates/mdx/ai-assistant.mdx.d.ts +1 -1
- package/dist/templates/mdx/ai-assistant.mdx.js +17 -1
- package/dist/templates/mdx/api-playground.mdx.d.ts +1 -1
- package/dist/templates/mdx/api-playground.mdx.js +5 -3
- package/dist/templates/mdx/authentication.mdx.d.ts +1 -1
- package/dist/templates/mdx/authentication.mdx.js +5 -3
- package/dist/templates/mdx/commands.mdx.d.ts +1 -1
- package/dist/templates/mdx/commands.mdx.js +20 -6
- package/dist/templates/mdx/components.mdx.d.ts +1 -1
- package/dist/templates/mdx/components.mdx.js +3 -0
- package/dist/templates/mdx/deployment-and-hosting.mdx.d.ts +1 -1
- package/dist/templates/mdx/deployment-and-hosting.mdx.js +2 -2
- package/dist/templates/mdx/globals.mdx.d.ts +1 -1
- package/dist/templates/mdx/globals.mdx.js +14 -11
- package/dist/templates/mdx/model-context-protocol.mdx.d.ts +1 -1
- package/dist/templates/mdx/model-context-protocol.mdx.js +22 -35
- package/dist/templates/mdx/platform/ai-assistant.mdx.d.ts +1 -1
- package/dist/templates/mdx/platform/ai-assistant.mdx.js +23 -0
- package/dist/templates/mdx/platform/api-playground.mdx.d.ts +1 -1
- package/dist/templates/mdx/platform/api-playground.mdx.js +7 -0
- package/dist/templates/mdx/platform/authentication.mdx.d.ts +1 -0
- package/dist/templates/mdx/platform/authentication.mdx.js +63 -0
- package/dist/templates/mdx/platform/import-from-github.mdx.d.ts +1 -1
- package/dist/templates/mdx/platform/import-from-github.mdx.js +2 -2
- package/dist/templates/mdx/platform/index.mdx.d.ts +1 -1
- package/dist/templates/mdx/platform/index.mdx.js +1 -0
- package/dist/templates/mdx/platform/mcp.mdx.d.ts +1 -1
- package/dist/templates/mdx/platform/mcp.mdx.js +2 -1
- package/dist/templates/mdx/platform/navigation-settings.mdx.d.ts +1 -1
- package/dist/templates/mdx/platform/navigation-settings.mdx.js +12 -1
- package/dist/templates/mdx/platform/publishing.mdx.d.ts +1 -1
- package/dist/templates/mdx/platform/publishing.mdx.js +43 -10
- package/dist/templates/mdx/platform/self-updating-docs.mdx.d.ts +1 -1
- package/dist/templates/mdx/platform/self-updating-docs.mdx.js +48 -9
- package/dist/templates/mdx/sections.mdx.d.ts +1 -1
- package/dist/templates/mdx/sections.mdx.js +4 -0
- package/dist/templates/mdx/space.mdx.d.ts +1 -0
- package/dist/templates/mdx/space.mdx.js +125 -0
- package/dist/templates/mdx/tabs.mdx.d.ts +1 -1
- package/dist/templates/mdx/tabs.mdx.js +4 -0
- package/dist/templates/next.config.js +20 -8
- package/dist/templates/package.js +3 -3
- package/dist/templates/prettierignore.d.ts +1 -1
- package/dist/templates/prettierignore.js +1 -0
- package/dist/templates/proxy.js +47 -19
- package/dist/templates/scripts/build-docs-index.d.ts +1 -1
- package/dist/templates/scripts/build-docs-index.js +1 -0
- package/dist/templates/services/mcp/docsContentStub.d.ts +1 -0
- package/dist/templates/services/mcp/docsContentStub.js +1 -0
- package/dist/templates/services/mcp/server.d.ts +1 -1
- package/dist/templates/services/mcp/server.js +140 -83
- package/dist/templates/services/mcp/tools.d.ts +1 -1
- package/dist/templates/services/mcp/tools.js +65 -137
- package/dist/templates/utils/playgroundAllowlist.d.ts +1 -1
- package/dist/templates/utils/playgroundAllowlist.js +13 -4
- package/dist/templates/utils/rateLimit.d.ts +1 -1
- package/dist/templates/utils/rateLimit.js +40 -3
- package/dist/templates/utils/requestBody.d.ts +1 -0
- package/dist/templates/utils/requestBody.js +56 -0
- package/dist/templates/utils/ssrfGuard.d.ts +1 -1
- package/dist/templates/utils/ssrfGuard.js +138 -33
- package/package.json +13 -5
- package/dist/lib/mdxToMarkdown.d.ts +0 -1
- package/dist/lib/mdxToMarkdown.js +0 -134
- package/dist/templates/mdx/image-and-embeds.mdx.d.ts +0 -1
- package/dist/templates/mdx/image-and-embeds.mdx.js +0 -118
|
@@ -97,6 +97,7 @@ function SearchProvider({
|
|
|
97
97
|
const [isClosing, setIsClosing] = useState(false);
|
|
98
98
|
const [query, setQuery] = useState("");
|
|
99
99
|
const [activeIndex, setActiveIndex] = useState(0);
|
|
100
|
+
const [returnFocusTo, setReturnFocusTo] = useState<HTMLElement | null>(null);
|
|
100
101
|
// Latest completed content search, keyed by the query that produced it.
|
|
101
102
|
// contentResults and isSearching are derived from it at render time, so
|
|
102
103
|
// the debounced-search effect never sets state synchronously (which would
|
|
@@ -107,10 +108,14 @@ function SearchProvider({
|
|
|
107
108
|
} | null>(null);
|
|
108
109
|
const resultsRef = useRef<HTMLUListElement>(null);
|
|
109
110
|
const closingRef = useRef(false);
|
|
111
|
+
const isVisibleRef = useRef(false);
|
|
112
|
+
const searchOpenerRef = useRef<HTMLElement | null>(null);
|
|
113
|
+
const restoreSearchFocusRef = useRef(true);
|
|
110
114
|
const abortRef = useRef<AbortController | null>(null);
|
|
111
115
|
const debounceRef = useRef<ReturnType<typeof setTimeout>>(null);
|
|
112
116
|
const router = useRouter();
|
|
113
|
-
const { isChatActive, askAssistant } =
|
|
117
|
+
const { isChatActive, askAssistant, closeChat, registerSearchClose } =
|
|
118
|
+
useContext(ChatContext);
|
|
114
119
|
|
|
115
120
|
const sectionLabels = useMemo(() => {
|
|
116
121
|
const map: Record<string, string> = {};
|
|
@@ -121,10 +126,23 @@ function SearchProvider({
|
|
|
121
126
|
}, [sections]);
|
|
122
127
|
|
|
123
128
|
const openSearch = useCallback(() => {
|
|
129
|
+
const previousOverlayOpener = closeChat(false);
|
|
124
130
|
closingRef.current = false;
|
|
131
|
+
restoreSearchFocusRef.current = true;
|
|
132
|
+
const activeElement = document.activeElement;
|
|
133
|
+
const opener =
|
|
134
|
+
previousOverlayOpener?.isConnected === true
|
|
135
|
+
? previousOverlayOpener
|
|
136
|
+
: activeElement instanceof HTMLElement &&
|
|
137
|
+
activeElement !== document.body
|
|
138
|
+
? activeElement
|
|
139
|
+
: null;
|
|
140
|
+
searchOpenerRef.current = opener;
|
|
141
|
+
setReturnFocusTo(opener);
|
|
142
|
+
isVisibleRef.current = true;
|
|
125
143
|
setIsClosing(false);
|
|
126
144
|
setIsVisible(true);
|
|
127
|
-
}, []);
|
|
145
|
+
}, [closeChat]);
|
|
128
146
|
|
|
129
147
|
const closeSearch = useCallback(() => {
|
|
130
148
|
closingRef.current = true;
|
|
@@ -133,16 +151,54 @@ function SearchProvider({
|
|
|
133
151
|
if (debounceRef.current) clearTimeout(debounceRef.current);
|
|
134
152
|
}, []);
|
|
135
153
|
|
|
154
|
+
const shouldRestoreSearchFocus = useCallback(
|
|
155
|
+
() => restoreSearchFocusRef.current,
|
|
156
|
+
[],
|
|
157
|
+
);
|
|
158
|
+
|
|
136
159
|
const handleCloseAnimationEnd = useCallback(() => {
|
|
137
160
|
if (!closingRef.current) return;
|
|
138
161
|
closingRef.current = false;
|
|
162
|
+
isVisibleRef.current = false;
|
|
139
163
|
setIsVisible(false);
|
|
140
164
|
setIsClosing(false);
|
|
141
165
|
setQuery("");
|
|
142
166
|
setActiveIndex(0);
|
|
143
167
|
setFetched(null);
|
|
168
|
+
searchOpenerRef.current = null;
|
|
169
|
+
setReturnFocusTo(null);
|
|
144
170
|
}, []);
|
|
145
171
|
|
|
172
|
+
const closeSearchImmediately = useCallback((restoreFocus = true) => {
|
|
173
|
+
if (!isVisibleRef.current) return null;
|
|
174
|
+
restoreSearchFocusRef.current = restoreFocus;
|
|
175
|
+
const opener = searchOpenerRef.current;
|
|
176
|
+
searchOpenerRef.current = null;
|
|
177
|
+
setReturnFocusTo(null);
|
|
178
|
+
closingRef.current = false;
|
|
179
|
+
isVisibleRef.current = false;
|
|
180
|
+
if (abortRef.current) abortRef.current.abort();
|
|
181
|
+
if (debounceRef.current) clearTimeout(debounceRef.current);
|
|
182
|
+
const activeElement = document.activeElement;
|
|
183
|
+
if (
|
|
184
|
+
activeElement instanceof HTMLElement &&
|
|
185
|
+
activeElement.closest("[data-search-dialog]")
|
|
186
|
+
) {
|
|
187
|
+
activeElement.blur();
|
|
188
|
+
}
|
|
189
|
+
setIsVisible(false);
|
|
190
|
+
setIsClosing(false);
|
|
191
|
+
setQuery("");
|
|
192
|
+
setActiveIndex(0);
|
|
193
|
+
setFetched(null);
|
|
194
|
+
return opener;
|
|
195
|
+
}, []);
|
|
196
|
+
|
|
197
|
+
useEffect(
|
|
198
|
+
() => registerSearchClose(closeSearchImmediately),
|
|
199
|
+
[closeSearchImmediately, registerSearchClose],
|
|
200
|
+
);
|
|
201
|
+
|
|
146
202
|
const trimmedQuery = query.trim();
|
|
147
203
|
const contentResults =
|
|
148
204
|
trimmedQuery.length >= 2 && fetched?.q === trimmedQuery
|
|
@@ -263,17 +319,11 @@ function SearchProvider({
|
|
|
263
319
|
const askAssistantWithQuery = useCallback(() => {
|
|
264
320
|
const q = query.trim();
|
|
265
321
|
if (!q) return;
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
}, [query, askAssistant,
|
|
322
|
+
const opener = closeSearchImmediately(false);
|
|
323
|
+
askAssistant(q, opener);
|
|
324
|
+
}, [query, askAssistant, closeSearchImmediately]);
|
|
269
325
|
|
|
270
326
|
// Global Cmd+K / Ctrl+K listener
|
|
271
|
-
const isVisibleRef = useRef(false);
|
|
272
|
-
|
|
273
|
-
useEffect(() => {
|
|
274
|
-
isVisibleRef.current = isVisible;
|
|
275
|
-
}, [isVisible]);
|
|
276
|
-
|
|
277
327
|
useEffect(() => {
|
|
278
328
|
function handleKeyDown(e: KeyboardEvent) {
|
|
279
329
|
if ((e.metaKey || e.ctrlKey) && e.key === "k") {
|
|
@@ -335,6 +385,8 @@ function SearchProvider({
|
|
|
335
385
|
navigate={navigate}
|
|
336
386
|
canAskAssistant={isChatActive}
|
|
337
387
|
onAskAssistant={askAssistantWithQuery}
|
|
388
|
+
shouldRestoreFocus={shouldRestoreSearchFocus}
|
|
389
|
+
returnFocusTo={returnFocusTo}
|
|
338
390
|
/>
|
|
339
391
|
)}
|
|
340
392
|
</SearchContext.Provider>
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare const searchModalContentTemplate = "\"use client\";\nimport React from \"react\";\nimport styled, { css, keyframes } from \"styled-components\";\nimport { Search, X } from \"lucide-react\";\nimport { IconButton } from \"cherry-styled-components\";\nimport { mq, Theme } from \"@/app/theme\";\nimport { Spinner } from \"@/components/Spinner\";\nimport {\n interactiveStyles,\n thinScrollbar,\n} from \"@/components/layout/SharedStyled\";\n\nexport interface PageItem {\n slug: string;\n title: string;\n description?: string;\n category: string;\n section?: string;\n}\n\nexport interface MergedResult {\n page: PageItem;\n snippet?: string;\n}\n\nexport interface SearchModalContentProps {\n isClosing: boolean;\n closeSearch: () => void;\n onCloseAnimationEnd: () => void;\n query: string;\n setQuery: (q: string) => void;\n activeIndex: number;\n setActiveIndex: (i: number | ((prev: number) => number)) => void;\n resultsRef: React.RefObject<HTMLUListElement | null>;\n onKeyDown: (e: React.KeyboardEvent) => void;\n merged: MergedResult[];\n sectionLabels: Record<string, string>;\n isSearching: boolean;\n navigate: (\n slug: string,\n modifiers?: { metaKey?: boolean; ctrlKey?: boolean; shiftKey?: boolean },\n ) => void;\n canAskAssistant: boolean;\n onAskAssistant: () => void;\n}\n\nconst ANIMATION_MS = 300;\n\nconst backdropIn = keyframes`\n from { opacity: 0; }\n to { opacity: 1; }\n`;\n\nconst backdropOut = keyframes`\n from { opacity: 1; }\n to { opacity: 0; }\n`;\n\nconst modalIn = keyframes`\n from { opacity: 0; transform: scale(0.96) translateY(-8px); }\n to { opacity: 1; transform: scale(1) translateY(0); }\n`;\n\nconst modalOut = keyframes`\n from { opacity: 1; transform: scale(1) translateY(0); }\n to { opacity: 0; transform: scale(0.96) translateY(-8px); }\n`;\n\nconst StyledBackdrop = styled.div<{ theme: Theme; $isClosing: boolean }>`\n position: fixed;\n inset: 0;\n z-index: 9999;\n /* Backdrop is intentionally near-black in both modes \u2014 covers everything\n behind the modal regardless of the user's theme. */\n background: rgba(0, 0, 0, 0.5);\n backdrop-filter: blur(4px);\n -webkit-backdrop-filter: blur(4px);\n display: flex;\n align-items: flex-start;\n justify-content: center;\n padding: 20px;\n animation: ${({ $isClosing }) => ($isClosing ? backdropOut : backdropIn)}\n ${ANIMATION_MS}ms ease forwards;\n\n ${mq(\"lg\")} {\n padding: 120px 20px 20px 20px;\n }\n`;\n\nconst StyledModal = styled.div<{ theme: Theme; $isClosing: boolean }>`\n background: ${({ theme }) => theme.colors.light};\n border-radius: ${({ theme }) => theme.spacing.radius.lg};\n box-shadow: ${({ theme }) => theme.shadows.xs};\n width: 100%;\n max-width: 560px;\n max-height: calc(100dvh - 40px);\n display: flex;\n flex-direction: column;\n border: solid 1px ${({ theme }) => theme.colors.grayLight};\n padding-bottom: 8px;\n animation: ${({ $isClosing }) => ($isClosing ? modalOut : modalIn)}\n ${ANIMATION_MS}ms ease forwards;\n\n ${mq(\"lg\")} {\n max-height: calc(100dvh - 240px);\n }\n`;\n\nconst StyledInputWrapper = styled.div<{ theme: Theme }>`\n display: flex;\n align-items: center;\n gap: 12px;\n padding: 16px;\n flex-shrink: 0;\n border-bottom: solid 1px ${({ theme }) => theme.colors.grayLight};\n\n & svg.lucide {\n color: ${({ theme }) => theme.colors.gray};\n flex-shrink: 0;\n }\n`;\n\nconst StyledInput = styled.input<{ theme: Theme }>`\n flex: 1;\n border: none;\n outline: none;\n background: transparent;\n font-size: ${({ theme }) => theme.fontSizes.text.lg};\n line-height: ${({ theme }) => theme.lineHeights.text.lg};\n color: ${({ theme }) => theme.colors.dark};\n font-family: inherit;\n\n &::placeholder {\n color: ${({ theme }) => theme.colors.gray};\n }\n`;\n\n// \"Ask AI\" affordance shown before the close button when the AI chat is\n// enabled. Desktop-only (like the header's Cmd+K hint) since it advertises the\n// Option+Enter shortcut; mobile users reach the assistant from the header CTA.\nconst StyledAskAssistant = styled.button<{ theme: Theme }>`\n ${interactiveStyles};\n display: none;\n align-items: center;\n gap: 6px;\n flex-shrink: 0;\n border: solid 1px ${({ theme }) => theme.colors.grayLight};\n background: ${({ theme }) => theme.colors.light};\n color: ${({ theme }) => theme.colors.grayDark};\n border-radius: ${({ theme }) => theme.spacing.radius.xs};\n padding: 5px 8px;\n font-family: inherit;\n font-size: ${({ theme }) => theme.fontSizes.small.lg};\n font-weight: 500;\n line-height: 1;\n white-space: nowrap;\n cursor: pointer;\n\n &:disabled {\n opacity: 0.5;\n cursor: default;\n\n &:hover {\n border-color: ${({ theme }) => theme.colors.grayLight};\n }\n }\n\n ${mq(\"lg\")} {\n display: inline-flex;\n }\n`;\n\nconst StyledAskKeys = styled.span`\n display: inline-flex;\n align-items: center;\n gap: 3px;\n`;\n\nconst StyledAskKbd = styled.kbd<{ theme: Theme }>`\n font-family: inherit;\n font-size: 12px;\n font-weight: 600;\n line-height: 1;\n min-width: 18px;\n height: 18px;\n padding: 0 4px;\n display: inline-flex;\n align-items: center;\n justify-content: center;\n background: ${({ theme }) => theme.colors.grayLight};\n color: ${({ theme }) => theme.colors.grayDark};\n border-radius: 4px;\n`;\n\n// The return glyph's ink sits high in its em box, so it reads top-aligned even\n// when the keycap flex-centers it. Nudge just the character (not the pill) down\n// a pixel so it optically centers and lines up with the option glyph.\nconst StyledReturnGlyph = styled.span`\n display: inline-block;\n transform: translateY(1.5px);\n`;\n\nconst StyledResults = styled.ul<{ theme: Theme }>`\n list-style: none;\n position: relative;\n margin: 8px 0 0 0;\n padding: 0 8px;\n overflow-y: auto;\n flex: 1;\n min-height: 0;\n -webkit-overflow-scrolling: touch;\n ${thinScrollbar};\n\n /* The results list is a keyboard-focusable scroll container; frame it with\n the app focus ring (inset so it hugs the visible viewport and isn't clipped\n by the scroll overflow) instead of the browser's default outline. */\n &:focus-visible {\n outline: none;\n border-radius: ${({ theme }) => theme.spacing.radius.xs};\n box-shadow: inset 0 0 0 2px ${({ theme }) => theme.colors.primaryLight};\n }\n`;\n\n// Single indicator that slides between result items instead of each item\n// fading its own background in and out. Positioned/sized inline from the active\n// item's measured offset (see the layout effect below); the transition on\n// transform + height produces the up/down glide as the active item changes.\nconst StyledHighlight = styled.div<{ theme: Theme }>`\n position: absolute;\n left: 8px;\n right: 8px;\n top: 0;\n z-index: 0;\n pointer-events: none;\n border-radius: ${({ theme }) => theme.spacing.radius.xs};\n background: ${({ theme }) =>\n `color-mix(in srgb, ${theme.colors.primaryLight} 20%, transparent)`};\n transition:\n transform 0.2s cubic-bezier(0.22, 1, 0.36, 1),\n height 0.2s cubic-bezier(0.22, 1, 0.36, 1);\n will-change: transform, height;\n\n @media (prefers-reduced-motion: reduce) {\n transition: none;\n }\n`;\n\nconst StyledResultItem = styled.li<{ theme: Theme; $isActive: boolean }>`\n position: relative;\n z-index: 1;\n padding: 10px 12px;\n border-radius: ${({ theme }) => theme.spacing.radius.xs};\n cursor: pointer;\n\n ${({ $isActive, theme }) =>\n $isActive &&\n css`\n & > span:first-of-type {\n color: ${theme.colors.primary};\n }\n `}\n`;\n\nconst StyledResultTitle = styled.span<{ theme: Theme }>`\n font-size: ${({ theme }) => theme.fontSizes.text.lg};\n font-weight: 500;\n color: ${({ theme }) => theme.colors.dark};\n display: block;\n transition: color 0.15s ease;\n`;\n\nconst StyledResultMeta = styled.span<{ theme: Theme }>`\n font-size: ${({ theme }) => theme.fontSizes.small.lg};\n color: ${({ theme }) => theme.colors.gray};\n display: block;\n margin-top: 2px;\n`;\n\nconst StyledSnippet = styled.span<{ theme: Theme }>`\n font-size: ${({ theme }) => theme.fontSizes.small.lg};\n color: ${({ theme }) => theme.colors.grayDark};\n display: block;\n margin-top: 4px;\n line-height: 1.4;\n overflow: hidden;\n text-overflow: ellipsis;\n white-space: nowrap;\n\n & mark {\n background: ${({ theme }) =>\n `color-mix(in srgb, ${theme.colors.primaryLight} 35%, transparent)`};\n color: inherit;\n border-radius: 4px;\n padding: 0 1px;\n }\n`;\n\nconst StyledEmpty = styled.div<{ theme: Theme }>`\n padding: 20px 20px 12px;\n min-height: 40px;\n display: flex;\n align-items: center;\n justify-content: center;\n text-align: center;\n font-size: ${({ theme }) => theme.fontSizes.small.lg};\n color: ${({ theme }) => theme.colors.gray};\n`;\n\nfunction escapeHtml(str: string): string {\n return str\n .replace(/&/g, \"&\")\n .replace(/</g, \"<\")\n .replace(/>/g, \">\")\n .replace(/\"/g, \""\");\n}\n\nfunction highlightMatch(snippet: string, query: string): string {\n const escaped = escapeHtml(snippet);\n if (!query.trim()) return escaped;\n const q = escapeHtml(query.trim());\n const regex = new RegExp(\n `(${q.replace(/[.*+?^${}()|[\\]\\\\]/g, \"\\\\$&\")})`,\n \"gi\",\n );\n return escaped.replace(regex, \"<mark>$1</mark>\");\n}\n\nexport function SearchModalContent({\n isClosing,\n closeSearch,\n onCloseAnimationEnd,\n query,\n setQuery,\n activeIndex,\n setActiveIndex,\n resultsRef,\n onKeyDown,\n merged,\n sectionLabels,\n isSearching,\n navigate,\n canAskAssistant,\n onAskAssistant,\n}: SearchModalContentProps) {\n // Measure the active item so the sliding highlight can be placed over it.\n // useLayoutEffect runs before paint, so the first position is committed\n // without a transition (no slide-in on open); later activeIndex/hover\n // changes glide because only the transform + height differ.\n const [indicator, setIndicator] = React.useState<{\n top: number;\n height: number;\n } | null>(null);\n\n React.useLayoutEffect(() => {\n const list = resultsRef.current;\n if (!list) {\n setIndicator(null);\n return;\n }\n const items = list.querySelectorAll(\"li\");\n const active = items[activeIndex] as HTMLElement | undefined;\n if (!active) {\n setIndicator(null);\n return;\n }\n setIndicator({ top: active.offsetTop, height: active.offsetHeight });\n\n // Keep the active row inside the scroll viewport in the SAME pre-paint pass\n // that positions the highlight, so the two never desync into a one-frame\n // flicker. merged is a dependency, so re-filtering (a new query) also\n // re-scrolls the reset active row into view even when its index is\n // unchanged. This list is flat (no group headers), so no header inclusion\n // is needed; a small margin keeps rows off the container's hard edges.\n // Adjust scrollTop directly (never scrollIntoView, which can also pan\n // ancestors).\n const margin = 8;\n const viewTop = active.offsetTop - margin;\n const viewBottom = active.offsetTop + active.offsetHeight + margin;\n\n if (viewTop < list.scrollTop) {\n list.scrollTop = viewTop;\n } else if (viewBottom > list.scrollTop + list.clientHeight) {\n list.scrollTop = viewBottom - list.clientHeight;\n }\n }, [activeIndex, merged, resultsRef]);\n\n return (\n <StyledBackdrop\n $isClosing={isClosing}\n onClick={closeSearch}\n onAnimationEnd={(e) => {\n // animationend bubbles, so this handler also fires for animations\n // completing on descendants (the inner modal, icons, result items).\n // Only the backdrop's own exit animation should trigger the unmount -\n // otherwise a child finishing its animation first tears the modal down\n // before the close animation plays, making it vanish instantly.\n if (e.target === e.currentTarget) onCloseAnimationEnd();\n }}\n >\n <StyledModal $isClosing={isClosing} onClick={(e) => e.stopPropagation()}>\n <StyledInputWrapper>\n <Search size={18} />\n <StyledInput\n autoFocus\n value={query}\n onChange={(e) => {\n setQuery(e.target.value);\n setActiveIndex(0);\n }}\n onKeyDown={onKeyDown}\n placeholder=\"Search docs...\"\n autoComplete=\"off\"\n spellCheck={false}\n />\n {canAskAssistant && (\n <StyledAskAssistant\n type=\"button\"\n onClick={onAskAssistant}\n disabled={!query.trim()}\n aria-label=\"Ask AI\"\n title=\"Ask AI\"\n >\n Ask AI\n <StyledAskKeys aria-hidden=\"true\">\n <StyledAskKbd>⌥</StyledAskKbd>\n <StyledAskKbd>\n <StyledReturnGlyph>↵</StyledReturnGlyph>\n </StyledAskKbd>\n </StyledAskKeys>\n </StyledAskAssistant>\n )}\n <IconButton\n onClick={closeSearch}\n aria-label=\"Close search\"\n title=\"Close search\"\n >\n <X />\n </IconButton>\n </StyledInputWrapper>\n {merged.length > 0 ? (\n <StyledResults ref={resultsRef}>\n {indicator && (\n <StyledHighlight\n aria-hidden=\"true\"\n style={{\n transform: `translateY(${indicator.top}px)`,\n height: `${indicator.height}px`,\n }}\n />\n )}\n {merged.map((result, index) => (\n <StyledResultItem\n key={result.page.slug + result.page.section}\n $isActive={index === activeIndex}\n onClick={(e) => navigate(result.page.slug, e)}\n onMouseEnter={() => setActiveIndex(index)}\n >\n <StyledResultTitle>{result.page.title}</StyledResultTitle>\n <StyledResultMeta>\n {result.page.section\n ? `${sectionLabels[result.page.section] || result.page.section} / `\n : \"\"}\n {result.page.category}\n </StyledResultMeta>\n {result.snippet && (\n <StyledSnippet\n dangerouslySetInnerHTML={{\n __html: highlightMatch(result.snippet, query),\n }}\n />\n )}\n </StyledResultItem>\n ))}\n </StyledResults>\n ) : (\n <StyledEmpty>\n {isSearching ? <Spinner size={18} /> : \"No results found\"}\n </StyledEmpty>\n )}\n </StyledModal>\n </StyledBackdrop>\n );\n}\n";
|
|
1
|
+
export declare const searchModalContentTemplate = "\"use client\";\nimport React from \"react\";\nimport styled, { css, keyframes } from \"styled-components\";\nimport { Search, X } from \"lucide-react\";\nimport { IconButton } from \"cherry-styled-components\";\nimport { mq, Theme } from \"@/app/theme\";\nimport { Spinner } from \"@/components/Spinner\";\nimport {\n interactiveStyles,\n thinScrollbar,\n} from \"@/components/layout/SharedStyled\";\n\nexport interface PageItem {\n slug: string;\n title: string;\n description?: string;\n category: string;\n section?: string;\n}\n\nexport interface MergedResult {\n page: PageItem;\n snippet?: string;\n}\n\nexport interface SearchModalContentProps {\n isClosing: boolean;\n closeSearch: () => void;\n onCloseAnimationEnd: () => void;\n query: string;\n setQuery: (q: string) => void;\n activeIndex: number;\n setActiveIndex: (i: number | ((prev: number) => number)) => void;\n resultsRef: React.RefObject<HTMLUListElement | null>;\n onKeyDown: (e: React.KeyboardEvent) => void;\n merged: MergedResult[];\n sectionLabels: Record<string, string>;\n isSearching: boolean;\n navigate: (\n slug: string,\n modifiers?: { metaKey?: boolean; ctrlKey?: boolean; shiftKey?: boolean },\n ) => void;\n canAskAssistant: boolean;\n onAskAssistant: () => void;\n shouldRestoreFocus: () => boolean;\n returnFocusTo: HTMLElement | null;\n}\n\nconst ANIMATION_MS = 300;\n\nconst backdropIn = keyframes`\n from { opacity: 0; }\n to { opacity: 1; }\n`;\n\nconst backdropOut = keyframes`\n from { opacity: 1; }\n to { opacity: 0; }\n`;\n\nconst modalIn = keyframes`\n from { opacity: 0; transform: scale(0.96) translateY(-8px); }\n to { opacity: 1; transform: scale(1) translateY(0); }\n`;\n\nconst modalOut = keyframes`\n from { opacity: 1; transform: scale(1) translateY(0); }\n to { opacity: 0; transform: scale(0.96) translateY(-8px); }\n`;\n\nconst StyledBackdrop = styled.div<{ theme: Theme; $isClosing: boolean }>`\n position: fixed;\n inset: 0;\n z-index: 9999;\n /* Backdrop is intentionally near-black in both modes \u2014 covers everything\n behind the modal regardless of the user's theme. */\n background: rgba(0, 0, 0, 0.5);\n backdrop-filter: blur(4px);\n -webkit-backdrop-filter: blur(4px);\n display: flex;\n align-items: flex-start;\n justify-content: center;\n padding: 20px;\n animation: ${({ $isClosing }) => ($isClosing ? backdropOut : backdropIn)}\n ${ANIMATION_MS}ms ease forwards;\n\n ${mq(\"lg\")} {\n padding: 120px 20px 20px 20px;\n }\n`;\n\nconst StyledModal = styled.div<{ theme: Theme; $isClosing: boolean }>`\n background: ${({ theme }) => theme.colors.light};\n border-radius: ${({ theme }) => theme.spacing.radius.lg};\n box-shadow: ${({ theme }) => theme.shadows.xs};\n width: 100%;\n max-width: 560px;\n max-height: calc(100dvh - 40px);\n display: flex;\n flex-direction: column;\n border: solid 1px ${({ theme }) => theme.colors.grayLight};\n padding-bottom: 8px;\n animation: ${({ $isClosing }) => ($isClosing ? modalOut : modalIn)}\n ${ANIMATION_MS}ms ease forwards;\n\n ${mq(\"lg\")} {\n max-height: calc(100dvh - 240px);\n }\n`;\n\nconst StyledInputWrapper = styled.div<{ theme: Theme }>`\n display: flex;\n align-items: center;\n gap: 12px;\n padding: 16px;\n flex-shrink: 0;\n border-bottom: solid 1px ${({ theme }) => theme.colors.grayLight};\n\n & svg.lucide {\n color: ${({ theme }) => theme.colors.gray};\n flex-shrink: 0;\n }\n`;\n\nconst StyledInput = styled.input<{ theme: Theme }>`\n flex: 1;\n border: none;\n outline: none;\n background: transparent;\n font-size: ${({ theme }) => theme.fontSizes.text.lg};\n line-height: ${({ theme }) => theme.lineHeights.text.lg};\n color: ${({ theme }) => theme.colors.dark};\n font-family: inherit;\n\n &::placeholder {\n color: ${({ theme }) => theme.colors.gray};\n }\n\n /* type=\"search\" gets the platform clear button, which duplicates the modal's\n own close control and sits at a size and color we do not control. */\n &::-webkit-search-cancel-button,\n &::-webkit-search-decoration {\n -webkit-appearance: none;\n appearance: none;\n }\n`;\n\n// \"Ask AI\" affordance shown before the close button when the AI chat is\n// enabled. Desktop-only (like the header's Cmd+K hint) since it advertises the\n// Option+Enter shortcut; mobile users reach the assistant from the header CTA.\nconst StyledAskAssistant = styled.button<{ theme: Theme }>`\n ${interactiveStyles};\n display: none;\n align-items: center;\n gap: 6px;\n flex-shrink: 0;\n border: solid 1px ${({ theme }) => theme.colors.grayLight};\n background: ${({ theme }) => theme.colors.light};\n color: ${({ theme }) => theme.colors.grayDark};\n border-radius: ${({ theme }) => theme.spacing.radius.xs};\n padding: 5px 8px;\n font-family: inherit;\n font-size: ${({ theme }) => theme.fontSizes.small.lg};\n font-weight: 500;\n line-height: 1;\n white-space: nowrap;\n cursor: pointer;\n\n &:disabled {\n opacity: 0.5;\n cursor: default;\n\n &:hover {\n border-color: ${({ theme }) => theme.colors.grayLight};\n }\n }\n\n ${mq(\"lg\")} {\n display: inline-flex;\n }\n`;\n\nconst StyledAskKeys = styled.span`\n display: inline-flex;\n align-items: center;\n gap: 3px;\n`;\n\nconst StyledAskKbd = styled.kbd<{ theme: Theme }>`\n font-family: inherit;\n font-size: 12px;\n font-weight: 600;\n line-height: 1;\n min-width: 18px;\n height: 18px;\n padding: 0 4px;\n display: inline-flex;\n align-items: center;\n justify-content: center;\n background: ${({ theme }) => theme.colors.grayLight};\n color: ${({ theme }) => theme.colors.grayDark};\n border-radius: 4px;\n`;\n\n// The return glyph's ink sits high in its em box, so it reads top-aligned even\n// when the keycap flex-centers it. Nudge just the character (not the pill) down\n// a pixel so it optically centers and lines up with the option glyph.\nconst StyledReturnGlyph = styled.span`\n display: inline-block;\n transform: translateY(1.5px);\n`;\n\nconst StyledResults = styled.ul<{ theme: Theme }>`\n list-style: none;\n position: relative;\n margin: 8px 0 0 0;\n padding: 0 8px;\n overflow-y: auto;\n flex: 1;\n min-height: 0;\n -webkit-overflow-scrolling: touch;\n ${thinScrollbar};\n\n &[hidden] {\n display: none;\n }\n`;\n\n// Single indicator that slides between result items instead of each item\n// fading its own background in and out. Positioned/sized from the active item's\n// measured offset (see the layout effect below); the transition on\n// transform + height produces the up/down glide as the active item changes.\nconst StyledHighlight = styled.div<{\n theme: Theme;\n $top: number;\n $height: number;\n}>`\n position: absolute;\n left: 8px;\n right: 8px;\n top: 0;\n z-index: 0;\n pointer-events: none;\n border-radius: ${({ theme }) => theme.spacing.radius.xs};\n background: ${({ theme }) =>\n `color-mix(in srgb, ${theme.colors.primaryLight} 20%, transparent)`};\n transition:\n transform 0.2s cubic-bezier(0.22, 1, 0.36, 1),\n height 0.2s cubic-bezier(0.22, 1, 0.36, 1);\n will-change: transform, height;\n transform: translateY(${({ $top }) => $top}px);\n height: ${({ $height }) => $height}px;\n\n @media (prefers-reduced-motion: reduce) {\n transition: none;\n }\n`;\n\nconst StyledResultItem = styled.li<{ theme: Theme; $isActive: boolean }>`\n position: relative;\n z-index: 1;\n padding: 10px 12px;\n border-radius: ${({ theme }) => theme.spacing.radius.xs};\n cursor: pointer;\n\n &:focus-visible {\n outline: none;\n box-shadow: inset 0 0 0 2px ${({ theme }) => theme.colors.primaryLight};\n }\n\n ${({ $isActive, theme }) =>\n $isActive &&\n css`\n & > span:first-of-type {\n color: ${theme.colors.primary};\n }\n `}\n`;\n\nconst StyledResultTitle = styled.span<{ theme: Theme }>`\n font-size: ${({ theme }) => theme.fontSizes.text.lg};\n font-weight: 500;\n color: ${({ theme }) => theme.colors.dark};\n display: block;\n transition: color 0.15s ease;\n`;\n\nconst StyledResultMeta = styled.span<{ theme: Theme }>`\n font-size: ${({ theme }) => theme.fontSizes.small.lg};\n color: ${({ theme }) => theme.colors.gray};\n display: block;\n margin-top: 2px;\n`;\n\nconst StyledSnippet = styled.span<{ theme: Theme }>`\n font-size: ${({ theme }) => theme.fontSizes.small.lg};\n color: ${({ theme }) => theme.colors.grayDark};\n display: block;\n margin-top: 4px;\n line-height: 1.4;\n overflow: hidden;\n text-overflow: ellipsis;\n white-space: nowrap;\n\n & mark {\n background: ${({ theme }) =>\n `color-mix(in srgb, ${theme.colors.primaryLight} 35%, transparent)`};\n color: inherit;\n border-radius: 4px;\n padding: 0 1px;\n }\n`;\n\nconst StyledEmpty = styled.div<{ theme: Theme }>`\n padding: 20px 20px 12px;\n min-height: 40px;\n display: flex;\n align-items: center;\n justify-content: center;\n /* Separates the spinner from \"Searching documentation...\"; the whitespace\n between them in the markup collapses away as an anonymous flex item. */\n gap: 8px;\n text-align: center;\n font-size: ${({ theme }) => theme.fontSizes.small.lg};\n color: ${({ theme }) => theme.colors.gray};\n`;\n\nconst StyledLiveStatus = styled.div`\n position: absolute;\n width: 1px;\n height: 1px;\n padding: 0;\n margin: -1px;\n overflow: hidden;\n clip: rect(0 0 0 0);\n white-space: nowrap;\n border: 0;\n`;\n\nfunction escapeHtml(str: string): string {\n return str\n .replace(/&/g, \"&\")\n .replace(/</g, \"<\")\n .replace(/>/g, \">\")\n .replace(/\"/g, \""\");\n}\n\nfunction highlightMatch(snippet: string, query: string): string {\n const escaped = escapeHtml(snippet);\n if (!query.trim()) return escaped;\n const q = escapeHtml(query.trim());\n const regex = new RegExp(\n `(${q.replace(/[.*+?^${}()|[\\]\\\\]/g, \"\\\\$&\")})`,\n \"gi\",\n );\n return escaped.replace(regex, \"<mark>$1</mark>\");\n}\n\nexport function SearchModalContent({\n isClosing,\n closeSearch,\n onCloseAnimationEnd,\n query,\n setQuery,\n activeIndex,\n setActiveIndex,\n resultsRef,\n onKeyDown,\n merged,\n sectionLabels,\n isSearching,\n navigate,\n canAskAssistant,\n onAskAssistant,\n shouldRestoreFocus,\n returnFocusTo,\n}: SearchModalContentProps) {\n const dialogRef = React.useRef<HTMLDivElement | null>(null);\n const inputRef = React.useRef<HTMLInputElement | null>(null);\n const previouslyFocusedRef = React.useRef<HTMLElement | null>(\n returnFocusTo?.isConnected === true\n ? returnFocusTo\n : typeof document === \"undefined\"\n ? null\n : (document.activeElement as HTMLElement | null),\n );\n const listboxId = React.useId();\n const optionId = (index: number) => listboxId + \"-option-\" + index;\n\n React.useEffect(() => {\n const previouslyFocused = previouslyFocusedRef.current;\n const containFocus = (event: FocusEvent) => {\n if (\n event.target instanceof Node &&\n !dialogRef.current?.contains(event.target)\n ) {\n inputRef.current?.focus();\n }\n };\n\n document.addEventListener(\"focusin\", containFocus);\n inputRef.current?.focus();\n\n return () => {\n document.removeEventListener(\"focusin\", containFocus);\n if (shouldRestoreFocus() && previouslyFocused?.isConnected) {\n previouslyFocused.focus();\n }\n };\n }, [shouldRestoreFocus]);\n\n const handleDialogKeyDown = (event: React.KeyboardEvent<HTMLDivElement>) => {\n if (event.key === \"Escape\") {\n event.preventDefault();\n event.stopPropagation();\n closeSearch();\n return;\n }\n\n if (event.key !== \"Tab\") return;\n\n const focusable = Array.from(\n dialogRef.current?.querySelectorAll<HTMLElement>(\n 'a[href], button:not([disabled]), input:not([disabled]), [tabindex]:not([tabindex=\"-1\"])',\n ) ?? [],\n ).filter(\n (element) =>\n element.offsetParent !== null &&\n element.getAttribute(\"aria-hidden\") !== \"true\",\n );\n\n if (focusable.length === 0) {\n event.preventDefault();\n return;\n }\n\n const first = focusable[0];\n const last = focusable[focusable.length - 1];\n if (event.shiftKey && document.activeElement === first) {\n event.preventDefault();\n last.focus();\n } else if (!event.shiftKey && document.activeElement === last) {\n event.preventDefault();\n first.focus();\n }\n };\n\n // Measure the active item so the sliding highlight can be placed over it.\n // useLayoutEffect runs before paint, so the first position is committed\n // without a transition (no slide-in on open); later activeIndex/hover\n // changes glide because only the transform + height differ.\n const [indicator, setIndicator] = React.useState<{\n top: number;\n height: number;\n } | null>(null);\n\n React.useLayoutEffect(() => {\n const list = resultsRef.current;\n if (!list) {\n setIndicator(null);\n return;\n }\n const items = list.querySelectorAll(\"li\");\n const active = items[activeIndex] as HTMLElement | undefined;\n if (!active) {\n setIndicator(null);\n return;\n }\n setIndicator({ top: active.offsetTop, height: active.offsetHeight });\n\n // Keep the active row inside the scroll viewport in the SAME pre-paint pass\n // that positions the highlight, so the two never desync into a one-frame\n // flicker. merged is a dependency, so re-filtering (a new query) also\n // re-scrolls the reset active row into view even when its index is\n // unchanged. This list is flat (no group headers), so no header inclusion\n // is needed; a small margin keeps rows off the container's hard edges.\n // Adjust scrollTop directly (never scrollIntoView, which can also pan\n // ancestors).\n const margin = 8;\n const viewTop = active.offsetTop - margin;\n const viewBottom = active.offsetTop + active.offsetHeight + margin;\n\n if (viewTop < list.scrollTop) {\n list.scrollTop = viewTop;\n } else if (viewBottom > list.scrollTop + list.clientHeight) {\n list.scrollTop = viewBottom - list.clientHeight;\n }\n }, [activeIndex, merged, resultsRef]);\n\n return (\n <StyledBackdrop\n $isClosing={isClosing}\n onClick={closeSearch}\n onAnimationEnd={(e) => {\n // animationend bubbles, so this handler also fires for animations\n // completing on descendants (the inner modal, icons, result items).\n // Only the backdrop's own exit animation should trigger the unmount -\n // otherwise a child finishing its animation first tears the modal down\n // before the close animation plays, making it vanish instantly.\n if (e.target === e.currentTarget) onCloseAnimationEnd();\n }}\n >\n <StyledModal\n ref={dialogRef}\n $isClosing={isClosing}\n role=\"dialog\"\n aria-modal=\"true\"\n aria-label=\"Search documentation\"\n data-search-dialog\n onKeyDownCapture={handleDialogKeyDown}\n onClick={(e) => e.stopPropagation()}\n >\n <StyledInputWrapper>\n <Search size={18} />\n <StyledInput\n ref={inputRef}\n type=\"search\"\n value={query}\n onChange={(e) => {\n setQuery(e.target.value);\n setActiveIndex(0);\n }}\n onKeyDown={onKeyDown}\n placeholder=\"Search docs...\"\n autoComplete=\"off\"\n spellCheck={false}\n aria-label=\"Search documentation\"\n role=\"combobox\"\n aria-autocomplete=\"list\"\n aria-haspopup=\"listbox\"\n aria-expanded=\"true\"\n aria-controls={listboxId}\n aria-activedescendant={\n merged[activeIndex] ? optionId(activeIndex) : undefined\n }\n />\n {canAskAssistant && (\n <StyledAskAssistant\n type=\"button\"\n onClick={onAskAssistant}\n disabled={!query.trim()}\n aria-label=\"Ask AI\"\n title=\"Ask AI\"\n >\n Ask AI\n <StyledAskKeys aria-hidden=\"true\">\n <StyledAskKbd>⌥</StyledAskKbd>\n <StyledAskKbd>\n <StyledReturnGlyph>↵</StyledReturnGlyph>\n </StyledAskKbd>\n </StyledAskKeys>\n </StyledAskAssistant>\n )}\n <IconButton\n onClick={closeSearch}\n aria-label=\"Close search\"\n title=\"Close search\"\n >\n <X />\n </IconButton>\n </StyledInputWrapper>\n <StyledLiveStatus role=\"status\" aria-live=\"polite\" aria-atomic=\"true\">\n {isSearching\n ? \"Searching documentation\"\n : merged.length === 0\n ? \"No search results found\"\n : `${merged.length} search ${merged.length === 1 ? \"result\" : \"results\"} available`}\n </StyledLiveStatus>\n <StyledResults\n ref={resultsRef}\n id={listboxId}\n role=\"listbox\"\n aria-label=\"Search results\"\n aria-busy={isSearching}\n hidden={merged.length === 0}\n >\n {indicator && (\n <StyledHighlight\n aria-hidden=\"true\"\n $top={indicator.top}\n $height={indicator.height}\n />\n )}\n {merged.map((result, index) => (\n <StyledResultItem\n key={result.page.slug + result.page.section}\n id={optionId(index)}\n role=\"option\"\n aria-selected={index === activeIndex}\n $isActive={index === activeIndex}\n onClick={(e) => navigate(result.page.slug, e)}\n onMouseEnter={() => setActiveIndex(index)}\n >\n <StyledResultTitle>{result.page.title}</StyledResultTitle>\n <StyledResultMeta>\n {result.page.section\n ? `${sectionLabels[result.page.section] || result.page.section} / `\n : \"\"}\n {result.page.category}\n </StyledResultMeta>\n {result.snippet && (\n <StyledSnippet\n dangerouslySetInnerHTML={{\n __html: highlightMatch(result.snippet, query),\n }}\n />\n )}\n </StyledResultItem>\n ))}\n </StyledResults>\n {merged.length === 0 && (\n <StyledEmpty aria-hidden=\"true\">\n {isSearching ? (\n <>\n <Spinner size={18} /> Searching documentation...\n </>\n ) : (\n \"No results found\"\n )}\n </StyledEmpty>\n )}\n </StyledModal>\n </StyledBackdrop>\n );\n}\n";
|
|
@@ -42,6 +42,8 @@ export interface SearchModalContentProps {
|
|
|
42
42
|
) => void;
|
|
43
43
|
canAskAssistant: boolean;
|
|
44
44
|
onAskAssistant: () => void;
|
|
45
|
+
shouldRestoreFocus: () => boolean;
|
|
46
|
+
returnFocusTo: HTMLElement | null;
|
|
45
47
|
}
|
|
46
48
|
|
|
47
49
|
const ANIMATION_MS = 300;
|
|
@@ -133,6 +135,14 @@ const StyledInput = styled.input<{ theme: Theme }>\`
|
|
|
133
135
|
&::placeholder {
|
|
134
136
|
color: \${({ theme }) => theme.colors.gray};
|
|
135
137
|
}
|
|
138
|
+
|
|
139
|
+
/* type="search" gets the platform clear button, which duplicates the modal's
|
|
140
|
+
own close control and sits at a size and color we do not control. */
|
|
141
|
+
&::-webkit-search-cancel-button,
|
|
142
|
+
&::-webkit-search-decoration {
|
|
143
|
+
-webkit-appearance: none;
|
|
144
|
+
appearance: none;
|
|
145
|
+
}
|
|
136
146
|
\`;
|
|
137
147
|
|
|
138
148
|
// "Ask AI" affordance shown before the close button when the AI chat is
|
|
@@ -211,21 +221,20 @@ const StyledResults = styled.ul<{ theme: Theme }>\`
|
|
|
211
221
|
-webkit-overflow-scrolling: touch;
|
|
212
222
|
\${thinScrollbar};
|
|
213
223
|
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
by the scroll overflow) instead of the browser's default outline. */
|
|
217
|
-
&:focus-visible {
|
|
218
|
-
outline: none;
|
|
219
|
-
border-radius: \${({ theme }) => theme.spacing.radius.xs};
|
|
220
|
-
box-shadow: inset 0 0 0 2px \${({ theme }) => theme.colors.primaryLight};
|
|
224
|
+
&[hidden] {
|
|
225
|
+
display: none;
|
|
221
226
|
}
|
|
222
227
|
\`;
|
|
223
228
|
|
|
224
229
|
// Single indicator that slides between result items instead of each item
|
|
225
|
-
// fading its own background in and out. Positioned/sized
|
|
226
|
-
//
|
|
230
|
+
// fading its own background in and out. Positioned/sized from the active item's
|
|
231
|
+
// measured offset (see the layout effect below); the transition on
|
|
227
232
|
// transform + height produces the up/down glide as the active item changes.
|
|
228
|
-
const StyledHighlight = styled.div<{
|
|
233
|
+
const StyledHighlight = styled.div<{
|
|
234
|
+
theme: Theme;
|
|
235
|
+
$top: number;
|
|
236
|
+
$height: number;
|
|
237
|
+
}>\`
|
|
229
238
|
position: absolute;
|
|
230
239
|
left: 8px;
|
|
231
240
|
right: 8px;
|
|
@@ -239,6 +248,8 @@ const StyledHighlight = styled.div<{ theme: Theme }>\`
|
|
|
239
248
|
transform 0.2s cubic-bezier(0.22, 1, 0.36, 1),
|
|
240
249
|
height 0.2s cubic-bezier(0.22, 1, 0.36, 1);
|
|
241
250
|
will-change: transform, height;
|
|
251
|
+
transform: translateY(\${({ $top }) => $top}px);
|
|
252
|
+
height: \${({ $height }) => $height}px;
|
|
242
253
|
|
|
243
254
|
@media (prefers-reduced-motion: reduce) {
|
|
244
255
|
transition: none;
|
|
@@ -252,6 +263,11 @@ const StyledResultItem = styled.li<{ theme: Theme; $isActive: boolean }>\`
|
|
|
252
263
|
border-radius: \${({ theme }) => theme.spacing.radius.xs};
|
|
253
264
|
cursor: pointer;
|
|
254
265
|
|
|
266
|
+
&:focus-visible {
|
|
267
|
+
outline: none;
|
|
268
|
+
box-shadow: inset 0 0 0 2px \${({ theme }) => theme.colors.primaryLight};
|
|
269
|
+
}
|
|
270
|
+
|
|
255
271
|
\${({ $isActive, theme }) =>
|
|
256
272
|
$isActive &&
|
|
257
273
|
css\`
|
|
@@ -301,11 +317,26 @@ const StyledEmpty = styled.div<{ theme: Theme }>\`
|
|
|
301
317
|
display: flex;
|
|
302
318
|
align-items: center;
|
|
303
319
|
justify-content: center;
|
|
320
|
+
/* Separates the spinner from "Searching documentation..."; the whitespace
|
|
321
|
+
between them in the markup collapses away as an anonymous flex item. */
|
|
322
|
+
gap: 8px;
|
|
304
323
|
text-align: center;
|
|
305
324
|
font-size: \${({ theme }) => theme.fontSizes.small.lg};
|
|
306
325
|
color: \${({ theme }) => theme.colors.gray};
|
|
307
326
|
\`;
|
|
308
327
|
|
|
328
|
+
const StyledLiveStatus = styled.div\`
|
|
329
|
+
position: absolute;
|
|
330
|
+
width: 1px;
|
|
331
|
+
height: 1px;
|
|
332
|
+
padding: 0;
|
|
333
|
+
margin: -1px;
|
|
334
|
+
overflow: hidden;
|
|
335
|
+
clip: rect(0 0 0 0);
|
|
336
|
+
white-space: nowrap;
|
|
337
|
+
border: 0;
|
|
338
|
+
\`;
|
|
339
|
+
|
|
309
340
|
function escapeHtml(str: string): string {
|
|
310
341
|
return str
|
|
311
342
|
.replace(/&/g, "&")
|
|
@@ -341,7 +372,79 @@ export function SearchModalContent({
|
|
|
341
372
|
navigate,
|
|
342
373
|
canAskAssistant,
|
|
343
374
|
onAskAssistant,
|
|
375
|
+
shouldRestoreFocus,
|
|
376
|
+
returnFocusTo,
|
|
344
377
|
}: SearchModalContentProps) {
|
|
378
|
+
const dialogRef = React.useRef<HTMLDivElement | null>(null);
|
|
379
|
+
const inputRef = React.useRef<HTMLInputElement | null>(null);
|
|
380
|
+
const previouslyFocusedRef = React.useRef<HTMLElement | null>(
|
|
381
|
+
returnFocusTo?.isConnected === true
|
|
382
|
+
? returnFocusTo
|
|
383
|
+
: typeof document === "undefined"
|
|
384
|
+
? null
|
|
385
|
+
: (document.activeElement as HTMLElement | null),
|
|
386
|
+
);
|
|
387
|
+
const listboxId = React.useId();
|
|
388
|
+
const optionId = (index: number) => listboxId + "-option-" + index;
|
|
389
|
+
|
|
390
|
+
React.useEffect(() => {
|
|
391
|
+
const previouslyFocused = previouslyFocusedRef.current;
|
|
392
|
+
const containFocus = (event: FocusEvent) => {
|
|
393
|
+
if (
|
|
394
|
+
event.target instanceof Node &&
|
|
395
|
+
!dialogRef.current?.contains(event.target)
|
|
396
|
+
) {
|
|
397
|
+
inputRef.current?.focus();
|
|
398
|
+
}
|
|
399
|
+
};
|
|
400
|
+
|
|
401
|
+
document.addEventListener("focusin", containFocus);
|
|
402
|
+
inputRef.current?.focus();
|
|
403
|
+
|
|
404
|
+
return () => {
|
|
405
|
+
document.removeEventListener("focusin", containFocus);
|
|
406
|
+
if (shouldRestoreFocus() && previouslyFocused?.isConnected) {
|
|
407
|
+
previouslyFocused.focus();
|
|
408
|
+
}
|
|
409
|
+
};
|
|
410
|
+
}, [shouldRestoreFocus]);
|
|
411
|
+
|
|
412
|
+
const handleDialogKeyDown = (event: React.KeyboardEvent<HTMLDivElement>) => {
|
|
413
|
+
if (event.key === "Escape") {
|
|
414
|
+
event.preventDefault();
|
|
415
|
+
event.stopPropagation();
|
|
416
|
+
closeSearch();
|
|
417
|
+
return;
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
if (event.key !== "Tab") return;
|
|
421
|
+
|
|
422
|
+
const focusable = Array.from(
|
|
423
|
+
dialogRef.current?.querySelectorAll<HTMLElement>(
|
|
424
|
+
'a[href], button:not([disabled]), input:not([disabled]), [tabindex]:not([tabindex="-1"])',
|
|
425
|
+
) ?? [],
|
|
426
|
+
).filter(
|
|
427
|
+
(element) =>
|
|
428
|
+
element.offsetParent !== null &&
|
|
429
|
+
element.getAttribute("aria-hidden") !== "true",
|
|
430
|
+
);
|
|
431
|
+
|
|
432
|
+
if (focusable.length === 0) {
|
|
433
|
+
event.preventDefault();
|
|
434
|
+
return;
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
const first = focusable[0];
|
|
438
|
+
const last = focusable[focusable.length - 1];
|
|
439
|
+
if (event.shiftKey && document.activeElement === first) {
|
|
440
|
+
event.preventDefault();
|
|
441
|
+
last.focus();
|
|
442
|
+
} else if (!event.shiftKey && document.activeElement === last) {
|
|
443
|
+
event.preventDefault();
|
|
444
|
+
first.focus();
|
|
445
|
+
}
|
|
446
|
+
};
|
|
447
|
+
|
|
345
448
|
// Measure the active item so the sliding highlight can be placed over it.
|
|
346
449
|
// useLayoutEffect runs before paint, so the first position is committed
|
|
347
450
|
// without a transition (no slide-in on open); later activeIndex/hover
|
|
@@ -397,11 +500,21 @@ export function SearchModalContent({
|
|
|
397
500
|
if (e.target === e.currentTarget) onCloseAnimationEnd();
|
|
398
501
|
}}
|
|
399
502
|
>
|
|
400
|
-
<StyledModal
|
|
503
|
+
<StyledModal
|
|
504
|
+
ref={dialogRef}
|
|
505
|
+
$isClosing={isClosing}
|
|
506
|
+
role="dialog"
|
|
507
|
+
aria-modal="true"
|
|
508
|
+
aria-label="Search documentation"
|
|
509
|
+
data-search-dialog
|
|
510
|
+
onKeyDownCapture={handleDialogKeyDown}
|
|
511
|
+
onClick={(e) => e.stopPropagation()}
|
|
512
|
+
>
|
|
401
513
|
<StyledInputWrapper>
|
|
402
514
|
<Search size={18} />
|
|
403
515
|
<StyledInput
|
|
404
|
-
|
|
516
|
+
ref={inputRef}
|
|
517
|
+
type="search"
|
|
405
518
|
value={query}
|
|
406
519
|
onChange={(e) => {
|
|
407
520
|
setQuery(e.target.value);
|
|
@@ -411,6 +524,15 @@ export function SearchModalContent({
|
|
|
411
524
|
placeholder="Search docs..."
|
|
412
525
|
autoComplete="off"
|
|
413
526
|
spellCheck={false}
|
|
527
|
+
aria-label="Search documentation"
|
|
528
|
+
role="combobox"
|
|
529
|
+
aria-autocomplete="list"
|
|
530
|
+
aria-haspopup="listbox"
|
|
531
|
+
aria-expanded="true"
|
|
532
|
+
aria-controls={listboxId}
|
|
533
|
+
aria-activedescendant={
|
|
534
|
+
merged[activeIndex] ? optionId(activeIndex) : undefined
|
|
535
|
+
}
|
|
414
536
|
/>
|
|
415
537
|
{canAskAssistant && (
|
|
416
538
|
<StyledAskAssistant
|
|
@@ -437,44 +559,64 @@ export function SearchModalContent({
|
|
|
437
559
|
<X />
|
|
438
560
|
</IconButton>
|
|
439
561
|
</StyledInputWrapper>
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
562
|
+
<StyledLiveStatus role="status" aria-live="polite" aria-atomic="true">
|
|
563
|
+
{isSearching
|
|
564
|
+
? "Searching documentation"
|
|
565
|
+
: merged.length === 0
|
|
566
|
+
? "No search results found"
|
|
567
|
+
: \`\${merged.length} search \${merged.length === 1 ? "result" : "results"} available\`}
|
|
568
|
+
</StyledLiveStatus>
|
|
569
|
+
<StyledResults
|
|
570
|
+
ref={resultsRef}
|
|
571
|
+
id={listboxId}
|
|
572
|
+
role="listbox"
|
|
573
|
+
aria-label="Search results"
|
|
574
|
+
aria-busy={isSearching}
|
|
575
|
+
hidden={merged.length === 0}
|
|
576
|
+
>
|
|
577
|
+
{indicator && (
|
|
578
|
+
<StyledHighlight
|
|
579
|
+
aria-hidden="true"
|
|
580
|
+
$top={indicator.top}
|
|
581
|
+
$height={indicator.height}
|
|
582
|
+
/>
|
|
583
|
+
)}
|
|
584
|
+
{merged.map((result, index) => (
|
|
585
|
+
<StyledResultItem
|
|
586
|
+
key={result.page.slug + result.page.section}
|
|
587
|
+
id={optionId(index)}
|
|
588
|
+
role="option"
|
|
589
|
+
aria-selected={index === activeIndex}
|
|
590
|
+
$isActive={index === activeIndex}
|
|
591
|
+
onClick={(e) => navigate(result.page.slug, e)}
|
|
592
|
+
onMouseEnter={() => setActiveIndex(index)}
|
|
593
|
+
>
|
|
594
|
+
<StyledResultTitle>{result.page.title}</StyledResultTitle>
|
|
595
|
+
<StyledResultMeta>
|
|
596
|
+
{result.page.section
|
|
597
|
+
? \`\${sectionLabels[result.page.section] || result.page.section} / \`
|
|
598
|
+
: ""}
|
|
599
|
+
{result.page.category}
|
|
600
|
+
</StyledResultMeta>
|
|
601
|
+
{result.snippet && (
|
|
602
|
+
<StyledSnippet
|
|
603
|
+
dangerouslySetInnerHTML={{
|
|
604
|
+
__html: highlightMatch(result.snippet, query),
|
|
605
|
+
}}
|
|
606
|
+
/>
|
|
607
|
+
)}
|
|
608
|
+
</StyledResultItem>
|
|
609
|
+
))}
|
|
610
|
+
</StyledResults>
|
|
611
|
+
{merged.length === 0 && (
|
|
612
|
+
<StyledEmpty aria-hidden="true">
|
|
613
|
+
{isSearching ? (
|
|
614
|
+
<>
|
|
615
|
+
<Spinner size={18} /> Searching documentation...
|
|
616
|
+
</>
|
|
617
|
+
) : (
|
|
618
|
+
"No results found"
|
|
450
619
|
)}
|
|
451
|
-
{merged.map((result, index) => (
|
|
452
|
-
<StyledResultItem
|
|
453
|
-
key={result.page.slug + result.page.section}
|
|
454
|
-
$isActive={index === activeIndex}
|
|
455
|
-
onClick={(e) => navigate(result.page.slug, e)}
|
|
456
|
-
onMouseEnter={() => setActiveIndex(index)}
|
|
457
|
-
>
|
|
458
|
-
<StyledResultTitle>{result.page.title}</StyledResultTitle>
|
|
459
|
-
<StyledResultMeta>
|
|
460
|
-
{result.page.section
|
|
461
|
-
? \`\${sectionLabels[result.page.section] || result.page.section} / \`
|
|
462
|
-
: ""}
|
|
463
|
-
{result.page.category}
|
|
464
|
-
</StyledResultMeta>
|
|
465
|
-
{result.snippet && (
|
|
466
|
-
<StyledSnippet
|
|
467
|
-
dangerouslySetInnerHTML={{
|
|
468
|
-
__html: highlightMatch(result.snippet, query),
|
|
469
|
-
}}
|
|
470
|
-
/>
|
|
471
|
-
)}
|
|
472
|
-
</StyledResultItem>
|
|
473
|
-
))}
|
|
474
|
-
</StyledResults>
|
|
475
|
-
) : (
|
|
476
|
-
<StyledEmpty>
|
|
477
|
-
{isSearching ? <Spinner size={18} /> : "No results found"}
|
|
478
620
|
</StyledEmpty>
|
|
479
621
|
)}
|
|
480
622
|
</StyledModal>
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare const sideBarTemplate = "\"use client\";\nimport { useContext, useEffect, useRef, useState } from \"react\";\nimport { usePathname } from \"next/navigation\";\nimport { Flex, Space, ThemeToggle } from \"cherry-styled-components\";\nimport { httpMethodBadgeColor } from \"@/components/layout/Badge\";\nimport {\n DocsSidebar,\n SectionBarContext,\n StyledSidebar,\n StyledSidebarList,\n StyledSidebarListItem,\n StyledStrong,\n StyledSidebarListItemLink,\n StyledSidebarMethodTag,\n StyledSidebarGroupButton,\n StyledSidebarGroupRow,\n StyledSidebarGroupLink,\n StyledSidebarGroupChevron,\n StyledSidebarGroupContent,\n StyledSidebarFooter,\n StyleMobileBar,\n StyledMobileBurger,\n} from \"@/components/layout/DocsComponents\";\nimport { FocusModeToggle } from \"@/components/layout/FocusModeToggle\";\nimport { Icon } from \"@/components/layout/Icon\";\nimport { useLockBodyScroll } from \"@/components/LockBodyScroll\";\n\n// A link can be a leaf (slug + title) or a group with nested children. Both the\n// category icon and the per-link icon are optional Lucide names.\ntype NavItemLink = {\n slug?: string;\n title: string;\n icon?: string;\n httpMethod?: string;\n links?: NavItemLink[];\n};\n\ntype NavItem = {\n label: string;\n icon?: string;\n links: NavItemLink[];\n};\n\ninterface SideBarProps {\n result: NavItem[];\n}\n\nfunction linkContainsActivePath(link: NavItemLink, pathname: string): boolean {\n if (link.slug !== undefined && pathname === `/${link.slug}`) {\n return true;\n }\n return (link.links ?? []).some((child) =>\n linkContainsActivePath(child, pathname),\n );\n}\n\nfunction SidebarNavLink({\n link,\n depth,\n pathname,\n onNavigate,\n}: {\n link: NavItemLink;\n depth: number;\n pathname: string;\n onNavigate: () => void;\n}) {\n const children = link.links ?? [];\n const hasChildren = children.length > 0;\n const href = link.slug !== undefined ? `/${link.slug}` : undefined;\n const isActive = href !== undefined && pathname === href;\n const indent = { paddingLeft: `${20 + depth * 14}px` };\n\n // Open collapsible groups that contain the active page so deep links land\n // with their ancestors already expanded.\n const [isOpen, setIsOpen] = useState(\n hasChildren\n ? children.some((child) => linkContainsActivePath(child, pathname))\n : false,\n );\n\n if (!hasChildren) {\n return (\n <StyledSidebarListItem>\n <StyledSidebarListItemLink\n href={href ?? \"#\"}\n $isActive={isActive}\n aria-current={isActive ? \"page\" : undefined}\n onClick={onNavigate}\n style={indent}\n >\n {link.httpMethod && (\n <StyledSidebarMethodTag\n mono\n solid={isActive}\n size=\"xs\"\n color={httpMethodBadgeColor(link.httpMethod)}\n >\n {link.httpMethod}\n </StyledSidebarMethodTag>\n )}\n {link.icon && <Icon name={link.icon} size={16} />}\n {link.title}\n </StyledSidebarListItemLink>\n </StyledSidebarListItem>\n );\n }\n\n const toggle = () => setIsOpen((prev) => !prev);\n const toggleLabel = isOpen\n ? `Collapse ${link.title}`\n : `Expand ${link.title}`;\n const groupActive = linkContainsActivePath(link, pathname);\n\n return (\n <li>\n {href !== undefined ? (\n <StyledSidebarGroupRow\n $isActive={groupActive}\n $isOpen={isOpen}\n style={indent}\n >\n <StyledSidebarGroupLink\n href={href}\n aria-current={pathname === href ? \"page\" : undefined}\n onClick={onNavigate}\n >\n {link.icon && <Icon name={link.icon} size={16} />}\n {link.title}\n </StyledSidebarGroupLink>\n <StyledSidebarGroupChevron\n type=\"button\"\n onClick={toggle}\n aria-expanded={isOpen}\n aria-label={toggleLabel}\n >\n <Icon name=\"chevron-right\" size={16} />\n </StyledSidebarGroupChevron>\n </StyledSidebarGroupRow>\n ) : (\n <StyledSidebarGroupButton\n type=\"button\"\n onClick={toggle}\n $isActive={groupActive}\n $isOpen={isOpen}\n style={indent}\n aria-expanded={isOpen}\n aria-label={toggleLabel}\n >\n {link.icon && <Icon name={link.icon} size={16} />}\n {link.title}\n <Icon name=\"chevron-right\" size={16} />\n </StyledSidebarGroupButton>\n )}\n <StyledSidebarGroupContent $isOpen={isOpen}>\n {children.map((child: NavItemLink, index: number) => (\n <SidebarNavLink\n key={index}\n link={child}\n depth={depth + 1}\n pathname={pathname}\n onNavigate={onNavigate}\n />\n ))}\n </StyledSidebarGroupContent>\n </li>\n );\n}\n\nfunction SideBar({ result }: SideBarProps) {\n const [isMobileMenuOpen, setIsMobileMenuOpen] = useState(false);\n const hasSectionBar = useContext(SectionBarContext);\n const pathname = usePathname();\n const navRef = useRef<HTMLElement>(null);\n const footerRef = useRef<HTMLDivElement>(null);\n\n useLockBodyScroll(isMobileMenuOpen);\n\n // Bring the current page's link into view within the sidebar's own scroll\n // area when it starts off-screen (deep pages, in-content links, search).\n // Scoped to the nav via scrollTo so the main document never jumps.\n useEffect(() => {\n const nav = navRef.current;\n if (!nav) return;\n const active = nav.querySelector<HTMLElement>('[aria-current=\"page\"]');\n if (!active) return;\n\n const navRect = nav.getBoundingClientRect();\n const activeRect = active.getBoundingClientRect();\n\n // The theme-toggle footer is sticky over the bottom of the scroll area\n // (~60px tall), so links underneath it are covered rather than visible.\n // Clamp the visible bottom to the footer's top edge when it's pinned.\n const footerRect = footerRef.current?.getBoundingClientRect();\n const visibleBottom = footerRect\n ? Math.min(navRect.bottom, footerRect.top)\n : navRect.bottom;\n\n const isOutOfView =\n activeRect.top < navRect.top || activeRect.bottom > visibleBottom;\n if (!isOutOfView) return;\n\n // Center within the visible band (nav top .. footer top), not the full\n // nav height, so the link never settles behind the footer.\n const visibleHeight = visibleBottom - navRect.top;\n const target =\n nav.scrollTop +\n (activeRect.top - navRect.top) -\n (visibleHeight - active.clientHeight) / 2;\n\n // Animate the scroll, but honor users who opt out of motion.\n const prefersReducedMotion = window.matchMedia(\n \"(prefers-reduced-motion: reduce)\",\n ).matches;\n nav.scrollTo({\n top: Math.max(0, target),\n behavior: prefersReducedMotion ? \"auto\" : \"smooth\",\n });\n }, [pathname]);\n\n return (\n <DocsSidebar>\n <StyleMobileBar\n onClick={() => setIsMobileMenuOpen(!isMobileMenuOpen)}\n $isActive={isMobileMenuOpen}\n aria-label={\n isMobileMenuOpen ? \"Close navigation menu\" : \"Open navigation menu\"\n }\n aria-expanded={isMobileMenuOpen}\n >\n <StyledMobileBurger $isActive={isMobileMenuOpen} />\n </StyleMobileBar>\n\n <StyledSidebar\n ref={navRef}\n $isActive={isMobileMenuOpen}\n $hasSectionBar={hasSectionBar}\n >\n {result &&\n result.map((item: NavItem, index: number) => {\n return (\n <StyledSidebarList key={index}>\n <StyledSidebarListItem>\n <StyledStrong>\n {item.icon && <Icon name={item.icon} size={16} />}\n {item.label}\n </StyledStrong>{\" \"}\n </StyledSidebarListItem>\n <li>\n <Space $size={20} />\n </li>\n {item.links &&\n item.links.map((link: NavItemLink, indexChild: number) => (\n <SidebarNavLink\n key={indexChild}\n link={link}\n depth={0}\n pathname={pathname}\n onNavigate={() => setIsMobileMenuOpen(false)}\n />\n ))}\n <li aria-hidden=\"true\">\n <Space $size={20} />\n </li>\n </StyledSidebarList>\n );\n })}\n <StyledSidebarFooter ref={footerRef}>\n <Flex $xsJustifyContent=\"flex-start\" $lgJustifyContent=\"flex-end\">\n <ThemeToggle $shortcut />\n </Flex>\n </StyledSidebarFooter>\n </StyledSidebar>\n\n {/* Outside StyledSidebar on purpose: focus mode slides that whole\n column away, and the toggle has to survive it. */}\n <FocusModeToggle />\n </DocsSidebar>\n );\n}\n\nexport { SideBar };\n";
|
|
1
|
+
export declare const sideBarTemplate = "\"use client\";\nimport { useContext, useEffect, useId, useRef, useState } from \"react\";\nimport { usePathname } from \"next/navigation\";\nimport { Flex, Space, ThemeToggle } from \"cherry-styled-components\";\nimport { httpMethodBadgeColor } from \"@/components/layout/Badge\";\nimport {\n DocsSidebar,\n SectionBarContext,\n StyledSidebar,\n StyledSidebarList,\n StyledSidebarListItem,\n StyledStrong,\n StyledSidebarListItemLink,\n StyledSidebarMethodTag,\n StyledSidebarGroupButton,\n StyledSidebarGroupRow,\n StyledSidebarGroupLink,\n StyledSidebarGroupChevron,\n StyledSidebarGroupContent,\n StyledSidebarFooter,\n StyleMobileBar,\n StyledMobileBurger,\n} from \"@/components/layout/DocsComponents\";\nimport { FocusModeToggle } from \"@/components/layout/FocusModeToggle\";\nimport { Icon } from \"@/components/layout/Icon\";\nimport { useLockBodyScroll } from \"@/components/LockBodyScroll\";\n\n// A link can be a leaf (slug + title) or a group with nested children. Both the\n// category icon and the per-link icon are optional Lucide names.\ntype NavItemLink = {\n slug?: string;\n title: string;\n icon?: string;\n httpMethod?: string;\n links?: NavItemLink[];\n};\n\ntype NavItem = {\n label: string;\n icon?: string;\n links: NavItemLink[];\n};\n\ninterface SideBarProps {\n result: NavItem[];\n}\n\nfunction linkContainsActivePath(link: NavItemLink, pathname: string): boolean {\n if (link.slug !== undefined && pathname === `/${link.slug}`) {\n return true;\n }\n return (link.links ?? []).some((child) =>\n linkContainsActivePath(child, pathname),\n );\n}\n\nfunction SidebarNavLink({\n link,\n depth,\n pathname,\n onNavigate,\n}: {\n link: NavItemLink;\n depth: number;\n pathname: string;\n onNavigate: () => void;\n}) {\n const children = link.links ?? [];\n const hasChildren = children.length > 0;\n const href = link.slug !== undefined ? `/${link.slug}` : undefined;\n const isActive = href !== undefined && pathname === href;\n const indent = { paddingLeft: `${20 + depth * 14}px` };\n const groupContentId = useId();\n\n // Open collapsible groups that contain the active page so deep links land\n // with their ancestors already expanded.\n const [isOpen, setIsOpen] = useState(\n hasChildren\n ? children.some((child) => linkContainsActivePath(child, pathname))\n : false,\n );\n\n if (!hasChildren) {\n return (\n <StyledSidebarListItem>\n <StyledSidebarListItemLink\n href={href ?? \"#\"}\n $isActive={isActive}\n aria-current={isActive ? \"page\" : undefined}\n onClick={onNavigate}\n style={indent}\n >\n {link.httpMethod && (\n <StyledSidebarMethodTag\n mono\n solid={isActive}\n size=\"xs\"\n color={httpMethodBadgeColor(link.httpMethod)}\n >\n {link.httpMethod}\n </StyledSidebarMethodTag>\n )}\n {link.icon && <Icon name={link.icon} size={16} />}\n {link.title}\n </StyledSidebarListItemLink>\n </StyledSidebarListItem>\n );\n }\n\n const toggle = () => setIsOpen((prev) => !prev);\n const toggleLabel = isOpen\n ? `Collapse ${link.title}`\n : `Expand ${link.title}`;\n const groupActive = linkContainsActivePath(link, pathname);\n\n return (\n <li>\n {href !== undefined ? (\n <StyledSidebarGroupRow\n $isActive={groupActive}\n $isOpen={isOpen}\n style={indent}\n >\n <StyledSidebarGroupLink\n href={href}\n aria-current={pathname === href ? \"page\" : undefined}\n onClick={onNavigate}\n >\n {link.icon && <Icon name={link.icon} size={16} />}\n {link.title}\n </StyledSidebarGroupLink>\n <StyledSidebarGroupChevron\n type=\"button\"\n onClick={toggle}\n aria-expanded={isOpen}\n aria-controls={groupContentId}\n aria-label={toggleLabel}\n >\n <Icon name=\"chevron-right\" size={16} />\n </StyledSidebarGroupChevron>\n </StyledSidebarGroupRow>\n ) : (\n <StyledSidebarGroupButton\n type=\"button\"\n onClick={toggle}\n $isActive={groupActive}\n $isOpen={isOpen}\n style={indent}\n aria-expanded={isOpen}\n aria-controls={groupContentId}\n aria-label={toggleLabel}\n >\n {link.icon && <Icon name={link.icon} size={16} />}\n {link.title}\n <Icon name=\"chevron-right\" size={16} />\n </StyledSidebarGroupButton>\n )}\n <StyledSidebarGroupContent id={groupContentId} $isOpen={isOpen}>\n {children.map((child: NavItemLink, index: number) => (\n <SidebarNavLink\n key={index}\n link={child}\n depth={depth + 1}\n pathname={pathname}\n onNavigate={onNavigate}\n />\n ))}\n </StyledSidebarGroupContent>\n </li>\n );\n}\n\nfunction SideBar({ result }: SideBarProps) {\n const [isMobileMenuOpen, setIsMobileMenuOpen] = useState(false);\n const hasSectionBar = useContext(SectionBarContext);\n const pathname = usePathname();\n const sidebarId = useId();\n const navRef = useRef<HTMLElement>(null);\n const footerRef = useRef<HTMLDivElement>(null);\n\n useLockBodyScroll(isMobileMenuOpen);\n\n // Bring the current page's link into view within the sidebar's own scroll\n // area when it starts off-screen (deep pages, in-content links, search).\n // Scoped to the nav via scrollTo so the main document never jumps.\n useEffect(() => {\n const nav = navRef.current;\n if (!nav) return;\n const active = nav.querySelector<HTMLElement>('[aria-current=\"page\"]');\n if (!active) return;\n\n const navRect = nav.getBoundingClientRect();\n const activeRect = active.getBoundingClientRect();\n\n // The theme-toggle footer is sticky over the bottom of the scroll area\n // (~60px tall), so links underneath it are covered rather than visible.\n // Clamp the visible bottom to the footer's top edge when it's pinned.\n const footerRect = footerRef.current?.getBoundingClientRect();\n const visibleBottom = footerRect\n ? Math.min(navRect.bottom, footerRect.top)\n : navRect.bottom;\n\n const isOutOfView =\n activeRect.top < navRect.top || activeRect.bottom > visibleBottom;\n if (!isOutOfView) return;\n\n // Center within the visible band (nav top .. footer top), not the full\n // nav height, so the link never settles behind the footer.\n const visibleHeight = visibleBottom - navRect.top;\n const target =\n nav.scrollTop +\n (activeRect.top - navRect.top) -\n (visibleHeight - active.clientHeight) / 2;\n\n // Animate the scroll, but honor users who opt out of motion.\n const prefersReducedMotion = window.matchMedia(\n \"(prefers-reduced-motion: reduce)\",\n ).matches;\n nav.scrollTo({\n top: Math.max(0, target),\n behavior: prefersReducedMotion ? \"auto\" : \"smooth\",\n });\n }, [pathname]);\n\n return (\n <DocsSidebar>\n <StyleMobileBar\n onClick={() => setIsMobileMenuOpen(!isMobileMenuOpen)}\n $isActive={isMobileMenuOpen}\n aria-label={\n isMobileMenuOpen ? \"Close navigation menu\" : \"Open navigation menu\"\n }\n aria-expanded={isMobileMenuOpen}\n aria-controls={sidebarId}\n >\n <StyledMobileBurger $isActive={isMobileMenuOpen} />\n </StyleMobileBar>\n\n <StyledSidebar\n id={sidebarId}\n ref={navRef}\n $isActive={isMobileMenuOpen}\n $hasSectionBar={hasSectionBar}\n >\n {result &&\n result.map((item: NavItem, index: number) => {\n return (\n <StyledSidebarList key={index}>\n <StyledSidebarListItem>\n <StyledStrong>\n {item.icon && <Icon name={item.icon} size={16} />}\n {item.label}\n </StyledStrong>{\" \"}\n </StyledSidebarListItem>\n <li>\n <Space $size={20} />\n </li>\n {item.links &&\n item.links.map((link: NavItemLink, indexChild: number) => (\n <SidebarNavLink\n key={indexChild}\n link={link}\n depth={0}\n pathname={pathname}\n onNavigate={() => setIsMobileMenuOpen(false)}\n />\n ))}\n <li aria-hidden=\"true\">\n <Space $size={20} />\n </li>\n </StyledSidebarList>\n );\n })}\n <StyledSidebarFooter ref={footerRef}>\n <Flex $xsJustifyContent=\"flex-start\" $lgJustifyContent=\"flex-end\">\n <ThemeToggle $shortcut />\n </Flex>\n </StyledSidebarFooter>\n </StyledSidebar>\n\n {/* Outside StyledSidebar on purpose: focus mode slides that whole\n column away, and the toggle has to survive it. */}\n <FocusModeToggle />\n </DocsSidebar>\n );\n}\n\nexport { SideBar };\n";
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export const sideBarTemplate = `"use client";
|
|
2
|
-
import { useContext, useEffect, useRef, useState } from "react";
|
|
2
|
+
import { useContext, useEffect, useId, useRef, useState } from "react";
|
|
3
3
|
import { usePathname } from "next/navigation";
|
|
4
4
|
import { Flex, Space, ThemeToggle } from "cherry-styled-components";
|
|
5
5
|
import { httpMethodBadgeColor } from "@/components/layout/Badge";
|
|
@@ -70,6 +70,7 @@ function SidebarNavLink({
|
|
|
70
70
|
const href = link.slug !== undefined ? \`/\${link.slug}\` : undefined;
|
|
71
71
|
const isActive = href !== undefined && pathname === href;
|
|
72
72
|
const indent = { paddingLeft: \`\${20 + depth * 14}px\` };
|
|
73
|
+
const groupContentId = useId();
|
|
73
74
|
|
|
74
75
|
// Open collapsible groups that contain the active page so deep links land
|
|
75
76
|
// with their ancestors already expanded.
|
|
@@ -132,6 +133,7 @@ function SidebarNavLink({
|
|
|
132
133
|
type="button"
|
|
133
134
|
onClick={toggle}
|
|
134
135
|
aria-expanded={isOpen}
|
|
136
|
+
aria-controls={groupContentId}
|
|
135
137
|
aria-label={toggleLabel}
|
|
136
138
|
>
|
|
137
139
|
<Icon name="chevron-right" size={16} />
|
|
@@ -145,6 +147,7 @@ function SidebarNavLink({
|
|
|
145
147
|
$isOpen={isOpen}
|
|
146
148
|
style={indent}
|
|
147
149
|
aria-expanded={isOpen}
|
|
150
|
+
aria-controls={groupContentId}
|
|
148
151
|
aria-label={toggleLabel}
|
|
149
152
|
>
|
|
150
153
|
{link.icon && <Icon name={link.icon} size={16} />}
|
|
@@ -152,7 +155,7 @@ function SidebarNavLink({
|
|
|
152
155
|
<Icon name="chevron-right" size={16} />
|
|
153
156
|
</StyledSidebarGroupButton>
|
|
154
157
|
)}
|
|
155
|
-
<StyledSidebarGroupContent $isOpen={isOpen}>
|
|
158
|
+
<StyledSidebarGroupContent id={groupContentId} $isOpen={isOpen}>
|
|
156
159
|
{children.map((child: NavItemLink, index: number) => (
|
|
157
160
|
<SidebarNavLink
|
|
158
161
|
key={index}
|
|
@@ -171,6 +174,7 @@ function SideBar({ result }: SideBarProps) {
|
|
|
171
174
|
const [isMobileMenuOpen, setIsMobileMenuOpen] = useState(false);
|
|
172
175
|
const hasSectionBar = useContext(SectionBarContext);
|
|
173
176
|
const pathname = usePathname();
|
|
177
|
+
const sidebarId = useId();
|
|
174
178
|
const navRef = useRef<HTMLElement>(null);
|
|
175
179
|
const footerRef = useRef<HTMLDivElement>(null);
|
|
176
180
|
|
|
@@ -227,11 +231,13 @@ function SideBar({ result }: SideBarProps) {
|
|
|
227
231
|
isMobileMenuOpen ? "Close navigation menu" : "Open navigation menu"
|
|
228
232
|
}
|
|
229
233
|
aria-expanded={isMobileMenuOpen}
|
|
234
|
+
aria-controls={sidebarId}
|
|
230
235
|
>
|
|
231
236
|
<StyledMobileBurger $isActive={isMobileMenuOpen} />
|
|
232
237
|
</StyleMobileBar>
|
|
233
238
|
|
|
234
239
|
<StyledSidebar
|
|
240
|
+
id={sidebarId}
|
|
235
241
|
ref={navRef}
|
|
236
242
|
$isActive={isMobileMenuOpen}
|
|
237
243
|
$hasSectionBar={hasSectionBar}
|