Index of every page on this site: llms.txt
All skills
spences10 avatar

/svelte-template-directives

@5fe4989
Run it yourself

Svelte template directives ({@attach}, {@html}, {@render}, {@const}, {@debug}). Use for DOM manipulation, third-party libs, tooltips, canvas, dynamic HTML. @attach replaces use: actions.

Use this Skill: https://skilld.dev/gh/spences10/svelte-skills-kit/svelte-template-directives

Nothing lands on disk. Nothing to clean up.

Fork this Skill

Edit a local copy. It keeps the author and licence.

referencesattach-patterns.md

≈1.8k tokens on demand. Your agent reads this file only when SKILL.md points to it.

@attach Patterns

Available in Svelte 5.29+

Why @attach Over use: Actions?

Attachments are fully reactive. When dependencies change, the attachment re-runs automatically. Actions don't do this - they only run once on mount.

<!-- use: - runs ONCE, ignores content changes -->
<button use:tooltip={content}>Won't update</button>

<!-- @attach - re-runs when content changes -->
<button {@attach tooltip(content)}>Updates!</button>

Pattern 1: Third-Party Library Integration

The most common use case - integrating DOM-manipulating libraries like image zoom, tooltips, editors, etc.

ImageZoom Example

<script>
	import ImageZoom from 'js-image-zoom';

	const options = { width: 400, zoomWidth: 500 };

	function useZoom(dom) {
		new ImageZoom(dom, options);
		return () => {
			console.log('cleaning up');
		};
	}
</script>

<figure {@attach useZoom}>
	<img src="photo.jpg" alt="zoomable photo" />
</figure>

Tippy.js Tooltips

<script>
	import tippy from 'tippy.js';

	function tooltip(content) {
		return (element) => {
			const instance = tippy(element, { content });
			return instance.destroy;
		};
	}

	let tip = $state('Hello!');
</script>

<!-- Tooltip content updates reactively -->
<button {@attach tooltip(tip)}>Hover me</button>

<input bind:value={tip} placeholder="Change tooltip" />

Pattern 2: Canvas Drawing

Perfect for canvas where you need reactive updates without recreating the context.

<script>
	let color = $state('#ff0000');
	let size = $state(50);
</script>

<canvas
	width="200"
	height="200"
	{@attach (canvas) => {
		const ctx = canvas.getContext('2d');

		// This inner effect re-runs on color/size change
		// but canvas context is preserved
		$effect(() => {
			ctx.clearRect(0, 0, canvas.width, canvas.height);
			ctx.fillStyle = color;
			ctx.fillRect(
				(canvas.width - size) / 2,
				(canvas.height - size) / 2,
				size,
				size
			);
		});
	}}
/>

<input type="color" bind:value={color} />
<input type="range" bind:value={size} min="10" max="100" />

Pattern 3: Component Pass-Through

Attachments automatically pass through wrapper components when you spread props. This enables "augmented element" patterns.

<!-- Button.svelte -->
<script>
	let { children, ...props } = $props();
</script>

<button {...props}>
	{@render children?.()}
</button>
<!-- App.svelte -->
<script>
	import Button from './Button.svelte';
	import { tooltip } from './attachments.js';
</script>

<!-- The attachment passes through to the inner <button>! -->
<Button {@attach tooltip('Click me for help')}>
	Help
</Button>

Pattern 4: Attachment Factories

Factory functions return attachment implementations, enabling parameterized behavior.

<script>
	function highlight(color) {
		return (element) => {
			const original = element.style.backgroundColor;
			element.style.backgroundColor = color;

			return () => {
				element.style.backgroundColor = original;
			};
		};
	}

	let isActive = $state(false);
</script>

<!-- Attachment recreates when isActive changes -->
{#if isActive}
	<div {@attach highlight('yellow')}>Highlighted!</div>
{/if}

Pattern 5: Avoiding Expensive Re-runs

For expensive setup work, pass data via accessor function and read it in a child effect.

<script>
	function expensiveChart(getData) {
		return (node) => {
			// Expensive - runs ONCE
			const chart = createComplexChart(node);

			// Cheap - re-runs on data change
			$effect(() => {
				chart.update(getData());
			});

			return () => chart.destroy();
		};
	}

	let data = $state([1, 2, 3]);
</script>

<!-- Pass accessor function, not the data directly -->
<div {@attach expensiveChart(() => data)}>Chart</div>

Pattern 6: Converting Legacy Actions

Use fromAction to convert existing action libraries to attachments.

<script>
	import { fromAction } from 'svelte/attachments';
	import { someAction } from 'some-legacy-library';

	const attached = fromAction(someAction);
</script>

<!-- Now works as an attachment with full reactivity -->
<div {@attach attached(options)}>...</div>

Pattern 7: Multiple Attachments

Elements can have any number of attachments.

<button
	{@attach tooltip('Help text')}
	{@attach trackClicks}
	{@attach highlight(isActive ? 'yellow' : 'transparent')}
>
	Multi-attached button
</button>

Pattern 8: Inline Attachments

For one-off cases, define attachments inline.

<div
	{@attach (el) => {
		console.log('mounted:', el);
		return () => console.log('unmounted');
	}}
>
	Lifecycle logging
</div>

Pattern 9: DOM-Controlling Libraries (ProseMirror, etc.)

For libraries that want to control their own DOM segment, combine @attach with the imperative component API.

<script>
	import { mount, unmount } from 'svelte';
	import MyComponent from './MyComponent.svelte';

	function proseMirrorNodeView(node) {
		return (dom) => {
			// ProseMirror controls this DOM node
			// but we can mount Svelte components inside it
			const component = mount(MyComponent, {
				target: dom,
				props: { data: node.attrs }
			});

			return () => unmount(component);
		};
	}
</script>

Pattern 10: Registering Elements with Global State

Use @attach to register DOM elements with state classes. This avoids $effect sync loops and is cleaner than bind:this chains.

// modal-state.svelte.ts
class ModalState {
  dialog: HTMLDialogElement | null = null;
  input: HTMLInputElement | null = null;
  is_open = $state(false);

  // Attach functions return cleanup
  register = (el: HTMLDialogElement) => {
    this.dialog = el;
    return () => {
      this.dialog = null;
    };
  };

  register_input = (el: HTMLInputElement) => {
    this.input = el;
    return () => {
      this.input = null;
    };
  };

  open() {
    if (!this.dialog?.open) {
      this.is_open = true;
      this.dialog?.showModal();
      this.input?.focus();
    }
  }

  close() {
    this.is_open = false;
    this.dialog?.close();
  }

  toggle() {
    this.is_open ? this.close() : this.open();
  }
}

export const modal_state = new ModalState();
<!-- Modal.svelte -->
<script>
	import { modal_state } from './modal-state.svelte';
</script>

<dialog
	{@attach modal_state.register}
	onclose={modal_state.close}
>
	<input {@attach modal_state.register_input} />
</dialog>
<!-- Anywhere else - no component ref needed -->
<script>
	import { modal_state } from './modal-state.svelte';
</script>

<button onclick={modal_state.toggle}>Open Modal</button>

Benefits:

  • No $effect needed for state/DOM sync
  • State controls element directly via imperative methods
  • Clean cleanup on unmount
  • Any component can open/close without bind:this chains
  • Avoids event loops from dialog.close() firing onclose

When to Still Use use: Actions

  • Legacy code/libraries not yet updated
  • When you specifically DON'T want re-runs on argument change
  • Simple one-time DOM setup with no reactive dependencies

Source: SKILL.md on GitHub

No rule matched.

skilld matched fixed text patterns in SKILL.md and file names. Patterns miss obfuscated code.

skilld run checks every file with the same patterns. It asks for approval before it loads a Skill with a behavior marked Needs approval.

No alerts2mo3 checks · Risk SAFE
  • Gen Agent Trust Hub2mo

    This skill is a technical reference guide for Svelte 5 template directives. It demonstrates security best practices by identifying potential risks associated with the raw HTML rendering directive and providing explicit instructions on how to use sanitization libraries like DOMPurify to mitigate those risks. No malicious intent, obfuscation, or unauthorized data access patterns were detected.

  • Socket2mo

    No alerts

  • Snyk2mo

    Risk: LOW · No issues

Signed by skilld at 5fe4989. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub last month.

Steadyupdated 7 months ago

README badge

README badge for spences10/svelte-skills-kit/svelte-template-directives