Skip to content

Commit 59125f2

Browse files
committed
fix: 🐛 fix double SVG element with directive
1 parent 24b73e8 commit 59125f2

File tree

1 file changed

+26
-16
lines changed

1 file changed

+26
-16
lines changed

src/directive.ts

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ export const svgSpriteDirective: Directive = {
3535
)
3636
}
3737

38-
const children: VNodeArrayChildren = []
38+
// REVIEW: see below
39+
// const children: VNodeArrayChildren = []
3940
// <use> has already been added server-side?
4041
const hasUseNode = el && el.querySelector('use') !== null
4142

@@ -47,25 +48,34 @@ export const svgSpriteDirective: Directive = {
4748
// Override with "local" value without modifying the global one
4849
url: url === undefined ? options.url : url,
4950
})
50-
const use = h('use', {
51-
'xmlns:xlink': 'http://www.w3.org/1999/xlink',
52-
// eslint-disable-next-line quote-props
53-
href,
54-
'xlink-href': href,
55-
})
51+
// REVIEW: see below
52+
// const use = h('use', {
53+
// 'xmlns:xlink': 'http://www.w3.org/1999/xlink',
54+
// // eslint-disable-next-line quote-props
55+
// href,
56+
// 'xlink-href': href,
57+
// })
58+
const use = document.createElementNS('http://www.w3.org/2000/svg', 'use')
59+
// REVIEW: see below
60+
// children.push(use)
5661

57-
children.push(use)
62+
use.setAttributeNS('http://www.w3.org/1999/xlink', 'xlink:href', href)
63+
use.setAttribute('href', href)
64+
el.appendChild(use)
5865
}
5966

6067
// Render full SVG (no more document.createElement)
61-
const svg = h(
62-
'svg',
63-
{
64-
class: classes ? `${classes} ${options.class}` : options.class,
65-
},
66-
children
67-
)
68-
render(svg, el)
68+
// Not working because `render(svg, el)` append an `svg` element inside `original `svg`
69+
// and `render(use, el)` do not render
70+
// so we switch back to an old school `createElementNS`
71+
// const svg = h(
72+
// 'svg',
73+
// {
74+
// class: classes ? `${classes} ${options.class}` : options.class,
75+
// },
76+
// children
77+
// )
78+
// render(svg, el)
6979
},
7080
updated(el, binding, vnode) {
7181
// NOTE: guess it's only when expression is used…

0 commit comments

Comments
 (0)