docusaurus-theme-openapi-docs 1.0.3 → 1.0.6

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 (46) hide show
  1. package/lib/theme/ApiDemoPanel/Body/index.js +11 -9
  2. package/lib/theme/ApiDemoPanel/Body/json2xml.js +43 -0
  3. package/lib/theme/ApiDemoPanel/CodeTabs/index.js +188 -0
  4. package/lib/theme/ApiDemoPanel/CodeTabs/styles.module.css +11 -0
  5. package/lib/theme/ApiDemoPanel/Curl/index.js +41 -110
  6. package/lib/theme/ApiDemoPanel/LiveEditor/index.js +61 -0
  7. package/lib/theme/ApiDemoPanel/LiveEditor/styles.module.css +34 -0
  8. package/lib/theme/ApiItem/icons/bash-original.svg +1 -0
  9. package/lib/theme/ApiItem/icons/go-original-wordmark.svg +1 -0
  10. package/lib/theme/ApiItem/icons/javascript-original.svg +1 -0
  11. package/lib/theme/ApiItem/icons/linux-original.svg +1 -0
  12. package/lib/theme/ApiItem/icons/python-original.svg +1 -0
  13. package/lib/theme/ApiItem/styles.module.css +89 -0
  14. package/lib-next/theme/ApiDemoPanel/Body/index.js +11 -15
  15. package/lib-next/theme/ApiDemoPanel/Body/json2xml.js +43 -0
  16. package/lib-next/theme/ApiDemoPanel/CodeTabs/index.js +214 -0
  17. package/lib-next/theme/ApiDemoPanel/CodeTabs/styles.module.css +11 -0
  18. package/lib-next/theme/ApiDemoPanel/Curl/index.js +46 -135
  19. package/lib-next/theme/ApiDemoPanel/LiveEditor/index.js +49 -0
  20. package/lib-next/theme/ApiDemoPanel/LiveEditor/styles.module.css +34 -0
  21. package/lib-next/theme/ApiItem/icons/bash-original.svg +1 -0
  22. package/lib-next/theme/ApiItem/icons/go-original-wordmark.svg +1 -0
  23. package/lib-next/theme/ApiItem/icons/javascript-original.svg +1 -0
  24. package/lib-next/theme/ApiItem/icons/linux-original.svg +1 -0
  25. package/lib-next/theme/ApiItem/icons/python-original.svg +1 -0
  26. package/lib-next/theme/ApiItem/styles.module.css +89 -0
  27. package/package.json +4 -6
  28. package/src/theme/ApiDemoPanel/Body/index.tsx +9 -13
  29. package/src/theme/ApiDemoPanel/Body/json2xml.js +43 -0
  30. package/src/theme/ApiDemoPanel/CodeTabs/index.tsx +237 -0
  31. package/src/theme/ApiDemoPanel/CodeTabs/styles.module.css +11 -0
  32. package/src/theme/ApiDemoPanel/Curl/index.tsx +45 -124
  33. package/src/theme/ApiDemoPanel/LiveEditor/index.tsx +59 -0
  34. package/src/theme/ApiDemoPanel/LiveEditor/styles.module.css +34 -0
  35. package/src/theme/ApiItem/icons/bash-original.svg +1 -0
  36. package/src/theme/ApiItem/icons/go-original-wordmark.svg +1 -0
  37. package/src/theme/ApiItem/icons/javascript-original.svg +1 -0
  38. package/src/theme/ApiItem/icons/linux-original.svg +1 -0
  39. package/src/theme/ApiItem/icons/python-original.svg +1 -0
  40. package/src/theme/ApiItem/styles.module.css +89 -0
  41. package/lib/theme/ApiDemoPanel/VSCode/index.js +0 -252
  42. package/lib/theme/ApiDemoPanel/VSCode/styles.module.css +0 -19
  43. package/lib-next/theme/ApiDemoPanel/VSCode/index.js +0 -265
  44. package/lib-next/theme/ApiDemoPanel/VSCode/styles.module.css +0 -19
  45. package/src/theme/ApiDemoPanel/VSCode/index.tsx +0 -205
  46. package/src/theme/ApiDemoPanel/VSCode/styles.module.css +0 -19
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "docusaurus-theme-openapi-docs",
3
3
  "description": "OpenAPI theme for Docusaurus.",
4
- "version": "1.0.3",
4
+ "version": "1.0.6",
5
5
  "license": "MIT",
6
6
  "keywords": [
7
7
  "openapi",
@@ -44,19 +44,17 @@
44
44
  "dependencies": {
45
45
  "@docusaurus/theme-common": "2.0.0-beta.21",
46
46
  "@mdx-js/react": "^1.6.21",
47
- "@monaco-editor/react": "^4.3.1",
48
47
  "@paloaltonetworks/postman-code-generators": "1.1.5-hotfix.5",
49
48
  "@paloaltonetworks/postman-collection": "^4.1.0",
50
49
  "@reduxjs/toolkit": "^1.7.1",
51
50
  "buffer": "^6.0.3",
52
51
  "clsx": "^1.1.1",
53
52
  "crypto-js": "^4.1.1",
54
- "docusaurus-plugin-openapi-docs": "^1.0.3",
53
+ "docusaurus-plugin-openapi-docs": "^1.0.6",
55
54
  "immer": "^9.0.7",
56
55
  "lodash": "^4.17.20",
57
- "monaco-editor": "^0.31.1",
58
- "prism-react-renderer": "^1.2.1",
59
56
  "process": "^0.11.10",
57
+ "react-live": "^3.1.1",
60
58
  "react-magic-dropzone": "^1.0.1",
61
59
  "react-markdown": "^8.0.1",
62
60
  "react-redux": "^7.2.0",
@@ -70,5 +68,5 @@
70
68
  "engines": {
71
69
  "node": ">=14"
72
70
  },
73
- "gitHead": "618a438ec42148f525fb92cb11d291ce50d2ad06"
71
+ "gitHead": "ff7622e334e96e7f39b0daf5ef2cb17bfa832834"
74
72
  }
@@ -12,17 +12,18 @@ import { RequestBodyObject } from "docusaurus-plugin-openapi-docs/src/openapi/ty
12
12
  import ContentType from "../ContentType";
13
13
  import FormSelect from "../FormSelect";
14
14
  import { useTypedDispatch, useTypedSelector } from "../hooks";
15
+ import LiveApp from "../LiveEditor";
15
16
  import FormFileUpload from "./../FormFileUpload";
16
17
  import FormItem from "./../FormItem";
17
18
  import FormTextInput from "./../FormTextInput";
18
- import VSCode from "./../VSCode";
19
+ // @ts-ignore
20
+ import json2xml from "./json2xml";
19
21
  import {
20
22
  clearFormBodyKey,
21
23
  clearRawBody,
22
24
  setFileFormBody,
23
25
  setFileRawBody,
24
26
  setStringFormBody,
25
- setStringRawBody,
26
27
  } from "./slice";
27
28
 
28
29
  interface Props {
@@ -195,22 +196,17 @@ function Body({ requestBodyMetadata, jsonRequestBodyExample }: Props) {
195
196
  }
196
197
 
197
198
  if (contentType === "application/xml") {
199
+ if (jsonRequestBodyExample) {
200
+ exampleBodyString = json2xml(jsonRequestBodyExample);
201
+ }
198
202
  language = "xml";
199
203
  }
200
204
 
201
205
  return (
202
206
  <FormItem label="Body">
203
- <VSCode
204
- value={exampleBodyString}
205
- language={language}
206
- onChange={(value) => {
207
- if (value.trim() === "") {
208
- dispatch(clearRawBody());
209
- return;
210
- }
211
- dispatch(setStringRawBody(value));
212
- }}
213
- />
207
+ <LiveApp action={dispatch} language={language}>
208
+ {exampleBodyString}
209
+ </LiveApp>
214
210
  </FormItem>
215
211
  );
216
212
  }
@@ -0,0 +1,43 @@
1
+ /* ============================================================================
2
+ * Copyright (c) Palo Alto Networks
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ * ========================================================================== */
7
+
8
+ export default function json2xml(o, tab) {
9
+ var toXml = function (v, name, ind) {
10
+ var xml = "";
11
+ if (v instanceof Array) {
12
+ for (var i = 0, n = v.length; i < n; i++)
13
+ xml += ind + toXml(v[i], name, ind + "\t") + "\n";
14
+ } else if (typeof v == "object") {
15
+ var hasChild = false;
16
+ xml += ind + "<" + name;
17
+ for (var m in v) {
18
+ if (m.charAt(0) === "@")
19
+ xml += " " + m.substr(1) + '="' + v[m].toString() + '"';
20
+ else hasChild = true;
21
+ }
22
+ xml += hasChild ? ">" : "/>";
23
+ if (hasChild) {
24
+ for (var m2 in v) {
25
+ if (m2 === "#text") xml += v[m2];
26
+ else if (m2 === "#cdata") xml += "<![CDATA[" + v[m2] + "]]>";
27
+ else if (m2.charAt(0) !== "@") xml += toXml(v[m2], m2, ind + "\t");
28
+ }
29
+ xml +=
30
+ (xml.charAt(xml.length - 1) === "\n" ? ind : "") +
31
+ "</" +
32
+ name +
33
+ ">";
34
+ }
35
+ } else {
36
+ xml += ind + "<" + name + ">" + v.toString() + "</" + name + ">";
37
+ }
38
+ return xml;
39
+ },
40
+ xml = "";
41
+ for (var m3 in o) xml += toXml(o[m3], m3, "");
42
+ return tab ? xml.replace(/\t/g, tab) : xml.replace(/\t|\n/g, "");
43
+ }
@@ -0,0 +1,237 @@
1
+ /* ============================================================================
2
+ * Copyright (c) Palo Alto Networks
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ * ========================================================================== */
7
+
8
+ import React, {
9
+ useState,
10
+ cloneElement,
11
+ isValidElement,
12
+ type ReactElement,
13
+ } from "react";
14
+
15
+ import {
16
+ useScrollPositionBlocker,
17
+ duplicates,
18
+ useTabGroupChoice,
19
+ } from "@docusaurus/theme-common";
20
+ import useIsBrowser from "@docusaurus/useIsBrowser";
21
+ import type { Props as TabItemProps } from "@theme/TabItem";
22
+ import clsx from "clsx";
23
+
24
+ import { languageSet } from "../Curl";
25
+ import styles from "./styles.module.css";
26
+
27
+ // A very rough duck type, but good enough to guard against mistakes while
28
+ // allowing customization
29
+ function isTabItem(
30
+ comp: ReactElement<object>
31
+ ): comp is ReactElement<TabItemProps> {
32
+ return "value" in comp.props;
33
+ }
34
+
35
+ export interface Props {
36
+ readonly lazy?: boolean;
37
+ readonly block?: boolean;
38
+ readonly children: readonly ReactElement<TabItemProps>[];
39
+ readonly defaultValue?: string | null;
40
+ readonly values?: readonly {
41
+ value: string;
42
+ label?: string;
43
+ attributes?: { [key: string]: unknown };
44
+ }[];
45
+ readonly groupId?: string;
46
+ readonly className?: string;
47
+ readonly action?: any;
48
+ }
49
+
50
+ function TabsComponent(props: Props): JSX.Element {
51
+ const {
52
+ lazy,
53
+ block,
54
+ defaultValue: defaultValueProp,
55
+ values: valuesProp,
56
+ groupId,
57
+ className,
58
+ action,
59
+ } = props;
60
+ const children = React.Children.map(props.children, (child) => {
61
+ if (isValidElement(child) && isTabItem(child)) {
62
+ return child;
63
+ }
64
+ // child.type.name will give non-sensical values in prod because of
65
+ // minification, but we assume it won't throw in prod.
66
+ throw new Error(
67
+ `Docusaurus error: Bad <Tabs> child <${
68
+ // @ts-expect-error: guarding against unexpected cases
69
+ typeof child.type === "string" ? child.type : child.type.name
70
+ }>: all children of the <Tabs> component should be <TabItem>, and every <TabItem> should have a unique "value" prop.`
71
+ );
72
+ });
73
+ const values =
74
+ valuesProp ??
75
+ // Only pick keys that we recognize. MDX would inject some keys by default
76
+ children.map(({ props: { value, label, attributes } }) => ({
77
+ value,
78
+ label,
79
+ attributes,
80
+ }));
81
+ const dup = duplicates(values, (a, b) => a.value === b.value);
82
+ if (dup.length > 0) {
83
+ throw new Error(
84
+ `Docusaurus error: Duplicate values "${dup
85
+ .map((a) => a.value)
86
+ .join(", ")}" found in <Tabs>. Every value needs to be unique.`
87
+ );
88
+ }
89
+ // When defaultValueProp is null, don't show a default tab
90
+ const defaultValue =
91
+ defaultValueProp === null
92
+ ? defaultValueProp
93
+ : defaultValueProp ??
94
+ children.find((child) => child.props.default)?.props.value ??
95
+ children[0]!.props.value;
96
+ if (defaultValue !== null && !values.some((a) => a.value === defaultValue)) {
97
+ throw new Error(
98
+ `Docusaurus error: The <Tabs> has a defaultValue "${defaultValue}" but none of its children has the corresponding value. Available values are: ${values
99
+ .map((a) => a.value)
100
+ .join(
101
+ ", "
102
+ )}. If you intend to show no default tab, use defaultValue={null} instead.`
103
+ );
104
+ }
105
+
106
+ const { tabGroupChoices, setTabGroupChoices } = useTabGroupChoice();
107
+ const [selectedValue, setSelectedValue] = useState(defaultValue);
108
+ const tabRefs: (HTMLLIElement | null)[] = [];
109
+ const { blockElementScrollPositionUntilNextRender } =
110
+ useScrollPositionBlocker();
111
+
112
+ if (groupId != null) {
113
+ const relevantTabGroupChoice = tabGroupChoices[groupId];
114
+ if (
115
+ relevantTabGroupChoice != null &&
116
+ relevantTabGroupChoice !== selectedValue &&
117
+ values.some((value) => value.value === relevantTabGroupChoice)
118
+ ) {
119
+ setSelectedValue(relevantTabGroupChoice);
120
+ }
121
+ }
122
+
123
+ const handleTabChange = (
124
+ event: React.FocusEvent<HTMLLIElement> | React.MouseEvent<HTMLLIElement>
125
+ ) => {
126
+ const newTab = event.currentTarget;
127
+ const newTabIndex = tabRefs.indexOf(newTab);
128
+ const newTabValue = values[newTabIndex]!.value;
129
+
130
+ if (newTabValue !== selectedValue) {
131
+ blockElementScrollPositionUntilNextRender(newTab);
132
+ setSelectedValue(newTabValue);
133
+ if (action) {
134
+ const newLanguage = languageSet.filter(
135
+ (lang) => lang.language === newTabValue
136
+ );
137
+ action(newLanguage[0]);
138
+ }
139
+
140
+ if (groupId != null) {
141
+ setTabGroupChoices(groupId, String(newTabValue));
142
+ }
143
+ }
144
+ };
145
+
146
+ const handleKeydown = (event: React.KeyboardEvent<HTMLLIElement>) => {
147
+ let focusElement: HTMLLIElement | null = null;
148
+
149
+ switch (event.key) {
150
+ case "ArrowRight": {
151
+ const nextTab = tabRefs.indexOf(event.currentTarget) + 1;
152
+ focusElement = tabRefs[nextTab] ?? tabRefs[0]!;
153
+ break;
154
+ }
155
+ case "ArrowLeft": {
156
+ const prevTab = tabRefs.indexOf(event.currentTarget) - 1;
157
+ focusElement = tabRefs[prevTab] ?? tabRefs[tabRefs.length - 1]!;
158
+ break;
159
+ }
160
+ default:
161
+ break;
162
+ }
163
+
164
+ focusElement?.focus();
165
+ };
166
+
167
+ return (
168
+ <div className={clsx("tabs-container", styles.tabList)}>
169
+ <ul
170
+ role="tablist"
171
+ aria-orientation="horizontal"
172
+ className={clsx(
173
+ "tabs",
174
+ {
175
+ "tabs--block": block,
176
+ },
177
+ styles.code__tabs,
178
+ className
179
+ )}
180
+ >
181
+ {values.map(({ value, label, attributes }) => (
182
+ <li
183
+ role="tab"
184
+ tabIndex={selectedValue === value ? 0 : -1}
185
+ aria-selected={selectedValue === value}
186
+ key={value}
187
+ ref={(tabControl) => tabRefs.push(tabControl)}
188
+ onKeyDown={handleKeydown}
189
+ onFocus={handleTabChange}
190
+ onClick={handleTabChange}
191
+ {...attributes}
192
+ className={clsx(
193
+ "tabs__item",
194
+ styles.tabItem,
195
+ attributes?.className as string,
196
+ {
197
+ "tabs__item--active": selectedValue === value,
198
+ }
199
+ )}
200
+ >
201
+ {label ?? value}
202
+ </li>
203
+ ))}
204
+ </ul>
205
+
206
+ {lazy ? (
207
+ cloneElement(
208
+ children.filter(
209
+ (tabItem) => tabItem.props.value === selectedValue
210
+ )[0]!,
211
+ { className: "margin-top--md" }
212
+ )
213
+ ) : (
214
+ <div className="margin-top--md">
215
+ {children.map((tabItem, i) =>
216
+ cloneElement(tabItem, {
217
+ key: i,
218
+ hidden: tabItem.props.value !== selectedValue,
219
+ })
220
+ )}
221
+ </div>
222
+ )}
223
+ </div>
224
+ );
225
+ }
226
+
227
+ export default function CodeTabs(props: Props): JSX.Element {
228
+ const isBrowser = useIsBrowser();
229
+ return (
230
+ <TabsComponent
231
+ // Remount tabs after hydration
232
+ // Temporary fix for https://github.com/facebook/docusaurus/issues/5653
233
+ key={String(isBrowser)}
234
+ {...props}
235
+ />
236
+ );
237
+ }
@@ -0,0 +1,11 @@
1
+ .tabList {
2
+ margin-bottom: var(--ifm-leading);
3
+ }
4
+
5
+ .tabItem {
6
+ margin-top: 0 !important;
7
+ }
8
+
9
+ .code__tabs {
10
+ justify-content: space-around;
11
+ }
@@ -5,32 +5,35 @@
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  * ========================================================================== */
7
7
 
8
- import React, { useRef, useState, useEffect } from "react";
8
+ import React, { useState, useEffect } from "react";
9
9
 
10
10
  import useDocusaurusContext from "@docusaurus/useDocusaurusContext";
11
11
  import codegen from "@paloaltonetworks/postman-code-generators";
12
12
  import sdk from "@paloaltonetworks/postman-collection";
13
+ import CodeBlock from "@theme/CodeBlock";
13
14
  import clsx from "clsx";
14
- import Highlight, { defaultProps } from "prism-react-renderer";
15
15
 
16
+ import CodeTabs from "../CodeTabs";
16
17
  import { useTypedSelector } from "../hooks";
17
18
  import buildPostmanRequest from "./../buildPostmanRequest";
18
- import FloatingButton from "./../FloatingButton";
19
19
  import styles from "./styles.module.css";
20
20
 
21
21
  interface Language {
22
22
  tabName: string;
23
23
  highlight: string;
24
24
  language: string;
25
+ codeLanguage?: string;
25
26
  variant: string;
26
27
  options: { [key: string]: boolean };
28
+ source?: string;
27
29
  }
28
30
 
29
- const languageSet: Language[] = [
31
+ export const languageSet: Language[] = [
30
32
  {
31
33
  tabName: "cURL",
32
34
  highlight: "bash",
33
35
  language: "curl",
36
+ codeLanguage: "bash",
34
37
  variant: "curl",
35
38
  options: {
36
39
  longFormat: false,
@@ -39,12 +42,12 @@ const languageSet: Language[] = [
39
42
  },
40
43
  },
41
44
  {
42
- tabName: "Node",
43
- highlight: "javascript",
44
- language: "nodejs",
45
- variant: "axios",
45
+ tabName: "Python",
46
+ highlight: "python",
47
+ language: "python",
48
+ codeLanguage: "python",
49
+ variant: "requests",
46
50
  options: {
47
- ES6_enabled: true,
48
51
  followRedirect: true,
49
52
  trimRequestBody: true,
50
53
  },
@@ -53,6 +56,7 @@ const languageSet: Language[] = [
53
56
  tabName: "Go",
54
57
  highlight: "go",
55
58
  language: "go",
59
+ codeLanguage: "go",
56
60
  variant: "native",
57
61
  options: {
58
62
  followRedirect: true,
@@ -60,79 +64,41 @@ const languageSet: Language[] = [
60
64
  },
61
65
  },
62
66
  {
63
- tabName: "Python",
64
- highlight: "python",
65
- language: "python",
66
- variant: "requests",
67
+ tabName: "Node",
68
+ highlight: "javascript",
69
+ language: "nodejs",
70
+ codeLanguage: "javascript",
71
+ variant: "axios",
67
72
  options: {
73
+ ES6_enabled: true,
68
74
  followRedirect: true,
69
75
  trimRequestBody: true,
70
76
  },
71
77
  },
72
78
  ];
73
79
 
74
- const languageTheme = {
75
- plain: {
76
- color: "var(--ifm-code-color)",
77
- },
78
- styles: [
79
- {
80
- types: ["inserted", "attr-name"],
81
- style: {
82
- color: "var(--openapi-code-green)",
83
- },
84
- },
85
- {
86
- types: ["string", "url"],
87
- style: {
88
- color: "var(--openapi-code-green)",
89
- },
90
- },
91
- {
92
- types: ["builtin", "char", "constant", "function"],
93
- style: {
94
- color: "var(--openapi-code-blue)",
95
- },
96
- },
97
- {
98
- types: ["punctuation", "operator"],
99
- style: {
100
- color: "var(--openapi-code-dim)",
101
- },
102
- },
103
- {
104
- types: ["class-name"],
105
- style: {
106
- color: "var(--openapi-code-orange)",
107
- },
108
- },
109
- {
110
- types: ["tag", "arrow", "keyword"],
111
- style: {
112
- color: "var(--openapi-code-purple)",
113
- },
114
- },
115
- {
116
- types: ["boolean"],
117
- style: {
118
- color: "var(--openapi-code-red)",
119
- },
120
- },
121
- ],
122
- };
123
-
124
80
  interface Props {
125
81
  postman: sdk.Request;
126
82
  codeSamples: any; // TODO: Type this...
127
83
  }
128
84
 
85
+ function CodeTab({ children, hidden, className, onClick }: any): JSX.Element {
86
+ return (
87
+ <div
88
+ role="tabpanel"
89
+ className={clsx(styles.tabItem, className)}
90
+ {...{ hidden }}
91
+ >
92
+ {children}
93
+ </div>
94
+ );
95
+ }
96
+
129
97
  function Curl({ postman, codeSamples }: Props) {
130
98
  // TODO: match theme for vscode.
131
99
 
132
100
  const { siteConfig } = useDocusaurusContext();
133
101
 
134
- const [copyText, setCopyText] = useState("Copy");
135
-
136
102
  const contentType = useTypedSelector((state) => state.contentType.value);
137
103
  const accept = useTypedSelector((state) => state.accept.value);
138
104
  const server = useTypedSelector((state) => state.server.value);
@@ -152,7 +118,12 @@ function Curl({ postman, codeSamples }: Props) {
152
118
  ...codeSamples,
153
119
  ];
154
120
 
155
- const [language, setLanguage] = useState(langs[0]);
121
+ const defaultLang: Language[] = languageSet.filter(
122
+ (lang) =>
123
+ lang.codeLanguage === localStorage.getItem("docusaurus.tab.code-samples")
124
+ );
125
+
126
+ const [language, setLanguage] = useState(defaultLang[0] ?? langs[0]);
156
127
 
157
128
  const [codeText, setCodeText] = useState("");
158
129
 
@@ -201,76 +172,26 @@ function Curl({ postman, codeSamples }: Props) {
201
172
  auth,
202
173
  ]);
203
174
 
204
- const ref = useRef<HTMLDivElement>(null);
205
-
206
- const handleCurlCopy = () => {
207
- setCopyText("Copied");
208
- setTimeout(() => {
209
- setCopyText("Copy");
210
- }, 2000);
211
- if (ref.current?.innerText) {
212
- navigator.clipboard.writeText(ref.current.innerText);
213
- }
214
- };
215
-
216
175
  if (language === undefined) {
217
176
  return null;
218
177
  }
219
178
 
220
179
  return (
221
180
  <>
222
- <div className={clsx(styles.buttonGroup, "api-code-tab-group")}>
181
+ <CodeTabs groupId="code-samples" action={setLanguage}>
223
182
  {langs.map((lang) => {
224
183
  return (
225
- <button
184
+ <CodeTab
185
+ value={lang.language}
186
+ label={""}
226
187
  key={lang.tabName || lang.label}
227
- className={clsx(
228
- language === lang ? styles.selected : undefined,
229
- language === lang ? "api-code-tab--active" : undefined,
230
- "api-code-tab"
231
- )}
232
- onClick={() => setLanguage(lang)}
188
+ attributes={{ className: `code__tab--${lang.codeLanguage}` }}
233
189
  >
234
- {lang.tabName || lang.label}
235
- </button>
190
+ <CodeBlock language={lang.codeLanguage}>{codeText}</CodeBlock>
191
+ </CodeTab>
236
192
  );
237
193
  })}
238
- </div>
239
-
240
- <Highlight
241
- {...defaultProps}
242
- theme={languageTheme}
243
- code={codeText}
244
- language={language.highlight || language.lang}
245
- >
246
- {({ className, tokens, getLineProps, getTokenProps }) => (
247
- <FloatingButton onClick={handleCurlCopy} label={copyText}>
248
- <pre
249
- className={className}
250
- style={{
251
- background: "var(--openapi-card-background-color)",
252
- paddingRight: "60px",
253
- borderRadius:
254
- "2px 2px var(--openapi-card-border-radius) var(--openapi-card-border-radius)",
255
- }}
256
- >
257
- <code ref={ref}>
258
- {tokens.map((line, i) => (
259
- <span {...getLineProps({ line, key: i })}>
260
- {line.map((token, key) => {
261
- if (token.types.includes("arrow")) {
262
- token.types = ["arrow"];
263
- }
264
- return <span {...getTokenProps({ token, key })} />;
265
- })}
266
- {"\n"}
267
- </span>
268
- ))}
269
- </code>
270
- </pre>
271
- </FloatingButton>
272
- )}
273
- </Highlight>
194
+ </CodeTabs>
274
195
  </>
275
196
  );
276
197
  }
@@ -0,0 +1,59 @@
1
+ /* ============================================================================
2
+ * Copyright (c) Palo Alto Networks
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ * ========================================================================== */
7
+
8
+ import React from "react";
9
+
10
+ import { usePrismTheme } from "@docusaurus/theme-common";
11
+ import useIsBrowser from "@docusaurus/useIsBrowser";
12
+ import { LiveProvider, LiveEditor, withLive } from "react-live";
13
+
14
+ import { setStringRawBody } from "../Body/slice";
15
+ import styles from "./styles.module.css";
16
+
17
+ function Live({ onEdit }: any) {
18
+ const isBrowser = useIsBrowser();
19
+ return (
20
+ <>
21
+ <LiveEditor
22
+ key={String(isBrowser)}
23
+ className={styles.playgroundEditor}
24
+ onChange={onEdit}
25
+ />
26
+ </>
27
+ );
28
+ }
29
+
30
+ const LiveComponent = withLive(Live);
31
+
32
+ function App({
33
+ children,
34
+ transformCode,
35
+ value,
36
+ language,
37
+ action,
38
+ ...props
39
+ }: any): JSX.Element {
40
+ const prismTheme = usePrismTheme();
41
+ const [code, setCode] = React.useState(children);
42
+ action(setStringRawBody(code));
43
+ return (
44
+ <div className={styles.playgroundContainer}>
45
+ <LiveProvider
46
+ code={children.replace(/\n$/, "")}
47
+ transformCode={transformCode ?? ((code) => `${code};`)}
48
+ theme={prismTheme}
49
+ language={language}
50
+ {...props}
51
+ >
52
+ <LiveComponent onEdit={setCode} />
53
+ </LiveProvider>
54
+ </div>
55
+ );
56
+ }
57
+
58
+ const LiveApp = withLive(App);
59
+ export default LiveApp;