docusaurus-theme-openapi-docs 5.0.0 → 5.0.2

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.
@@ -71,14 +71,25 @@ exports.default = CopyButton;
71
71
  const react_1 = __importStar(require("react"));
72
72
  const Translate_1 = require("@docusaurus/Translate");
73
73
  const clsx_1 = __importDefault(require("clsx"));
74
- const copy_text_to_clipboard_1 = __importDefault(
75
- require("copy-text-to-clipboard")
76
- );
74
+ async function copyToClipboard(text) {
75
+ // The clipboard API is only defined in secure contexts (HTTPS / localhost).
76
+ // See https://developer.mozilla.org/en-US/docs/Web/API/Clipboard
77
+ if (navigator.clipboard) {
78
+ return navigator.clipboard.writeText(text);
79
+ }
80
+ // Fall back to copy-text-to-clipboard for non-secure contexts (e.g. HTTP
81
+ // on a local network). The fallback is lazily loaded to avoid bundle
82
+ // overhead for the common HTTPS case.
83
+ const { default: copy } = await Promise.resolve().then(() =>
84
+ __importStar(require("copy-text-to-clipboard"))
85
+ );
86
+ return copy(text);
87
+ }
77
88
  function CopyButton({ code, className }) {
78
89
  const [isCopied, setIsCopied] = (0, react_1.useState)(false);
79
90
  const copyTimeout = (0, react_1.useRef)(undefined);
80
- const handleCopyCode = (0, react_1.useCallback)(() => {
81
- (0, copy_text_to_clipboard_1.default)(code);
91
+ const handleCopyCode = (0, react_1.useCallback)(async () => {
92
+ await copyToClipboard(code);
82
93
  setIsCopied(true);
83
94
  copyTimeout.current = window.setTimeout(() => {
84
95
  setIsCopied(false);
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": "5.0.0",
4
+ "version": "5.0.2",
5
5
  "license": "MIT",
6
6
  "keywords": [
7
7
  "openapi",
@@ -38,7 +38,7 @@
38
38
  "@types/postman-collection": "^3.5.11",
39
39
  "@types/react-modal": "^3.16.3",
40
40
  "concurrently": "^9.2.0",
41
- "docusaurus-plugin-openapi-docs": "^5.0.0",
41
+ "docusaurus-plugin-openapi-docs": "^5.0.2",
42
42
  "docusaurus-plugin-sass": "^0.2.6",
43
43
  "eslint-plugin-prettier": "^5.5.1"
44
44
  },
@@ -82,5 +82,5 @@
82
82
  "engines": {
83
83
  "node": ">=14"
84
84
  },
85
- "gitHead": "516bd48c628fb16b7fe90d8f996168ab75ce5562"
85
+ "gitHead": "2cfff9eee1b2fadb15c21bf8eff21c32121b16e7"
86
86
  }
@@ -9,21 +9,33 @@ import React, { useCallback, useState, useRef, useEffect } from "react";
9
9
 
10
10
  import { translate } from "@docusaurus/Translate";
11
11
  import clsx from "clsx";
12
- import copy from "copy-text-to-clipboard";
13
12
 
14
13
  interface CopyButtonProps {
15
14
  code: string;
16
15
  className?: string;
17
16
  }
18
17
 
18
+ async function copyToClipboard(text: string) {
19
+ // The clipboard API is only defined in secure contexts (HTTPS / localhost).
20
+ // See https://developer.mozilla.org/en-US/docs/Web/API/Clipboard
21
+ if (navigator.clipboard) {
22
+ return navigator.clipboard.writeText(text);
23
+ }
24
+ // Fall back to copy-text-to-clipboard for non-secure contexts (e.g. HTTP
25
+ // on a local network). The fallback is lazily loaded to avoid bundle
26
+ // overhead for the common HTTPS case.
27
+ const { default: copy } = await import("copy-text-to-clipboard");
28
+ return copy(text);
29
+ }
30
+
19
31
  export default function CopyButton({
20
32
  code,
21
33
  className,
22
34
  }: CopyButtonProps): React.JSX.Element {
23
35
  const [isCopied, setIsCopied] = useState(false);
24
36
  const copyTimeout = useRef<number | undefined>(undefined);
25
- const handleCopyCode = useCallback(() => {
26
- copy(code);
37
+ const handleCopyCode = useCallback(async () => {
38
+ await copyToClipboard(code);
27
39
  setIsCopied(true);
28
40
  copyTimeout.current = window.setTimeout(() => {
29
41
  setIsCopied(false);