Skip to content

Commit e80f805

Browse files
bbohenCole Bemis
authored andcommitted
feat: Updated replace() to pass id from placeholder element (#193)
1 parent b7d2229 commit e80f805

File tree

3 files changed

+11
-7
lines changed

3 files changed

+11
-7
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -229,13 +229,13 @@ You can pass `feather.replace()` an `options` object:
229229
</script>
230230
```
231231

232-
All classes on a placeholder element (i.e. `<i>`) will be copied to the `<svg>` tag:
232+
The id and classes on a placeholder element (i.e. `<i>`) will be copied to the `<svg>` tag:
233233

234234
```html
235-
<i class="foo bar" data-feather="circle"></i>
235+
<i id="my-circle-icon" class="foo bar" data-feather="circle"></i>
236236
<!--
237237
<i> will be replaced with:
238-
<svg 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="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="10"></circle></svg>
238+
<svg id="my-circle-icon" 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="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="10"></circle></svg>
239239
-->
240240

241241
<script>

src/replace.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,13 @@ function replaceElement(element, options) {
4242
}
4343

4444
const elementClassAttr = element.getAttribute('class') || '';
45+
const elementIdAttr = element.getAttribute('id');
4546
const classNames = (
4647
options.class ? `${options.class} ${elementClassAttr}` : elementClassAttr
4748
);
4849

49-
const svgString = toSvg(key, Object.assign({}, options, { class: classNames }));
50+
const svgOptions = Object.assign({}, options, { class: classNames, id: elementIdAttr });
51+
const svgString = toSvg(key, svgOptions);
5052
const svgDocument = new DOMParser().parseFromString(svgString, 'image/svg+xml');
5153
const svgElement = svgDocument.querySelector('svg');
5254

src/to-svg.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export default function toSvg(key, options = {}) {
3535

3636
combinedOptions.class = addDefaultClassNames(combinedOptions.class, key);
3737

38-
const attributes = optionsToAtrributes(combinedOptions);
38+
const attributes = optionsToAttributes(combinedOptions);
3939

4040
return `<svg ${attributes}>${icons[key]}</svg>`;
4141
}
@@ -64,11 +64,13 @@ function addDefaultClassNames(classNames, key) {
6464
* @param {Object} options
6565
* @returns {string}
6666
*/
67-
function optionsToAtrributes(options) {
67+
function optionsToAttributes(options) {
6868
const attributes = [];
6969

7070
Object.keys(options).forEach(key => {
71-
attributes.push(`${key}="${options[key]}"`);
71+
if (options[key]) {
72+
attributes.push(`${key}="${options[key]}"`);
73+
}
7274
});
7375

7476
return attributes.join(' ');

0 commit comments

Comments
 (0)