Skip to content

Commit 64a165a

Browse files
committed
fix(ui-svg-images): fix Firefox discarding numeric width/height values
Firefox interprets unitless width/height values as undefined when they are strings, e.g. width="100" This fix converts numerical string values like "3.5" to number to make FF happy INSTUI-4650
1 parent d6e4cd5 commit 64a165a

File tree

3 files changed

+28
-7
lines changed

3 files changed

+28
-7
lines changed

packages/ui-svg-images/src/InlineSVG/index.tsx

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,14 +143,33 @@ class InlineSVG extends Component<InlineSVGProps> {
143143
}
144144
}
145145

146+
// Firefox workaround: If width/height is a string that's a number (e.g. '100')
147+
// it needs to be converted to a number, otherwise it is not added to the
148+
// stylesheet
149+
numberToNumberType(n: string | number | undefined) {
150+
if (typeof n === 'string') {
151+
const parsed = parseFloat(n)
152+
// from https://stackoverflow.com/a/52986361/319473
153+
if (!isNaN(parsed) && isFinite(n as unknown as number)) {
154+
return parsed
155+
}
156+
}
157+
return n
158+
}
159+
146160
render() {
147161
const { style, title, description, focusable, src, styles, ...props } =
148162
this.props
149163

150164
// if width or height are 'auto', don't supply anything to the SVG
151-
const width = this.props.width === 'auto' ? undefined : this.props.width
152-
const height = this.props.height === 'auto' ? undefined : this.props.height
153-
165+
const width =
166+
this.props.width === 'auto'
167+
? undefined
168+
: this.numberToNumberType(this.props.width)
169+
const height =
170+
this.props.height === 'auto'
171+
? undefined
172+
: this.numberToNumberType(this.props.height)
154173
return (
155174
<svg
156175
{...parseAttributes(src)}

packages/ui-svg-images/src/InlineSVG/props.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,12 @@ type InlineSVGOwnProps = {
3939
description?: string
4040
focusable?: boolean
4141
/**
42+
* Width of the SVG. Accepts valid CSS unit strings like '1rem'
4243
* To let the SVG expand to fill its container, use "`auto`"
4344
*/
4445
width?: string | number
4546
/**
47+
* Height of the SVG. Accepts valid CSS unit strings like '1rem'
4648
* To let the SVG expand to fill its container, use "`auto`"
4749
*/
4850
height?: string | number

packages/ui-svg-images/src/SVGIcon/README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,15 @@ type: example
4747
</div>
4848
```
4949

50-
If you need a size that is not offered via the `size` prop, adjust the
51-
`font-size` on the icon's parent element.
50+
If you need a size that is not offered via the `size` prop, use the `width` or `height` props, these accept a valid CSS length value.
5251

5352
```js
5453
---
5554
type: example
5655
---
57-
<div style={{fontSize: '15rem', lineHeight: 1}}>
58-
<SVGIcon src={iconExample} title="Icon Example" />
56+
<div>
57+
<SVGIcon width="1rem" height="1rem" src={iconExample} title="Icon Example" />
58+
<SVGIcon width="55px" height="55px" src={iconExample} title="Icon Example" />
5959
</div>
6060
```
6161

0 commit comments

Comments
 (0)