-
-
Notifications
You must be signed in to change notification settings - Fork 411
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add "-native" option to target React Native
- Loading branch information
Showing
11 changed files
with
217 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import jsx from 'h2x-plugin-jsx' | ||
import h2x from '../plugins/h2x' | ||
import removeStyle from './removeStyle' | ||
|
||
describe('removeStyle', () => { | ||
it('should remove style elements', () => { | ||
const result = h2x( | ||
` | ||
<svg> | ||
<style></style> | ||
<style></style> | ||
<g stroke="#063855" stroke-width="2"> | ||
<path d="M51,37 L37,51" id="Shape"></path> | ||
</g> | ||
</svg> | ||
`, | ||
{ plugins: [jsx, removeStyle] }, | ||
) | ||
|
||
expect(result.trim()).toBe(`<svg> | ||
<g stroke="#063855" strokeWidth={2}> | ||
<path d="M51,37 L37,51" id="Shape" /> | ||
</g> | ||
</svg>`) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
const supportedElements = { | ||
svg: 'Svg', | ||
circle: 'Circle', | ||
ellipse: 'Ellipse', | ||
g: 'G', | ||
linearGradient: 'LinearGradient', | ||
radialGradient: 'RadialGradient', | ||
line: 'Line', | ||
path: 'Path', | ||
polygon: 'Polygon', | ||
polyline: 'Polyline', | ||
rect: 'Rect', | ||
symbol: 'Symbol', | ||
text: 'Text', | ||
use: 'Use', | ||
defs: 'Defs', | ||
stop: 'Stop', | ||
} | ||
|
||
const reverse = Object.keys(supportedElements).reduce( | ||
(map, key) => ({ ...map, [supportedElements[key]]: key }), | ||
{}, | ||
) | ||
|
||
const toReactNative = () => ({ | ||
visitor: { | ||
JSXElement: { | ||
enter(path) { | ||
if (supportedElements[path.node.name]) { | ||
path.node.name = supportedElements[path.node.name] | ||
} else if (!reverse[path.node.name]) { | ||
path.remove() | ||
} | ||
}, | ||
}, | ||
}, | ||
}) | ||
|
||
export default toReactNative |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import jsx from 'h2x-plugin-jsx' | ||
import h2x from '../plugins/h2x' | ||
import toReactNative from './toReactNative' | ||
|
||
describe('toReactNative', () => { | ||
it('should take svg and convert it to react-native', () => { | ||
const result = h2x( | ||
` | ||
<svg> | ||
<g stroke="#063855" stroke-width="2"> | ||
<path d="M51,37 L37,51" id="Shape"></path> | ||
</g> | ||
</svg> | ||
`, | ||
{ plugins: [jsx, toReactNative] }, | ||
) | ||
|
||
expect(result.trim()).toBe(`<Svg> | ||
<G stroke="#063855" strokeWidth={2}> | ||
<Path d="M51,37 L37,51" id="Shape" /> | ||
</G> | ||
</Svg>`) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
export default (opts = {}) => (code, state) => `import React from 'react' | ||
import Svg, { | ||
Circle, | ||
Ellipse, | ||
G, | ||
LinearGradient, | ||
RadialGradient, | ||
Line, | ||
Path, | ||
Polygon, | ||
Polyline, | ||
Rect, | ||
Symbol, | ||
Text, | ||
Use, | ||
Defs, | ||
Stop | ||
} from 'react-native-svg'; | ||
const ${state.componentName} = (${opts.expandProps ? 'props' : ''}) => ${code} | ||
export default ${state.componentName}` |