Other frameworks
<ml-sigil-icon> is a standard custom element; render it like any tag.
import '@munsonlabs/sigil/element'
export const Heart = ({ active }) => <ml-sigil-icon name="heart" variant={active ? 'active' : undefined} />
<script>
import '@munsonlabs/sigil/element'
export let active = false
</script>
<ml-sigil-icon name="heart" variant={active ? 'active' : undefined}></ml-sigil-icon>
import '@munsonlabs/sigil/element'
const el = document.createElement('ml-sigil-icon')
el.name = 'heart'
button.append(el)
Without the element
watchIcon is what <ml-sigil-icon> and <Sigil> are built on: it resolves synchronously when it can, asynchronously otherwise, runs again whenever the registry changes, and drops results that arrive late. Reach for it directly when you don't want a custom element in the tree at all - a design system that wants a real <span>/<i>, SSR without a hydration mismatch to worry about, or a node you don't control as a component.
import { useEffect, useState } from 'react'
import { watchIcon } from '@munsonlabs/sigil'
import type { IconQuery, ResolvedIcon } from '@munsonlabs/sigil'
interface IconProps {
name: string
library?: string
variant?: string
}
export function Icon({ name, library, variant }: IconProps) {
const [icon, setIcon] = useState<ResolvedIcon>()
useEffect(() => {
const query: IconQuery = { library, variant }
return watchIcon(name, query, setIcon) // watchIcon's return value is already the cleanup function
}, [name, library, variant])
if (!icon) return null
const Tag = icon.tag as keyof JSX.IntrinsicElements
return icon.html !== undefined ? (
<Tag className={icon.className} aria-hidden="true" dangerouslySetInnerHTML={{ __html: icon.html }} />
) : (
<Tag className={icon.className} aria-hidden="true">
{icon.text}
</Tag>
)
}
<Icon name="bookmark" library="mlv" variant="active" />
<script>
import { onDestroy } from 'svelte'
import { watchIcon } from '@munsonlabs/sigil'
export let name
export let library = undefined
export let variant = undefined
let icon
let stop
$: {
stop?.()
stop = watchIcon(name, { library, variant }, (resolved) => (icon = resolved))
}
onDestroy(() => stop?.())
</script>
{#if icon}
<svelte:element this={icon.tag} class={icon.className} aria-hidden="true">
{#if icon.html !== undefined}
{@html icon.html}
{:else}
{icon.text}
{/if}
</svelte:element>
{/if}
<Icon name="bookmark" library="mlv" variant="active" />
import { register, watchIcon } from '@munsonlabs/sigil'
import { createIconNode } from '@munsonlabs/sigil/element?defer'
register(
'lucide',
{
resolver: (name) => `https://cdn.jsdelivr.net/npm/lucide-static/icons/${name}.svg`,
},
{ default: true },
)
const target = document.querySelector('#save-button')!
const stop = watchIcon('bookmark', { variant: 'active' }, (icon) => {
target.replaceChildren(...(icon ? [createIconNode(icon)] : []))
})
// when the host goes away
stop()
?defer on the element import gives you createIconNode without defining <ml-sigil-icon>, so nothing is registered on the page that you don't render yourself.
Each framework's version re-subscribes whenever name/library/variant change and cleans up the old subscription first - React via the effect's dependency array, Svelte via the reactive statement re-running and stop?.() before it re-assigns. For a one-off lookup there are getIconSync(name, query) and getIcon(name, query); subscribe(listener) fires after every registry change.