@stainless-api/docs-ui 0.1.0-beta.8 → 0.1.0-beta.80

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 (155) hide show
  1. package/dist/chunk-BYypO7fO.js +18 -0
  2. package/dist/components/MaskedInput.d.ts +14 -0
  3. package/dist/components/MaskedInput.js +76 -0
  4. package/dist/components/breadcrumbs.d.ts +25 -0
  5. package/dist/components/breadcrumbs.js +64 -0
  6. package/dist/components/icons.d.ts +65 -0
  7. package/dist/components/icons.js +941 -0
  8. package/dist/components/index.d.ts +16 -0
  9. package/dist/components/index.js +75 -0
  10. package/dist/components/method.d.ts +59 -0
  11. package/dist/components/method.js +97 -0
  12. package/dist/components/overview.d.ts +39 -0
  13. package/dist/components/overview.js +139 -0
  14. package/dist/components/primitives.d.ts +107 -0
  15. package/dist/components/primitives.js +253 -0
  16. package/dist/components/properties.d.ts +54 -0
  17. package/dist/components/properties.js +140 -0
  18. package/dist/components/sdk-sidebar.d.ts +15 -0
  19. package/dist/components/sdk-sidebar.js +52 -0
  20. package/dist/components/sdk.d.ts +128 -0
  21. package/dist/components/sdk.js +490 -0
  22. package/dist/components/snippets.d.ts +71 -0
  23. package/dist/components/snippets.js +184 -0
  24. package/dist/components/stl-sidebar.d.ts +58 -0
  25. package/dist/components/stl-sidebar.js +114 -0
  26. package/dist/contexts/component-generics.d.ts +30 -0
  27. package/dist/contexts/component-generics.js +33 -0
  28. package/dist/contexts/component-types.d.ts +8 -0
  29. package/dist/contexts/component-types.js +0 -0
  30. package/dist/contexts/component.d.ts +21 -0
  31. package/dist/contexts/component.js +24 -0
  32. package/dist/contexts/docs.d.ts +62 -0
  33. package/dist/contexts/docs.js +91 -0
  34. package/dist/contexts/index.d.ts +13 -0
  35. package/dist/contexts/index.js +45 -0
  36. package/dist/contexts/markdown.d.ts +22 -0
  37. package/dist/contexts/markdown.js +49 -0
  38. package/dist/contexts/navigation.d.ts +29 -0
  39. package/dist/contexts/navigation.js +24 -0
  40. package/dist/contexts/use-components.d.ts +6 -0
  41. package/dist/contexts/use-components.js +8 -0
  42. package/dist/index-pvh-w1xa.d.ts +1776 -0
  43. package/dist/index.d.ts +7 -0
  44. package/dist/index.js +9 -7542
  45. package/dist/languages/cli.d.ts +38 -0
  46. package/dist/languages/cli.js +167 -0
  47. package/dist/languages/csharp.d.ts +42 -0
  48. package/dist/languages/csharp.js +284 -0
  49. package/dist/languages/go.d.ts +40 -0
  50. package/dist/languages/go.js +304 -0
  51. package/dist/languages/http.d.ts +46 -0
  52. package/dist/languages/http.js +391 -0
  53. package/dist/languages/index.d.ts +51 -0
  54. package/dist/languages/index.js +33 -0
  55. package/dist/languages/java.d.ts +43 -0
  56. package/dist/languages/java.js +404 -0
  57. package/dist/languages/php.d.ts +40 -0
  58. package/dist/languages/php.js +313 -0
  59. package/dist/languages/python.d.ts +40 -0
  60. package/dist/languages/python.js +270 -0
  61. package/dist/languages/ruby.d.ts +40 -0
  62. package/dist/languages/ruby.js +388 -0
  63. package/dist/languages/terraform.d.ts +48 -0
  64. package/dist/languages/terraform.js +244 -0
  65. package/dist/languages/typescript.d.ts +40 -0
  66. package/dist/languages/typescript.js +450 -0
  67. package/dist/markdown/index.d.ts +11 -0
  68. package/dist/markdown/index.js +80 -0
  69. package/dist/markdown/md.d.ts +15 -0
  70. package/dist/markdown/md.js +41 -0
  71. package/dist/markdown/printer.d.ts +13 -0
  72. package/dist/markdown/printer.js +76 -0
  73. package/dist/markdown/utils.d.ts +21 -0
  74. package/dist/markdown/utils.js +31 -0
  75. package/dist/routing.d.ts +61 -0
  76. package/dist/routing.js +176 -150
  77. package/dist/spec.d.ts +65 -0
  78. package/dist/spec.js +167 -0
  79. package/dist/style.d.ts +177 -0
  80. package/dist/style.js +178 -0
  81. package/dist/styles/main.css +788 -752
  82. package/dist/styles/main.js +0 -0
  83. package/dist/styles/primitives.css +326 -458
  84. package/dist/styles/primitives.js +0 -0
  85. package/dist/styles/resets.css +32 -41
  86. package/dist/styles/resets.js +0 -0
  87. package/dist/styles/search.css +192 -270
  88. package/dist/styles/search.js +0 -0
  89. package/dist/styles/sidebar.css +182 -69
  90. package/dist/styles/sidebar.js +0 -0
  91. package/dist/styles/variables.css +28 -89
  92. package/dist/styles/variables.js +0 -0
  93. package/dist/styles.css +3333 -0
  94. package/dist/styles.mjs +1 -0
  95. package/dist/use-strict-context-L0c8JKg4.js +16 -0
  96. package/dist/utils.d.ts +20 -0
  97. package/dist/utils.js +54 -0
  98. package/package.json +74 -22
  99. package/dist/mcp.js +0 -16003
  100. package/dist/styles/snippets.css +0 -132
  101. package/src/components/breadcrumbs.tsx +0 -94
  102. package/src/components/chat.tsx +0 -154
  103. package/src/components/dropdown.tsx +0 -91
  104. package/src/components/icons.tsx +0 -584
  105. package/src/components/index.ts +0 -9
  106. package/src/components/method.tsx +0 -146
  107. package/src/components/overview.tsx +0 -160
  108. package/src/components/primitives.tsx +0 -284
  109. package/src/components/properties.tsx +0 -130
  110. package/src/components/scripts/dropdown.ts +0 -78
  111. package/src/components/sdk.tsx +0 -403
  112. package/src/components/sidebar.tsx +0 -99
  113. package/src/components/snippets.tsx +0 -180
  114. package/src/contexts/component-generics.tsx +0 -48
  115. package/src/contexts/component-types.tsx +0 -5
  116. package/src/contexts/component.tsx +0 -32
  117. package/src/contexts/docs.tsx +0 -75
  118. package/src/contexts/index.tsx +0 -17
  119. package/src/contexts/markdown.tsx +0 -43
  120. package/src/contexts/navigation.tsx +0 -44
  121. package/src/contexts/search.tsx +0 -28
  122. package/src/contexts/use-components.tsx +0 -4
  123. package/src/index.ts +0 -6
  124. package/src/languages/go.tsx +0 -281
  125. package/src/languages/http.tsx +0 -321
  126. package/src/languages/index.ts +0 -26
  127. package/src/languages/java.tsx +0 -358
  128. package/src/languages/python.tsx +0 -255
  129. package/src/languages/ruby.tsx +0 -320
  130. package/src/languages/typescript.tsx +0 -393
  131. package/src/markdown/index.ts +0 -107
  132. package/src/markdown/md.ts +0 -45
  133. package/src/markdown/utils.ts +0 -49
  134. package/src/routing.ts +0 -238
  135. package/src/search/form.tsx +0 -127
  136. package/src/search/index.tsx +0 -1
  137. package/src/search/indexer.ts +0 -246
  138. package/src/search/mcp.ts +0 -61
  139. package/src/search/printer.tsx +0 -86
  140. package/src/search/providers/algolia.ts +0 -88
  141. package/src/search/providers/fuse.ts +0 -19
  142. package/src/search/providers/pagefind.ts +0 -17
  143. package/src/search/providers/walker.ts +0 -30
  144. package/src/search/results.tsx +0 -182
  145. package/src/search/state.ts +0 -64
  146. package/src/search/types.ts +0 -116
  147. package/src/style.ts +0 -196
  148. package/src/styles/main.css +0 -986
  149. package/src/styles/primitives.css +0 -580
  150. package/src/styles/resets.css +0 -39
  151. package/src/styles/search.css +0 -341
  152. package/src/styles/sidebar.css +0 -85
  153. package/src/styles/snippets.css +0 -130
  154. package/src/styles/variables.css +0 -86
  155. 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
- }