@stainless-api/docs-ui 0.1.0-beta.9 → 0.1.0-beta.90

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 (162) hide show
  1. package/dist/breadcrumbs-DC2_z7nh.d.ts +25 -0
  2. package/dist/chunk-pbuEa-1d.js +13 -0
  3. package/dist/component-generics-DYxBWv1v.js +42 -0
  4. package/dist/component-generics-q-ujwX4P.d.ts +30 -0
  5. package/dist/components/MaskedInput.d.ts +14 -0
  6. package/dist/components/MaskedInput.js +74 -0
  7. package/dist/components/breadcrumbs.d.ts +2 -0
  8. package/dist/components/breadcrumbs.js +62 -0
  9. package/dist/components/icons.d.ts +72 -0
  10. package/dist/components/icons.js +953 -0
  11. package/dist/components/index.d.ts +10 -0
  12. package/dist/components/index.js +73 -0
  13. package/dist/components/method.d.ts +2 -0
  14. package/dist/components/method.js +95 -0
  15. package/dist/components/overview.d.ts +2 -0
  16. package/dist/components/overview.js +140 -0
  17. package/dist/components/primitives.d.ts +2 -0
  18. package/dist/components/primitives.js +251 -0
  19. package/dist/components/properties.d.ts +2 -0
  20. package/dist/components/properties.js +144 -0
  21. package/dist/components/sdk-sidebar.d.ts +2 -0
  22. package/dist/components/sdk-sidebar.js +50 -0
  23. package/dist/components/sdk.d.ts +2 -0
  24. package/dist/components/sdk.js +488 -0
  25. package/dist/components/snippets.d.ts +2 -0
  26. package/dist/components/snippets.js +184 -0
  27. package/dist/components/stl-sidebar.d.ts +2 -0
  28. package/dist/components/stl-sidebar.js +112 -0
  29. package/dist/contexts/component-generics.d.ts +2 -0
  30. package/dist/contexts/component-generics.js +2 -0
  31. package/dist/contexts/component-types.d.ts +6 -0
  32. package/dist/contexts/component-types.js +0 -0
  33. package/dist/contexts/component.d.ts +20 -0
  34. package/dist/contexts/component.js +21 -0
  35. package/dist/contexts/docs.d.ts +2 -0
  36. package/dist/contexts/docs.js +89 -0
  37. package/dist/contexts/index.d.ts +6 -0
  38. package/dist/contexts/index.js +43 -0
  39. package/dist/contexts/markdown.d.ts +2 -0
  40. package/dist/contexts/markdown.js +62 -0
  41. package/dist/contexts/navigation.d.ts +2 -0
  42. package/dist/contexts/navigation.js +21 -0
  43. package/dist/contexts/use-components.d.ts +5 -0
  44. package/dist/contexts/use-components.js +5 -0
  45. package/dist/docs-DSjdvCh5.d.ts +62 -0
  46. package/dist/index-DFr9Mesr.d.ts +1776 -0
  47. package/dist/index-gGDaf6FK.d.ts +464 -0
  48. package/dist/index.d.ts +5 -0
  49. package/dist/index.js +8 -7542
  50. package/dist/languages/cli.d.ts +2 -0
  51. package/dist/languages/cli.js +165 -0
  52. package/dist/languages/csharp.d.ts +2 -0
  53. package/dist/languages/csharp.js +282 -0
  54. package/dist/languages/go.d.ts +2 -0
  55. package/dist/languages/go.js +302 -0
  56. package/dist/languages/http.d.ts +2 -0
  57. package/dist/languages/http.js +385 -0
  58. package/dist/languages/index.d.ts +2 -0
  59. package/dist/languages/index.js +31 -0
  60. package/dist/languages/java.d.ts +2 -0
  61. package/dist/languages/java.js +402 -0
  62. package/dist/languages/php.d.ts +2 -0
  63. package/dist/languages/php.js +311 -0
  64. package/dist/languages/python.d.ts +2 -0
  65. package/dist/languages/python.js +268 -0
  66. package/dist/languages/ruby.d.ts +2 -0
  67. package/dist/languages/ruby.js +386 -0
  68. package/dist/languages/terraform.d.ts +2 -0
  69. package/dist/languages/terraform.js +259 -0
  70. package/dist/languages/typescript.d.ts +2 -0
  71. package/dist/languages/typescript.js +448 -0
  72. package/dist/markdown/index.d.ts +11 -0
  73. package/dist/markdown/index.js +87 -0
  74. package/dist/markdown/md.d.ts +15 -0
  75. package/dist/markdown/md.js +39 -0
  76. package/dist/markdown/printer.d.ts +13 -0
  77. package/dist/markdown/printer.js +74 -0
  78. package/dist/markdown/utils.d.ts +28 -0
  79. package/dist/markdown/utils.js +31 -0
  80. package/dist/markdown-Cwdboi_V.d.ts +33 -0
  81. package/dist/method-CNRMOUfn.d.ts +59 -0
  82. package/dist/navigation-C9ckAfpV.d.ts +29 -0
  83. package/dist/overview-CDvh184n.d.ts +39 -0
  84. package/dist/primitives-DFE_4MvV.d.ts +107 -0
  85. package/dist/properties-CVwrzWVa.d.ts +54 -0
  86. package/dist/routing-yTSG7nWM.d.ts +61 -0
  87. package/dist/routing.d.ts +2 -0
  88. package/dist/routing.js +174 -150
  89. package/dist/sdk-BkW3MDco.d.ts +128 -0
  90. package/dist/sdk-sidebar-BpDlOMat.d.ts +15 -0
  91. package/dist/spec.d.ts +65 -0
  92. package/dist/spec.js +165 -0
  93. package/dist/stl-sidebar-CKrR5ZGs.d.ts +58 -0
  94. package/dist/style.d.ts +178 -0
  95. package/dist/style.js +178 -0
  96. package/dist/styles/main.css +765 -755
  97. package/dist/styles/primitives.css +359 -442
  98. package/dist/styles/resets.css +25 -41
  99. package/dist/styles/search.css +187 -270
  100. package/dist/styles/sidebar.css +155 -69
  101. package/dist/styles/variables.css +23 -89
  102. package/dist/styles.css +3171 -0
  103. package/dist/utils.d.ts +20 -0
  104. package/dist/utils.js +52 -0
  105. package/package.json +75 -22
  106. package/dist/mcp.js +0 -16003
  107. package/dist/styles/snippets.css +0 -132
  108. package/src/components/breadcrumbs.tsx +0 -94
  109. package/src/components/chat.tsx +0 -154
  110. package/src/components/dropdown.tsx +0 -91
  111. package/src/components/icons.tsx +0 -584
  112. package/src/components/index.ts +0 -9
  113. package/src/components/method.tsx +0 -146
  114. package/src/components/overview.tsx +0 -160
  115. package/src/components/primitives.tsx +0 -284
  116. package/src/components/properties.tsx +0 -130
  117. package/src/components/scripts/dropdown.ts +0 -78
  118. package/src/components/sdk.tsx +0 -403
  119. package/src/components/sidebar.tsx +0 -99
  120. package/src/components/snippets.tsx +0 -180
  121. package/src/contexts/component-generics.tsx +0 -48
  122. package/src/contexts/component-types.tsx +0 -5
  123. package/src/contexts/component.tsx +0 -32
  124. package/src/contexts/docs.tsx +0 -75
  125. package/src/contexts/index.tsx +0 -17
  126. package/src/contexts/markdown.tsx +0 -43
  127. package/src/contexts/navigation.tsx +0 -44
  128. package/src/contexts/search.tsx +0 -28
  129. package/src/contexts/use-components.tsx +0 -4
  130. package/src/index.ts +0 -6
  131. package/src/languages/go.tsx +0 -281
  132. package/src/languages/http.tsx +0 -321
  133. package/src/languages/index.ts +0 -26
  134. package/src/languages/java.tsx +0 -358
  135. package/src/languages/python.tsx +0 -255
  136. package/src/languages/ruby.tsx +0 -320
  137. package/src/languages/typescript.tsx +0 -393
  138. package/src/markdown/index.ts +0 -107
  139. package/src/markdown/md.ts +0 -45
  140. package/src/markdown/utils.ts +0 -49
  141. package/src/routing.ts +0 -238
  142. package/src/search/form.tsx +0 -127
  143. package/src/search/index.tsx +0 -1
  144. package/src/search/indexer.ts +0 -246
  145. package/src/search/mcp.ts +0 -61
  146. package/src/search/printer.tsx +0 -86
  147. package/src/search/providers/algolia.ts +0 -88
  148. package/src/search/providers/fuse.ts +0 -19
  149. package/src/search/providers/pagefind.ts +0 -17
  150. package/src/search/providers/walker.ts +0 -30
  151. package/src/search/results.tsx +0 -182
  152. package/src/search/state.ts +0 -64
  153. package/src/search/types.ts +0 -116
  154. package/src/style.ts +0 -196
  155. package/src/styles/main.css +0 -986
  156. package/src/styles/primitives.css +0 -580
  157. package/src/styles/resets.css +0 -39
  158. package/src/styles/search.css +0 -341
  159. package/src/styles/sidebar.css +0 -85
  160. package/src/styles/snippets.css +0 -130
  161. package/src/styles/variables.css +0 -86
  162. package/src/utils.ts +0 -41
@@ -1,146 +0,0 @@
1
- import * as React from 'react';
2
- import type { ReactNode } from 'react';
3
- import { useComponents } from '../contexts/use-components';
4
- import { X, ArrowDownLeft, ArrowUpRight, LucideIcon } from 'lucide-react';
5
- import style from '../style';
6
- import clsx from 'clsx';
7
- import { PropertyToggle } from './properties';
8
-
9
- export const HttpMethods = ['get', 'head', 'put', 'delete', 'post', 'patch'];
10
- export type HTTPMethod = (typeof HttpMethods)[number];
11
-
12
- export const HttpMethodIcons: Record<HTTPMethod, LucideIcon> = {
13
- get: ArrowDownLeft,
14
- put: ArrowUpRight,
15
- post: ArrowUpRight,
16
- patch: ArrowUpRight,
17
- delete: X,
18
- };
19
-
20
- export type MethodIconProps = {
21
- httpMethod: string;
22
- showName?: boolean;
23
- };
24
-
25
- export function MethodIconBadge({ httpMethod, showName }: MethodIconProps) {
26
- if (!HttpMethods.includes(httpMethod)) return null;
27
-
28
- return (
29
- <span
30
- className={clsx(style.MethodRouteHttpMethod, style.MethodRouteHttpMethodIconOnly)}
31
- data-method={httpMethod}
32
- >
33
- {React.createElement(HttpMethodIcons[httpMethod], {
34
- size: 14,
35
- strokeWidth: 3,
36
- className: style.Icon,
37
- })}
38
- {showName && httpMethod}
39
- </span>
40
- );
41
- }
42
-
43
- export type MethodHeaderProps = {
44
- title: ReactNode;
45
- level?: 'h1' | 'h2' | 'h3' | 'h4' | 'h5';
46
- signature?: ReactNode;
47
- badges?: ReactNode;
48
- children?: ReactNode;
49
- };
50
-
51
- export function MethodHeader({ title, badges, signature, children, level }: MethodHeaderProps) {
52
- const Heading = level ?? 'h5';
53
- return (
54
- <div className={style.MethodHeader}>
55
- <Heading className={style.MethodTitle}>{title}</Heading>
56
- {badges && <div className={style.MethodBadges}>{badges}</div>}
57
- {signature}
58
- {children}
59
- </div>
60
- );
61
- }
62
-
63
- export type MethodRouteProps = {
64
- httpMethod?: string;
65
- endpoint?: string | ReactNode;
66
- iconOnly?: boolean;
67
- };
68
-
69
- export function MethodRoute({ httpMethod, endpoint, iconOnly }: MethodRouteProps) {
70
- return (
71
- <div className={style.MethodRoute}>
72
- <MethodIconBadge httpMethod={httpMethod} showName={!iconOnly} />
73
- {endpoint && <span className={style.MethodRouteEndpoint}>{endpoint}</span>}
74
- </div>
75
- );
76
- }
77
-
78
- export type MethodDescriptionProps = {
79
- description?: string;
80
- };
81
-
82
- export function MethodDescription({ description }: MethodDescriptionProps) {
83
- const { Markdown } = useComponents();
84
-
85
- if (description)
86
- return (
87
- <div className={style.MethodDescription}>
88
- <Markdown content={description} />
89
- </div>
90
- );
91
- }
92
-
93
- export type MethodInfoProps = {
94
- children?: ReactNode;
95
- parameters?: ReactNode;
96
- returns?: ReactNode;
97
- };
98
-
99
- export function MethodInfo({ children, parameters, returns }: MethodInfoProps) {
100
- return (
101
- <div className={style.MethodInfo}>
102
- {children && <div className={style.MethodContent}>{children}</div>}
103
-
104
- {parameters && (
105
- <div className={style.MethodInfoSection}>
106
- <h5>
107
- Parameters
108
- <PropertyToggle target="parameters" />
109
- </h5>
110
- <div className={style.MethodParameters} data-stldocs-property-group="parameters">
111
- {parameters}
112
- </div>
113
- </div>
114
- )}
115
-
116
- {returns && (
117
- <div className={style.MethodInfoSection}>
118
- <h5>
119
- Returns
120
- <PropertyToggle target="returns" />
121
- </h5>
122
- <div className={style.MethodReturns} data-stldocs-property-group="returns">
123
- {returns}
124
- </div>
125
- </div>
126
- )}
127
- </div>
128
- );
129
- }
130
-
131
- export type MethodProps = {
132
- id?: string;
133
- header?: ReactNode;
134
- children?: ReactNode;
135
- } & React.HTMLProps<HTMLDivElement>;
136
-
137
- export function Method({ id, header, children, className, ...props }: MethodProps) {
138
- const Docs = useComponents();
139
-
140
- return (
141
- <div id={id} className={clsx(style.Method, className)} tabIndex={0} {...props}>
142
- {header}
143
- <div className={style.MethodBody}>{children}</div>
144
- </div>
145
- );
146
- }
@@ -1,160 +0,0 @@
1
- import * as React from 'react';
2
- import { ChevronRight } from 'lucide-react';
3
- import type * as SDKJSON from '~/lib/json-spec-v2/types';
4
-
5
- import { flatResources, getResourceFromSpec } from '../utils';
6
- import { useDeclaration, useLanguage, useLanguageComponents, useSpec } from '../contexts';
7
- import { useComponents } from '../contexts/use-components';
8
-
9
- import style from '../style';
10
- import { PropertyToggle } from './properties';
11
- import { Markdown } from './primitives';
12
- import { parseStainlessPath } from '../routing';
13
-
14
- export type SDKResourceProps = {
15
- resource: SDKJSON.Resource;
16
- parents?: SDKJSON.Resource[];
17
- };
18
-
19
- export function SDKResourceHeader({ resource, parents }: SDKResourceProps) {
20
- // const language = useLanguage();
21
-
22
- const segments = parents?.map((parent, index) => (
23
- <span className={style.ResourceTitleSegment} key={parent.stainlessPath}>
24
- {parent.title}
25
- {index < parents.length && <ChevronRight size={16} className={style.Icon} />}
26
- </span>
27
- ));
28
-
29
- if (!segments && !resource.description) {
30
- return null;
31
- }
32
-
33
- return (
34
- <div className={style.ResourceHeader}>
35
- {segments && (
36
- <h4 id={resource.stainlessPath} className={style.ResourceTitle}>
37
- {segments}
38
- <span className={style.ResourceTitleSegment}>{resource.title}</span>
39
- </h4>
40
- )}
41
- {resource.description && (
42
- <div className={style.ResourceDescription}>
43
- <Markdown content={resource.description} />
44
- </div>
45
- )}
46
- </div>
47
- );
48
- }
49
-
50
- export type SDKMethodSummaryProps = {
51
- method: SDKJSON.Method;
52
- };
53
-
54
- export function SDKMethodSummary({ method }: SDKMethodSummaryProps) {
55
- const Docs = useComponents();
56
- const Lang = useLanguageComponents();
57
- const decl = useDeclaration(method.stainlessPath);
58
-
59
- return (
60
- <Docs.MethodHeader
61
- level="h5"
62
- title={<Docs.Link stainlessPath={method.stainlessPath}>{method.summary ?? method.title}</Docs.Link>}
63
- signature={<Lang.MethodSignature decl={decl} />}
64
- badges={method.deprecated && <Docs.Badge id="deprecated">Deprecated</Docs.Badge>}
65
- >
66
- <Docs.MethodRoute httpMethod={method.httpMethod} endpoint={method.endpoint.split(' ', 2).at(-1)} />
67
- {/* Removing temporarily per design review */}
68
- {/* <Docs.MethodDescription description={method.description} /> */}
69
- </Docs.MethodHeader>
70
- );
71
- }
72
-
73
- export function SDKResource({ resource, parents, showModels }: SDKResourceProps & { showModels?: boolean }) {
74
- const Docs = useComponents();
75
- const language = useLanguage();
76
- const spec = useSpec();
77
-
78
- const methods = Object.values(resource.methods).filter(
79
- (method) => spec.decls?.[language]?.[method.stainlessPath],
80
- );
81
-
82
- const models = Object.values(resource.models).filter(
83
- (model) => spec.decls?.[language]?.[`${model.stainlessPath} > (schema)`],
84
- );
85
-
86
- return (
87
- <div className={style.Resource}>
88
- <div className={style.ResourceContent}>
89
- <Docs.SDKResourceHeader resource={resource} parents={parents} />
90
- {methods.length > 0 && (
91
- <div className={style.ResourceContentGroup}>
92
- {methods
93
- .toSorted((first, second) => first.name.localeCompare(second.name))
94
- .map((method) => (
95
- <div className={style.MethodSummary} key={method.stainlessPath}>
96
- <Docs.SDKMethodSummary method={method} />
97
- </div>
98
- ))}
99
- </div>
100
- )}
101
-
102
- {showModels !== false && models.length > 0 && (
103
- <div className={style.ResourceContentGroup} data-stldocs-property-group="models">
104
- <h5 className={style.ResourceContentGroupModelTitle}>
105
- Models
106
- <PropertyToggle target="models" />
107
- </h5>
108
- {models.map((model) => (
109
- <Docs.SDKModel model={model} key={model.stainlessPath} />
110
- ))}
111
- </div>
112
- )}
113
- </div>
114
- </div>
115
- );
116
- }
117
-
118
- export type SDKOverviewProps = {
119
- resource: SDKJSON.Resource;
120
- };
121
-
122
- export function SDKOverview({ resource }: SDKOverviewProps) {
123
- const { SDKResource } = useComponents();
124
-
125
- const nested = React.useMemo(() => flatResources(resource.subresources, [resource]), [resource]);
126
-
127
- return (
128
- <div className={style.Overview}>
129
- <div className={style.OverviewHeader}>
130
- <h1>{resource.title}</h1>
131
- </div>
132
- <SDKResource resource={resource} />
133
- {nested.map((props, index) => (
134
- <SDKResource {...props} key={index} />
135
- ))}
136
- </div>
137
- );
138
- }
139
-
140
- export type SDKRootProps = {
141
- stainlessPath: string;
142
- };
143
-
144
- export function SDKRoot({ stainlessPath }: SDKRootProps) {
145
- const spec = useSpec();
146
- const Docs = useComponents();
147
-
148
- const parsed = parseStainlessPath(stainlessPath);
149
- const resource = getResourceFromSpec(stainlessPath, spec);
150
-
151
- if (!resource || !parsed) return null;
152
-
153
- const content = parsed.method ? (
154
- <Docs.SDKMethod method={resource.methods[parsed.method]} />
155
- ) : (
156
- <Docs.SDKOverview resource={resource} />
157
- );
158
-
159
- return <div className={style.Root}>{content}</div>;
160
- }
@@ -1,284 +0,0 @@
1
- import * as React from 'react';
2
- import { ReactNode, Fragment, useState } from 'react';
3
- import { useLanguage, useRenderMarkdown, useNavigation, useSettings } from '../contexts';
4
- import { generateRoute } from '../routing';
5
- import style from '../style';
6
- import { MinusIcon, PlusIcon } from 'lucide-react';
7
-
8
- type JoinProps = { items: ReactNode[]; limit?: number; children: ReactNode };
9
-
10
- export function Join({ items, limit, children }: JoinProps) {
11
- const arr =
12
- limit && items.length > limit + 1
13
- ? [...items.slice(0, limit), <span className={style.Truncation}>{items.length - limit} more</span>]
14
- : items;
15
-
16
- return arr.map((item, index) => (
17
- <Fragment key={`iterator:${index}`}>
18
- {!!index && children}
19
- {item}
20
- </Fragment>
21
- ));
22
- }
23
-
24
- type ExpanderProps = {
25
- id?: string;
26
- open?: boolean;
27
- summary: ReactNode;
28
- virtual?: boolean;
29
- muted?: boolean;
30
- children?: ReactNode;
31
- };
32
-
33
- export function Expander({ id, open, summary, virtual, muted, children }: ExpanderProps) {
34
- const { virtualExpanders } = useSettings();
35
-
36
- if (virtual || virtualExpanders)
37
- return (
38
- <VirtualExpander summary={summary} open={open} muted={muted} id={id}>
39
- {children}
40
- </VirtualExpander>
41
- );
42
-
43
- return (
44
- <details
45
- className={style.Expander}
46
- open={open}
47
- data-stldocs-expander-muted={muted}
48
- data-stldocs-expander-initial-state={open}
49
- >
50
- <summary className={style.ExpanderSummary}>
51
- <div className={style.ExpanderSummaryIcon}>
52
- <PlusIcon size={16} strokeWidth={1} className={style.Icon} />
53
- <MinusIcon size={16} strokeWidth={1} className={style.Icon} />
54
- </div>
55
- <div className={style.ExpanderSummaryContent}>{summary}</div>
56
- </summary>
57
- <div className={style.ExpanderContent} id={id}>
58
- {children}
59
- </div>
60
- </details>
61
- );
62
- }
63
-
64
- export function VirtualExpander({
65
- id,
66
- open: isOpen,
67
- muted,
68
- summary,
69
- children,
70
- }: Omit<ExpanderProps, 'virtual'>) {
71
- const [open, setOpen] = useState(isOpen);
72
-
73
- React.useEffect(() => {
74
- if (isOpen) setOpen(true);
75
- }, [isOpen]);
76
-
77
- return (
78
- <div
79
- className={style.Expander}
80
- data-open={open}
81
- data-stldocs-expander-muted={muted}
82
- data-stldocs-expander-initial-state={open}
83
- >
84
- <div className={style.ExpanderSummary} onClick={() => setOpen(!open)}>
85
- <div className={style.ExpanderSummaryIcon}>
86
- <PlusIcon size={16} className={style.Icon} />
87
- <MinusIcon size={16} className={style.Icon} />
88
- </div>
89
- <div className={style.ExpanderSummaryContent}>{summary}</div>
90
- </div>
91
- {open && (
92
- <div className={style.ExpanderContent} id={id}>
93
- {children}
94
- </div>
95
- )}
96
- </div>
97
- );
98
- }
99
-
100
- export function Markdown({ content }: { content: string }) {
101
- const rendered = useRenderMarkdown(content);
102
-
103
- return (
104
- rendered && (
105
- <div className={`${style.Markdown} ${style.Content}`} dangerouslySetInnerHTML={{ __html: rendered }} />
106
- )
107
- );
108
- }
109
-
110
- type BadgeProps = {
111
- id: string;
112
- children?: ReactNode;
113
- };
114
-
115
- export function Badge({ id, children }: BadgeProps) {
116
- return (
117
- <span className={style.Badge} data-badge-id={id}>
118
- {children ?? id}
119
- </span>
120
- );
121
- }
122
-
123
- const TooltipNestingContext = React.createContext<boolean>(false);
124
-
125
- type TooltipProps = {
126
- content: ReactNode;
127
- children: ReactNode;
128
- };
129
-
130
- export function Tooltip({ content, children }: TooltipProps) {
131
- const nested = React.useContext(TooltipNestingContext);
132
-
133
- if (nested) return children;
134
-
135
- return (
136
- <span className={style.Tooltip}>
137
- <span className={style.TooltipContent}>
138
- <TooltipNestingContext.Provider value={true}>{content}</TooltipNestingContext.Provider>
139
- </span>
140
- <span className={style.TooltipHost}>{children}</span>
141
- </span>
142
- );
143
- }
144
-
145
- type LinkProps = {
146
- stainlessPath: string;
147
- scroll?: boolean;
148
- children?: ReactNode;
149
- } & React.HTMLProps<HTMLAnchorElement>;
150
-
151
- export function Link({ stainlessPath, scroll = true, children, ...props }: LinkProps) {
152
- const { basePath, onNavigate } = useNavigation();
153
- const language = useLanguage();
154
-
155
- const href = React.useMemo(() => {
156
- if (props.href) return props.href;
157
- if (stainlessPath) return generateRoute(basePath, language, stainlessPath);
158
- }, [basePath, language, stainlessPath]);
159
-
160
- const handleClick = React.useCallback(
161
- (e: React.MouseEvent<HTMLAnchorElement>) => {
162
- if (props.onClick) props.onClick(e);
163
- if (e.button !== 0 || e.metaKey || e.ctrlKey || e.shiftKey || e.altKey) return;
164
- if (href) onNavigate?.(e, { href, language, stainlessPath, scroll });
165
- },
166
- [href, scroll, onNavigate],
167
- );
168
-
169
- if (!href) return children;
170
-
171
- return (
172
- <a href={href} onClick={handleClick} {...props}>
173
- {children}
174
- </a>
175
- );
176
- }
177
-
178
- export type InputProps = {
179
- left?: ReactNode;
180
- right?: ReactNode;
181
- } & React.InputHTMLAttributes<HTMLInputElement>;
182
-
183
- export const Input = React.forwardRef<HTMLInputElement, InputProps>(({ left, right, ...props }, ref) => {
184
- return (
185
- <div className={style.Input}>
186
- {left}
187
- <input {...props} ref={ref} className={style.InputTextField} />
188
- {right}
189
- </div>
190
- );
191
- });
192
-
193
- export type ToggleButtonProps = {
194
- children?: ReactNode;
195
- selected?: boolean;
196
- } & React.ButtonHTMLAttributes<HTMLButtonElement>;
197
-
198
- export const ToggleButton = React.forwardRef<HTMLButtonElement, ToggleButtonProps>(
199
- ({ children, selected, ...props }, ref) => {
200
- return (
201
- <button {...props} ref={ref} className={style.ToggleButton} data-stldocs-toggle-selected={selected}>
202
- {children}
203
- </button>
204
- );
205
- },
206
- );
207
-
208
- export type ListViewProps<TItem> = {
209
- items: Array<TItem>;
210
- itemDelegate: (item: TItem, selected: boolean) => React.ReactNode;
211
- onSelectListItem?: (item: TItem) => void;
212
- } & React.HTMLAttributes<HTMLDivElement>;
213
-
214
- export function ListView<TItem>({ items, itemDelegate, onSelectListItem, ...rest }: ListViewProps<TItem>) {
215
- const [selectedIndex, setSelectedIndex] = React.useState<number>(0);
216
- const [keyboardIndex, setKeyboardIndex] = React.useState<number>(0);
217
- const listRef = React.useRef<HTMLDivElement>(null);
218
- const itemRef = React.useRef<HTMLDivElement>(null);
219
-
220
- React.useEffect(() => {
221
- setSelectedIndex(0);
222
- setKeyboardIndex(0);
223
- }, [items]);
224
-
225
- React.useEffect(() => {
226
- function handleKeyPress(ev: KeyboardEvent) {
227
- switch (ev.key) {
228
- case 'ArrowUp': {
229
- ev.preventDefault();
230
- const value = Math.max(0, selectedIndex - 1);
231
- setSelectedIndex(value);
232
- setKeyboardIndex(value);
233
- break;
234
- }
235
-
236
- case 'ArrowDown': {
237
- ev.preventDefault();
238
- const value = Math.min(items.length, selectedIndex + 1);
239
- setSelectedIndex(value);
240
- setKeyboardIndex(value);
241
- break;
242
- }
243
-
244
- case 'Enter':
245
- ev.preventDefault();
246
- onSelectListItem?.(items[selectedIndex]);
247
- break;
248
- }
249
- }
250
-
251
- addEventListener('keydown', handleKeyPress);
252
- return () => removeEventListener('keydown', handleKeyPress);
253
- }, [items, selectedIndex]);
254
-
255
- React.useEffect(() => {
256
- if (!keyboardIndex) {
257
- listRef?.current?.scroll(0, 0);
258
- return;
259
- }
260
-
261
- const selectedBounds = itemRef.current.getBoundingClientRect();
262
- const listBounds = listRef.current.getBoundingClientRect();
263
- const needsScroll = selectedBounds.top < listBounds.top || selectedBounds.bottom > listBounds.bottom;
264
-
265
- if (needsScroll) itemRef.current.scrollIntoView({ block: 'nearest' });
266
- }, [keyboardIndex, items, listRef]);
267
-
268
- return (
269
- <div ref={listRef} className={style.ListView} tabIndex={0} {...rest}>
270
- {items.map((item, index) => (
271
- <div
272
- key={index}
273
- ref={index === selectedIndex ? itemRef : null}
274
- className={style.ListViewItem}
275
- data-stldocs-listview-selected={index === selectedIndex}
276
- onClick={() => onSelectListItem?.(items[selectedIndex])}
277
- onMouseMove={() => setSelectedIndex(index)}
278
- >
279
- {itemDelegate(item, index === selectedIndex)}
280
- </div>
281
- ))}
282
- </div>
283
- );
284
- }