Skip to content

Commit 5e4ea4a

Browse files
committed
add icon button to copy icon React name
1 parent 2520666 commit 5e4ea4a

File tree

3 files changed

+71
-28
lines changed

3 files changed

+71
-28
lines changed

.storybook/components/Icons.module.css

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,14 @@
9595
transform: translate(-50%, -50%) rotate(-30deg);
9696
}
9797

98-
.copy {
98+
.actions {
9999
position: absolute;
100100
right: 50%;
101101
bottom: 0;
102102
display: none;
103103
transform: translateX(50%);
104104
}
105105

106-
.wrapper:hover .copy {
107-
display: block;
106+
.wrapper:hover .actions {
107+
display: flex;
108108
}

.storybook/components/Icons.tsx

Lines changed: 65 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import {
2121
} from 'react';
2222
import { Unstyled } from '@storybook/addon-docs/blocks';
2323
import * as iconComponents from '@sumup-oss/icons';
24-
import type { IconComponentType } from '@sumup-oss/icons';
24+
import { getIconURL, type IconComponentType } from '@sumup-oss/icons';
2525
import type { IconsManifest } from '@sumup-oss/icons';
2626
import iconsManifest from '@sumup-oss/icons/manifest.json';
2727
import { Badge } from '../../packages/circuit-ui/components/Badge/Badge.js';
@@ -38,7 +38,7 @@ import { clsx } from '../../packages/circuit-ui/styles/clsx.js';
3838
import { utilClasses } from '../../packages/circuit-ui/styles/utility.js';
3939
import { slugify } from '../slugify.js';
4040
import classes from './Icons.module.css';
41-
41+
import ReactIcon from '../public/images/react.svg';
4242
function groupBy(
4343
icons: IconsManifest['icons'],
4444
key: 'name' | 'category' | 'size',
@@ -218,21 +218,50 @@ function Icon({
218218
});
219219
};
220220

221+
const copyIconReactName = () => {
222+
const reactName = getComponentName(icon.name);
223+
navigator.clipboard
224+
.writeText(reactName)
225+
.then(() => {
226+
setToast({
227+
variant: 'success',
228+
body: `Copied the ${icon.name} icon's React name to the clipboard.`,
229+
});
230+
})
231+
.catch((error) => {
232+
console.error(error);
233+
setToast({
234+
variant: 'danger',
235+
body: `Failed to copy the ${icon.name} icon's React name to the clipboard.`,
236+
});
237+
});
238+
};
239+
221240
return (
222241
<div className={classes.wrapper}>
223242
<div className={clsx(classes['icon-wrapper'], classes[scale])}>
224-
<Icon
225-
aria-labelledby={id}
226-
size={icon.size}
227-
className={classes.icon}
228-
style={{
229-
color,
230-
backgroundColor:
231-
color === 'var(--cui-fg-on-strong)'
232-
? 'var(--cui-bg-strong)'
233-
: 'var(--cui-bg-normal)',
234-
}}
235-
/>
243+
{icon.skipComponentFile ? (
244+
<img
245+
src={getIconURL(icon.name)}
246+
aria-labelledby={id}
247+
alt={icon.name}
248+
height={14}
249+
width={18}
250+
/>
251+
) : (
252+
<Icon
253+
aria-labelledby={id}
254+
size={icon.size}
255+
className={classes.icon}
256+
style={{
257+
color,
258+
backgroundColor:
259+
color === 'var(--cui-fg-on-strong)'
260+
? 'var(--cui-bg-strong)'
261+
: 'var(--cui-bg-normal)',
262+
}}
263+
/>
264+
)}
236265
</div>
237266
<span id={id} className={classes.label}>
238267
{icon.name}
@@ -254,17 +283,28 @@ function Icon({
254283
)}
255284
/>
256285
)}
257-
{navigator.clipboard && (
258-
<IconButton
259-
variant="tertiary"
260-
size="s"
261-
icon={iconComponents.Link}
262-
className={classes.copy}
263-
onClick={copyIconURL}
264-
>
265-
Copy URL
266-
</IconButton>
267-
)}
286+
<div className={classes.actions}>
287+
{navigator.clipboard && (
288+
<IconButton
289+
variant="tertiary"
290+
size="s"
291+
icon={iconComponents.Link}
292+
onClick={copyIconURL}
293+
>
294+
Copy URL
295+
</IconButton>
296+
)}
297+
{!icon.skipComponentFile && (
298+
<IconButton
299+
variant="tertiary"
300+
size="s"
301+
icon={() => <img src={ReactIcon} alt="React Logo" />}
302+
onClick={copyIconReactName}
303+
>
304+
Copy React component name
305+
</IconButton>
306+
)}
307+
</div>
268308
</div>
269309
);
270310
}

.storybook/public/images/react.svg

Lines changed: 3 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)