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

feat(icon): update copy icon on website #731

Closed
wants to merge 1 commit into from
Closed
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
88 changes: 55 additions & 33 deletions components/icons/AllIcons.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
import React from 'react';
import * as Icons from '@radix-ui/react-icons';
import { Grid, Tooltip, Heading, Box, IconButton, Flex } from '@radix-ui/themes';
import {
Grid,
Tooltip,
Heading,
Box,
IconButton,
Flex,
DropdownMenu,
Text,
} from '@radix-ui/themes';
import { useCopyToast } from './CopyToast';

import styles from './AllIcons.module.css';
Expand Down Expand Up @@ -47,41 +56,54 @@ type CopyButtonProps = {
const CopyButton = ({ children, label }: CopyButtonProps) => {
const { showCopyToast } = useCopyToast();

return (
<Tooltip className="radix-themes-custom-fonts" content={label} side="top" sideOffset={5}>
<IconButton
highContrast
variant="ghost"
size="4"
onClick={(event: React.MouseEvent) => {
const svg = event.currentTarget.querySelector('svg');
const code = svg ? svg.outerHTML : null;
const handleCopyCode = React.useCallback(
(code) => {
navigator.clipboard.writeText(code);
showCopyToast(code);
},
[label, showCopyToast]
);

// Copy code to clipboard via a hidden textarea element
if (code) {
// Temporary shim until a proper focus-visible handler is added
if (document.activeElement instanceof HTMLButtonElement) {
document.activeElement.blur();
}
const handleCopyLabel = React.useCallback(() => {
const formattedLabel = label
.split(' ')
.map((word) => word[0].toUpperCase() + word.slice(1))
.join('')
.concat('Icon');

const textarea = document.createElement('textarea');
textarea.value = code.toString();
textarea.setAttribute('readonly', '');
textarea.style.position = 'absolute';
textarea.style.left = '-9999px';
document.body.appendChild(textarea);
textarea.select();
document.execCommand('copy');
document.body.removeChild(textarea);
navigator.clipboard.writeText(formattedLabel);
showCopyToast(formattedLabel);
}, [label, showCopyToast]);

return (
<DropdownMenu.Root>
<Tooltip className="radix-themes-custom-fonts" content={label} side="top" sideOffset={5}>
<DropdownMenu.Trigger>
<IconButton highContrast variant="ghost" size="4">
{children}
</IconButton>
</DropdownMenu.Trigger>
</Tooltip>
<DropdownMenu.Content>
<DropdownMenu.Item
onClick={(event: React.MouseEvent) => {
const svg = event.currentTarget.querySelector('svg');
const code = svg ? svg.outerHTML : null;

// Show CopyToast and set latest icon
showCopyToast(code);
}
}}
>
{children}
</IconButton>
</Tooltip>
code ? handleCopyCode(code) : null;
}}
>
Copy SVG <Text hidden>{children}</Text>
</DropdownMenu.Item>
<DropdownMenu.Item
onClick={() => {
handleCopyLabel();
}}
>
Copy label
</DropdownMenu.Item>
</DropdownMenu.Content>
</DropdownMenu.Root>
);
};

Expand Down