Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/docusaurus-theme-classic/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"@docusaurus/utils-validation": "3.9.2",
"@mdx-js/react": "^3.0.0",
"clsx": "^2.0.0",
"copy-text-to-clipboard": "^3.2.0",
"infima": "0.2.0-alpha.45",
"lodash": "^4.17.21",
"nprogress": "^0.2.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,19 @@ function ariaLabel(isCopied: boolean) {
});
}

async function copyToClipboard(text: string) {
// The clipboard API is only defined in secure contexts (HTTPS / localhost).
// See https://developer.mozilla.org/en-US/docs/Web/API/Clipboard
if (navigator.clipboard) {
return navigator.clipboard.writeText(text);
}
// Fall back to copy-text-to-clipboard for non-secure contexts (e.g. HTTP
// on a local network). The fallback is lazily loaded to avoid bundle
// overhead for the common HTTPS case.
const {default: copy} = await import('copy-text-to-clipboard');
return copy(text);
}

function useCopyButton() {
const {
metadata: {code},
Expand All @@ -52,12 +65,14 @@ function useCopyButton() {
const copyTimeout = useRef<number | undefined>(undefined);

const copyCode = useCallback(() => {
navigator.clipboard.writeText(code).then(() => {
copyToClipboard(code).then(() => {
setIsCopied(true);
copyTimeout.current = window.setTimeout(() => {
setIsCopied(false);
}, 1000);
});
// Errors are intentionally not caught so they remain unhandled and can
// be captured by observability tools (e.g. Sentry, PostHog).
}, [code]);

useEffect(() => () => window.clearTimeout(copyTimeout.current), []);
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6907,6 +6907,11 @@ cookie@~0.4.1:
resolved "https://registry.yarnpkg.com/cookie/-/cookie-0.4.2.tgz#0e41f24de5ecf317947c82fc789e06a884824432"
integrity sha512-aSWTXFzaKWkvHO1Ny/s+ePFpvKsPnjc551iI41v3ny/ow6tBG5Vd+FuqGNhh1LxOmVzOlGUriIlOaokOvhaStA==

copy-text-to-clipboard@^3.2.0:
version "3.2.2"
resolved "https://registry.yarnpkg.com/copy-text-to-clipboard/-/copy-text-to-clipboard-3.2.2.tgz#99bc79db3f2d355ec33a08d573aff6804491ddb9"
integrity sha512-T6SqyLd1iLuqPA90J5N4cTalrtovCySh58iiZDGJ6FGznbclKh4UI+FGacQSgFzwKG77W7XT5gwbVEbd9cIH1A==

copy-webpack-plugin@^11.0.0:
version "11.0.0"
resolved "https://registry.yarnpkg.com/copy-webpack-plugin/-/copy-webpack-plugin-11.0.0.tgz#96d4dbdb5f73d02dd72d0528d1958721ab72e04a"
Expand Down
Loading