Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[docs-infra] Simplify copy logic #41167

Merged
Merged
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
28 changes: 16 additions & 12 deletions docs/src/modules/utils/CodeCopy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -160,24 +160,28 @@ export function CodeCopyProvider({ children }: CodeCopyProviderProps) {
const rootNode = React.useRef<HTMLDivElement | null>(null);
React.useEffect(() => {
document.addEventListener('keydown', (event) => {
if (hasNativeSelection(event.target as HTMLTextAreaElement)) {
// Skip if user is highlighting a text.
return;
}
// event.key === 'c' is not enough as alt+c can lead to ©, ç, or other characters on macOS.
// event.code === 'KeyC' is not enough as event.code assume a QWERTY keyboard layout which would
// be wrong with a Dvorak keyboard (as if pressing J).
const isModifierKeyPressed = event.ctrlKey || event.metaKey || event.altKey;
if (String.fromCharCode(event.keyCode) !== 'C' || !isModifierKeyPressed) {
if (!rootNode.current) {
return;
}
if (!rootNode.current) {

// Skip if user is highlighting a text.
if (hasNativeSelection(event.target as HTMLTextAreaElement)) {
return;
}
const copyBtn = rootNode.current.querySelector('.MuiCode-copy') as HTMLButtonElement | null;
if (!copyBtn) {

// Skip if it's not a copy keyboard event
if (
!(
(event.ctrlKey || event.metaKey) &&
event.key.toLowerCase() === 'c' &&
!event.shiftKey &&
!event.altKey
)
) {
return;
}

const copyBtn = rootNode.current.querySelector('.MuiCode-copy') as HTMLButtonElement;
const initialEventAction = copyBtn.getAttribute('data-ga-event-action');
// update the 'data-ga-event-action' on the button to track keyboard interaction
copyBtn.dataset.gaEventAction =
Expand Down
Loading