forked from unplugin/unplugin-icons
-
Notifications
You must be signed in to change notification settings - Fork 0
/
web-components.ts
27 lines (23 loc) · 855 Bytes
/
web-components.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import { camelize } from '@iconify/utils/lib/misc/strings'
import type { Compiler } from './types'
export const WebComponentsCompiler = ((svg, collection, icon, { webComponents: options }) => {
let id = `${collection}-${icon}`
if (options.iconPrefix)
id = `${options.iconPrefix}-${id}`
const name = camelize(id)
let code = `export default class ${name} extends HTMLElement {`
if (options.shadow) {
code += `constructor() {
super()
this.attachShadow({ mode: 'open' }).innerHTML = ${JSON.stringify(svg)}
}`
}
else {
// use connectedCallback because children can't be appended in the constructor of a CE:
code += `connectedCallback() { this.innerHTML = ${JSON.stringify(svg)} }`
}
code += '}'
if (options.autoDefine)
code += `\ncustomElements.define('${id}', ${name})`
return code
}) as Compiler