Skip to content

@shadekit/element

The zero-wrapper path: a real custom element, so it works anywhere a <div> works — Astro templates, markdown output, CMS HTML, SSR.

import { defineShaderElement } from '@shadekit/element';
import { aurora, nebula } from '@shadekit/shaders';
import { styles } from '@shadekit/shaders/styles';
defineShaderElement({ aurora, nebula }, { styles });
defineShaderElement(
shaders: Record<string, ShaderDefinition>,
options?: { tag?: string; styles?: Record<string, ShaderStyleLike> } & ShaderOptions,
): void
Option Description
tag Custom element tag name. Default 'sk-shader'
styles Preset registry for style="..." — pass @shadekit/shaders’ styles
...ShaderOptions Every engine option (backend, pointer, maxDpr, paused, …) applies to every instance the element creates

Calling defineShaderElement twice with the same tag is a no-op — safe to run in a shared layout and again per page.

<!-- named preset: engine + uniforms from the styles registry -->
<sk-shader style="aurora-ember"></sk-shader>
<!-- direct engine + uniform attributes -->
<sk-shader shader="aurora" speed="0.4" colors="[0.8, 0.3, 0.4]"></sk-shader>

Precedence: engine defaults < preset < explicit attributes. Any attribute matching a uniform name parses as a float, or as a JSON array when it starts with [:

  • speed="0.4" → 0.4
  • colors="[0.8, 0.3, 0.4]" → [0.8, 0.3, 0.4]

The element observes shader, style, and every uniform attribute:

  • Setting a uniform attribute calls updateUniforms() — the shader changes without re-mounting.
  • Changing shader (or style to a different engine) swaps the underlying instance; a same-engine preset swap only updates uniforms and keeps the compiled pipeline warm.
  • Removing the element from the DOM destroys the instance — no leaks.

Unknown style/shader names log a warning and render nothing, so a typo fails loudly in dev tools instead of silently black-boxing.

Each element exposes its engine for imperative control:

const el = document.querySelector('sk-shader');
el.instance?.pause();
el.instance?.updateUniforms({ speed: 0.8 });

instance is undefined until the element connects and mounts — check before use, or set uniforms via attributes and let the element sequence them (ready-queued).

<sk-shader style="gradient-dusk"></sk-shader>
<script>
import { defineShaderElement } from '@shadekit/element';
import { gradient } from '@shadekit/shaders/gradient';
import { styles } from '@shadekit/shaders/styles';
defineShaderElement({ gradient }, { styles });
</script>

Astro ships the script once per page; registration is idempotent.