docusaurus-theme-openapi-docs 5.0.1 → 5.1.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 (137) hide show
  1. package/lib/markdown/schema.js +38 -15
  2. package/lib/markdown/schema.test.d.ts +1 -0
  3. package/lib/markdown/schema.test.js +86 -0
  4. package/lib/theme/ApiExplorer/ApiCodeBlock/Container/index.js +4 -2
  5. package/lib/theme/ApiExplorer/ApiCodeBlock/Content/String.js +9 -6
  6. package/lib/theme/ApiExplorer/ApiCodeBlock/Line/index.d.ts +1 -1
  7. package/lib/theme/ApiExplorer/ApiCodeBlock/index.d.ts +1 -1
  8. package/lib/theme/ApiExplorer/Authorization/index.js +9 -10
  9. package/lib/theme/ApiExplorer/Body/index.js +4 -5
  10. package/lib/theme/ApiExplorer/CodeSnippets/index.js +96 -61
  11. package/lib/theme/ApiExplorer/CodeSnippets/languages.js +12 -1
  12. package/lib/theme/ApiExplorer/CodeSnippets/languages.test.d.ts +1 -0
  13. package/lib/theme/ApiExplorer/CodeSnippets/languages.test.js +102 -0
  14. package/lib/theme/ApiExplorer/CodeTabs/index.d.ts +1 -1
  15. package/lib/theme/ApiExplorer/CodeTabs/index.js +6 -5
  16. package/lib/theme/ApiExplorer/Export/index.js +9 -2
  17. package/lib/theme/ApiExplorer/FormFileUpload/index.js +1 -2
  18. package/lib/theme/ApiExplorer/FormLabel/index.js +1 -2
  19. package/lib/theme/ApiExplorer/FormTextInput/index.js +1 -2
  20. package/lib/theme/ApiExplorer/LiveEditor/index.js +1 -2
  21. package/lib/theme/ApiExplorer/ParamOptions/ParamFormItems/ParamArrayFormItem.js +5 -3
  22. package/lib/theme/ApiExplorer/ParamOptions/ParamFormItems/ParamBooleanFormItem.js +75 -4
  23. package/lib/theme/ApiExplorer/ParamOptions/ParamFormItems/ParamMultiSelectFormItem.js +1 -2
  24. package/lib/theme/ApiExplorer/ParamOptions/ParamFormItems/ParamSelectFormItem.js +67 -4
  25. package/lib/theme/ApiExplorer/ParamOptions/ParamFormItems/ParamTextFormItem.js +65 -1
  26. package/lib/theme/ApiExplorer/ParamOptions/index.js +2 -3
  27. package/lib/theme/ApiExplorer/Request/index.js +17 -18
  28. package/lib/theme/ApiExplorer/Response/index.js +54 -12
  29. package/lib/theme/ApiExplorer/SecuritySchemes/index.js +57 -50
  30. package/lib/theme/ApiExplorer/Server/index.js +2 -3
  31. package/lib/theme/ApiItem/index.js +59 -33
  32. package/lib/theme/ApiTabs/index.d.ts +1 -1
  33. package/lib/theme/ApiTabs/index.js +7 -7
  34. package/lib/theme/DiscriminatorTabs/index.d.ts +1 -1
  35. package/lib/theme/DiscriminatorTabs/index.js +6 -5
  36. package/lib/theme/Example/index.js +3 -4
  37. package/lib/theme/MimeTabs/index.d.ts +1 -1
  38. package/lib/theme/MimeTabs/index.js +6 -5
  39. package/lib/theme/OperationTabs/index.d.ts +1 -1
  40. package/lib/theme/OperationTabs/index.js +6 -5
  41. package/lib/theme/ParamsDetails/index.js +1 -2
  42. package/lib/theme/ParamsItem/index.js +7 -8
  43. package/lib/theme/RequestSchema/index.js +57 -57
  44. package/lib/theme/ResponseExamples/index.js +3 -4
  45. package/lib/theme/ResponseSchema/index.js +26 -24
  46. package/lib/theme/Schema/index.js +148 -27
  47. package/lib/theme/SchemaExpansion/_SchemaExpansion.scss +113 -0
  48. package/lib/theme/SchemaExpansion/context.d.ts +24 -0
  49. package/lib/theme/SchemaExpansion/context.js +187 -0
  50. package/lib/theme/SchemaExpansion/index.d.ts +4 -0
  51. package/lib/theme/SchemaExpansion/index.js +314 -0
  52. package/lib/theme/SchemaItem/index.js +9 -10
  53. package/lib/theme/SchemaTabs/index.d.ts +1 -1
  54. package/lib/theme/SchemaTabs/index.js +6 -5
  55. package/lib/theme/StatusCodes/index.js +2 -4
  56. package/lib/theme/TabItem/index.d.ts +5 -0
  57. package/lib/theme/TabItem/index.js +51 -0
  58. package/lib/theme/TabItem/styles.module.css +3 -0
  59. package/lib/theme/Tabs/index.d.ts +5 -0
  60. package/lib/theme/Tabs/index.js +148 -0
  61. package/lib/theme/Tabs/styles.module.css +7 -0
  62. package/lib/theme/styles.scss +1 -0
  63. package/lib/theme/translationIds.d.ts +1 -93
  64. package/lib/theme/translationIds.js +0 -109
  65. package/lib/theme/utils/codeBlockUtils.d.ts +28 -0
  66. package/lib/theme/utils/codeBlockUtils.js +223 -0
  67. package/lib/theme/utils/reactUtils.d.ts +1 -0
  68. package/lib/theme/utils/reactUtils.js +23 -0
  69. package/lib/theme/utils/scrollUtils.d.ts +7 -0
  70. package/lib/theme/utils/scrollUtils.js +175 -0
  71. package/lib/theme/utils/tabsUtils.d.ts +47 -0
  72. package/lib/theme/utils/tabsUtils.js +299 -0
  73. package/lib/theme/utils/useCodeWordWrap.d.ts +8 -0
  74. package/lib/theme/utils/useCodeWordWrap.js +84 -0
  75. package/lib/theme/utils/useMutationObserver.d.ts +3 -0
  76. package/lib/theme/utils/useMutationObserver.js +34 -0
  77. package/package.json +4 -4
  78. package/src/markdown/schema.test.ts +102 -0
  79. package/src/markdown/schema.ts +42 -15
  80. package/src/theme/ApiExplorer/ApiCodeBlock/Container/index.tsx +2 -1
  81. package/src/theme/ApiExplorer/ApiCodeBlock/Content/String.tsx +8 -7
  82. package/src/theme/ApiExplorer/ApiCodeBlock/Line/index.tsx +1 -1
  83. package/src/theme/ApiExplorer/ApiCodeBlock/index.tsx +1 -1
  84. package/src/theme/ApiExplorer/Authorization/index.tsx +9 -10
  85. package/src/theme/ApiExplorer/Body/index.tsx +7 -5
  86. package/src/theme/ApiExplorer/CodeSnippets/index.tsx +103 -59
  87. package/src/theme/ApiExplorer/CodeSnippets/languages.test.ts +109 -0
  88. package/src/theme/ApiExplorer/CodeSnippets/languages.ts +13 -1
  89. package/src/theme/ApiExplorer/CodeTabs/index.tsx +5 -5
  90. package/src/theme/ApiExplorer/Export/index.tsx +6 -2
  91. package/src/theme/ApiExplorer/FormFileUpload/index.tsx +1 -2
  92. package/src/theme/ApiExplorer/FormLabel/index.tsx +1 -2
  93. package/src/theme/ApiExplorer/FormTextInput/index.tsx +1 -2
  94. package/src/theme/ApiExplorer/LiveEditor/index.tsx +1 -2
  95. package/src/theme/ApiExplorer/ParamOptions/ParamFormItems/ParamArrayFormItem.tsx +5 -3
  96. package/src/theme/ApiExplorer/ParamOptions/ParamFormItems/ParamBooleanFormItem.tsx +20 -4
  97. package/src/theme/ApiExplorer/ParamOptions/ParamFormItems/ParamMultiSelectFormItem.tsx +1 -2
  98. package/src/theme/ApiExplorer/ParamOptions/ParamFormItems/ParamSelectFormItem.tsx +15 -4
  99. package/src/theme/ApiExplorer/ParamOptions/ParamFormItems/ParamTextFormItem.tsx +11 -1
  100. package/src/theme/ApiExplorer/ParamOptions/index.tsx +2 -3
  101. package/src/theme/ApiExplorer/Request/index.tsx +23 -18
  102. package/src/theme/ApiExplorer/Response/index.tsx +63 -9
  103. package/src/theme/ApiExplorer/SecuritySchemes/index.tsx +60 -52
  104. package/src/theme/ApiExplorer/Server/index.tsx +8 -3
  105. package/src/theme/ApiItem/index.tsx +43 -21
  106. package/src/theme/ApiTabs/index.tsx +8 -8
  107. package/src/theme/DiscriminatorTabs/index.tsx +6 -5
  108. package/src/theme/Example/index.tsx +3 -4
  109. package/src/theme/MimeTabs/index.tsx +9 -8
  110. package/src/theme/OperationTabs/index.tsx +5 -4
  111. package/src/theme/ParamsDetails/index.tsx +1 -2
  112. package/src/theme/ParamsItem/index.tsx +13 -8
  113. package/src/theme/RequestSchema/index.tsx +38 -40
  114. package/src/theme/ResponseExamples/index.tsx +3 -4
  115. package/src/theme/ResponseSchema/index.tsx +16 -17
  116. package/src/theme/Schema/index.tsx +156 -27
  117. package/src/theme/SchemaExpansion/_SchemaExpansion.scss +113 -0
  118. package/src/theme/SchemaExpansion/context.tsx +154 -0
  119. package/src/theme/SchemaExpansion/index.tsx +236 -0
  120. package/src/theme/SchemaItem/index.tsx +18 -10
  121. package/src/theme/SchemaTabs/index.tsx +6 -5
  122. package/src/theme/StatusCodes/index.tsx +2 -3
  123. package/src/theme/TabItem/index.tsx +61 -0
  124. package/src/theme/TabItem/styles.module.css +3 -0
  125. package/src/theme/Tabs/index.tsx +164 -0
  126. package/src/theme/Tabs/styles.module.css +7 -0
  127. package/src/theme/styles.scss +1 -0
  128. package/src/theme/translationIds.ts +37 -106
  129. package/src/theme/utils/codeBlockUtils.ts +296 -0
  130. package/src/theme/utils/reactUtils.ts +22 -0
  131. package/src/theme/utils/scrollUtils.tsx +153 -0
  132. package/src/theme/utils/tabsUtils.tsx +329 -0
  133. package/src/theme/utils/useCodeWordWrap.ts +110 -0
  134. package/src/theme/utils/useMutationObserver.ts +43 -0
  135. package/src/theme-classic.d.ts +0 -96
  136. package/src/types.d.ts +27 -0
  137. package/tsconfig.tsbuildinfo +1 -1
@@ -0,0 +1,153 @@
1
+ /* ============================================================================
2
+ * Portions Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ * Portions Copyright (c) Palo Alto Networks
4
+ *
5
+ * Vendored subset of @docusaurus/theme-common/src/utils/scrollUtils.tsx (MIT)
6
+ * to remove the dependency on @docusaurus/theme-common/internal. Only the
7
+ * ScrollControllerProvider + useScrollPositionBlocker surface is kept, since
8
+ * that's all our tab renderers need. The ScrollControllerProvider must be
9
+ * mounted in the React tree above any consumer of useScrollPositionBlocker
10
+ * (see ApiItem/index.tsx).
11
+ * See: https://github.com/PaloAltoNetworks/docusaurus-openapi-docs/issues/1140
12
+ *
13
+ * This source code is licensed under the MIT license found in the
14
+ * LICENSE file in the root directory of this source tree.
15
+ * ========================================================================== */
16
+
17
+ import React, {
18
+ useCallback,
19
+ useContext,
20
+ useMemo,
21
+ useRef,
22
+ type ReactNode,
23
+ } from "react";
24
+
25
+ import useIsomorphicLayoutEffect from "@docusaurus/useIsomorphicLayoutEffect";
26
+ import { ReactContextError } from "@docusaurus/theme-common";
27
+
28
+ type ScrollController = {
29
+ scrollEventsEnabledRef: React.RefObject<boolean>;
30
+ enableScrollEvents: () => void;
31
+ disableScrollEvents: () => void;
32
+ };
33
+
34
+ function useScrollControllerContextValue(): ScrollController {
35
+ const scrollEventsEnabledRef = useRef(true);
36
+
37
+ return useMemo(
38
+ () => ({
39
+ scrollEventsEnabledRef,
40
+ enableScrollEvents: () => {
41
+ scrollEventsEnabledRef.current = true;
42
+ },
43
+ disableScrollEvents: () => {
44
+ scrollEventsEnabledRef.current = false;
45
+ },
46
+ }),
47
+ []
48
+ );
49
+ }
50
+
51
+ const ScrollMonitorContext = React.createContext<ScrollController | undefined>(
52
+ undefined
53
+ );
54
+
55
+ export function ScrollControllerProvider({
56
+ children,
57
+ }: {
58
+ children: ReactNode;
59
+ }): ReactNode {
60
+ const value = useScrollControllerContextValue();
61
+ return (
62
+ <ScrollMonitorContext.Provider value={value}>
63
+ {children}
64
+ </ScrollMonitorContext.Provider>
65
+ );
66
+ }
67
+
68
+ function useScrollController(): ScrollController {
69
+ const context = useContext(ScrollMonitorContext);
70
+ if (context == null) {
71
+ throw new ReactContextError("ScrollControllerProvider");
72
+ }
73
+ return context;
74
+ }
75
+
76
+ type UseScrollPositionSaver = {
77
+ save: (elem: HTMLElement) => void;
78
+ restore: () => { restored: boolean };
79
+ };
80
+
81
+ function useScrollPositionSaver(): UseScrollPositionSaver {
82
+ const lastElementRef = useRef<{ elem: HTMLElement | null; top: number }>({
83
+ elem: null,
84
+ top: 0,
85
+ });
86
+
87
+ const save = useCallback((elem: HTMLElement) => {
88
+ lastElementRef.current = {
89
+ elem,
90
+ top: elem.getBoundingClientRect().top,
91
+ };
92
+ }, []);
93
+
94
+ const restore = useCallback(() => {
95
+ const {
96
+ current: { elem, top },
97
+ } = lastElementRef;
98
+ if (!elem) {
99
+ return { restored: false };
100
+ }
101
+ const newTop = elem.getBoundingClientRect().top;
102
+ const heightDiff = newTop - top;
103
+ if (heightDiff) {
104
+ window.scrollBy({ left: 0, top: heightDiff });
105
+ }
106
+ lastElementRef.current = { elem: null, top: 0 };
107
+
108
+ return { restored: heightDiff !== 0 };
109
+ }, []);
110
+
111
+ return useMemo(() => ({ save, restore }), [restore, save]);
112
+ }
113
+
114
+ export function useScrollPositionBlocker(): {
115
+ blockElementScrollPositionUntilNextRender: (el: HTMLElement) => void;
116
+ } {
117
+ const scrollController = useScrollController();
118
+ const scrollPositionSaver = useScrollPositionSaver();
119
+
120
+ const nextLayoutEffectCallbackRef = useRef<(() => void) | undefined>(
121
+ undefined
122
+ );
123
+
124
+ const blockElementScrollPositionUntilNextRender = useCallback(
125
+ (el: HTMLElement) => {
126
+ scrollPositionSaver.save(el);
127
+ scrollController.disableScrollEvents();
128
+ nextLayoutEffectCallbackRef.current = () => {
129
+ const { restored } = scrollPositionSaver.restore();
130
+ nextLayoutEffectCallbackRef.current = undefined;
131
+
132
+ if (restored) {
133
+ const handleScrollRestoreEvent = () => {
134
+ scrollController.enableScrollEvents();
135
+ window.removeEventListener("scroll", handleScrollRestoreEvent);
136
+ };
137
+ window.addEventListener("scroll", handleScrollRestoreEvent);
138
+ } else {
139
+ scrollController.enableScrollEvents();
140
+ }
141
+ };
142
+ },
143
+ [scrollController, scrollPositionSaver]
144
+ );
145
+
146
+ useIsomorphicLayoutEffect(() => {
147
+ queueMicrotask(() => nextLayoutEffectCallbackRef.current?.());
148
+ });
149
+
150
+ return {
151
+ blockElementScrollPositionUntilNextRender,
152
+ };
153
+ }
@@ -0,0 +1,329 @@
1
+ /* ============================================================================
2
+ * Portions Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ * Portions Copyright (c) Palo Alto Networks
4
+ *
5
+ * Vendored from @docusaurus/theme-common/src/utils/tabsUtils.tsx (MIT) to
6
+ * remove the dependency on @docusaurus/theme-common/internal. The
7
+ * useQueryStringValue dependency from theme-common's historyUtils is inlined
8
+ * below to avoid pulling another internal module.
9
+ * See: https://github.com/PaloAltoNetworks/docusaurus-openapi-docs/issues/1140
10
+ *
11
+ * This source code is licensed under the MIT license found in the
12
+ * LICENSE file in the root directory of this source tree.
13
+ * ========================================================================== */
14
+
15
+ import React, {
16
+ createContext,
17
+ isValidElement,
18
+ useCallback,
19
+ useMemo,
20
+ useState,
21
+ useSyncExternalStore,
22
+ type ReactElement,
23
+ type ReactNode,
24
+ } from "react";
25
+
26
+ import { useHistory } from "@docusaurus/router";
27
+ import { duplicates, useStorageSlot } from "@docusaurus/theme-common";
28
+ import useIsomorphicLayoutEffect from "@docusaurus/useIsomorphicLayoutEffect";
29
+
30
+ import { ScrollControllerProvider } from "./scrollUtils";
31
+
32
+ export interface TabValue {
33
+ readonly value: string;
34
+ readonly label?: string;
35
+ readonly attributes?: { [key: string]: unknown };
36
+ readonly default?: boolean;
37
+ }
38
+
39
+ export interface TabsProps {
40
+ readonly lazy?: boolean;
41
+ readonly block?: boolean;
42
+ readonly children: ReactNode;
43
+ readonly defaultValue?: string | null;
44
+ readonly values?: readonly TabValue[];
45
+ readonly groupId?: string;
46
+ readonly className?: string;
47
+ readonly queryString?: string | boolean;
48
+ }
49
+
50
+ // Extended Tabs type used across the OpenAPI theme; preserves the historical
51
+ // shape that included an optional `length` field.
52
+ export interface TabProps extends TabsProps {
53
+ length?: number;
54
+ }
55
+
56
+ export interface TabItemProps {
57
+ readonly children: ReactNode;
58
+ readonly value: string;
59
+ readonly default?: boolean;
60
+ readonly label?: string;
61
+ readonly className?: string;
62
+ readonly attributes?: { [key: string]: unknown };
63
+ }
64
+
65
+ export function sanitizeTabsChildren(children: ReactNode): ReactNode {
66
+ return React.Children.toArray(children).filter((child) => child !== "\n");
67
+ }
68
+
69
+ function extractChildrenTabValues(children: ReactNode): TabValue[] {
70
+ function isTabItemWithValueProp(
71
+ comp: ReactElement
72
+ ): comp is ReactElement<TabItemProps> {
73
+ const { props } = comp;
74
+ return !!props && typeof props === "object" && "value" in props;
75
+ }
76
+
77
+ const elements = React.Children.toArray(children).flatMap((child) => {
78
+ if (!child) {
79
+ return [];
80
+ }
81
+ if (isValidElement(child) && isTabItemWithValueProp(child)) {
82
+ return [child];
83
+ }
84
+ const badChildTypeName =
85
+ // @ts-expect-error: guarding against unexpected cases
86
+ typeof child.type === "string" ? child.type : child.type.name;
87
+ throw new Error(
88
+ `Docusaurus error: Bad <Tabs> child <${badChildTypeName}>: all children of the <Tabs> component should be <TabItem>, and every <TabItem> should have a unique "value" prop.
89
+ If you do not want to pass on a "value" prop to the direct children of <Tabs>, you can also pass an explicit <Tabs values={...}> prop.`
90
+ );
91
+ });
92
+
93
+ return elements.map(
94
+ ({ props: { value, label, attributes, default: isDefault } }) => ({
95
+ value,
96
+ label,
97
+ attributes,
98
+ default: isDefault,
99
+ })
100
+ );
101
+ }
102
+
103
+ function ensureNoDuplicateValue(values: readonly TabValue[]) {
104
+ const dup = duplicates(values, (a, b) => a.value === b.value);
105
+ if (dup.length > 0) {
106
+ throw new Error(
107
+ `Docusaurus error: Duplicate values "${dup
108
+ .map((a) => `'${a.value}'`)
109
+ .join(", ")}" found in <Tabs>. Every value needs to be unique.`
110
+ );
111
+ }
112
+ }
113
+
114
+ function useTabValues(
115
+ props: Pick<TabsProps, "values" | "children">
116
+ ): readonly TabValue[] {
117
+ const { values: valuesProp, children } = props;
118
+ return useMemo(() => {
119
+ const values = valuesProp ?? extractChildrenTabValues(children);
120
+ ensureNoDuplicateValue(values);
121
+ return values;
122
+ }, [valuesProp, children]);
123
+ }
124
+
125
+ function isValidValue({
126
+ value,
127
+ tabValues,
128
+ }: {
129
+ value: string | null | undefined;
130
+ tabValues: readonly TabValue[];
131
+ }) {
132
+ return tabValues.some((a) => a.value === value);
133
+ }
134
+
135
+ function getInitialStateValue({
136
+ defaultValue,
137
+ tabValues,
138
+ }: {
139
+ defaultValue: TabsProps["defaultValue"];
140
+ tabValues: readonly TabValue[];
141
+ }): string {
142
+ if (tabValues.length === 0) {
143
+ throw new Error(
144
+ "Docusaurus error: the <Tabs> component requires at least one <TabItem> children component"
145
+ );
146
+ }
147
+ if (defaultValue) {
148
+ if (!isValidValue({ value: defaultValue, tabValues })) {
149
+ throw new Error(
150
+ `Docusaurus error: The <Tabs> has a defaultValue "${defaultValue}" but none of its children has the corresponding value. Available values are: ${tabValues
151
+ .map((a) => a.value)
152
+ .join(
153
+ ", "
154
+ )}. If you intend to show no default tab, use defaultValue={null} instead.`
155
+ );
156
+ }
157
+ return defaultValue;
158
+ }
159
+ const defaultTabValue =
160
+ tabValues.find((tabValue) => tabValue.default) ?? tabValues[0];
161
+ if (!defaultTabValue) {
162
+ throw new Error("Unexpected error: 0 tabValues");
163
+ }
164
+ return defaultTabValue.value;
165
+ }
166
+
167
+ function getStorageKey(groupId: string | undefined) {
168
+ if (!groupId) {
169
+ return null;
170
+ }
171
+ return `docusaurus.tab.${groupId}`;
172
+ }
173
+
174
+ function getQueryStringKey({
175
+ queryString = false,
176
+ groupId,
177
+ }: Pick<TabsProps, "queryString" | "groupId">) {
178
+ if (typeof queryString === "string") {
179
+ return queryString;
180
+ }
181
+ if (queryString === false) {
182
+ return null;
183
+ }
184
+ if (queryString === true && !groupId) {
185
+ throw new Error(
186
+ `Docusaurus error: The <Tabs> component groupId prop is required if queryString=true, because this value is used as the search param name. You can also provide an explicit value such as queryString="my-search-param".`
187
+ );
188
+ }
189
+ return groupId ?? null;
190
+ }
191
+
192
+ // Inlined from @docusaurus/theme-common/internal historyUtils.useQueryStringValue
193
+ function useQueryStringValue(key: string | null): string | null {
194
+ const history = useHistory();
195
+ return useSyncExternalStore(
196
+ history.listen,
197
+ () =>
198
+ key === null
199
+ ? null
200
+ : new URLSearchParams(history.location.search).get(key),
201
+ () => null
202
+ );
203
+ }
204
+
205
+ function useTabQueryString({
206
+ queryString = false,
207
+ groupId,
208
+ }: Pick<TabsProps, "queryString" | "groupId">) {
209
+ const history = useHistory();
210
+ const key = getQueryStringKey({ queryString, groupId });
211
+ const value = useQueryStringValue(key);
212
+
213
+ const setValue = useCallback(
214
+ (newValue: string) => {
215
+ if (!key) {
216
+ return;
217
+ }
218
+ const searchParams = new URLSearchParams(history.location.search);
219
+ searchParams.set(key, newValue);
220
+ history.replace({ ...history.location, search: searchParams.toString() });
221
+ },
222
+ [key, history]
223
+ );
224
+
225
+ return [value, setValue] as const;
226
+ }
227
+
228
+ function useTabStorage({ groupId }: Pick<TabsProps, "groupId">) {
229
+ const key = getStorageKey(groupId);
230
+ const [value, storageSlot] = useStorageSlot(key);
231
+
232
+ const setValue = useCallback(
233
+ (newValue: string) => {
234
+ if (!key) {
235
+ return;
236
+ }
237
+ storageSlot.set(newValue);
238
+ },
239
+ [key, storageSlot]
240
+ );
241
+
242
+ return [value, setValue] as const;
243
+ }
244
+
245
+ type TabsContextValue = {
246
+ selectedValue: string;
247
+ selectValue: (value: string) => void;
248
+ tabValues: readonly TabValue[];
249
+ lazy: boolean;
250
+ block: boolean;
251
+ };
252
+
253
+ export function useTabsContextValue(props: TabsProps): TabsContextValue {
254
+ const { defaultValue, queryString = false, groupId } = props;
255
+ const tabValues = useTabValues(props);
256
+
257
+ const [selectedValue, setSelectedValue] = useState(() =>
258
+ getInitialStateValue({ defaultValue, tabValues })
259
+ );
260
+
261
+ const [queryStringValue, setQueryString] = useTabQueryString({
262
+ queryString,
263
+ groupId,
264
+ });
265
+
266
+ const [storageValue, setStorageValue] = useTabStorage({
267
+ groupId,
268
+ });
269
+
270
+ const valueToSync = (() => {
271
+ const value = queryStringValue ?? storageValue;
272
+ if (!isValidValue({ value, tabValues })) {
273
+ return null;
274
+ }
275
+ return value;
276
+ })();
277
+
278
+ useIsomorphicLayoutEffect(() => {
279
+ if (valueToSync) {
280
+ setSelectedValue(valueToSync);
281
+ }
282
+ }, [valueToSync]);
283
+
284
+ const selectValue = useCallback(
285
+ (newValue: string) => {
286
+ if (!isValidValue({ value: newValue, tabValues })) {
287
+ throw new Error(`Can't select invalid tab value=${newValue}`);
288
+ }
289
+ setSelectedValue(newValue);
290
+ setQueryString(newValue);
291
+ setStorageValue(newValue);
292
+ },
293
+ [setQueryString, setStorageValue, tabValues]
294
+ );
295
+
296
+ return {
297
+ selectedValue,
298
+ selectValue,
299
+ tabValues,
300
+ lazy: props.lazy ?? false,
301
+ block: props.block ?? false,
302
+ };
303
+ }
304
+
305
+ const TabsContext = createContext<TabsContextValue | null>(null);
306
+
307
+ export function useTabs(): TabsContextValue {
308
+ const contextValue = React.useContext(TabsContext);
309
+ if (!contextValue) {
310
+ throw new Error("useTabsContext() must be used within a Tabs component");
311
+ }
312
+ return contextValue;
313
+ }
314
+
315
+ export function TabsProvider(props: {
316
+ children: ReactNode;
317
+ value: TabsContextValue;
318
+ }): ReactNode {
319
+ // ScrollControllerProvider is mounted here so every tab consumer
320
+ // (our six OpenAPI tab variants + the swizzled @theme/Tabs) gets a working
321
+ // useScrollPositionBlocker without callers needing a separate provider.
322
+ return (
323
+ <ScrollControllerProvider>
324
+ <TabsContext.Provider value={props.value}>
325
+ {props.children}
326
+ </TabsContext.Provider>
327
+ </ScrollControllerProvider>
328
+ );
329
+ }
@@ -0,0 +1,110 @@
1
+ /* ============================================================================
2
+ * Portions Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ * Portions Copyright (c) Palo Alto Networks
4
+ *
5
+ * Vendored from @docusaurus/theme-common/src/hooks/useCodeWordWrap.ts (MIT)
6
+ * to remove the dependency on @docusaurus/theme-common/internal.
7
+ * See: https://github.com/PaloAltoNetworks/docusaurus-openapi-docs/issues/1140
8
+ *
9
+ * This source code is licensed under the MIT license found in the
10
+ * LICENSE file in the root directory of this source tree.
11
+ * ========================================================================== */
12
+
13
+ import { useCallback, useEffect, useRef, useState } from "react";
14
+ import type { RefObject } from "react";
15
+
16
+ import { useMutationObserver } from "./useMutationObserver";
17
+
18
+ // Callback fires when the "hidden" attribute of a tabpanel changes
19
+ // See https://github.com/facebook/docusaurus/pull/7485
20
+ function useTabBecameVisibleCallback(
21
+ codeBlockRef: RefObject<HTMLPreElement | null>,
22
+ callback: () => void
23
+ ) {
24
+ const [hiddenTabElement, setHiddenTabElement] = useState<
25
+ Element | null | undefined
26
+ >();
27
+
28
+ const updateHiddenTabElement = useCallback(() => {
29
+ setHiddenTabElement(
30
+ codeBlockRef.current?.closest("[role=tabpanel][hidden]")
31
+ );
32
+ }, [codeBlockRef, setHiddenTabElement]);
33
+
34
+ useEffect(() => {
35
+ updateHiddenTabElement();
36
+ }, [updateHiddenTabElement]);
37
+
38
+ useMutationObserver(
39
+ hiddenTabElement,
40
+ (mutations: MutationRecord[]) => {
41
+ mutations.forEach((mutation) => {
42
+ if (
43
+ mutation.type === "attributes" &&
44
+ mutation.attributeName === "hidden"
45
+ ) {
46
+ callback();
47
+ updateHiddenTabElement();
48
+ }
49
+ });
50
+ },
51
+ {
52
+ attributes: true,
53
+ characterData: false,
54
+ childList: false,
55
+ subtree: false,
56
+ }
57
+ );
58
+ }
59
+
60
+ export type WordWrap = {
61
+ readonly codeBlockRef: RefObject<HTMLPreElement | null>;
62
+ readonly isEnabled: boolean;
63
+ readonly isCodeScrollable: boolean;
64
+ readonly toggle: () => void;
65
+ };
66
+
67
+ export function useCodeWordWrap(): WordWrap {
68
+ const [isEnabled, setIsEnabled] = useState(false);
69
+ const [isCodeScrollable, setIsCodeScrollable] = useState<boolean>(false);
70
+ const codeBlockRef = useRef<HTMLPreElement>(null);
71
+
72
+ const toggle = useCallback(() => {
73
+ const codeElement = codeBlockRef.current!.querySelector("code")!;
74
+
75
+ if (isEnabled) {
76
+ codeElement.removeAttribute("style");
77
+ } else {
78
+ codeElement.style.whiteSpace = "pre-wrap";
79
+ codeElement.style.overflowWrap = "anywhere";
80
+ }
81
+
82
+ setIsEnabled((value) => !value);
83
+ }, [codeBlockRef, isEnabled]);
84
+
85
+ const updateCodeIsScrollable = useCallback(() => {
86
+ const { scrollWidth, clientWidth } = codeBlockRef.current!;
87
+ const isScrollable =
88
+ scrollWidth > clientWidth ||
89
+ codeBlockRef.current!.querySelector("code")!.hasAttribute("style");
90
+ setIsCodeScrollable(isScrollable);
91
+ }, [codeBlockRef]);
92
+
93
+ useTabBecameVisibleCallback(codeBlockRef, updateCodeIsScrollable);
94
+
95
+ useEffect(() => {
96
+ updateCodeIsScrollable();
97
+ }, [isEnabled, updateCodeIsScrollable]);
98
+
99
+ useEffect(() => {
100
+ window.addEventListener("resize", updateCodeIsScrollable, {
101
+ passive: true,
102
+ });
103
+
104
+ return () => {
105
+ window.removeEventListener("resize", updateCodeIsScrollable);
106
+ };
107
+ }, [updateCodeIsScrollable]);
108
+
109
+ return { codeBlockRef, isEnabled, isCodeScrollable, toggle };
110
+ }
@@ -0,0 +1,43 @@
1
+ /* ============================================================================
2
+ * Portions Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ * Portions Copyright (c) Palo Alto Networks
4
+ *
5
+ * Vendored from @docusaurus/theme-common/src/hooks/useMutationObserver.ts (MIT)
6
+ * to remove the dependency on @docusaurus/theme-common/internal.
7
+ * See: https://github.com/PaloAltoNetworks/docusaurus-openapi-docs/issues/1140
8
+ *
9
+ * This source code is licensed under the MIT license found in the
10
+ * LICENSE file in the root directory of this source tree.
11
+ * ========================================================================== */
12
+
13
+ import { useEffect } from "react";
14
+
15
+ import { useEvent } from "@docusaurus/theme-common";
16
+
17
+ import { useShallowMemoObject } from "./reactUtils";
18
+
19
+ type Options = MutationObserverInit;
20
+
21
+ const DefaultOptions: Options = {
22
+ attributes: true,
23
+ characterData: true,
24
+ childList: true,
25
+ subtree: true,
26
+ };
27
+
28
+ export function useMutationObserver(
29
+ target: Element | undefined | null,
30
+ callback: MutationCallback,
31
+ options: Options = DefaultOptions
32
+ ): void {
33
+ const stableCallback = useEvent(callback);
34
+ const stableOptions: Options = useShallowMemoObject(options);
35
+
36
+ useEffect(() => {
37
+ const observer = new MutationObserver(stableCallback);
38
+ if (target) {
39
+ observer.observe(target, stableOptions);
40
+ }
41
+ return () => observer.disconnect();
42
+ }, [target, stableCallback, stableOptions]);
43
+ }