@stainless-api/docs-ui 0.1.0-beta.4 → 0.1.0-beta.40

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 (188) hide show
  1. package/dist/breadcrumbs-CZg0Fk4V.d.ts +20 -0
  2. package/dist/chunk-Bf_Gc_jT.js +18 -0
  3. package/dist/component-generics-BLXXX1dz.d.ts +30 -0
  4. package/dist/component-generics-D3ybuN5r.js +45 -0
  5. package/dist/components/breadcrumbs.d.ts +2 -0
  6. package/dist/components/breadcrumbs.js +15 -0
  7. package/dist/components/icons.d.ts +50 -0
  8. package/dist/components/icons.js +3 -0
  9. package/dist/components/index.d.ts +11 -0
  10. package/dist/components/index.js +15 -0
  11. package/dist/components/method.d.ts +2 -0
  12. package/dist/components/method.js +15 -0
  13. package/dist/components/overview.d.ts +2 -0
  14. package/dist/components/overview.js +15 -0
  15. package/dist/components/primitives.d.ts +2 -0
  16. package/dist/components/primitives.js +15 -0
  17. package/dist/components/properties.d.ts +2 -0
  18. package/dist/components/properties.js +15 -0
  19. package/dist/components/sdk.d.ts +3 -0
  20. package/dist/components/sdk.js +15 -0
  21. package/dist/components/sidebar.d.ts +2 -0
  22. package/dist/components/sidebar.js +15 -0
  23. package/dist/components/snippets.d.ts +4 -0
  24. package/dist/components/snippets.js +15 -0
  25. package/dist/contexts/component-generics.d.ts +2 -0
  26. package/dist/contexts/component-generics.js +3 -0
  27. package/dist/contexts/component-types.d.ts +17 -0
  28. package/dist/contexts/component-types.js +0 -0
  29. package/dist/contexts/component.d.ts +30 -0
  30. package/dist/contexts/component.js +15 -0
  31. package/dist/contexts/docs.d.ts +2 -0
  32. package/dist/contexts/docs.js +3 -0
  33. package/dist/contexts/index.d.ts +19 -0
  34. package/dist/contexts/index.js +15 -0
  35. package/dist/contexts/markdown.d.ts +2 -0
  36. package/dist/contexts/markdown.js +4 -0
  37. package/dist/contexts/navigation.d.ts +3 -0
  38. package/dist/contexts/navigation.js +3 -0
  39. package/dist/contexts/search.d.ts +4 -0
  40. package/dist/contexts/search.js +15 -0
  41. package/dist/contexts/use-components.d.ts +15 -0
  42. package/dist/contexts/use-components.js +4 -0
  43. package/dist/contexts-B-OYzYpz.js +3837 -0
  44. package/dist/docs-BeJaKOCE.d.ts +46 -0
  45. package/dist/docs-CopwX9a7.js +54 -0
  46. package/dist/icons-yb-Jhkmg.js +753 -0
  47. package/dist/index-CBi59jcw.d.ts +240 -0
  48. package/dist/index-DT1wzg21.d.ts +15 -0
  49. package/dist/index-DteYqCJO.d.ts +14 -0
  50. package/dist/index.d.ts +21 -0
  51. package/dist/index.js +15 -7542
  52. package/dist/languages/go.d.ts +12 -0
  53. package/dist/languages/go.js +15 -0
  54. package/dist/languages/http.d.ts +12 -0
  55. package/dist/languages/http.js +15 -0
  56. package/dist/languages/index.d.ts +12 -0
  57. package/dist/languages/index.js +15 -0
  58. package/dist/languages/java.d.ts +12 -0
  59. package/dist/languages/java.js +15 -0
  60. package/dist/languages/python.d.ts +12 -0
  61. package/dist/languages/python.js +15 -0
  62. package/dist/languages/ruby.d.ts +12 -0
  63. package/dist/languages/ruby.js +15 -0
  64. package/dist/languages/typescript.d.ts +12 -0
  65. package/dist/languages/typescript.js +15 -0
  66. package/dist/markdown/index.d.ts +12 -0
  67. package/dist/markdown/index.js +15 -0
  68. package/dist/markdown/md.d.ts +15 -0
  69. package/dist/markdown/md.js +3 -0
  70. package/dist/markdown/utils.d.ts +4 -0
  71. package/dist/markdown/utils.js +3 -0
  72. package/dist/markdown-DTVxnxGP.d.ts +22 -0
  73. package/dist/markdown-Diyg-eof.js +48 -0
  74. package/dist/md-DxiV1_vy.js +41 -0
  75. package/dist/method-DHjDGGA_.d.ts +71 -0
  76. package/dist/navigation-CGr5_w6z.js +24 -0
  77. package/dist/navigation-rt_B_1lt.d.ts +29 -0
  78. package/dist/overview-CKJV2xUO.d.ts +39 -0
  79. package/dist/pagefind-BaK1krMe.js +15 -0
  80. package/dist/primitives-CiVFjnRA.d.ts +99 -0
  81. package/dist/properties-Z45x-uLZ.d.ts +46 -0
  82. package/dist/routing-DNN8R6bZ.d.ts +54 -0
  83. package/dist/routing-qs2YNG9o.js +189 -0
  84. package/dist/routing.d.ts +2 -0
  85. package/dist/routing.js +3 -180
  86. package/dist/sdk-DDWWoPPD.d.ts +119 -0
  87. package/dist/search/index.d.ts +26 -0
  88. package/dist/search/index.js +334 -0
  89. package/dist/search/providers/algolia.d.ts +24 -0
  90. package/dist/search/providers/algolia.js +15 -0
  91. package/dist/search/providers/fuse.d.ts +18 -0
  92. package/dist/search/providers/fuse.js +30 -0
  93. package/dist/search/providers/pagefind.d.ts +7 -0
  94. package/dist/search/providers/pagefind.js +3 -0
  95. package/dist/search/providers/walker.d.ts +9 -0
  96. package/dist/search/providers/walker.js +34 -0
  97. package/dist/search/types.d.ts +3 -0
  98. package/dist/search/types.js +3 -0
  99. package/dist/search-CCDWCiAd.d.ts +21 -0
  100. package/dist/sidebar-DrtsF85U.d.ts +36 -0
  101. package/dist/snippets-B2EEG7AK.d.ts +46 -0
  102. package/dist/spec.d.ts +65 -0
  103. package/dist/spec.js +167 -0
  104. package/dist/style-BhhgPVF6.js +165 -0
  105. package/dist/style-p4qTfnmO.d.ts +164 -0
  106. package/dist/style.d.ts +2 -0
  107. package/dist/style.js +3 -0
  108. package/dist/styles/main.css +724 -754
  109. package/dist/styles/main.js +0 -0
  110. package/dist/styles/primitives.css +287 -466
  111. package/dist/styles/primitives.js +0 -0
  112. package/dist/styles/resets.css +31 -41
  113. package/dist/styles/resets.js +0 -0
  114. package/dist/styles/search.css +175 -274
  115. package/dist/styles/search.js +0 -0
  116. package/dist/styles/sidebar.css +59 -60
  117. package/dist/styles/sidebar.js +0 -0
  118. package/dist/styles/variables.css +26 -89
  119. package/dist/styles/variables.js +0 -0
  120. package/dist/styles.css +1753 -0
  121. package/dist/styles.mjs +1 -0
  122. package/dist/types-DNSz4kuM.d.ts +83 -0
  123. package/dist/types-__XoFvJ_.js +34 -0
  124. package/dist/use-components-BHEwm0mE.js +7 -0
  125. package/dist/utils-CA3mYNaX.d.ts +21 -0
  126. package/dist/utils-D43p_yTd.d.ts +14 -0
  127. package/dist/utils-FnJ4tyuN.js +32 -0
  128. package/dist/utils-Jo_o4DX9.js +31 -0
  129. package/dist/utils.d.ts +2 -0
  130. package/dist/utils.js +4 -0
  131. package/package.json +84 -19
  132. package/dist/mcp.js +0 -16003
  133. package/dist/styles/snippets.css +0 -132
  134. package/src/components/breadcrumbs.tsx +0 -94
  135. package/src/components/chat.tsx +0 -154
  136. package/src/components/dropdown.tsx +0 -91
  137. package/src/components/icons.tsx +0 -584
  138. package/src/components/index.ts +0 -9
  139. package/src/components/method.tsx +0 -144
  140. package/src/components/overview.tsx +0 -159
  141. package/src/components/primitives.tsx +0 -284
  142. package/src/components/properties.tsx +0 -130
  143. package/src/components/scripts/dropdown.ts +0 -78
  144. package/src/components/sdk.tsx +0 -402
  145. package/src/components/sidebar.tsx +0 -99
  146. package/src/components/snippets.tsx +0 -179
  147. package/src/contexts/component-generics.tsx +0 -48
  148. package/src/contexts/component-types.tsx +0 -5
  149. package/src/contexts/component.tsx +0 -32
  150. package/src/contexts/docs.tsx +0 -75
  151. package/src/contexts/index.tsx +0 -17
  152. package/src/contexts/markdown.tsx +0 -43
  153. package/src/contexts/navigation.tsx +0 -44
  154. package/src/contexts/search.tsx +0 -28
  155. package/src/contexts/use-components.tsx +0 -4
  156. package/src/index.ts +0 -6
  157. package/src/languages/go.tsx +0 -281
  158. package/src/languages/http.tsx +0 -321
  159. package/src/languages/index.ts +0 -26
  160. package/src/languages/java.tsx +0 -358
  161. package/src/languages/python.tsx +0 -255
  162. package/src/languages/ruby.tsx +0 -320
  163. package/src/languages/typescript.tsx +0 -393
  164. package/src/markdown/index.ts +0 -107
  165. package/src/markdown/md.ts +0 -45
  166. package/src/markdown/utils.ts +0 -49
  167. package/src/routing.ts +0 -238
  168. package/src/search/form.tsx +0 -127
  169. package/src/search/index.tsx +0 -1
  170. package/src/search/indexer.ts +0 -246
  171. package/src/search/mcp.ts +0 -61
  172. package/src/search/printer.tsx +0 -86
  173. package/src/search/providers/algolia.ts +0 -88
  174. package/src/search/providers/fuse.ts +0 -19
  175. package/src/search/providers/pagefind.ts +0 -17
  176. package/src/search/providers/walker.ts +0 -30
  177. package/src/search/results.tsx +0 -181
  178. package/src/search/state.ts +0 -64
  179. package/src/search/types.ts +0 -116
  180. package/src/style.ts +0 -197
  181. package/src/styles/main.css +0 -993
  182. package/src/styles/primitives.css +0 -572
  183. package/src/styles/resets.css +0 -47
  184. package/src/styles/search.css +0 -342
  185. package/src/styles/sidebar.css +0 -87
  186. package/src/styles/snippets.css +0 -132
  187. package/src/styles/variables.css +0 -90
  188. package/src/utils.ts +0 -41
@@ -1,144 +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
- signature?: ReactNode;
46
- badges?: ReactNode;
47
- children?: ReactNode;
48
- };
49
-
50
- export function MethodHeader({ title, badges, signature, children }: MethodHeaderProps) {
51
- return (
52
- <div className={style.MethodHeader}>
53
- <h5 className={style.MethodTitle}>{title}</h5>
54
- {badges && <div className={style.MethodBadges}>{badges}</div>}
55
- {signature}
56
- {children}
57
- </div>
58
- );
59
- }
60
-
61
- export type MethodRouteProps = {
62
- httpMethod?: string;
63
- endpoint?: string | ReactNode;
64
- iconOnly?: boolean;
65
- };
66
-
67
- export function MethodRoute({ httpMethod, endpoint, iconOnly }: MethodRouteProps) {
68
- return (
69
- <div className={style.MethodRoute}>
70
- <MethodIconBadge httpMethod={httpMethod} showName={!iconOnly} />
71
- {endpoint && <span className={style.MethodRouteEndpoint}>{endpoint}</span>}
72
- </div>
73
- );
74
- }
75
-
76
- export type MethodDescriptionProps = {
77
- description?: string;
78
- };
79
-
80
- export function MethodDescription({ description }: MethodDescriptionProps) {
81
- const { Markdown } = useComponents();
82
-
83
- if (description)
84
- return (
85
- <div className={style.MethodDescription}>
86
- <Markdown content={description} />
87
- </div>
88
- );
89
- }
90
-
91
- export type MethodInfoProps = {
92
- children?: ReactNode;
93
- parameters?: ReactNode;
94
- returns?: ReactNode;
95
- };
96
-
97
- export function MethodInfo({ children, parameters, returns }: MethodInfoProps) {
98
- return (
99
- <div className={style.MethodInfo}>
100
- {children && <div className={style.MethodContent}>{children}</div>}
101
-
102
- {parameters && (
103
- <div className={style.MethodInfoSection}>
104
- <h5>
105
- Parameters
106
- <PropertyToggle target="parameters" />
107
- </h5>
108
- <div className={style.MethodParameters} data-stldocs-property-group="parameters">
109
- {parameters}
110
- </div>
111
- </div>
112
- )}
113
-
114
- {returns && (
115
- <div className={style.MethodInfoSection}>
116
- <h5>
117
- Returns
118
- <PropertyToggle target="returns" />
119
- </h5>
120
- <div className={style.MethodReturns} data-stldocs-property-group="returns">
121
- {returns}
122
- </div>
123
- </div>
124
- )}
125
- </div>
126
- );
127
- }
128
-
129
- export type MethodProps = {
130
- id?: string;
131
- header?: ReactNode;
132
- children?: ReactNode;
133
- } & React.HTMLProps<HTMLDivElement>;
134
-
135
- export function Method({ id, header, children, className, ...props }: MethodProps) {
136
- const Docs = useComponents();
137
-
138
- return (
139
- <div id={id} className={clsx(style.Method, className)} tabIndex={0} {...props}>
140
- {header}
141
- <div className={style.MethodBody}>{children}</div>
142
- </div>
143
- );
144
- }
@@ -1,159 +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
- title={<Docs.Link stainlessPath={method.stainlessPath}>{method.summary ?? method.title}</Docs.Link>}
62
- signature={<Lang.MethodSignature decl={decl} />}
63
- badges={method.deprecated && <Docs.Badge id="deprecated">Deprecated</Docs.Badge>}
64
- >
65
- <Docs.MethodRoute httpMethod={method.httpMethod} endpoint={method.endpoint.split(' ', 2).at(-1)} />
66
- {/* Removing temporarily per design review */}
67
- {/* <Docs.MethodDescription description={method.description} /> */}
68
- </Docs.MethodHeader>
69
- );
70
- }
71
-
72
- export function SDKResource({ resource, parents, showModels }: SDKResourceProps & { showModels?: boolean }) {
73
- const Docs = useComponents();
74
- const language = useLanguage();
75
- const spec = useSpec();
76
-
77
- const methods = Object.values(resource.methods).filter(
78
- (method) => spec.decls?.[language]?.[method.stainlessPath],
79
- );
80
-
81
- const models = Object.values(resource.models).filter(
82
- (model) => spec.decls?.[language]?.[`${model.stainlessPath} > (schema)`],
83
- );
84
-
85
- return (
86
- <div className={style.Resource}>
87
- <div className={style.ResourceContent}>
88
- <Docs.SDKResourceHeader resource={resource} parents={parents} />
89
- {methods.length > 0 && (
90
- <div className={style.ResourceContentGroup}>
91
- {methods
92
- .toSorted((first, second) => first.name.localeCompare(second.name))
93
- .map((method) => (
94
- <div className={style.MethodSummary} key={method.stainlessPath}>
95
- <Docs.SDKMethodSummary method={method} />
96
- </div>
97
- ))}
98
- </div>
99
- )}
100
-
101
- {showModels !== false && models.length > 0 && (
102
- <div className={style.ResourceContentGroup} data-stldocs-property-group="models">
103
- <h5 className={style.ResourceContentGroupModelTitle}>
104
- Models
105
- <PropertyToggle target="models" />
106
- </h5>
107
- {models.map((model) => (
108
- <Docs.SDKModel model={model} key={model.stainlessPath} />
109
- ))}
110
- </div>
111
- )}
112
- </div>
113
- </div>
114
- );
115
- }
116
-
117
- export type SDKOverviewProps = {
118
- resource: SDKJSON.Resource;
119
- };
120
-
121
- export function SDKOverview({ resource }: SDKOverviewProps) {
122
- const { SDKResource } = useComponents();
123
-
124
- const nested = React.useMemo(() => flatResources(resource.subresources, [resource]), [resource]);
125
-
126
- return (
127
- <div className={style.Overview}>
128
- <div className={style.OverviewHeader}>
129
- <h1>{resource.title}</h1>
130
- </div>
131
- <SDKResource resource={resource} />
132
- {nested.map((props, index) => (
133
- <SDKResource {...props} key={index} />
134
- ))}
135
- </div>
136
- );
137
- }
138
-
139
- export type SDKRootProps = {
140
- stainlessPath: string;
141
- };
142
-
143
- export function SDKRoot({ stainlessPath }: SDKRootProps) {
144
- const spec = useSpec();
145
- const Docs = useComponents();
146
-
147
- const parsed = parseStainlessPath(stainlessPath);
148
- const resource = getResourceFromSpec(stainlessPath, spec);
149
-
150
- if (!resource || !parsed) return null;
151
-
152
- const content = parsed.method ? (
153
- <Docs.SDKMethod method={resource.methods[parsed.method]} />
154
- ) : (
155
- <Docs.SDKOverview resource={resource} />
156
- );
157
-
158
- return <div className={style.Root}>{content}</div>;
159
- }
@@ -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
- }