Skip to content

Instantly share code, notes, and snippets.

@mizchi
Created October 14, 2025 11:16
Show Gist options
  • Select an option

  • Save mizchi/f856963c4a2f2c3bdc0e05ba59bf3988 to your computer and use it in GitHub Desktop.

Select an option

Save mizchi/f856963c4a2f2c3bdc0e05ba59bf3988 to your computer and use it in GitHub Desktop.
///|
fnalias @svg.node as svg_node
///|
fn[M] common_svg_node(
tag_name : String,
new_attrs : Array[@svg.Attribute[M]],
style~ : Array[String]?,
class~ : String?,
id~ : String?,
children~ : Array[@svg.Svg[M]]?,
) -> @svg.Svg[M] {
let attrs : Array[@svg.Attribute[M]] = []
for attr in new_attrs {
attrs.push(attr)
}
if id is Some(val) {
attrs.push(@svg.attribute("id", val))
}
if class is Some(val) {
attrs.push(@svg.attribute("class", val))
}
if style is Some(val) {
attrs.push(@svg.attribute("style", val.join(";")))
}
svg_node(tag_name, attrs, children.unwrap_or([]))
}
///|
/// svg element
pub fn[M] svg(
// common attributes
id? : String,
class? : String,
style? : Array[String],
// required
width~ : Int,
height~ : Int,
children : Array[@svg.Svg[M]],
) -> @html.Html[M] {
let attrs : Array[@svg.Attribute[M]] = []
if id is Some(val) {
attrs.push(@svg.attribute("id", val))
}
if class is Some(val) {
attrs.push(@svg.attribute("class", val))
}
if style is Some(val) {
attrs.push(@svg.attribute("style", val.join(";")))
}
let attrs : Array[@svg.Attribute[M]] = [
@svg.attribute("width", width.to_string()),
@svg.attribute("height", height.to_string()),
]
@svg.svg(attrs, children)
}
///|
/// svg rect element
pub fn[M] rect(
// common attributes
id? : String,
class? : String,
style? : Array[String],
// required
x~ : Int,
y~ : Int,
width~ : Int,
height~ : Int,
// optional
rx? : Int,
ry? : Int,
fill? : String,
stroke? : String,
stroke_width? : Int,
) -> @svg.Svg[M] {
let attrs : Array[@svg.Attribute[M]] = [
@svg.attribute("x", x.to_string()),
@svg.attribute("y", y.to_string()),
@svg.attribute("width", width.to_string()),
@svg.attribute("height", height.to_string()),
]
if rx is Some(val) {
attrs.push(@svg.attribute("rx", val.to_string()))
}
if ry is Some(val) {
attrs.push(@svg.attribute("ry", val.to_string()))
}
if style is Some(val) {
attrs.push(@svg.attribute("style", val.join(";")))
}
if fill is Some(val) {
attrs.push(@svg.attribute("fill", val))
}
if stroke is Some(val) {
attrs.push(@svg.attribute("stroke", val))
}
if stroke_width is Some(val) {
attrs.push(@svg.attribute("stroke-width", val.to_string()))
}
common_svg_node("rect", attrs, id~, style~, class~, children=None)
}
///|
/// svg text
pub fn[M] svg_text(
// common attributes
id? : String,
class? : String,
style? : Array[String],
// required
x~ : Int,
y~ : Int,
text~ : String,
// optional
dx? : Int,
dy? : Int,
rotate? : String,
length_adjust? : String,
text_length? : Int,
) -> @svg.Svg[M] {
let attrs : Array[@svg.Attribute[M]] = [
@svg.attribute("x", x.to_string()),
@svg.attribute("y", y.to_string()),
@svg.property("textContent", String(text)),
]
if dx is Some(val) {
attrs.push(@svg.attribute("dx", val.to_string()))
}
if dy is Some(val) {
attrs.push(@svg.attribute("dy", val.to_string()))
}
if rotate is Some(val) {
attrs.push(@svg.attribute("rotate", val))
}
if length_adjust is Some(val) {
attrs.push(@svg.attribute("lengthAdjust", val))
}
if text_length is Some(val) {
attrs.push(@svg.attribute("textLength", val.to_string()))
}
common_svg_node("text", attrs, id~, style~, class~, children=None)
}
///|
/// svg text
pub fn[M] path(
// common attributes
id? : String,
class? : String,
style? : Array[String],
// required
d~ : String,
) -> @svg.Svg[M] {
let attrs : Array[@svg.Attribute[M]] = [@svg.attribute("d", d)]
common_svg_node("path", attrs, id~, style~, class~, children=None)
}
///|
/// svg g element
pub fn[M] g(
children : Array[@svg.Svg[M]],
// common attributes
id? : String,
class? : String,
style? : Array[String],
// optional
fill? : String,
stroke? : String,
) -> @svg.Svg[M] {
let attrs : Array[@svg.Attribute[M]] = []
if fill is Some(val) {
attrs.push(@svg.attribute("fill", val))
}
if stroke is Some(val) {
attrs.push(@svg.attribute("stroke", val))
}
common_svg_node("g", attrs, id~, style~, class~, children=Some(children))
}
///|
/// svg g element
pub fn[M] image(
// common attributes
id? : String,
class? : String,
style? : Array[String],
// required
href~ : String,
x~ : Int,
y~ : Int,
// optional
preserveAspectRatio? : String,
) -> @svg.Svg[M] {
let attrs : Array[@svg.Attribute[M]] = [
@svg.attribute("href", href),
@svg.attribute("x", x.to_string()),
@svg.attribute("y", y.to_string()),
]
if preserveAspectRatio is Some(val) {
attrs.push(@svg.attribute("preserveAspectRatio", val))
}
common_svg_node("image", attrs, id~, style~, class~, children=None)
}
///|
/// svg circle element
pub fn[M] circle(
// common attributes
id? : String,
class? : String,
children? : Array[@svg.Svg[M]],
style? : Array[String],
// required
cx~ : Int,
cy~ : Int,
r~ : Int,
// optional
fill? : String,
stroke? : String,
stroke_width? : Int,
fill_opacity? : Int,
) -> @svg.Svg[M] {
let attrs : Array[@svg.Attribute[M]] = [
@svg.attribute("r", r.to_string()),
@svg.attribute("cx", cx.to_string()),
@svg.attribute("cy", cy.to_string()),
]
if fill is Some(val) {
attrs.push(@svg.attribute("fill", val))
}
if stroke is Some(val) {
attrs.push(@svg.attribute("stroke", val))
}
if stroke_width is Some(val) {
attrs.push(@svg.attribute("stroke-width", val.to_string()))
}
if fill_opacity is Some(val) {
attrs.push(@svg.attribute("fill-opacity", val.to_string()))
}
common_svg_node("circle", attrs, id~, style~, class~, children~)
}
///|
/// svg line element
pub fn[M] line(
// common attributes
id? : String,
class? : String,
children? : Array[@svg.Svg[M]],
style? : Array[String],
// required
x1? : Int,
y1? : Int,
x2? : Int,
y2? : Int,
// optional
fill? : String,
stroke? : String,
stroke_width? : Int,
stroke_linecap? : String,
) -> @svg.Svg[M] {
let attrs : Array[@svg.Attribute[M]] = []
if x1 is Some(val) {
attrs.push(@svg.attribute("x1", val.to_string()))
}
if y1 is Some(val) {
attrs.push(@svg.attribute("y1", val.to_string()))
}
if x2 is Some(val) {
attrs.push(@svg.attribute("x2", val.to_string()))
}
if y2 is Some(val) {
attrs.push(@svg.attribute("y2", val.to_string()))
}
if fill is Some(val) {
attrs.push(@svg.attribute("fill", val))
}
if stroke is Some(val) {
attrs.push(@svg.attribute("stroke", val))
}
if stroke_width is Some(val) {
attrs.push(@svg.attribute("stroke-width", val.to_string()))
}
if stroke_linecap is Some(val) {
attrs.push(@svg.attribute("stroke-linecap", val))
}
common_svg_node("line", attrs, id~, style~, class~, children~)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment