Skip to content

Commit e94bfdb

Browse files
committed
fix: normalize viewbox to be visible, omit fill colors from the svg
1 parent 3154353 commit e94bfdb

2 files changed

Lines changed: 16 additions & 5 deletions

File tree

‎src/block-components/icon/index.js‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,14 @@ const LinearGradient = ( {
6363
const NOOP = () => {}
6464

6565
const getSvgDef = ( href, viewBox = '0 0 24 24' ) => {
66-
return `<svg viewBox="${ viewBox }"><use href="${ href }" xlink:href="${ href }"></use></svg>`
66+
const viewBoxValues = viewBox.trim().split( /[\s,]+/ )
67+
// The symbol keeps its original origin to map its paths, while the wrapper
68+
// starts at zero so the <use> instance remains inside the visible viewport.
69+
const normalizedViewBox = viewBoxValues.length === 4 && viewBoxValues.every( value => Number.isFinite( Number( value ) ) )
70+
? `0 0 ${ viewBoxValues[ 2 ] } ${ viewBoxValues[ 3 ] }`
71+
: viewBox
72+
73+
return `<svg viewBox="${ normalizedViewBox }"><use href="${ href }" xlink:href="${ href }"></use></svg>`
6774
}
6875

6976
const generateIconId = () => {
@@ -457,4 +464,3 @@ Icon.InspectorControls = Edit
457464
Icon.addAttributes = addAttributes
458465

459466
Icon.addStyles = addStyles
460-

‎src/plugins/page-icons/page-icons.js‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,15 @@ const parseSVGString = svgString => {
7272
while ( ( attrMatch = attrRegex.exec( attributesPart ) ) !== null ) {
7373
const key = attrMatch[ 1 ]
7474
const attrNameLower = key.toLowerCase()
75+
const value = attrMatch[ 2 ] || attrMatch[ 3 ] || attrMatch[ 4 ] || ''
7576
// Skip width and height as symbols don't need them
76-
if ( attrNameLower !== 'width' && attrNameLower !== 'height' && ! attrNameLower.startsWith( 'on' ) ) {
77-
// Value can be in double quotes, single quotes, or unquoted
78-
const value = attrMatch[ 2 ] || attrMatch[ 3 ] || attrMatch[ 4 ] || ''
77+
// Skip explicit fills so each <use> instance can inherit its block's icon color.
78+
// Preserve fill="none" because it is structural for stroke-based icons.
79+
if ( attrNameLower !== 'width' &&
80+
attrNameLower !== 'height' &&
81+
! attrNameLower.startsWith( 'on' ) &&
82+
( attrNameLower !== 'fill' || value.trim().toLowerCase() === 'none' )
83+
) {
7984
svgAttributes[ key ] = value
8085
}
8186
}

0 commit comments

Comments
 (0)