Skip to content

Commit f476c8e

Browse files
adipascugregberge
authored andcommitted
fix: keep viewBox when dimensions are removed (#281)
1 parent 31cb597 commit f476c8e

5 files changed

Lines changed: 16 additions & 3 deletions

File tree

packages/cli/src/__snapshots__/index.test.js.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ exports[`cli should support various args: --no-dimensions 1`] = `
218218
"import React from 'react'
219219
220220
const SvgFile = props => (
221-
<svg {...props}>
221+
<svg viewBox=\\"0 0 48 1\\" {...props}>
222222
<path d=\\"M0 0h48v1H0z\\" fill=\\"#063855\\" fillRule=\\"evenodd\\" />
223223
</svg>
224224
)

packages/core/src/__snapshots__/convert.test.js.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ exports[`convert config should support options { dimensions: false } 1`] = `
44
"import React from 'react'
55
66
const SvgComponent = props => (
7-
<svg {...props}>
7+
<svg viewBox=\\"0 0 88 88\\" {...props}>
88
<g
99
stroke=\\"#063855\\"
1010
strokeWidth={2}

packages/plugin-svgo/src/__snapshots__/index.test.js.snap

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ exports[`svgo should not load runtime configuration with \`runtimeConfig: false\
66
77
exports[`svgo should not remove viewBox with icon option 1`] = `"<svg width=\\"88\\" height=\\"88\\" viewBox=\\"0 0 88 88\\" xmlns=\\"http://www.w3.org/2000/svg\\"><g id=\\"svgo__Blocks\\" stroke=\\"none\\" stroke-width=\\"1\\" fill=\\"none\\" fill-rule=\\"evenodd\\" stroke-linecap=\\"square\\"><g id=\\"svgo__Dismiss\\" stroke=\\"#063855\\" stroke-width=\\"2\\"><path d=\\"M51 37L37 51\\" id=\\"svgo__Shape\\"/><path d=\\"M51 51L37 37\\"/><style></style></g></g></svg>"`;
88
9+
exports[`svgo should not remove viewBox with when dimensions is false 1`] = `"<svg width=\\"88\\" height=\\"88\\" viewBox=\\"0 0 88 88\\" xmlns=\\"http://www.w3.org/2000/svg\\"><g id=\\"svgo__Blocks\\" stroke=\\"none\\" stroke-width=\\"1\\" fill=\\"none\\" fill-rule=\\"evenodd\\" stroke-linecap=\\"square\\"><g id=\\"svgo__Dismiss\\" stroke=\\"#063855\\" stroke-width=\\"2\\"><path d=\\"M51 37L37 51\\" id=\\"svgo__Shape\\"/><path d=\\"M51 51L37 37\\"/><style></style></g></g></svg>"`;
10+
911
exports[`svgo should optimize svg 1`] = `"<svg width=\\"88\\" height=\\"88\\" xmlns=\\"http://www.w3.org/2000/svg\\"><g id=\\"prefix__Blocks\\" stroke=\\"none\\" stroke-width=\\"1\\" fill=\\"none\\" fill-rule=\\"evenodd\\" stroke-linecap=\\"square\\"><g id=\\"prefix__Dismiss\\" stroke=\\"#063855\\" stroke-width=\\"2\\"><path d=\\"M51 37L37 51\\" id=\\"prefix__Shape\\"/><path d=\\"M51 51L37 37\\"/><style></style></g></g></svg>"`;
1012
1113
exports[`svgo should support config.svgoConfig 1`] = `"<svg width=\\"88\\" height=\\"88\\" xmlns=\\"http://www.w3.org/2000/svg\\"><desc>Created with Sketch.</desc><g id=\\"prefix__Blocks\\" stroke=\\"none\\" stroke-width=\\"1\\" fill=\\"none\\" fill-rule=\\"evenodd\\" stroke-linecap=\\"square\\"><g id=\\"prefix__Dismiss\\" stroke=\\"#063855\\" stroke-width=\\"2\\"><path d=\\"M51 37L37 51\\" id=\\"prefix__Shape\\"/><path d=\\"M51 51L37 37\\"/><style></style></g></g></svg>"`;

packages/plugin-svgo/src/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,8 @@ function getBaseSvgoConfig(config) {
8787
const baseSvgoConfig = {
8888
plugins: [{ prefixIds: true }],
8989
}
90-
if (config.icon) baseSvgoConfig.plugins.push({ removeViewBox: false })
90+
if (config.icon || config.dimensions === false)
91+
baseSvgoConfig.plugins.push({ removeViewBox: false })
9192
return baseSvgoConfig
9293
}
9394

packages/plugin-svgo/src/index.test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,16 @@ describe('svgo', () => {
8383
expect(result).toMatchSnapshot()
8484
})
8585

86+
it('should not remove viewBox with when dimensions is false', () => {
87+
const result = svgo(
88+
baseSvg,
89+
{ svgo: true, dimensions: false, runtimeConfig: true },
90+
{ filePath: path.join(__dirname, '../__fixtures__/svgo') },
91+
)
92+
93+
expect(result).toMatchSnapshot()
94+
})
95+
8696
it('should be possible to disable id prefixing', () => {
8797
const result = svgo(
8898
baseSvg,

0 commit comments

Comments
 (0)