Skip to content

Commit

Permalink
Update component API
Browse files Browse the repository at this point in the history
- Non-documented API props are now spread onto the wrapper element.
- Some API props have been renamed for clarity.
- Tests updated to reflect the required wrapper elements.
- New test added regarding the presence of a parent element for SVGInjector, since this has been coming up a bit in the issue tracker.
  • Loading branch information
tanem committed Apr 29, 2018
1 parent 06c46c2 commit a064035
Show file tree
Hide file tree
Showing 12 changed files with 265 additions and 223 deletions.
17 changes: 17 additions & 0 deletions MIGRATING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Migrating

## v3 API Changes

**Naming**

- `callback` -> `onInjected`
- `className` -> `svgClassName`
- `style` -> `svgStyle`

**Additions**

All additional non-documented props will now be spread onto the wrapper element.

**Removals**

`wrapperClassName` has been removed. Instead, just pass `className` since it will be spread onto the wrapper element.
35 changes: 21 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,14 @@ import ReactSVG from 'react-svg'
ReactDOM.render(
<ReactSVG
path="atomic.svg"
callback={svg => {
console.log(svg)
onInjected={svg => {
console.log('onInjected', svg)
}}
svgClassName="svg-class-name"
className="wrapper-class-name"
onClick={() => {
console.log('wrapper onClick')
}}
className="class-name"
wrapperClassName="wrapper-class-name"
/>,
document.querySelector('.Root')
)
Expand All @@ -34,24 +37,28 @@ There is a working version of the above in the `examples/basic` dir. First run `
__Props__

- `path` - Path to the SVG.
- `callback` - *Optional* Function to call after the SVG is injected. Receives the newly injected SVG DOM element as a parameter. Defaults to `null`.
- `className` - *Optional* Class name to be added to the SVG. Defaults to `''`.
- `wrapperClassName` - *Optional* Class name to be added to the wrapping `div`. Defaults to `''`.
- `evalScripts` - *Optional* Run any script blocks found in the SVG (`always`, `once`, or `never`). Defaults to `never`.
- `style` - *Optional* Inline styles to be added to the SVG. Defaults to `{}`.
- `onInjected` - *Optional* Function to call after the SVG is injected. Receives the injected SVG DOM element as a parameter. Defaults to `null`.
- `svgClassName` - *Optional* Class name to be added to the injected SVG DOM element. Defaults to `null`.
- `svgStyle` - *Optional* Inline styles to be added to the injected SVG DOM element. Defaults to `{}`.

Other non-documented properties are applied to the wrapper element.

__Example__

```js
<ReactSVG
path="atomic.svg"
callback={svg => {
console.log(svg)
evalScripts="always"
onInjected={svg => {
console.log('onInjected', svg)
}}
svgClassName="svg-class-name"
svgStyle={{ width: 200 }}
className="wrapper-classname"
onClick={() => {
console.log('wrapper onClick')
}}
className="example"
wrapperClassName="example-wrapper-classname"
evalScript="always"
style={{ width: 200 }}
/>
```

Expand Down
12 changes: 7 additions & 5 deletions examples/basic/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import ReactSVG from '../../src'
ReactDOM.render(
<ReactSVG
path="atomic.svg"
callback={svg => {
// eslint-disable-next-line no-console
console.log(svg)
onInjected={svg => {
console.log('onInjected', svg)
}}
svgClassName="svg-class-name"
className="wrapper-class-name"
onClick={() => {
console.log('wrapper onClick')
}}
className="class-name"
wrapperClassName="wrapper-class-name"
/>,
document.querySelector('.Root')
)
4 changes: 2 additions & 2 deletions examples/issue-49/fill/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
<meta charset="utf-8">
<title>issue 49 | fill | react-svg</title>
<style>
.class-name g {
.svg-class-name g {
fill: blue;
}
.class-name:hover g {
.svg-class-name:hover g {
fill: black;
}
</style>
Expand Down
2 changes: 1 addition & 1 deletion examples/issue-49/fill/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ import ReactDOM from 'react-dom'
import ReactSVG from '../../../src'

ReactDOM.render(
<ReactSVG path="atomic.svg" className="class-name" />,
<ReactSVG path="atomic.svg" svgClassName="svg-class-name" />,
document.querySelector('.Root')
)
4 changes: 2 additions & 2 deletions examples/issue-49/swap/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ class Swap extends React.Component {
>
<ReactSVG
path={this.state.isHovered ? 'atomic-black.svg' : 'atomic-blue.svg'}
className="class-name"
wrapperClassName="wrapper-class-name"
svgClassName="svg-class-name"
className="wrapper-class-name"
/>
</div>
)
Expand Down

0 comments on commit a064035

Please sign in to comment.