fumadocs-openapi 12.2.0 → 12.3.0

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.
Files changed (27) hide show
  1. package/css/generated/shared.css +1 -1
  2. package/dist/.translations/index.d.ts +1 -3
  3. package/dist/.translations/keys.js +1 -3
  4. package/dist/packages/shared-api/dist/.translations/index.d.ts +1 -0
  5. package/dist/packages/shared-api/dist/.translations/keys.js +1 -0
  6. package/dist/packages/shared-api/dist/auto-anchor/index.js +54 -10
  7. package/dist/packages/shared-api/dist/components/accordion.js +3 -11
  8. package/dist/packages/shared-api/dist/components/collapsible.js +16 -0
  9. package/dist/packages/shared-api/dist/components/schema/client.d.ts +5 -0
  10. package/dist/packages/shared-api/dist/components/schema/client.js +408 -455
  11. package/dist/packages/shared-api/dist/components/select-tab.js +7 -12
  12. package/dist/ui/components/dialog.js +14 -0
  13. package/dist/ui/components/endpoint.js +11 -8
  14. package/dist/ui/components/heading.js +1 -1
  15. package/dist/ui/components/schema.js +1 -1
  16. package/dist/ui/{playground/components → components}/segmented.js +2 -2
  17. package/dist/ui/operation/index.js +261 -236
  18. package/dist/ui/playground/client.js +5 -4
  19. package/dist/ui/playground/components/fields.js +1 -1
  20. package/dist/ui/playground/components/request-panel.js +1 -1
  21. package/dist/ui/playground/components/response-panel.js +1 -1
  22. package/dist/ui/playground/components/result-display.js +1 -1
  23. package/dist/ui/playground/components/server-select.js +1 -1
  24. package/package.json +5 -5
  25. package/dist/packages/shared-api/dist/auto-anchor/client.js +0 -20
  26. package/dist/packages/shared-api/dist/components/popover.js +0 -23
  27. package/dist/packages/shared-api/dist/utils/merge-refs.js +0 -11
@@ -1,512 +1,465 @@
1
1
  "use client";
2
2
  import { cn } from "../../utils/cn.js";
3
- import { Popover, PopoverContent, PopoverTrigger } from "../popover.js";
3
+ import { Collapsible, CollapsibleContent } from "../collapsible.js";
4
4
  import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "../select.js";
5
- import { mergeRefs } from "../../utils/merge-refs.js";
6
- import { Fragment, Suspense, createContext, use, useCallback, useDeferredValue, useEffect, useMemo, useRef, useState } from "react";
5
+ import { Fragment, createContext, use, useCallback, useRef, useState } from "react";
7
6
  import { Fragment as Fragment$1, jsx, jsxs } from "react/jsx-runtime";
8
7
  import { useTranslations } from "@fuma-translate/react";
9
- import { cva } from "class-variance-authority";
10
- import { CheckIcon, FilterIcon, LinkIcon } from "lucide-react";
8
+ import { CheckIcon, ChevronRightIcon, FilterIcon, FilterXIcon, LinkIcon } from "lucide-react";
11
9
  import { buttonVariants } from "fumadocs-ui/components/ui/button";
10
+ import { flushSync } from "react-dom";
12
11
  import { useCopyButton } from "fumadocs-ui/utils/use-copy-button";
12
+ import { Menu } from "@base-ui/react/menu";
13
+ import { SchemaUIProvider, useSchemaUI } from "@fumadocs/json-schema/react/client";
13
14
  //#region ../shared-api/dist/components/schema/client.js
14
- const SchemaUIContext = createContext(null);
15
- /** roots that already restored their path from the URL */
16
- const restored = /* @__PURE__ */ new Set();
17
- function SchemaUIProvider({ rootId, name, generated, children }) {
18
- const [path, setPath] = useState(() => [{
19
- $ref: generated.$root,
20
- name
21
- }]);
22
- useEffect(() => {
23
- if (restored.has(rootId)) return;
24
- const url = new URL(window.location.href);
25
- const param = url.searchParams.get("path");
26
- if (url.hash !== `#${rootId}` || !param) return;
27
- const decoded = decodePath(param, url.searchParams.get("s-highlight"));
28
- if (decoded.length === 0 || decoded.some((item) => !generated.refs[item.$ref])) return;
29
- setPath(decoded);
30
- restored.add(rootId);
31
- if (!decoded.at(-1).highlighted) document.getElementById(rootId)?.scrollIntoView({ behavior: "smooth" });
32
- }, [rootId, generated.refs]);
33
- return /* @__PURE__ */ jsx(SchemaUIContext, {
34
- value: useMemo(() => ({
35
- rootId,
36
- path,
37
- setPath,
38
- generated,
39
- open: (name, $ref) => setPath([...path, {
40
- name,
41
- $ref
42
- }]),
43
- back: (index) => setPath(path.slice(0, index + 1))
44
- }), [
45
- rootId,
46
- path,
47
- generated
48
- ]),
49
- children
50
- });
51
- }
52
- function usePathState() {
53
- const ctx = use(SchemaUIContext);
54
- if (!ctx) throw new Error("Component must be used under <SchemaUIProvider />");
55
- return ctx;
56
- }
57
- function useSchemaUI() {
58
- return usePathState();
59
- }
60
- /**
61
- * The selected member of a union schema on the path item at `pathIndex`, `depth` for nested unions.
62
- */
63
- function useSchemaTabs(pathIndex, depth) {
64
- const { path, setPath } = usePathState();
65
- return [path[pathIndex].tabValues?.[depth] || void 0, (value) => {
66
- const tabValues = path[pathIndex].tabValues?.slice(0, depth) ?? [];
67
- tabValues[depth] = value;
68
- setPath(path.with(pathIndex, {
69
- ...path[pathIndex],
70
- tabValues
71
- }));
72
- }];
73
- }
74
- /**
75
- * Open state of the popover showing the schema `$ref` of the root property `pathName`.
76
- */
77
- function useSchemaPopover(pathName, $ref) {
78
- const { path, setPath } = usePathState();
79
- return {
80
- open: path.length > 1 && path[1].$ref === $ref && path[1].name === pathName && !path[0].closed,
81
- onOpenChange(open) {
82
- if (open) setPath([{
83
- ...path[0],
84
- closed: false
85
- }, {
86
- name: pathName,
87
- $ref
88
- }]);
89
- else setPath(path.map((item, i) => i === 0 ? {
90
- ...item,
91
- closed: true
92
- } : item));
93
- }
94
- };
95
- }
96
- /**
97
- * Whether the property `name` on the path item at `pathIndex` is highlighted by the link it was opened from.
98
- *
99
- * @returns the state and a ref to scroll the highlighted element into view
100
- */
101
- function useSchemaHighlight(pathIndex, name) {
102
- const { path } = usePathState();
103
- const item = path[pathIndex];
104
- const highlighted = item.highlighted === name;
105
- return [highlighted, useCallback((element) => {
106
- if (!element || !highlighted) return;
107
- window.setTimeout(() => {
108
- element.scrollIntoView({
109
- behavior: "smooth",
110
- block: "end"
111
- });
112
- delete item.highlighted;
113
- }, 300);
114
- }, [item, highlighted])];
115
- }
116
- /**
117
- * Copy the link to the property `name` of the current path.
118
- */
119
- function useCopySchemaLink(name) {
120
- const { path, rootId } = usePathState();
121
- return useCopyButton(() => {
122
- const url = new URL(window.location.href);
123
- url.hash = `#${rootId}`;
124
- url.searchParams.set("s-highlight", name);
125
- url.searchParams.set("path", encodePath(path));
126
- return navigator.clipboard.writeText(url.href);
127
- });
128
- }
129
- function encodePath(path) {
130
- return path.map((item) => [
131
- item.name,
132
- item.$ref,
133
- ...item.tabValues ?? []
134
- ].join("\0")).join("|");
135
- }
136
- function decodePath(path, highlighted) {
137
- const out = [];
138
- for (const part of path.split("|")) {
139
- const [name, $ref, ...tabValues] = part.split("\0");
140
- out.push({
141
- name,
142
- $ref,
143
- tabValues
144
- });
145
- }
146
- if (highlighted && out.length > 0) out[out.length - 1].highlighted = highlighted;
147
- return out;
148
- }
149
- const typeVariants = cva("text-sm text-start text-fd-muted-foreground font-mono", { variants: { variant: { trigger: "underline hover:text-fd-accent-foreground data-[popup-open]:text-fd-accent-foreground" } } });
150
- /** how a type opens its schema: a popover from the root, navigation inside the popover */
151
- const TriggerContext = createContext((props) => /* @__PURE__ */ jsx(RootTypeInfoTrigger, { ...props }));
152
- const renderPopoverTrigger = (props) => /* @__PURE__ */ jsx(PopoverTypeInfoTrigger, { ...props });
153
- function SchemaUI({ rootId, name, required = false, as = "property", generated }) {
15
+ const TypeContext = createContext(null);
16
+ const typeClassName = "font-mono text-xs text-fd-muted-foreground";
17
+ const crumbClassName = "truncate rounded-md px-1.5 py-1 text-fd-muted-foreground transition-colors hover:bg-fd-accent hover:text-fd-accent-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-fd-ring data-popup-open:bg-fd-accent";
18
+ const chevronClassName = "size-3.5 shrink-0 text-fd-muted-foreground/60";
19
+ /** below the sticky headers of docs layouts */
20
+ const scrollMarginClassName = "scroll-mt-[calc(var(--fd-docs-row-3,0px)+1rem)]";
21
+ const tagClassName = "rounded-md border bg-fd-secondary px-1.5 py-0.5 text-xs text-fd-secondary-foreground";
22
+ function SchemaUI({ rootId, name, required = false, as = "property", selector, actions, generated }) {
154
23
  return /* @__PURE__ */ jsx(SchemaUIProvider, {
155
- rootId,
24
+ id: rootId,
156
25
  name,
157
26
  generated,
158
- children: /* @__PURE__ */ jsx(SchemaUIContent, {
27
+ children: as === "body" && generated.refs[generated.$root].type !== "primitive" ? /* @__PURE__ */ jsx(SchemaCard, {
28
+ id: rootId,
29
+ selector,
30
+ actions
31
+ }) : /* @__PURE__ */ jsx(RootProperty, {
32
+ id: rootId,
159
33
  name,
34
+ $type: generated.$root,
160
35
  required,
161
- as
36
+ actions: /* @__PURE__ */ jsxs(Fragment$1, { children: [selector, actions] })
162
37
  })
163
38
  });
164
39
  }
165
- function SchemaUIContent({ name, required, as }) {
166
- const { rootId, generated } = useSchemaUI();
167
- if (as === "property" || generated.refs[generated.$root].type === "primitive") return /* @__PURE__ */ jsx(ObjectProperty, {
168
- id: rootId,
169
- name,
170
- $type: generated.$root,
171
- parentPathIndex: 0,
172
- required
173
- });
174
- return /* @__PURE__ */ jsx("div", {
175
- id: rootId,
176
- children: /* @__PURE__ */ jsx(PathItemBody, { pathIndex: 0 })
177
- });
178
- }
179
- function RootTypeInfoTrigger({ pathName, $ref, children }) {
180
- const { path, rootId } = useSchemaUI();
181
- const { open, onOpenChange } = useSchemaPopover(pathName, $ref);
182
- const popoverRef = useCallback((element) => {
183
- if (!element) return;
184
- element.scrollTop = scrollTops.get(getScrollKey(rootId, path)) ?? 0;
185
- const current = parseFloat(element.style.getPropertyValue("--min-height") || "200px");
186
- element.style.setProperty("--min-height", Math.max(element.clientHeight + 2, current) + "px");
187
- }, [rootId, path]);
188
- return /* @__PURE__ */ jsxs(Popover, {
189
- open,
190
- onOpenChange,
191
- children: [/* @__PURE__ */ jsx(PopoverTrigger, {
192
- className: cn(typeVariants({ variant: "trigger" })),
193
- children
194
- }), /* @__PURE__ */ jsx(PopoverContent, {
195
- ref: popoverRef,
196
- className: "w-[600px] max-w-(--available-width) min-h-(--min-height,200px) fd-scroll-container max-h-[460px] px-3 pt-0",
197
- onScrollEnd: (e) => {
198
- scrollTops.set(getScrollKey(rootId, path), e.target.scrollTop);
199
- },
200
- children: /* @__PURE__ */ jsx(SchemaUIPopover, {})
201
- })]
40
+ function RootProperty({ id, name, $type, required, actions }) {
41
+ const { refs, path, open, back } = useSchemaUI();
42
+ const [collapsed, setCollapsed] = useState(false);
43
+ function isExpanded(name, $ref) {
44
+ return !collapsed && path[1]?.name === name && path[1].$ref === $ref;
45
+ }
46
+ return /* @__PURE__ */ jsx(TypeContext, {
47
+ value: {
48
+ isExpanded,
49
+ open(name, $ref) {
50
+ if (isExpanded(name, $ref)) return setCollapsed(true);
51
+ setCollapsed(false);
52
+ back(0);
53
+ open(name, $ref);
54
+ }
55
+ },
56
+ children: /* @__PURE__ */ jsx(ObjectProperty, {
57
+ id,
58
+ name,
59
+ $type,
60
+ required,
61
+ parentIndex: 0,
62
+ actions,
63
+ children: refs[$type].type !== "primitive" && /* @__PURE__ */ jsx(Collapsible, {
64
+ open: !collapsed && path.length > 1,
65
+ children: /* @__PURE__ */ jsx(CollapsibleContent, {
66
+ className: "flow-root overflow-clip",
67
+ children: /* @__PURE__ */ jsx(SchemaCard, {
68
+ from: 1,
69
+ className: "mt-2.5"
70
+ })
71
+ })
72
+ })
73
+ })
202
74
  });
203
75
  }
204
- /** scroll positions of opened schemas, restored when navigating back */
205
- const scrollTops = /* @__PURE__ */ new Map();
206
- function getScrollKey(rootId, path) {
207
- const last = path.at(-1);
208
- return `${rootId}\0${path.length}\0${last.name}\0${last.$ref}`;
209
- }
210
- function PopoverTypeInfoTrigger({ pathName, $ref, children }) {
211
- const { open } = useSchemaUI();
212
- return /* @__PURE__ */ jsx("button", {
213
- className: cn(typeVariants({ variant: "trigger" })),
214
- onClick: () => open(pathName, $ref),
215
- children
76
+ const motionClassNames = {
77
+ forward: "motion-safe:starting:translate-x-6",
78
+ back: "motion-safe:starting:-translate-x-6"
79
+ };
80
+ /** the last schema of the path, with the path from `from` as breadcrumbs */
81
+ function SchemaCard({ from = 0, id, selector, actions, className }) {
82
+ const t = useTranslations({ note: "schema UI" });
83
+ const { path, open, back } = useSchemaUI();
84
+ const index = path.length - 1;
85
+ const { name, $ref, unions, schema } = path[index];
86
+ const panelKey = `${index}\0${name}\0${$ref}`;
87
+ const [filter, setFilter] = useState({
88
+ key: panelKey,
89
+ value: ""
216
90
  });
217
- }
218
- function SchemaDescription({ schema, ...props }) {
219
- return /* @__PURE__ */ jsxs("div", {
220
- ...props,
221
- className: cn("prose-no-margin py-2 empty:hidden", props.className),
222
- children: [schema.description, schema.infoTags && schema.infoTags.length > 0 && /* @__PURE__ */ jsx("div", {
223
- className: "flex flex-row gap-2 flex-wrap mt-2 empty:hidden",
224
- children: schema.infoTags.map((tag, i) => /* @__PURE__ */ jsx(Fragment, { children: "node" in tag ? tag.node : "list" in tag ? /* @__PURE__ */ jsx(BlockTag, {
225
- label: tag.label,
226
- children: /* @__PURE__ */ jsx("ul", { children: tag.list.map((item, i) => /* @__PURE__ */ jsx("li", {
227
- className: "font-mono list-disc list-inside ps-1 marker:text-fd-muted-foreground",
228
- children: item
229
- }, i)) })
230
- }) : tag.block ? /* @__PURE__ */ jsx(BlockTag, {
231
- label: tag.label,
232
- children: tag.value
233
- }) : /* @__PURE__ */ jsx(InlineTag, {
234
- label: tag.label,
235
- prose: tag.prose,
236
- children: tag.value
237
- }) }, i))
238
- })]
91
+ const search = filter.key === panelKey ? filter.value : "";
92
+ const query = search.trim().toLowerCase();
93
+ const [motion, setMotion] = useState();
94
+ const headerRef = useRef(null);
95
+ const filterable = schema.type === "object" && schema.props.length > 0;
96
+ const described = from > 0 && index === from && $ref === path[0].$ref;
97
+ let content = null;
98
+ function navigate(direction, update) {
99
+ flushSync(() => {
100
+ setMotion(direction);
101
+ update();
102
+ });
103
+ headerRef.current?.scrollIntoView({
104
+ behavior: "smooth",
105
+ block: "nearest"
106
+ });
107
+ }
108
+ if (schema.type === "object") {
109
+ const props = [];
110
+ for (const prop of schema.props) {
111
+ if (query && !prop.name.toLowerCase().includes(query)) continue;
112
+ props.push(/* @__PURE__ */ jsx(ObjectProperty, {
113
+ name: prop.name,
114
+ $type: prop.$type,
115
+ required: prop.required,
116
+ parentIndex: index
117
+ }, prop.name));
118
+ }
119
+ content = props;
120
+ if (props.length === 0 && query) content = /* @__PURE__ */ jsxs("div", {
121
+ className: "not-prose my-3 flex flex-col items-center gap-3 rounded-lg border border-dashed px-4 py-6 text-center text-fd-muted-foreground",
122
+ children: [/* @__PURE__ */ jsx(FilterXIcon, { className: "box-content size-4 rounded-lg border bg-fd-secondary p-2" }), /* @__PURE__ */ jsxs("p", { children: [
123
+ t("No property matching"),
124
+ " ",
125
+ /* @__PURE__ */ jsx("span", {
126
+ className: "font-medium break-all text-fd-foreground",
127
+ children: `"${search}"`
128
+ })
129
+ ] })]
130
+ });
131
+ } else if (schema.type === "array") content = /* @__PURE__ */ jsx(ObjectProperty, {
132
+ name: "[index: integer]",
133
+ $type: schema.item.$type,
134
+ parentIndex: index
239
135
  });
240
- }
241
- function ObjectProperty({ name, $type, required, parentPathIndex, ...props }) {
242
- const t = useTranslations({ note: "schema UI" });
243
- const { generated: { refs } } = useSchemaUI();
244
- const schema = refs[$type];
245
- const [highlighted, ref] = useSchemaHighlight(parentPathIndex, name);
246
- const [isChecked, onClick] = useCopySchemaLink(name);
247
136
  return /* @__PURE__ */ jsxs("div", {
248
- ...props,
249
- ref: mergeRefs(props.ref, ref),
250
- className: cn("text-sm border-t py-4 scroll-m-20 first:border-t-0", props.className),
137
+ id,
138
+ className: cn("@container overflow-clip rounded-xl border bg-fd-card text-fd-card-foreground", scrollMarginClassName, className),
251
139
  children: [/* @__PURE__ */ jsxs("div", {
252
- className: "flex flex-wrap items-center gap-2 not-prose",
140
+ ref: headerRef,
141
+ className: cn("not-prose flex flex-wrap border-b", scrollMarginClassName),
253
142
  children: [
254
- /* @__PURE__ */ jsxs("span", {
255
- className: "font-medium font-mono",
256
- children: [/* @__PURE__ */ jsx("span", {
257
- className: cn(highlighted ? "bg-fd-primary text-fd-primary-foreground rounded-sm" : "text-fd-primary", schema.deprecated && "line-through opacity-80"),
258
- children: name
259
- }), required ? /* @__PURE__ */ jsx("span", {
260
- className: "text-red-400",
261
- children: "*"
262
- }) : /* @__PURE__ */ jsx("span", {
263
- className: "text-fd-muted-foreground",
264
- children: "?"
265
- })]
143
+ selector && /* @__PURE__ */ jsx("div", {
144
+ className: "flex h-9 basis-full items-center border-b ps-1.5 pe-1 text-[0.8125rem]",
145
+ children: selector
266
146
  }),
267
- schema.type === "primitive" ? /* @__PURE__ */ jsx("span", {
268
- className: cn(typeVariants()),
269
- children: schema.aliasName
270
- }) : /* @__PURE__ */ jsx(TypeInfoTrigger, {
271
- pathName: name,
272
- $ref: $type,
273
- children: schema.aliasName
147
+ /* @__PURE__ */ jsx(Breadcrumbs, {
148
+ from,
149
+ unions,
150
+ onBack: (index) => navigate("back", () => back(index))
274
151
  }),
275
- /* @__PURE__ */ jsx("div", { className: "flex-1" }),
276
- schema.deprecated && /* @__PURE__ */ jsx("span", {
277
- className: "text-xs font-mono text-yellow-600 dark:text-yellow-400",
278
- children: t("Deprecated")
279
- }),
280
- /* @__PURE__ */ jsxs("button", {
281
- "aria-live": "polite",
282
- className: cn(buttonVariants({
283
- size: "icon-xs",
284
- variant: "ghost"
285
- }), "text-fd-muted-foreground [&_svg]:size-3.5"),
286
- onClick,
287
- children: [isChecked ? /* @__PURE__ */ jsx(CheckIcon, {}) : /* @__PURE__ */ jsx(LinkIcon, {}), /* @__PURE__ */ jsx("span", {
288
- className: "sr-only",
289
- children: isChecked ? t("Copied Link", { note: "aria-label" }) : t("Copy Link", { note: "aria-label" })
290
- })]
152
+ (filterable || actions) && /* @__PURE__ */ jsxs("div", {
153
+ className: cn("flex h-9 items-center gap-1 pe-1", filterable && "@max-xl:basis-full @max-xl:border-t @max-xl:ps-1.5"),
154
+ children: [filterable && /* @__PURE__ */ jsxs("label", {
155
+ className: "flex h-7 cursor-text items-center gap-1.5 rounded-md border bg-fd-secondary px-2 text-fd-muted-foreground transition-shadow focus-within:ring-1 focus-within:ring-fd-ring @max-xl:flex-1 @xl:w-40",
156
+ children: [/* @__PURE__ */ jsx(FilterIcon, { className: "size-3.5 shrink-0" }), /* @__PURE__ */ jsx("input", {
157
+ value: search,
158
+ onChange: (e) => setFilter({
159
+ key: panelKey,
160
+ value: e.target.value
161
+ }),
162
+ "aria-label": t("Filter Properties"),
163
+ placeholder: t("Filter Properties"),
164
+ className: "min-w-0 flex-1 bg-transparent text-[0.8125rem] text-fd-foreground outline-none placeholder:text-fd-muted-foreground"
165
+ })]
166
+ }), actions]
291
167
  })
292
168
  ]
293
- }), /* @__PURE__ */ jsx(SchemaDescription, {
294
- schema,
295
- className: "pb-0"
169
+ }), /* @__PURE__ */ jsx(TypeContext, {
170
+ value: { open: (name, $ref) => navigate("forward", () => open(name, $ref)) },
171
+ children: /* @__PURE__ */ jsxs("div", {
172
+ className: cn("px-3", motion && "starting:opacity-0 motion-safe:transition-[opacity,translate] motion-safe:duration-300 motion-safe:ease-[cubic-bezier(0.16,1,0.3,1)]", motion && motionClassNames[motion]),
173
+ children: [!described && /* @__PURE__ */ jsx(SchemaDescription, {
174
+ schema,
175
+ className: "py-2.5"
176
+ }), content]
177
+ }, panelKey)
296
178
  })]
297
179
  });
298
180
  }
299
- function PathItemBody({ pathIndex, asSchema, tabDepth = 0, objectSearchOverrides }) {
300
- const { path, generated: { refs } } = useSchemaUI();
301
- const [selected, setSelected] = useSchemaTabs(pathIndex, tabDepth);
302
- const schema = asSchema ?? refs[path[pathIndex].$ref];
303
- if ((schema.type === "or" || schema.type === "and") && schema.items.length > 0) {
304
- const value = selected ?? schema.items[0].$type;
305
- const items = schema.items.map((item) => ({
306
- label: /* @__PURE__ */ jsx("code", {
307
- className: "text-xs font-medium",
308
- children: item.name
309
- }),
310
- value: item.$type
311
- }));
312
- return /* @__PURE__ */ jsxs(Select, {
313
- items,
314
- value,
315
- onValueChange: (v) => v && setSelected(v),
316
- children: [/* @__PURE__ */ jsxs("div", {
317
- className: "flex flex-row my-2 gap-2 items-center",
318
- children: [
319
- /* @__PURE__ */ jsx(SchemaDescription, {
320
- schema,
321
- className: "flex-1 py-0"
322
- }),
323
- /* @__PURE__ */ jsx(SelectTrigger, {
324
- className: "not-prose w-fit min-w-0 mb-auto *:min-w-0",
325
- children: /* @__PURE__ */ jsx(SelectValue, {})
326
- }),
327
- /* @__PURE__ */ jsx(SelectContent, { children: items.map(({ label, value }) => /* @__PURE__ */ jsx(SelectItem, {
328
- value,
329
- children: label
330
- }, value)) })
331
- ]
332
- }), /* @__PURE__ */ jsx(PathItemBody, {
333
- asSchema: refs[value],
334
- pathIndex,
335
- tabDepth: tabDepth + 1
181
+ /**
182
+ * The path from `from`, the crumbs between the first and last ones collapse into a menu when they don't fit.
183
+ */
184
+ function Breadcrumbs({ from, unions, onBack }) {
185
+ const t = useTranslations({ note: "schema UI" });
186
+ const { path } = useSchemaUI();
187
+ const last = path.length - 1;
188
+ const [collapsed, setCollapsed] = useState(0);
189
+ const measure = useCallback((copy) => {
190
+ if (!copy) return;
191
+ const element = copy.parentElement;
192
+ const after = copy.previousElementSibling;
193
+ const update = () => setCollapsed(countCollapsed(copy, element.clientWidth - after.getBoundingClientRect().width));
194
+ const observer = new ResizeObserver(update);
195
+ update();
196
+ observer.observe(element);
197
+ observer.observe(after);
198
+ return () => observer.disconnect();
199
+ }, []);
200
+ const hidden = Math.min(collapsed, Math.max(0, last - from - 1));
201
+ const crumbs = [];
202
+ const menuItems = [];
203
+ const copy = [];
204
+ let names = "";
205
+ for (let i = from; i <= last; i++) {
206
+ const name = formatName(path[i].name);
207
+ const chevron = i > from && /* @__PURE__ */ jsx(ChevronRightIcon, { className: chevronClassName });
208
+ names += `${name}\0`;
209
+ copy.push(/* @__PURE__ */ jsxs("li", {
210
+ className: "flex items-center gap-0.5",
211
+ children: [chevron, /* @__PURE__ */ jsx("span", {
212
+ className: cn("px-1.5 py-1", i === last && "font-medium"),
213
+ children: name
336
214
  })]
337
- });
215
+ }, i));
216
+ if (i > from && i <= from + hidden) {
217
+ menuItems.push(/* @__PURE__ */ jsx(Menu.Item, {
218
+ onClick: () => onBack(i),
219
+ className: "truncate rounded-md px-2 py-1.5 outline-none data-highlighted:bg-fd-accent data-highlighted:text-fd-accent-foreground",
220
+ children: name
221
+ }, i));
222
+ continue;
223
+ }
224
+ crumbs.push(/* @__PURE__ */ jsxs("li", {
225
+ className: cn("flex items-center gap-0.5 overflow-hidden", i > from && "motion-safe:transition-opacity motion-safe:starting:opacity-0"),
226
+ children: [chevron, i < last ? /* @__PURE__ */ jsx("button", {
227
+ type: "button",
228
+ onClick: () => onBack(i),
229
+ className: crumbClassName,
230
+ children: name
231
+ }) : /* @__PURE__ */ jsx("span", {
232
+ "aria-current": "page",
233
+ className: "truncate px-1.5 py-1 font-medium",
234
+ children: name
235
+ })]
236
+ }, i));
338
237
  }
339
- if (schema.type === "object" && schema.props.length > 0) return /* @__PURE__ */ jsx(ObjectSearch, {
340
- pathIndex,
341
- schema,
342
- ...objectSearchOverrides,
343
- children: /* @__PURE__ */ jsx(SchemaDescription, { schema })
238
+ if (menuItems.length > 0) crumbs.splice(1, 0, /* @__PURE__ */ jsxs("li", {
239
+ className: "flex items-center gap-0.5",
240
+ children: [/* @__PURE__ */ jsx(ChevronRightIcon, { className: chevronClassName }), /* @__PURE__ */ jsxs(Menu.Root, { children: [/* @__PURE__ */ jsx(Menu.Trigger, {
241
+ "aria-label": t("Show Path", { note: "aria-label" }),
242
+ className: crumbClassName,
243
+ children: "…"
244
+ }), /* @__PURE__ */ jsx(Menu.Portal, { children: /* @__PURE__ */ jsx(Menu.Positioner, {
245
+ align: "start",
246
+ sideOffset: 4,
247
+ className: "z-50",
248
+ children: /* @__PURE__ */ jsx(Menu.Popup, {
249
+ className: "flex max-h-(--available-height) max-w-72 min-w-40 origin-(--transform-origin) flex-col overflow-y-auto rounded-xl border bg-fd-popover p-1 font-mono text-[0.8125rem] text-fd-popover-foreground shadow-lg outline-none transition-[opacity,scale] duration-150 ease-[cubic-bezier(0.22,1,0.36,1)] data-ending-style:scale-[0.97] data-ending-style:opacity-0 data-starting-style:scale-95 data-starting-style:opacity-0 motion-reduce:transition-none",
250
+ children: menuItems
251
+ })
252
+ }) })] })]
253
+ }, "menu"));
254
+ return /* @__PURE__ */ jsxs("div", {
255
+ className: "relative ms-1.5 me-1 flex h-9 min-w-0 flex-1 items-center font-mono text-[0.8125rem]",
256
+ children: [
257
+ /* @__PURE__ */ jsx("ol", {
258
+ className: "grid min-w-0 auto-cols-[minmax(0,max-content)] grid-flow-col items-center",
259
+ children: crumbs
260
+ }),
261
+ /* @__PURE__ */ jsx("div", {
262
+ className: "flex min-w-0 items-center gap-0.5",
263
+ children: unions.map((union, depth) => /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsx(ChevronRightIcon, { className: chevronClassName }), /* @__PURE__ */ jsx(UnionSelect, { union })] }, depth))
264
+ }),
265
+ /* @__PURE__ */ jsxs("ol", {
266
+ ref: measure,
267
+ "aria-hidden": true,
268
+ className: "invisible absolute top-0 flex w-max",
269
+ children: [copy, /* @__PURE__ */ jsxs("li", {
270
+ className: "flex items-center gap-0.5",
271
+ children: [/* @__PURE__ */ jsx(ChevronRightIcon, { className: chevronClassName }), /* @__PURE__ */ jsx("span", {
272
+ className: "px-1.5 py-1",
273
+ children: "…"
274
+ })]
275
+ })]
276
+ }, names)
277
+ ]
344
278
  });
345
- if (schema.type === "array") return /* @__PURE__ */ jsxs(Fragment$1, { children: [/* @__PURE__ */ jsx(SchemaDescription, { schema }), /* @__PURE__ */ jsx(ObjectProperty, {
346
- name: "[index: integer]",
347
- $type: schema.item.$type,
348
- parentPathIndex: pathIndex
349
- })] });
350
- return /* @__PURE__ */ jsx(SchemaDescription, { schema });
351
279
  }
352
- function ObjectSearch({ variant = "default", schema, pathIndex, children }) {
353
- const { open } = useSchemaUI();
354
- const [search, setSearch] = useState("");
355
- const deferredValue = useDeferredValue(search);
356
- const firstItemRef = useRef(null);
357
- const t = useTranslations({ note: "schema UI" });
358
- return /* @__PURE__ */ jsxs(Fragment$1, { children: [
359
- /* @__PURE__ */ jsxs("div", {
360
- className: cn("flex items-center bg-fd-secondary text-fd-secondary-foreground transition-colors", variant === "in-popover" && "sticky top-10 -mx-3 ps-3 border-b focus-within:[&_svg]:text-fd-primary", variant === "default" && "border rounded-md ps-2 shadow-sm focus-within:ring-2 focus-within:ring-fd-ring"),
361
- children: [/* @__PURE__ */ jsx(FilterIcon, { className: "text-fd-muted-foreground size-3.5 transition-colors" }), /* @__PURE__ */ jsx("input", {
362
- value: search,
363
- "data-object-search-input": "",
364
- onChange: (e) => setSearch(e.target.value),
365
- placeholder: t("Filter Properties"),
366
- className: "text-sm ps-2 py-2 flex-1 outline-none placeholder:text-fd-muted-foreground",
367
- onKeyDown: (e) => {
368
- if (e.key === "Enter") {
369
- const item = firstItemRef.current;
370
- if (item) open(item.name, item.$type);
371
- e.preventDefault();
372
- }
373
- }
374
- })]
375
- }),
376
- children,
377
- /* @__PURE__ */ jsx(Suspense, { children: /* @__PURE__ */ jsx(ObjectSearchContent, {
378
- search: deferredValue,
379
- properties: schema.props,
380
- firstItemRef,
381
- empty: () => /* @__PURE__ */ jsxs("p", {
382
- className: "text-fd-muted-foreground text-sm my-2!",
383
- children: [
384
- t("No property matching"),
385
- " ",
386
- /* @__PURE__ */ jsx("span", {
387
- className: "text-fd-foreground font-medium",
388
- children: `"${deferredValue}"`
389
- })
390
- ]
391
- }),
392
- render: (item) => /* @__PURE__ */ jsx(ObjectProperty, {
393
- name: item.name,
394
- $type: item.$type,
395
- required: item.required,
396
- parentPathIndex: pathIndex
397
- }, item.name)
398
- }) })
399
- ] });
280
+ /** the number of crumbs after the first one to collapse, measured from the hidden copy of all crumbs */
281
+ function countCollapsed(copy, available) {
282
+ const widths = Array.from(copy.children, (child) => child.getBoundingClientRect().width);
283
+ const menu = widths.pop();
284
+ let total = 0;
285
+ for (const width of widths) total += width;
286
+ if (total <= available) return 0;
287
+ let count = 0;
288
+ total += menu;
289
+ while (count < widths.length - 2 && total > available) total -= widths[++count];
290
+ return count;
400
291
  }
401
- function ObjectSearchContent({ search: rawSearch, firstItemRef, properties, empty, render }) {
402
- const filtered = useMemo(() => {
403
- const search = rawSearch.trim().toLowerCase();
404
- return search.length > 0 ? properties.filter((prop) => prop.name.toLowerCase().includes(search)) : properties;
405
- }, [properties, rawSearch]);
406
- firstItemRef.current = filtered[0] ?? null;
407
- if (filtered.length === 0) return empty();
408
- return filtered.map(render);
292
+ /** shorten the names of items and additional properties, like `[index]` */
293
+ function formatName(name) {
294
+ const match = /^\[(\w+): \w+]$/.exec(name);
295
+ return match ? `[${match[1]}]` : name;
409
296
  }
410
- function InlineTag({ label, prose = false, children }) {
411
- if (prose) return /* @__PURE__ */ jsxs("div", {
412
- className: "inline-flex gap-2 bg-fd-secondary border rounded-lg text-xs p-1.5 shadow-md max-w-full",
413
- children: [/* @__PURE__ */ jsx("span", {
414
- className: "font-medium not-prose",
415
- children: label
416
- }), /* @__PURE__ */ jsx("span", {
417
- className: "min-w-0 flex-1 text-fd-muted-foreground prose-sm prose-no-margin",
418
- children
419
- })]
420
- });
421
- return /* @__PURE__ */ jsxs("div", {
422
- className: "inline-flex gap-2 bg-fd-secondary border rounded-lg text-xs p-1.5 shadow-md max-w-full not-prose",
423
- children: [/* @__PURE__ */ jsx("span", {
424
- className: "font-medium",
425
- children: label
426
- }), /* @__PURE__ */ jsx("code", {
427
- className: "min-w-0 flex-1 text-fd-muted-foreground wrap-break-word",
428
- children
429
- })]
297
+ function UnionSelect({ union: { schema, value, select } }) {
298
+ const items = schema.items.map((item) => ({
299
+ label: item.name,
300
+ value: item.$type
301
+ }));
302
+ return /* @__PURE__ */ jsxs(Select, {
303
+ items,
304
+ value,
305
+ onValueChange: (v) => v && select(v),
306
+ children: [/* @__PURE__ */ jsx(SelectTrigger, {
307
+ className: "h-7 w-auto min-w-0 gap-1 border-0 bg-transparent px-1.5 py-0 text-[0.8125rem] font-medium text-fd-foreground hover:bg-fd-accent focus:ring-0 focus-visible:ring-2 focus-visible:ring-inset data-popup-open:bg-fd-accent",
308
+ children: /* @__PURE__ */ jsx(SelectValue, { className: "truncate" })
309
+ }), /* @__PURE__ */ jsx(SelectContent, { children: items.map((item) => /* @__PURE__ */ jsx(SelectItem, {
310
+ value: item.value,
311
+ className: "font-mono text-[0.8125rem]",
312
+ children: item.label
313
+ }, item.value)) })]
430
314
  });
431
315
  }
432
- function BlockTag({ label, children }) {
316
+ function ObjectProperty({ id, name, $type, required = false, parentIndex, actions, children }) {
317
+ const t = useTranslations({ note: "schema UI" });
318
+ const { refs, path, highlighted, getLink } = useSchemaUI();
319
+ const schema = refs[$type];
320
+ const isHighlighted = highlighted === name && parentIndex === path.length - 1;
321
+ const [isChecked, onCopy] = useCopyButton(() => navigator.clipboard.writeText(getLink(parentIndex, name)));
322
+ const ref = useCallback((element) => {
323
+ if (!element || !isHighlighted) return;
324
+ const timer = window.setTimeout(() => {
325
+ element.scrollIntoView({
326
+ behavior: "smooth",
327
+ block: "center"
328
+ });
329
+ }, 300);
330
+ return () => window.clearTimeout(timer);
331
+ }, [isHighlighted]);
433
332
  return /* @__PURE__ */ jsxs("div", {
434
- className: "flex flex-col w-full gap-2 bg-fd-secondary border rounded-lg p-1.5 shadow-md not-prose",
435
- children: [/* @__PURE__ */ jsx("p", {
436
- className: "font-medium text-xs",
437
- children: label
438
- }), children]
439
- });
440
- }
441
- function SchemaUIPopover() {
442
- const { path, back } = useSchemaUI();
443
- const ref = useRef(null);
444
- useEffect(() => {
445
- ref.current?.querySelector("input[data-object-search-input]")?.focus({ preventScroll: true });
446
- }, [path]);
447
- return /* @__PURE__ */ jsx(TriggerContext, {
448
- value: renderPopoverTrigger,
449
- children: /* @__PURE__ */ jsxs("div", {
450
- ref,
451
- children: [/* @__PURE__ */ jsx("div", {
452
- className: "sticky top-0 -mx-3 flex overflow-x-auto overflow-y-hidden items-center text-sm font-medium font-mono bg-fd-secondary text-fd-secondary-foreground px-3 h-10 border-b z-20",
453
- children: path.map((item, i) => {
454
- if (i === 0) return;
455
- const isDuplicated = path.some((other, j) => j !== i && other.$ref === item.$ref);
456
- let text;
457
- const indexItemMatch = /^\[(\w+): (\w+)]$/.exec(item.name);
458
- if (indexItemMatch) text = `[${indexItemMatch[1]}]`;
459
- else if (i > 1) text = `.${item.name}`;
460
- else text = item.name;
461
- return /* @__PURE__ */ jsx("button", {
462
- onClick: () => back(i),
463
- className: cn("hover:underline hover:text-fd-accent-foreground", isDuplicated && "text-orange-400"),
464
- children: text
465
- }, i);
466
- })
467
- }), /* @__PURE__ */ jsx(PathItemBody, {
468
- pathIndex: path.length - 1,
469
- objectSearchOverrides: { variant: "in-popover" }
470
- })]
471
- })
333
+ id,
334
+ ref,
335
+ className: "group/property scroll-m-20 border-t py-2.5 first:border-t-0",
336
+ children: [
337
+ /* @__PURE__ */ jsxs("div", {
338
+ className: "not-prose flex flex-wrap items-center gap-x-2 gap-y-1",
339
+ children: [
340
+ /* @__PURE__ */ jsxs("code", {
341
+ className: "text-[0.8125rem] font-medium",
342
+ children: [/* @__PURE__ */ jsx("span", {
343
+ className: cn(isHighlighted ? "rounded-sm bg-fd-primary text-fd-primary-foreground" : "text-fd-primary", schema.deprecated && "line-through opacity-80"),
344
+ children: name
345
+ }), required ? /* @__PURE__ */ jsx("span", {
346
+ className: "text-red-400",
347
+ children: "*"
348
+ }) : /* @__PURE__ */ jsx("span", {
349
+ className: "text-fd-muted-foreground",
350
+ children: "?"
351
+ })]
352
+ }),
353
+ schema.type === "primitive" ? /* @__PURE__ */ jsx("span", {
354
+ className: typeClassName,
355
+ children: schema.aliasName
356
+ }) : /* @__PURE__ */ jsx(TypeInfo, {
357
+ name,
358
+ $ref: $type,
359
+ children: schema.aliasName
360
+ }),
361
+ schema.deprecated && /* @__PURE__ */ jsx("span", {
362
+ className: "font-mono text-xs text-yellow-600 dark:text-yellow-400",
363
+ children: t("Deprecated")
364
+ }),
365
+ /* @__PURE__ */ jsxs("button", {
366
+ type: "button",
367
+ "aria-live": "polite",
368
+ onClick: onCopy,
369
+ className: cn(buttonVariants({
370
+ size: "icon-xs",
371
+ variant: "ghost"
372
+ }), "-my-1 ms-auto text-fd-muted-foreground opacity-0 transition-opacity group-hover/property:opacity-100 focus-visible:opacity-100 pointer-coarse:opacity-100 [&_svg]:size-3.5"),
373
+ children: [isChecked ? /* @__PURE__ */ jsx(CheckIcon, {}) : /* @__PURE__ */ jsx(LinkIcon, {}), /* @__PURE__ */ jsx("span", {
374
+ className: "sr-only",
375
+ children: isChecked ? t("Copied Link", { note: "aria-label" }) : t("Copy Link", { note: "aria-label" })
376
+ })]
377
+ }),
378
+ actions
379
+ ]
380
+ }),
381
+ /* @__PURE__ */ jsx(SchemaDescription, {
382
+ schema,
383
+ className: "mt-1"
384
+ }),
385
+ children
386
+ ]
472
387
  });
473
388
  }
474
- function TypeInfoTrigger({ pathName, $ref, children }) {
475
- const { generated: { refs } } = useSchemaUI();
476
- const renderTrigger = use(TriggerContext);
389
+ function TypeInfo({ name, $ref, children }) {
390
+ const { refs } = useSchemaUI();
391
+ const { open, isExpanded } = use(TypeContext);
477
392
  const schema = refs[$ref];
478
- if (schema.type === "primitive" && !schema.description && (!schema.infoTags || schema.infoTags.length === 0)) return /* @__PURE__ */ jsx("span", {
479
- className: cn(typeVariants()),
393
+ if (schema.type === "primitive" && !schema.description && !schema.infoTags?.length) return /* @__PURE__ */ jsx("span", {
394
+ className: typeClassName,
480
395
  children
481
396
  });
482
- if (schema.type === "and" || schema.type === "or") {
483
- const sep = schema.type === "and" ? "&" : "|";
484
- return /* @__PURE__ */ jsx("span", {
485
- className: cn(typeVariants(), "flex flex-row gap-2 items-center flex-wrap"),
486
- children: schema.items.map((item, i) => /* @__PURE__ */ jsxs(Fragment, { children: [i > 0 && /* @__PURE__ */ jsx("span", { children: sep }), /* @__PURE__ */ jsx(TypeInfoTrigger, {
487
- pathName,
488
- $ref: item.$type,
489
- children: item.name
490
- })] }, item.$type))
491
- });
492
- }
397
+ if (schema.type === "and" || schema.type === "or") return /* @__PURE__ */ jsx("span", {
398
+ className: cn(typeClassName, "flex flex-wrap items-center gap-1.5"),
399
+ children: schema.items.map((item, i) => /* @__PURE__ */ jsxs(Fragment, { children: [i > 0 && (schema.type === "and" ? "&" : "|"), /* @__PURE__ */ jsx(TypeInfo, {
400
+ name,
401
+ $ref: item.$type,
402
+ children: item.name
403
+ })] }, item.$type))
404
+ });
493
405
  if (schema.type === "array") return /* @__PURE__ */ jsxs("span", {
494
- className: cn(typeVariants(), "flex flex-row items-center flex-wrap"),
406
+ className: cn(typeClassName, "flex flex-wrap items-center"),
495
407
  children: [
496
408
  "array<",
497
- /* @__PURE__ */ jsx(TypeInfoTrigger, {
498
- pathName: `${pathName}[]`,
409
+ /* @__PURE__ */ jsx(TypeInfo, {
410
+ name: `${name}[]`,
499
411
  $ref: schema.item.$type,
500
412
  children: refs[schema.item.$type].aliasName
501
413
  }),
502
414
  ">"
503
415
  ]
504
416
  });
505
- return renderTrigger({
506
- pathName,
507
- $ref,
417
+ return /* @__PURE__ */ jsx("button", {
418
+ type: "button",
419
+ "aria-expanded": isExpanded?.(name, $ref),
420
+ onClick: () => open(name, $ref),
421
+ className: cn(typeClassName, "text-start underline decoration-dotted underline-offset-4 transition-colors hover:text-fd-accent-foreground aria-expanded:text-fd-accent-foreground aria-expanded:decoration-solid"),
508
422
  children
509
423
  });
510
424
  }
425
+ function SchemaDescription({ schema: { description, infoTags = [] }, className }) {
426
+ if (!description && infoTags.length === 0) return null;
427
+ return /* @__PURE__ */ jsxs("div", {
428
+ className: cn("flex flex-col gap-2 text-fd-muted-foreground", className),
429
+ children: [description && /* @__PURE__ */ jsx("div", {
430
+ className: "prose-no-margin",
431
+ children: description
432
+ }), infoTags.length > 0 && /* @__PURE__ */ jsx("div", {
433
+ className: "flex flex-wrap gap-1.5",
434
+ children: infoTags.map((tag, i) => {
435
+ if ("node" in tag) return /* @__PURE__ */ jsx(Fragment, { children: tag.node }, i);
436
+ if ("list" in tag) return /* @__PURE__ */ jsxs("div", {
437
+ className: "not-prose flex w-full flex-wrap items-center gap-1.5 text-xs",
438
+ children: [tag.label, tag.list.map((item, j) => /* @__PURE__ */ jsx("code", {
439
+ className: tagClassName,
440
+ children: item
441
+ }, j))]
442
+ }, i);
443
+ if (tag.block) return /* @__PURE__ */ jsxs("div", {
444
+ className: "not-prose flex w-full flex-col gap-1.5 text-xs",
445
+ children: [tag.label, tag.value]
446
+ }, i);
447
+ return /* @__PURE__ */ jsxs("div", {
448
+ className: cn(tagClassName, "inline-flex max-w-full items-baseline gap-1.5"),
449
+ children: [/* @__PURE__ */ jsx("span", {
450
+ className: "not-prose shrink-0 text-fd-muted-foreground",
451
+ children: tag.label
452
+ }), tag.prose ? /* @__PURE__ */ jsx("div", {
453
+ className: "prose-sm prose-no-margin min-w-0 flex-1",
454
+ children: tag.value
455
+ }) : /* @__PURE__ */ jsx("code", {
456
+ className: "not-prose min-w-0 wrap-break-word",
457
+ children: tag.value
458
+ })]
459
+ }, i);
460
+ })
461
+ })]
462
+ });
463
+ }
511
464
  //#endregion
512
- export { BlockTag, InlineTag, SchemaUI, SchemaUIProvider, useCopySchemaLink, useSchemaHighlight, useSchemaPopover, useSchemaTabs, useSchemaUI };
465
+ export { SchemaUI };