Skip to content

Commit 8a2f526

Browse files
dmoranpslorber
andauthored
fix(theme): restore copy-text-to-clipboard as lazy fallback for non-secure contexts (facebook#11796)
Co-authored-by: sebastien <lorber.sebastien@gmail.com>
1 parent 2a7f8b9 commit 8a2f526

3 files changed

Lines changed: 22 additions & 1 deletion

File tree

packages/docusaurus-theme-classic/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
"@docusaurus/utils-validation": "3.9.2",
3636
"@mdx-js/react": "^3.0.0",
3737
"clsx": "^2.0.0",
38+
"copy-text-to-clipboard": "^3.2.0",
3839
"infima": "0.2.0-alpha.45",
3940
"lodash": "^4.17.21",
4041
"nprogress": "^0.2.0",

packages/docusaurus-theme-classic/src/theme/CodeBlock/Buttons/CopyButton/index.tsx

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,19 @@ function ariaLabel(isCopied: boolean) {
4444
});
4545
}
4646

47+
async function copyToClipboard(text: string) {
48+
// The clipboard API is only defined in secure contexts (HTTPS / localhost).
49+
// See https://developer.mozilla.org/en-US/docs/Web/API/Clipboard
50+
if (navigator.clipboard) {
51+
return navigator.clipboard.writeText(text);
52+
}
53+
// Fall back to copy-text-to-clipboard for non-secure contexts (e.g. HTTP
54+
// on a local network). The fallback is lazily loaded to avoid bundle
55+
// overhead for the common HTTPS case.
56+
const {default: copy} = await import('copy-text-to-clipboard');
57+
return copy(text);
58+
}
59+
4760
function useCopyButton() {
4861
const {
4962
metadata: {code},
@@ -52,12 +65,14 @@ function useCopyButton() {
5265
const copyTimeout = useRef<number | undefined>(undefined);
5366

5467
const copyCode = useCallback(() => {
55-
navigator.clipboard.writeText(code).then(() => {
68+
copyToClipboard(code).then(() => {
5669
setIsCopied(true);
5770
copyTimeout.current = window.setTimeout(() => {
5871
setIsCopied(false);
5972
}, 1000);
6073
});
74+
// Errors are intentionally not caught so they remain unhandled and can
75+
// be captured by observability tools (e.g. Sentry, PostHog).
6176
}, [code]);
6277

6378
useEffect(() => () => window.clearTimeout(copyTimeout.current), []);

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6907,6 +6907,11 @@ cookie@~0.4.1:
69076907
resolved "https://registry.yarnpkg.com/cookie/-/cookie-0.4.2.tgz#0e41f24de5ecf317947c82fc789e06a884824432"
69086908
integrity sha512-aSWTXFzaKWkvHO1Ny/s+ePFpvKsPnjc551iI41v3ny/ow6tBG5Vd+FuqGNhh1LxOmVzOlGUriIlOaokOvhaStA==
69096909

6910+
copy-text-to-clipboard@^3.2.0:
6911+
version "3.2.2"
6912+
resolved "https://registry.yarnpkg.com/copy-text-to-clipboard/-/copy-text-to-clipboard-3.2.2.tgz#99bc79db3f2d355ec33a08d573aff6804491ddb9"
6913+
integrity sha512-T6SqyLd1iLuqPA90J5N4cTalrtovCySh58iiZDGJ6FGznbclKh4UI+FGacQSgFzwKG77W7XT5gwbVEbd9cIH1A==
6914+
69106915
copy-webpack-plugin@^11.0.0:
69116916
version "11.0.0"
69126917
resolved "https://registry.yarnpkg.com/copy-webpack-plugin/-/copy-webpack-plugin-11.0.0.tgz#96d4dbdb5f73d02dd72d0528d1958721ab72e04a"

0 commit comments

Comments
 (0)