File tree Expand file tree Collapse file tree
packages/docusaurus-theme-classic
src/theme/CodeBlock/Buttons/CopyButton Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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" ,
Original file line number Diff line number Diff 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+
4760function 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 ) , [ ] ) ;
Original file line number Diff line number Diff 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+
69106915copy-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"
You can’t perform that action at this time.
0 commit comments