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(readme): update readMe, update replace method #23

Merged
merged 4 commits into from Nov 12, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
63 changes: 61 additions & 2 deletions README.md
Expand Up @@ -67,6 +67,16 @@ import * as eva from 'eva-icons';
```
*Thanks to Feather Icons for the build process inspiration.*

- Additional attributes:
* `data-eva-fill`: set icon color
* `data-eva-height`: set icon height
* `data-eva-width`: set icon width
* `data-eva-animation`: [set icon animation](#animation)

```html
<i data-eva="github" data-eva-fill="#ff0000" data-eva-height="48" data-eva-width="48"></i>
```

### Fonts

Eva Icons are also available as a Web Font.
Expand All @@ -86,9 +96,58 @@ We recommend using SVG icons due to better rendering and performance capabilitie

## Documentation

### `eva.replace({ ... })`
### `eva.replace(options)`

Replaces all elements that have a `data-eva` attribute with SVG markup.

`options` optional object.

#### Available 'option' properties:
| Name | Type | Default value | Description |
|------| ------ | ------------- |-------------|
| fill | string | none | Icon color |
| width | string or number | 24px | Icon width |
| height | string or number | 24px | Icon height |
| class | string | none | Custom css class |
| animation | object | none | [Icon animation](#animation) |

### Animation
- Add the `data-eva-animation` attribute with the animation type `(zoom, pulse, shake and flip)` to an element:

```html
<i data-eva="github" data-eva-animation="zoom"></i>
```

- By default animation works by hover and has infinity `false`, if you want change this behavior add `data-eva-hover="true | false"` and `data-eva-infinite="true | false"`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this sentence is quite hard to understand, try to rephrase


```html
<i data-eva="github" data-eva-animation="zoom" data-eva-hover="false" data-eva-infinite="true"></i>
```

> **Note:** In the above example github icon will be always animated.

- Pass animation as property in a `eva.replace` method.

```js
eva.replace({
aniamtion: {
type: string, // zoom, pulse, shake, flip
hover: booleam, // default true
infinite: boolean, // default false
}
});
```
> **Note:** The animation will be applied to all replaced elements.

- Add class `eva-parent-hover` to parent container if you want that the animation works by hover on the parent element.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- Add class `eva-parent-hover` to parent container if you want that the animation works by hover on the parent element.
- Add `eva-parent-hover` class to the parent container in a case you want to activate the animation hovering on the parent element.


```html
<div class="eva-parent-hover">
<i data-eva="github" data-eva-animation="zoom"></i>
Zoom aniamtion
</div>
```

Replaces all elements that have a `data-eva` attribute with SVG markup corresponding to the element's `data-eva` attribute value.
## License
[MIT](LICENSE.txt) license.

Expand Down
34 changes: 23 additions & 11 deletions package/src/replace.js
Expand Up @@ -20,25 +20,26 @@ const dataAttributesKeys = {
'data-eva-fill': 'fill',
};

function replace(attrs = {}) {
function replace(options = {}) {
if (typeof document === 'undefined') {
throw new Error('`eva.replace()` only works in a browser environment.');
}

const elementsToReplace = document.querySelectorAll('[data-eva]');

Array.from(elementsToReplace).forEach(element =>
replaceElement(element, attrs),
replaceElement(element, options),
);
}

function replaceElement(element, attrs = {}) {
function replaceElement(element, options = {}) {
const { name, ...elementAttrs } = getAttrs(element);

const svgString = icons[name].toSvg({
...attrs,
...options,
...elementAttrs,
...{ class: classnames(attrs.class, elementAttrs.class) },
animation: getAnimationObject(options.animation, elementAttrs.animation),
...{ class: classnames(options.class, elementAttrs.class) },
});
const svgDocument = new DOMParser().parseFromString(
svgString,
Expand Down Expand Up @@ -69,14 +70,25 @@ function getAttrs(element) {

function getAttr(attr) {
if (!!dataAttributesKeys[attr.name]) {
return {
return ({
[dataAttributesKeys[attr.name]]: attr.value,
};
} else {
return {
[attr.name]: attr.value,
};
});
}

return ({
[attr.name]: attr.value,
});
}

function getAnimationObject(optionsAnimation, elementAttrsAnimation) {
if (optionsAnimation || elementAttrsAnimation) {
return ({
...optionsAnimation,
...elementAttrsAnimation,
});
}

return null;
}

export default replace;