-
-
Notifications
You must be signed in to change notification settings - Fork 425
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow to pass dynamic svg props: In this diff I introduce the new option `svgProps` which is similar to svgAttributes but applies values in interpolation style `{true}`. This allows to specify expressions depending on template values for example default `currentColor` in `fill` prop. `svgAttributes` is now deprecated and will be removed in v3.
- Loading branch information
Showing
11 changed files
with
272 additions
and
39 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
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,69 @@ | ||
import { JSXAttribute } from 'h2x-plugin-jsx' | ||
|
||
const compareAttrsName = attr => otherAttr => attr.name === otherAttr.name | ||
const compareAttrsValue = attr => otherAttr => attr.value === otherAttr.value | ||
|
||
const compareAttrs = attr => otherAttr => | ||
compareAttrsName(attr)(otherAttr) && compareAttrsValue(attr)(otherAttr) | ||
|
||
const areAttrsAlreadyInjected = (node, attributes = {}) => { | ||
const nodeAttrs = node.attributes | ||
|
||
return Object.keys(attributes).reduce((accumulation, key) => { | ||
if (nodeAttrs.some(compareAttrs({ name: key, value: attributes[key] }))) | ||
return accumulation | ||
return false | ||
}, true) | ||
} | ||
|
||
const svgProps = (props = {}) => () => { | ||
const interpolated = new Set() | ||
const keys = Object.keys(props) | ||
const attributes = keys.reduce((acc, prop) => { | ||
const value = props[prop] | ||
if ( | ||
typeof value === 'string' && | ||
value.startsWith('{') && | ||
value.endsWith('}') | ||
) { | ||
acc[prop] = value.slice(1, -1) | ||
interpolated.add(prop) | ||
} else { | ||
acc[prop] = value | ||
} | ||
return acc | ||
}, {}) | ||
|
||
return { | ||
visitor: { | ||
JSXElement: { | ||
enter(path) { | ||
if (path.node.name !== 'svg') return | ||
if (areAttrsAlreadyInjected(path.node, attributes)) return | ||
|
||
const parseAttributes = keys.reduce((accumulation, key) => { | ||
const prop = new JSXAttribute() | ||
prop.name = key | ||
prop.value = attributes[key] | ||
prop.literal = interpolated.has(key) | ||
return [...accumulation, prop] | ||
}, []) | ||
|
||
const mergeAttributes = path.node.attributes.reduce( | ||
(accumulation, value) => { | ||
if (accumulation.some(compareAttrsName(value))) | ||
return accumulation | ||
return [...accumulation, value] | ||
}, | ||
parseAttributes, | ||
) | ||
|
||
path.node.attributes = mergeAttributes | ||
path.replace(path.node) | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
export default svgProps |
Oops, something went wrong.