mirror of
				https://github.com/feathericons/feather.git
				synced 2023-08-10 21:13:24 +03:00 
			
		
		
		
	 f243624fbd
			
		
	
	f243624fbd
	
	
	
		
			
			BREAKING CHANGE: 
Each icon in the `feather.icons` object is now an `Icon` object with a `name`, `contents`, `tags` and `attrs` property.
```js
/* BEFORE */
feather.icons.x
// '<line ... /><line ... />'
/* AFTER */
feather.icons.x
// {
//    name: 'x',
//    contents: '<line ... /><line ... />`,
//    tags: ['cancel', 'close', 'delete', 'remove'],
//    attrs: {
//      class: 'feather feather-x',
//      xmlns: 'http://www.w3.org/2000/svg',
//      width: 24,
//      height: 24,
//      viewBox: '0 0 24 24',
//      fill: 'none',
//      stroke: 'currentColor',
//      'stroke-width': 2,
//      'stroke-linecap': 'round',
//      'stroke-linejoin': 'round',
//    }
// }
```
`feather.toSvg()` has been deprecated in favor of `feather.icons[name].toSvg()`:
```js
/* BEFORE */
feather.toSvg('x')
/* AFTER */
feather.icons.x.toSvg()
```
`feather.replace()` now copies all attributes on the placeholder element (i.e. `<i>`) to the `<svg>` tag instead of just `class` and `id`:
```html
<i data-feather="circle" id="my-circle" class="foo bar" stroke-width="1"></i>
<!--
<i> will be replaced with:
<svg id="my-circle" class="feather feather-circle foo bar" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="10"></circle></svg>
-->
```
		
	
		
			
				
	
	
		
			61 lines
		
	
	
		
			1.6 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
			
		
		
	
	
			61 lines
		
	
	
		
			1.6 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
| /* eslint-env browser */
 | |
| import classnames from 'classnames/dedupe';
 | |
| 
 | |
| import icons from './icons';
 | |
| 
 | |
| /**
 | |
|  * Replace all HTML elements that have a `data-feather` attribute with SVG markup
 | |
|  * corresponding to the element's `data-feather` attribute value.
 | |
|  * @param {Object} attrs
 | |
|  */
 | |
| function replace(attrs = {}) {
 | |
|   if (typeof document === 'undefined') {
 | |
|     throw new Error('`feather.replace()` only works in a browser environment.');
 | |
|   }
 | |
| 
 | |
|   const elementsToReplace = document.querySelectorAll('[data-feather]');
 | |
| 
 | |
|   Array.from(elementsToReplace).forEach(element =>
 | |
|     replaceElement(element, attrs),
 | |
|   );
 | |
| }
 | |
| 
 | |
| /**
 | |
|  * Replace a single HTML element with SVG markup
 | |
|  * corresponding to the element's `data-feather` attribute value.
 | |
|  * @param {HTMLElement} element
 | |
|  * @param {Object} attrs
 | |
|  */
 | |
| function replaceElement(element, attrs = {}) {
 | |
|   const elementAttrs = getAttrs(element);
 | |
|   const name = elementAttrs['data-feather'];
 | |
|   delete elementAttrs['data-feather'];
 | |
| 
 | |
|   const svgString = icons[name].toSvg({
 | |
|     ...attrs,
 | |
|     ...elementAttrs,
 | |
|     ...{ class: classnames(attrs.class, elementAttrs.class) },
 | |
|   });
 | |
|   const svgDocument = new DOMParser().parseFromString(
 | |
|     svgString,
 | |
|     'image/svg+xml',
 | |
|   );
 | |
|   const svgElement = svgDocument.querySelector('svg');
 | |
| 
 | |
|   element.parentNode.replaceChild(svgElement, element);
 | |
| }
 | |
| 
 | |
| /**
 | |
|  * Get the attributes of an HTML element.
 | |
|  * @param {HTMLElement} element
 | |
|  * @returns {Object}
 | |
|  */
 | |
| function getAttrs(element) {
 | |
|   return Array.from(element.attributes).reduce((attrs, attr) => {
 | |
|     attrs[attr.name] = attr.value;
 | |
|     return attrs;
 | |
|   }, {});
 | |
| }
 | |
| 
 | |
| export default replace;
 |