Skip to content

Icons: maintain absolute stroke-width regardless of icon-size - #78774

Merged
jasmussen merged 4 commits into
trunkfrom
update/icons-stroke-width-support
May 29, 2026
Merged

jasmussen merged 4 commits into
trunkfrom
update/icons-stroke-width-support

Conversation

@jasmussen

@jasmussen jasmussen commented May 28, 2026 •

Copy link
Copy Markdown
Contributor

What?

This PR that I created with help from Claude does a few things:

  • It adds a new strokeWidth prop to the Icon component, so that not only does the stroke-width remain absolute across different sizes, you can change it from the default 1.5px.
  • Adds a rangeControl to the Icon library Storybook story so you can test this out.
  • Adds vector-effect="non-scaling-stroke", the only existing icon that is in trunk that is already fully stroke-based.

For the moment, the only visual change will be in Storybook, and that is intentional, the system is fully backwards compatible: existing icons will work just as they did, they'll just ignore the strokeWidth prop.

The purpose of this effort is to enable a parallel effort of redrawing our entire icon set from fills to strokes (which sounds like a lot, but isn't actually since that's already mostly the case in the Figma source of truth, it mostly needs re-exports).

Doing that—redrawing the remaining icons to be stroke-based, in batches—is one of the followups after this PR, the other is to add the stroke property to the PHP counterpart as well, assuming #78332 lands.

Before:

before

After:

after after2

Why?

The icon set is currently fill-based, which means it scales poorly, e.g. to 16×16 as used in places like the Badge component, or upwards to larger icons where suddenly fill-based strokes can be very thick and out of place in its context. While not used widely for that precise reason, larger icons can play a meaningful role in empty-states.

How?

This PR is intentionally invisible to existing consumers:

  • Backwards compatible. Nothing changes visually.
  • Opt-in per icon. Icons don't have to be updated to be stroke-based.

Changes

  • React Icon component — adds strokeWidth?: number prop, passed through cloneElement to the outer <svg> when set.
  • PHP WP_Icons_Registry::sanitize_icon_content() — adds stroke, stroke-width, stroke-linecap, stroke-linejoin, vector-effect, and fill to the wp_kses allowlist on <svg>, <path>, and <polygon> so stroke-based icons survive registry load.
  • square.svg (the only existing stroke-based icon) — updated to the new convention as a canary:
    • stroke, stroke-width, fill moved to the outer <svg> so the strokeWidth prop can override them.
    • vector-effect="non-scaling-stroke" added to the path so the stroke renders at a constant pixel weight at any size. At 24×24 this looks identical to before; at 16×16 and 32×32 it now renders at a true 1.5px instead of scaling down/up. The two existing consumers (zoom-out-toggle, HTML block modal) render at the default 24px so are visually unchanged.
  • Storybook — adds a Stroke width RangeControl (0.5–5, step 0.25) to the icons library story for visualising the system and spotting which icons still need redrawing.

Usage

import { Icon, square, plus } from '@wordpress/icons';

<Icon icon={ square } />                     // default 1.5px (from source)
<Icon icon={ square } size={ 16 } />         // still 1.5px stroke, at 16x16

Source SVG convention (for the redraw effort)

Stroke-based icons should carry the defaults in their source files so the raw .svg previews correctly outside its rendered context:

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"
     stroke="currentColor" stroke-width="1.5" fill="none">
  <path d="…" vector-effect="non-scaling-stroke"
        stroke-linecap="round" stroke-linejoin="round" />
</svg>

square.svg is the reference.

Follow-ups

  • PHP wp_get_icon() stroke_width arg — blocked on #78332 landing. Will be a one-line addition to wp_get_icon() that calls $processor->set_attribute( 'stroke-width', ... ) alongside the existing args. Tests to follow in that PR.
  • Icon redraws — converting fill-based icons to stroke-based versions, icon by icon. Will land as separate PRs, potentially stacked against this branch while it awaits review.

Testing instructions

  1. Storybook → Icons → Icon library. Search square.
  2. Drag the Icon size between 16/24/32 — the stroke should stay visually 1.5px at every size.
  3. Drag the Stroke width slider — the square icon should respond; fill-based icons (e.g. plus, check) should not change.
  4. Inspect the rendered <svg> in DevTools and confirm vector-effect="non-scaling-stroke" is present on the square path and that the outer <svg>'s stroke-width reflects the slider value.

Use of AI Tools

This PR was authored with Claude Code (Claude Opus 4.7). Tested and reviewed by the author; Claude Code is credited via Co-Authored-By in the commit.

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Feature] Icons Related to Icon registration API and Icon REST API [Package] Icons /packages/icons [Type] Enhancement A suggestion for improvement.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants