svg
Author SVG templates and bind shapes, colors, and text to reactive state.
svgUtilities
Author SVG templates and bind shapes, colors, and text to reactive state.
Overview
Use svg to author an SVG component. Bind its attributes and text to reactive props, then use the component in an HTML template. Changes to the props update the existing SVG elements.
Try it live
A reactive battery indicatorLive API
import { defineComponent, svg } from 'regor'
export function defineBattery() {
return defineComponent(
svg`<svg class="battery" viewBox="0 0 240 140" role="img" aria-label="Battery charge">
<title>{{ charge }} percent{{ charging ? ', charging' : '' }}</title>
<rect class="battery-outline" x="12" y="16" width="204" height="88" rx="12" />
<rect x="216" y="44" width="12" height="32" rx="4" fill="currentColor" />
<rect
x="20" y="24" height="72" rx="5"
:width="charge * 1.88"
:fill="charge <= 20 ? '#ef4444' : charge <= 50 ? '#f59e0b' : '#22c55e'"
/>
<path r-if="charging" d="M123 30 99 65H115L105 91 135 53H119Z" fill="currentColor" />
<text x="114" y="130" text-anchor="middle" fill="currentColor">{{ charge }}%</text>
</svg>`,
['charge', 'charging'],
)
} import { createApp, html, ref } from 'regor'
import { defineBattery } from './battery-svg'
export function createBattery() {
return {
charge: ref(65),
charging: ref(false),
components: { Battery: defineBattery() },
}
}
export const batteryTemplate = html`<div class="guide-demo guide-demo--split">
<div class="guide-controls">
<label for="battery-charge">Charge <output>{{ charge }}%</output></label>
<input
id="battery-charge"
type="range"
min="0"
max="100"
r-model.number="charge"
/>
<label class="guide-check">
<input type="checkbox" r-model="charging" /> Charging
</label>
</div>
<Battery :charge="charge" :charging="charging" />
</div>`
const element = globalThis.document?.querySelector('#battery-app')
if (element) createApp(createBattery(), { element, template: batteryTemplate }) Lower the charge to turn the battery amber, then red. Toggle Charging to show the bolt. The fill, text, and bolt are all SVG bindings.
Usage
import { createApp, html, ref } from 'regor'
import { defineBattery } from './battery-svg'
createApp(
{
charge: ref(65),
charging: ref(false),
components: { Battery: defineBattery() },
},
{
selector: '#battery-app',
template: html`
<label>Charge
<input type="range" min="0" max="100" r-model.number="charge" />
</label>
<Battery :charge="charge" :charging="charging" />
`,
},
) The battery-svg.ts tab contains the complete SVG component. Mount this example into <div id="battery-app"></div>. The parent owns the state; Battery receives reactive props.
Parameters
templates(TemplateStringsArray): Static segments of the template literal....args(unknown[]): Interpolated values inserted between template segments.
Return Value
- Returns a
stringcontaining the combined SVG markup.
The same bindings work inside SVG
Notes
svgbehaves exactly likehtmlat runtime (string assembly), so you can use it anywhere a template string is accepted.- Use
svgwhen you want semantic clarity in code and editor hinting for SVG snippets.