Repository navigation
feat(Icons): Introduce new tree shakable icon library #9113
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
Open
talissoncosta
wants to merge
6
commits into
main
Choose a base branch
from
feat/icon-library
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+3,225
−1
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e671692
feat(Icons): Add the Figma icon library as one component per icon
talissoncosta e6a663e
refactor(Icons): Keep the icon library in components/icons
talissoncosta e2a33d4
feat(Icons): Add the rest of the Figma icon library
talissoncosta 0970d67
feat(Icons): Generate the icons from Figma exports and hide decorativ…
talissoncosta b420a4d
fix(Icons): Run prettier without a shell in the icon generator
talissoncosta b4d1024
fix(Icons): Address review on the icon library
talissoncosta File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| import type { Meta, StoryObj } from 'storybook' | ||
| import * as icons from 'components/icons' | ||
| import type { IconComponent } from 'components/icons' | ||
|
|
||
| // eslint-disable-next-line @dword-design/import-alias/prefer-alias | ||
| import '../docs.scss' | ||
|
|
||
| const meta: Meta = { | ||
| parameters: { chromatic: { disableSnapshot: false }, layout: 'padded' }, | ||
| title: 'Components/Icon library', | ||
| } | ||
| export default meta | ||
|
|
||
| const ICONS = Object.entries(icons).filter( | ||
| (entry): entry is [string, IconComponent] => | ||
| entry[0] !== 'Icon' && typeof entry[1] === 'function', | ||
| ) | ||
|
|
||
| const SIZES = [14, 16, 20, 24] | ||
|
|
||
| export const Catalogue: StoryObj = { | ||
| render: () => ( | ||
| <div className='icon-catalogue__grid'> | ||
| {ICONS.map(([name, Svg]) => ( | ||
| <div className='icon-catalogue__card' key={name}> | ||
| <Svg /> | ||
| <code>{name}</code> | ||
| </div> | ||
| ))} | ||
| </div> | ||
| ), | ||
| } | ||
|
|
||
| // The 1.5px stroke scales with the icon, so small sizes draw thinner lines | ||
| export const Sizes: StoryObj = { | ||
| render: () => ( | ||
| <table> | ||
| <thead> | ||
| <tr> | ||
| <th /> | ||
| {SIZES.map((size) => ( | ||
| <th className='px-3 py-1' key={size}> | ||
| {size}px | ||
| </th> | ||
| ))} | ||
| </tr> | ||
| </thead> | ||
| <tbody> | ||
| {ICONS.map(([name, Svg]) => ( | ||
| <tr key={name}> | ||
| <td> | ||
| <code>{name}</code> | ||
| </td> | ||
| {SIZES.map((size) => ( | ||
| <td className='px-3 py-1' key={size}> | ||
| <Svg width={size} /> | ||
| </td> | ||
| ))} | ||
| </tr> | ||
| ))} | ||
| </tbody> | ||
| </table> | ||
| ), | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| /** | ||
| * Generate icon components from SVGs exported from the Figma icon library. | ||
| * | ||
| * Export each icon component from Figma as SVG, named like the component | ||
| * (trash.svg, chevron-down.svg), into one folder. Each SVG becomes | ||
| * web/components/icons/<Name>Icon.tsx, and the generated icons are | ||
| * re-exported from web/components/icons/index.ts. | ||
| * | ||
| * Usage: | ||
| * node scripts/generate-icons.mjs <folder of svgs> | ||
| * npm run generate:icons -- <folder of svgs> | ||
| */ | ||
|
|
||
| import { readFileSync, readdirSync, writeFileSync } from 'node:fs' | ||
| import { resolve, dirname, basename } from 'node:path' | ||
| import { fileURLToPath } from 'node:url' | ||
| import { execFileSync } from 'node:child_process' | ||
|
|
||
| const __dirname = dirname(fileURLToPath(import.meta.url)) | ||
| const ROOT = resolve(__dirname, '..') | ||
| const ICONS = resolve(ROOT, 'web/components/icons') | ||
| const HEADER = '// Generated by scripts/generate-icons.mjs' | ||
| const INDEX_MARKER = '// Icon library, generated by scripts/generate-icons.mjs' | ||
|
|
||
| const source = process.argv[2] | ||
| if (!source) { | ||
| console.error('Usage: node scripts/generate-icons.mjs <folder of svgs>') | ||
| process.exit(1) | ||
| } | ||
|
|
||
| const ATTRIBUTES = { | ||
| 'clip-rule': 'clipRule', | ||
| 'fill-rule': 'fillRule', | ||
| 'stroke-linecap': 'strokeLinecap', | ||
| 'stroke-linejoin': 'strokeLinejoin', | ||
| 'stroke-miterlimit': 'strokeMiterlimit', | ||
| 'stroke-width': 'strokeWidth', | ||
| } | ||
|
|
||
| const componentName = (file) => | ||
| `${basename(file, '.svg') | ||
| .split('-') | ||
| .map((part) => part[0].toUpperCase() + part.slice(1)) | ||
| .join('')}Icon` | ||
|
|
||
| // Figma exports may wrap the icon in its frame, so take the drawn shapes only | ||
| const toJsx = (svg) => | ||
| [ | ||
| ...svg.matchAll( | ||
| /<(path|circle|ellipse|line|polygon|polyline|rect)\b([^>]*?)\/?>/g, | ||
| ), | ||
| ] | ||
| .map(([, tag, attributes]) => { | ||
| const props = [...attributes.matchAll(/([\w-]+)="([^"]*)"/g)] | ||
| .filter(([, key]) => key !== 'id') | ||
| .map(([, key, value]) => | ||
| (key === 'stroke' || key === 'fill') && value.startsWith('#') | ||
| ? `${key}={fill}` | ||
| : `${ATTRIBUTES[key] ?? key}='${value}'`, | ||
| ) | ||
| return `<${tag} ${props.join(' ')} />` | ||
| }) | ||
| .join('\n') | ||
|
|
||
| const component = (name, shapes) => `${HEADER} | ||
| import { FC } from 'react' | ||
| import { IconProps } from './types' | ||
|
|
||
| const ${name}: FC<IconProps> = ({ | ||
| fill = 'currentColor', | ||
| height, | ||
| width = 24, | ||
| ...rest | ||
| }) => ( | ||
| <svg | ||
| width={width} | ||
| height={height ?? width} | ||
| viewBox='0 0 24 24' | ||
| fill='none' | ||
| xmlns='http://www.w3.org/2000/svg' | ||
| {...rest} | ||
| aria-hidden={rest['aria-label'] || rest['aria-labelledby'] ? undefined : true} | ||
| > | ||
| ${shapes} | ||
| </svg> | ||
| ) | ||
|
|
||
| export default ${name} | ||
| ` | ||
|
|
||
| const written = readdirSync(source) | ||
| .filter((file) => file.endsWith('.svg')) | ||
| .map((file) => { | ||
| const name = componentName(file) | ||
| const shapes = toJsx(readFileSync(resolve(source, file), 'utf-8')) | ||
| writeFileSync(resolve(ICONS, `${name}.tsx`), component(name, shapes)) | ||
| return `web/components/icons/${name}.tsx` | ||
| }) | ||
|
|
||
| // Re-export every generated icon, including ones from earlier runs | ||
| const generated = readdirSync(ICONS) | ||
| .filter( | ||
| (file) => | ||
| file.endsWith('Icon.tsx') && | ||
| readFileSync(resolve(ICONS, file), 'utf-8').startsWith(HEADER), | ||
| ) | ||
| .map((file) => basename(file, '.tsx')) | ||
| .sort() | ||
| const indexPath = resolve(ICONS, 'index.ts') | ||
| const index = readFileSync(indexPath, 'utf-8') | ||
| const handWritten = index.includes(INDEX_MARKER) | ||
| ? index.slice(0, index.indexOf(INDEX_MARKER)) | ||
| : index | ||
| writeFileSync( | ||
| indexPath, | ||
| `${handWritten.trimEnd()}\n\n${INDEX_MARKER}\n${generated | ||
| .map((name) => `export { default as ${name} } from './${name}'`) | ||
| .join('\n')}\nexport type { IconComponent, IconProps } from './types'\n`, | ||
| ) | ||
|
|
||
| execFileSync( | ||
| 'npx', | ||
| ['prettier', '--write', ...written, 'web/components/icons/index.ts'], | ||
| { cwd: ROOT, stdio: 'inherit' }, | ||
| ) | ||
| console.log(`Generated ${written.length} icons`) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| // Generated by scripts/generate-icons.mjs | ||
| import { FC } from 'react' | ||
| import { IconProps } from './types' | ||
|
|
||
| const AlertTriangleFilledIcon: FC<IconProps> = ({ | ||
| fill = 'currentColor', | ||
| height, | ||
| width = 24, | ||
| ...rest | ||
| }) => ( | ||
| <svg | ||
| width={width} | ||
| height={height ?? width} | ||
| viewBox='0 0 24 24' | ||
| fill='none' | ||
| xmlns='http://www.w3.org/2000/svg' | ||
| {...rest} | ||
| aria-hidden={ | ||
| rest['aria-label'] || rest['aria-labelledby'] ? undefined : true | ||
| } | ||
| > | ||
| <path | ||
| d='M9.63379 1.99365C11.1382 1.31439 12.8618 1.31441 14.3662 1.99365C15.3085 2.41915 16.0648 3.23887 16.8438 4.37256C17.6257 5.51077 18.5014 7.07432 19.6348 9.09814L19.9854 9.7251C21.0745 11.6699 21.9174 13.1739 22.46 14.3921C23.0012 15.6073 23.2965 16.649 23.1816 17.6538C22.9976 19.2622 22.1445 20.7191 20.8311 21.6655C20.0105 22.2567 18.9565 22.5079 17.6318 22.6294C16.3039 22.7511 14.5795 22.7505 12.3506 22.7505H11.6494C9.42046 22.7505 7.69613 22.7512 6.36816 22.6294C5.04343 22.5079 3.98951 22.2568 3.16895 21.6655C1.85552 20.7191 1.00243 19.2622 0.818359 17.6538C0.703455 16.6491 0.998863 15.6072 1.54004 14.3921C2.08263 13.1739 2.92558 11.6699 4.01465 9.7251L4.36523 9.09814C5.49851 7.07443 6.37431 5.51075 7.15625 4.37256C7.93508 3.23899 8.69168 2.41918 9.63379 1.99365ZM12 15.0005C11.4478 15.0006 11 15.4483 11 16.0005C11.0002 16.5526 11.4479 17.0004 12 17.0005C12.5522 17.0005 12.9998 16.5526 13 16.0005C13 15.4482 12.5523 15.0005 12 15.0005ZM12 8.25049C11.5859 8.2506 11.25 8.58634 11.25 9.00049V13.0005C11.2502 13.4145 11.586 13.7504 12 13.7505C12.4141 13.7505 12.7498 13.4146 12.75 13.0005V9.00049C12.75 8.58627 12.4142 8.25049 12 8.25049Z' | ||
| fill={fill} | ||
| /> | ||
| </svg> | ||
| ) | ||
|
|
||
| export default AlertTriangleFilledIcon |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| // Generated by scripts/generate-icons.mjs | ||
| import { FC } from 'react' | ||
| import { IconProps } from './types' | ||
|
|
||
| const AlertTriangleIcon: FC<IconProps> = ({ | ||
| fill = 'currentColor', | ||
| height, | ||
| width = 24, | ||
| ...rest | ||
| }) => ( | ||
| <svg | ||
| width={width} | ||
| height={height ?? width} | ||
| viewBox='0 0 24 24' | ||
| fill='none' | ||
| xmlns='http://www.w3.org/2000/svg' | ||
| {...rest} | ||
| aria-hidden={ | ||
| rest['aria-label'] || rest['aria-labelledby'] ? undefined : true | ||
| } | ||
| > | ||
| <path | ||
| d='M9.63379 1.99341C11.1381 1.31416 12.8619 1.31419 14.3662 1.99341C15.3085 2.4189 16.0648 3.23863 16.8438 4.37231C17.6257 5.51053 18.5014 7.07402 19.6348 9.0979L19.9854 9.72485C21.0745 11.6698 21.9173 13.1736 22.46 14.3918C23.0012 15.6071 23.2966 16.6487 23.1816 17.6536C22.9976 19.262 22.1445 20.7188 20.8311 21.6653C20.0105 22.2564 18.9565 22.5076 17.6318 22.6292C16.3039 22.7509 14.5795 22.7502 12.3506 22.7502H11.6494C9.42046 22.7502 7.69613 22.7509 6.36816 22.6292C5.04343 22.5076 3.9895 22.2565 3.16895 21.6653C1.85551 20.7188 1.0024 19.262 0.818359 17.6536C0.703453 16.6488 0.998835 15.607 1.54004 14.3918C2.08264 13.1737 2.92556 11.6697 4.01465 9.72485L4.36523 9.0979C5.49854 7.07414 6.37429 5.51051 7.15625 4.37231C7.93509 3.23873 8.69166 2.41893 9.63379 1.99341ZM13.749 3.35962C12.6372 2.85767 11.3628 2.85764 10.251 3.35962C9.69292 3.61168 9.12924 4.14969 8.39258 5.22192C7.65885 6.28992 6.82261 7.78164 5.6748 9.8313L5.32324 10.4583C4.21981 12.4287 3.41809 13.8618 2.91016 15.0022C2.40094 16.1455 2.24015 16.8853 2.30859 17.4836C2.44474 18.6723 3.07519 19.749 4.0459 20.4485C4.53453 20.8006 5.25848 21.0207 6.50488 21.135C7.74812 21.249 9.39078 21.2502 11.6494 21.2502H12.3506C14.6092 21.2502 16.2519 21.249 17.4951 21.135C18.7414 21.0207 19.4654 20.8005 19.9541 20.4485C20.9248 19.749 21.5552 18.6723 21.6914 17.4836C21.7599 16.8853 21.5991 16.1456 21.0898 15.0022C20.5819 13.8618 19.7802 12.4288 18.6768 10.4583L18.3252 9.8313C17.1773 7.78152 16.3412 6.28994 15.6074 5.22192C14.8706 4.14954 14.3072 3.61163 13.749 3.35962ZM12 15.0002C12.5523 15.0002 13 15.448 13 16.0002C12.9999 16.5524 12.5522 17.0002 12 17.0002C11.4479 17.0001 11.0001 16.5524 11 16.0002C11 15.448 11.4478 15.0004 12 15.0002ZM12 8.25024C12.4142 8.25024 12.75 8.58603 12.75 9.00024V13.0002C12.7499 13.4144 12.4141 13.7502 12 13.7502C11.5859 13.7501 11.2501 13.4143 11.25 13.0002V9.00024C11.25 8.5861 11.5859 8.25036 12 8.25024Z' | ||
| fill={fill} | ||
| /> | ||
| </svg> | ||
| ) | ||
|
|
||
| export default AlertTriangleIcon |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| // Generated by scripts/generate-icons.mjs | ||
| import { FC } from 'react' | ||
| import { IconProps } from './types' | ||
|
|
||
| const ArrowLeftIcon: FC<IconProps> = ({ | ||
| fill = 'currentColor', | ||
| height, | ||
| width = 24, | ||
| ...rest | ||
| }) => ( | ||
| <svg | ||
| width={width} | ||
| height={height ?? width} | ||
| viewBox='0 0 24 24' | ||
| fill='none' | ||
| xmlns='http://www.w3.org/2000/svg' | ||
| {...rest} | ||
| aria-hidden={ | ||
| rest['aria-label'] || rest['aria-labelledby'] ? undefined : true | ||
| } | ||
| > | ||
| <path | ||
| d='M16.1719 12H6.17188M10.1719 7L7.43462 9.73726C6.64258 10.5293 6.24657 10.9253 6.09819 11.382C5.96767 11.7837 5.96767 12.2163 6.09819 12.618C6.24657 13.0747 6.64258 13.4707 7.43462 14.2627L10.1719 17' | ||
| strokeWidth='1.5' | ||
| strokeLinecap='round' | ||
| strokeLinejoin='round' | ||
| stroke={fill} | ||
| /> | ||
| </svg> | ||
| ) | ||
|
|
||
| export default ArrowLeftIcon |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| // Generated by scripts/generate-icons.mjs | ||
| import { FC } from 'react' | ||
| import { IconProps } from './types' | ||
|
|
||
| const ArrowRightIcon: FC<IconProps> = ({ | ||
| fill = 'currentColor', | ||
| height, | ||
| width = 24, | ||
| ...rest | ||
| }) => ( | ||
| <svg | ||
| width={width} | ||
| height={height ?? width} | ||
| viewBox='0 0 24 24' | ||
| fill='none' | ||
| xmlns='http://www.w3.org/2000/svg' | ||
| {...rest} | ||
| aria-hidden={ | ||
| rest['aria-label'] || rest['aria-labelledby'] ? undefined : true | ||
| } | ||
| > | ||
| <path | ||
| d='M6 12H16M12 7L14.7373 9.73726C15.5293 10.5293 15.9253 10.9253 16.0737 11.382C16.2042 11.7837 16.2042 12.2163 16.0737 12.618C15.9253 13.0747 15.5293 13.4707 14.7373 14.2627L12 17' | ||
| strokeWidth='1.5' | ||
| strokeLinecap='round' | ||
| strokeLinejoin='round' | ||
| stroke={fill} | ||
| /> | ||
| </svg> | ||
| ) | ||
|
|
||
| export default ArrowRightIcon |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| // Generated by scripts/generate-icons.mjs | ||
| import { FC } from 'react' | ||
| import { IconProps } from './types' | ||
|
|
||
| const AwardIcon: FC<IconProps> = ({ | ||
| fill = 'currentColor', | ||
| height, | ||
| width = 24, | ||
| ...rest | ||
| }) => ( | ||
| <svg | ||
| width={width} | ||
| height={height ?? width} | ||
| viewBox='0 0 24 24' | ||
| fill='none' | ||
| xmlns='http://www.w3.org/2000/svg' | ||
| {...rest} | ||
| aria-hidden={ | ||
| rest['aria-label'] || rest['aria-labelledby'] ? undefined : true | ||
| } | ||
| > | ||
| <path | ||
| d='M16.2426 4.75736C18.5858 7.10051 18.5858 10.8995 16.2426 13.2426C13.8995 15.5858 10.1005 15.5858 7.75736 13.2426C5.41421 10.8995 5.41421 7.1005 7.75736 4.75736C10.1005 2.41421 13.8995 2.41421 16.2426 4.75736' | ||
| strokeWidth='1.5' | ||
| strokeLinecap='round' | ||
| strokeLinejoin='round' | ||
| stroke={fill} | ||
| /> | ||
| <path | ||
| d='M8 13.4629V20.9999L12 18.9999L16 20.9999V13.4629' | ||
| strokeWidth='1.5' | ||
| strokeLinecap='round' | ||
| strokeLinejoin='round' | ||
| stroke={fill} | ||
| /> | ||
| </svg> | ||
| ) | ||
|
|
||
| export default AwardIcon |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.