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
9 changes: 6 additions & 3 deletions src/components/CopyPageButton/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ function reportCopyEvent(params) {

export default function CopyPageButton({permalink, docsVersion}) {
const [state, setState] = useState('idle');
const isCloud = permalink === '/cloud' || permalink.startsWith('/cloud/');
const resetTimer = useRef(undefined);
const mounted = useRef(true);
useEffect(() => {
Expand All @@ -143,7 +144,6 @@ export default function CopyPageButton({permalink, docsVersion}) {
window.clearTimeout(resetTimer.current);
setState('loading');
const markdownPath = mdPath(permalink);
const isCloud = permalink === '/cloud' || permalink.startsWith('/cloud/');
const finish = (ok, error) => {
if (error) {
// Surface the cause (missing .md, clipboard denial) so a "Copy
Expand All @@ -166,7 +166,7 @@ export default function CopyPageButton({permalink, docsVersion}) {
() => finish(true),
(error) => finish(false, error),
);
}, [state, permalink, docsVersion]);
}, [state, permalink, docsVersion, isCloud]);

// aria-busy + click guard instead of `disabled`, which would drop keyboard
// focus; the live region sits outside the button so screen readers still
Expand All @@ -180,7 +180,10 @@ export default function CopyPageButton({permalink, docsVersion}) {
})}
onClick={handleClick}
aria-busy={state === 'loading'}
title={title()}>
title={title()}
data-attr="copy-page-markdown"
data-docs-instance={isCloud ? 'cloud' : 'framework'}
data-docs-version={docsVersion}>
<span className={styles.copyPageButtonIcon} aria-hidden="true">
{state === 'copied' ? <IconCheck /> : <IconCopy />}
</span>
Expand Down
16 changes: 13 additions & 3 deletions src/theme/CodeBlock/Buttons/CopyButton/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
* Swizzled from @docusaurus/theme-classic to strip leading shell prompts
* (`$ `) from text written to the clipboard. The prompt remains visible
* in the rendered code block, but the copied text is the runnable command.
* Also reports each copy to Google Analytics as a `copy_code_block` event.
* Also reports each copy to Google Analytics as a `copy_code_block` event,
* and tags the button with `data-*` attributes that PostHog autocapture
* records on every click.
*/
import React, {useCallback, useState, useRef, useEffect} from 'react';
import clsx from 'clsx';
Expand Down Expand Up @@ -93,15 +95,23 @@ function useCopyButton() {
});
}, [code, language, blockTitle]);
useEffect(() => () => window.clearTimeout(copyTimeout.current), []);
return {copyCode, isCopied};
return {
copyCode,
isCopied,
language,
blockTitle: typeof blockTitle === 'string' ? blockTitle : undefined,
};
}

export default function CopyButton({className}) {
const {copyCode, isCopied} = useCopyButton();
const {copyCode, isCopied, language, blockTitle} = useCopyButton();
return (
<Button
aria-label={ariaLabel(isCopied)}
title={title()}
data-attr="copy-code"
data-block-title={blockTitle}
data-language={language}
className={clsx(
className,
styles.copyButton,
Expand Down
Loading