|
1 | 1 | --- |
2 | 2 | name: svg-sprite-icons |
3 | | -description: Pattern for SVG icons via a symbol sprite — individual SVG files are compiled into a single sprite.svg by the svg-sprite package, and an Icon component renders them with <use href="sprite.svg#name">. Apply when setting up an icon system, adding icons to a project that uses this pattern, or reviewing icon-related code. |
| 3 | +description: Pattern for SVG icons via a symbol sprite — individual SVG files are compiled into a single sprite.svg by the svg-sprite package, and an Icon component renders them with <use href="sprite.svg#name">. Apply when setting up an icon system, adding icons to a project that uses this pattern, sizing icons or setting their stroke width, or reviewing icon-related code. |
4 | 4 | license: MIT |
5 | 5 | compatibility: |
6 | 6 | - Claude Code |
@@ -149,9 +149,55 @@ Key points: |
149 | 149 | - `width`/`height` set the rendered size; the symbol's own `viewBox` handles proportions. |
150 | 150 | - Usage: `<Icon name='copy'/>`; color is inherited via CSS `color` thanks to `currentColor` in the source icons. |
151 | 151 |
|
| 152 | +## Adaptive stroke width |
| 153 | +
|
| 154 | +An outline set draws one stroke on its grid, so its line thins as the icon shrinks: a 24-grid icon with `stroke-width="1.6"` draws a 1.33px line at 20px but a 1.07px one at 16px, lighter than the text beside it. Let the size step set the stroke, so that a smaller icon keeps the line of a reference size: |
| 155 | +
|
| 156 | +``` |
| 157 | +stroke at a size = the set's stroke × reference size / size |
| 158 | +``` |
| 159 | +
|
| 160 | +With a 1.6 stroke and 20px as the reference, that is 2 at 16 and 2.67 at 12. At and above the reference, keep the set's own stroke: an icon alone in a large control draws the heavier line the set was drawn with. |
| 161 | + |
| 162 | +The `Icon` component derives the stroke from its `size`, and no consumer writes a stroke width: |
| 163 | + |
| 164 | +```css |
| 165 | +/* Icon.module.css */ |
| 166 | +
|
| 167 | +/* The set draws 1.6 on its 24 grid; a smaller icon draws heavier, to keep a 20px icon's line */ |
| 168 | +
|
| 169 | +.root { |
| 170 | + stroke-width: 1.6; |
| 171 | +} |
| 172 | +
|
| 173 | +.sm { |
| 174 | + block-size: var(--sizeIconSm); |
| 175 | + inline-size: var(--sizeIconSm); |
| 176 | + stroke-width: 2; /* 1.6 × 20 / 16 */ |
| 177 | +} |
| 178 | +
|
| 179 | +.md { |
| 180 | + block-size: var(--sizeIconMd); |
| 181 | + inline-size: var(--sizeIconMd); |
| 182 | +} |
| 183 | +
|
| 184 | +.lg { |
| 185 | + block-size: var(--sizeIconLg); |
| 186 | + inline-size: var(--sizeIconLg); |
| 187 | +} |
| 188 | +``` |
| 189 | + |
| 190 | +- **Leave `stroke-width` out of each source's root `<svg>`, and nothing else.** |
| 191 | + - A symbol's own attribute beats whatever it inherits through `<use>`. |
| 192 | + - Keep `stroke="currentColor"` on the root. Without it, svgo (run by `svg-sprite`) drops a filled shape's `stroke="none"`, and filled dots grow an outline. |
| 193 | +- **Size only through `size`.** |
| 194 | + - The component drops its `width` and `height` props, since they bypass the scale and the stroke with it. It always applies `.root` and its `size` class. |
| 195 | + - A parent that resizes its icon in its own CSS bypasses the scale the same way, so the parent passes the step instead. |
| 196 | +- **Never thicken an icon for its role.** A badge, an error or a toast draws its icon like any icon of its size. |
| 197 | +
|
152 | 198 | ## Adding a new icon |
153 | 199 |
|
154 | | -1. Put the SVG file into `app/assets/icons/`, named after the icon (`arrow-left.svg`). Replace hardcoded colors with `currentColor` if the icon is monochrome. |
| 200 | +1. Put the SVG file into `app/assets/icons/`, named after the icon (`arrow-left.svg`). Replace hardcoded colors with `currentColor` if the icon is monochrome. If the `Icon` sets the stroke width, drop the root's `stroke-width`. |
155 | 201 | 2. Run `pnpm build:sprite` and commit the regenerated `sprite.svg` together with the source icon. |
156 | 202 | 3. Register the name wherever the component sources its names — the `IconName` union, or the exported array the union is derived from. Update anything that lists icons by hand, such as the `options` list in `Icon.stories.tsx`, unless it already reads that array. |
157 | 203 | 4. Use it: `<Icon name='arrow-left'/>`. |
|
0 commit comments