Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for newly supported svg filter in react-native-svg #969

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@svgr/babel-plugin-transform-react-native-svg",
"description": "Transform DOM elements into react-native-svg components",
"version": "8.1.0",
"version": "8.2.0",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"exports": {
Expand Down
40 changes: 40 additions & 0 deletions packages/babel-plugin-transform-react-native-svg/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,35 @@ describe('plugin', () => {
expect(code).toMatchInlineSnapshot(`"<Svg></Svg>;"`)
})

it('should transform elements with filter', () => {
const svg = `
<svg height="150" width="150">
<filter id="filter1">
<feGaussianBlur stdDeviation="3" />
</filter>
<g filter="url(#filter1)">
<circle cx="75" cy="50" r="40" fill="blue" fill-opacity="0.5" />
<circle cx="55" cy="90" r="40" fill="green" fill-opacity="0.5" />
<circle cx="95" cy="90" r="40" fill="red" fill-opacity="0.5" />
</g>
</svg>
`

const code = testPlugin(svg)
expect(code).toMatchInlineSnapshot(`
"<Svg height="150" width="150">
<Filter id="filter1">
<FeGaussianBlur stdDeviation="3" />
</Filter>
<G filter="url(#filter1)">
<Circle cx="75" cy="50" r="40" fill="blue" fill-opacity="0.5" />
<Circle cx="55" cy="90" r="40" fill="green" fill-opacity="0.5" />
<Circle cx="95" cy="90" r="40" fill="red" fill-opacity="0.5" />
</G>
</Svg>;"
`)
})

it('should add import', () => {
const code = testPlugin(
`import Svg from 'react-native-svg'; <svg><g /><div /></svg>;`,
Expand All @@ -27,6 +56,17 @@ describe('plugin', () => {
`)
})

it('should add version warning', () => {
const code = testPlugin(
`import Svg from 'react-native-svg'; <svg> <filter id="filter1"><feGaussianBlur stdDeviation="3" /></filter></svg>;`,
)
expect(code).toMatchInlineSnapshot(`
"import Svg, { Filter, FeGaussianBlur } from 'react-native-svg';
/* Using svg filters is only supported on react-native-svg v15.5.0 or later. */
<Svg> <Filter id="filter1"><FeGaussianBlur stdDeviation="3" /></Filter></Svg>;"
`)
})

it('should add deal with type imports properly', () => {
const code = transform(
`
Expand Down
64 changes: 61 additions & 3 deletions packages/babel-plugin-transform-react-native-svg/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { NodePath, types as t } from '@babel/core'
interface State {
replacedComponents: Set<string>
unsupportedComponents: Set<string>
filterComponents: Set<string>
}

const elementToComponent: { [key: string]: string } = {
Expand All @@ -30,6 +31,31 @@ const elementToComponent: { [key: string]: string } = {
mask: 'Mask',
image: 'Image',
foreignObject: 'ForeignObject',
filter: 'Filter',
fegaussianblur: 'FeGaussianBlur',
feblend: 'FeBlend',
fecolormatrix: 'FeColorMatrix',
fecomponenttransfer: 'FeComponentTransfer',
fecomposite: 'FeComposite',
feconvolvematrix: 'FeConvolveMatrix',
fediffuselighting: 'FeDiffuseLighting',
fedisplacementmap: 'FeDisplacementMap',
fedropshadow: 'FeDropShadow',
feflood: 'FeFlood',
fefunca: 'FeFuncA',
fefuncb: 'FeFuncB',
fefuncg: 'FeFuncG',
fefuncr: 'FeFuncR',
feimage: 'FeImage',
femerge: 'FeMerge',
femergenode: 'FeMergeNode',
femorphology: 'FeMorphology',
feoffset: 'FeOffset',
fepointlight: 'FePointLight',
fespecularlighting: 'FeSpecularLighting',
fespotlight: 'FeSpotLight',
fetile: 'FeTile',
feturbulence: 'FeTurbulence',
}

const plugin = () => {
Expand All @@ -39,8 +65,7 @@ const plugin = () => {
const { name } = namePath.node

// Replace element by react-native-svg components
const component = elementToComponent[name]

const component = elementToComponent[name.toLowerCase()] // Case insensitive
if (component) {
namePath.replaceWith(t.jsxIdentifier(component))
if (path.has('closingElement')) {
Expand All @@ -49,11 +74,15 @@ const plugin = () => {
.get('name') as NodePath<t.JSXIdentifier>
closingNamePath.replaceWith(t.jsxIdentifier(component))
}
// Add filter elements to show warning
if (name.includes('fe') || name.includes('filter')) {
state.filterComponents.add(name)
}
state.replacedComponents.add(component)
return
}

// Remove element if not supported
// Remove unsupported element
state.unsupportedComponents.add(name)
path.remove()
}
Expand Down Expand Up @@ -119,6 +148,34 @@ const plugin = () => {
` SVGR has dropped some elements not supported by react-native-svg: ${componentList} `,
)
}

if (state.filterComponents.size && !path.has('trailingComments')) {
state.filterComponents.forEach((filter) => {
if (
!path
.get('specifiers')
.some((specifier) =>
specifier
.get('local')
.isIdentifier({ name: elementToComponent[filter] }),
)
) {
if (elementToComponent[filter]) {
path.pushContainer(
'specifiers',
t.importSpecifier(
t.identifier(elementToComponent[filter]),
t.identifier(elementToComponent[filter]),
),
)
}
}
})
path.addComment(
'trailing',
` Using svg filters is only supported on react-native-svg v15.5.0 or later. `,
)
}
},
}

Expand All @@ -127,6 +184,7 @@ const plugin = () => {
Program(path: NodePath<t.Program>, state: Partial<State>) {
state.replacedComponents = new Set()
state.unsupportedComponents = new Set()
state.filterComponents = new Set()

path.traverse(svgElementVisitor, state as State)
path.traverse(importDeclarationVisitor, state as State)
Expand Down