02 / blog / how-toNovember 20266 min read

Scroll-drawn Svelte 5 components, installed with shadcn-svelte

The shadcn idea, copying a component's source into your project instead of installing a package of them, works in Svelte too, through the shadcn-svelte CLI. This post installs scroll animation sections that way into a SvelteKit app, then covers the three Svelte details we hit making them: when the action has to mount, the whitespace Svelte trims, and where a registry's files land.

  • How-To
  • Svelte 5
  • SvelteKit
  • shadcn-svelte
Svelte blocks
9 on /blocks
Tested with
Svelte 5 · SvelteKit 2
CLI
shadcn-svelte
Runtime
svg-scroll-draw only
01Once

Point the CLI at your project

shadcn-svelte reads a components.json at the root of the app to learn where your components and library code live. npx shadcn-svelte@latest init writes one; this is the file it needs, if you would rather write it yourself:

components.json13 lines
{
  "$schema": "https://shadcn-svelte.com/schema.json",
  "tailwind": { "css": "src/app.css", "baseColor": "neutral" },
  "aliases": {
    "components": "$lib/components",
    "utils": "$lib/utils",
    "ui": "$lib/components/ui",
    "hooks": "$lib/hooks",
    "lib": "$lib"
  },
  "typescript": true,
  "registry": "https://shadcn-svelte.com/registry"
}

The blocks are styled with Tailwind CSS, so the app needs Tailwind set up, and they use the svg-scroll-draw package at runtime. The CLI installs that dependency for you.

02Add

Add a section

Each block is one JSON file in the registry. Give the CLI its URL:

terminal1 line
npx shadcn-svelte@latest add https://svg-scroll-draw.vercel.app/r/svelte/signature-draw.json

The component lands at src/lib/components/scroll/SignatureDraw.svelte, as source you own and can edit, and you use it like any component:

src/routes/+page.svelte5 lines
<script lang="ts">
  import SignatureDraw from '$lib/components/scroll/SignatureDraw.svelte';
</script>

<SignatureDraw name="Ada Lovelace" note="London, 1843" accent="#ff3e00" />

The same works for the other 8 on /blocks: reveal cards, stat counter row, sticky feature, headline text reveal, pinned feature tour, horizontal case strip, video scrub, reading progress. Readers who ask for reduced motion get every one of them in its finished state.

03Timing

Mount the action after the drawing exists

A drawing generated in code, rather than written into the markup, is best made in the browser: the server HTML stays small and the server does no maths. In Svelte that means onMount, and that is where the first trap is. use:scrollDraw runs as soon as its element mounts and collects the paths inside it then, once. If the element is already on the page while the path has no d yet, the action measures a line of length zero.

Wrap the element in {#if}, so it only mounts once the drawing is there:

Spiral.svelte28 lines
<script lang="ts">
  import { onMount } from 'svelte';
  import { scrollDraw } from 'svg-scroll-draw/svelte';

  // Made in the browser: the server HTML carries no path data.
  function spiral(turns = 9) {
    const points: string[] = [];
    for (let t = 0; t <= turns * Math.PI * 2; t += 0.05) {
      const r = 46 * (1 - t / (turns * Math.PI * 2));
      points.push(`${(50 + r * Math.cos(t)).toFixed(2)} ${(50 + r * Math.sin(t)).toFixed(2)}`);
    }
    return 'M' + points.join('L');
  }

  let d = $state<string | null>(null);
  onMount(() => {
    d = spiral();
  });
</script>

<!-- The action collects the paths when it mounts, so it mounts with them. -->
{#if d}
  <div use:scrollDraw={{ easing: 'linear', trigger: { start: 'center 115%', end: 'center 50%' } }}>
    <svg viewBox="0 0 100 100" fill="none" stroke="currentColor" stroke-width="0.6">
      <path {d} />
    </svg>
  </div>
{/if}

The trigger runs from when the spiral's centre is 115% of the way down the screen to when it is at 50%, and linear keeps the pen in step with the scroll. In Chromium the spiral was 77% drawn with its centre at 65% of the screen and finished by 45%.

04Markup

Svelte trims the space at the start of an element

Svelte 5 removes whitespace at the start and end of an element's content. Usually that is what you want; in a caption built from pieces it eats a space you meant. Written as text, <span> · seed {seed}</span> compiles to · seed, and the caption reads “392 strokes· seed 417”. An expression is not trimmed:

Caption.svelte6 lines
<script lang="ts">
  let { strokes, seed }: { strokes: number; seed: number } = $props();
</script>

<!-- " · seed 417" written as text would lose its leading space. -->
<span>{strokes} strokes<span class="hidden sm:inline">{` · seed ${seed}`}</span></span>

This is a Svelte rule rather than anything about scroll animation, but ports from React and Vue hit it every time.

05Publishing

If you publish a shadcn-svelte registry

Both registries on this page installed every file one folder inside itself at first. The cause is the target field. The React shadcn CLI resolves a target from the project root, but shadcn-svelte resolves it inside the folder its alias points at: a component inside $lib/components, a registry:lib file inside $lib. A target of src/lib/components/scroll/Spiral.svelte therefore installed at src/lib/components/src/lib/components/scroll/Spiral.svelte. Give the path below the alias:

spiral.json12 lines
{
  "name": "spiral",
  "type": "registry:block",
  "dependencies": ["svg-scroll-draw"],
  "files": [
    {
      "type": "registry:component",
      "target": "scroll/Spiral.svelte",
      "content": "<script lang=\"ts\">…</script>…"
    }
  ]
}

The import still compiles if the file lands somewhere odd, so this does not show up as an error. Install each block into a fresh SvelteKit app and look at where its files went before you publish.

— / next

More sections, drawn like a plotter

scrollfx is a folio of plotter-art sections built on svg-scroll-draw (a flow-field hero, a hatched chart, a harmonograph call to action, a ridgeline footer), each available for React, Vue and Svelte and installed with the matching shadcn CLI.

npx shadcn-svelte@latest add https://svg-scroll-draw.vercel.app/r/svelte/signature-draw.jsonAll blocks →See scrollfx

Star on GitHubFound this useful? A star helps others find it.

→Parts list

Keep reading

More from the blog