This extension enhances HTMX functionality to work seamlessly with SVG elements, allowing for dynamic updates and interactions within SVG graphics.
The SVG extension supports the following swap methods:
innerSVG
: Updates the inner content of an SVG element.outerSVG
: Replaces an entire SVG element with new content.beforeendSVG
: Appends new SVG content to the end of the target element.deleteSVG
: Removes the target SVG element.beforebeginSVG
: Inserts new SVG content before the target element.afterbeginSVG
: Inserts new SVG content at the beginning of the target element.beforeendSVG
: Appends new SVG content to the end of the target element.afterendSVG
: Inserts new SVG content after the target element.deleteSVG
: Removes the target SVG element.
To use the SVG extension, include the svg_ext.js
script in your HTML and add the hx-ext="svg-ext"
attribute to your SVG elements or their containers. Instead of the normal hx-swap options use one of the svg swaps to interact with SVGs. The rest of the htmx functionality should work as normal.
To run the examples, you can use the following command:
python <example>
This example demonstrates how to update the radius of a circle dynamically:
G(id="ex-innerSVG", hx_ext="svg-ext", hx_swap="innerSVG", hx_target="#ex-innerSVG", hx_get="/innerSVG",hx_trigger="click")(...)
this activates the extension, specifies the swap method, the target element, the trigger, and the route.
This example shows how to add new SVG elements using the beforeendSVG
swap method:
G(id="ex-beforeEnd", hx_ext="svg-ext", hx_swap="beforeendSVG", hx_target="#ex-beforeEnd", hx_get="/beforeEnd",hx_trigger="click")
This example demonstrates how to replace an entire SVG element with new content:
This example shows how to perform an out-of-band inner SVG swap, updating both the SVG and a button: