01 / blog / how-toNovember 20266 min read

Scroll-drawn Vue 3 components, installed with shadcn-vue

shadcn-vue copies a component's source into your project instead of installing a package of them, so you own every line. This post installs scroll animation sections that way into a Vue 3 app, then covers the three Vue details we hit making them: when ScrollDraw has to mount, the difference between ref and :ref, and the one component that takes its options as an object.

  • How-To
  • Vue 3
  • shadcn-vue
  • Composition API
Vue blocks
9 on /blocks
Tested with
Vue 3 · Vite
CLI
shadcn-vue
Runtime
svg-scroll-draw only
01Once

Point the CLI at your project

npx shadcn-vue@latest init writes a components.json at the root of the app. The part the blocks rely on is aliases, which tells the CLI where components and library code live:

components.json (excerpt)12 lines
{
  "$schema": "https://shadcn-vue.com/schema.json",
  "typescript": true,
  "tailwind": { "css": "src/style.css", "baseColor": "neutral", "cssVariables": true },
  "aliases": {
    "components": "@/components",
    "utils": "@/lib/utils",
    "ui": "@/components/ui",
    "lib": "@/lib",
    "composables": "@/composables"
  }
}

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-vue@latest add https://svg-scroll-draw.vercel.app/r/vue/signature-draw.json

In a Vite app the component lands at src/components/scroll/SignatureDraw.vue, as source you own and can edit. Use it like any component:

App.vue7 lines
<script setup lang="ts">
import SignatureDraw from '@/components/scroll/SignatureDraw.vue';
</script>

<template>
  <SignatureDraw name="Ada Lovelace" note="London, 1843" accent="#42b883" />
</template>

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 ScrollDraw after the drawing exists

A drawing generated in code is best made in the browser, in onMounted, so the server HTML stays small. ScrollDraw starts when it mounts and collects the paths inside it then; it starts again only when one of its options changes. If it mounts while the path has no d yet, it measures a line of length zero.

Put v-if on it, so it only mounts once the drawing is there:

Spiral.vue28 lines
<script setup lang="ts">
import { onMounted, ref } from 'vue';
import { ScrollDraw } from 'svg-scroll-draw/vue';

// 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');
}

const d = ref<string | null>(null);
onMounted(() => {
  d.value = spiral();
});
</script>

<template>
  <!-- ScrollDraw collects the paths when it mounts, so it mounts with them. -->
  <ScrollDraw v-if="d" 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="d" />
    </svg>
  </ScrollDraw>
</template>

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, so it finishes as the reader reaches it.

04Composables

ref, not :ref

The composables (useScrollDraw, useScrollAnimate, useScrollCounter…) return a ref for you to put on an element. In <script setup>, Vue fills a ref when the template names it as a plain string attribute:

Logo.vue15 lines
<script setup lang="ts">
import { useScrollDraw } from 'svg-scroll-draw/vue';

// A ref the composable fills once the element mounts.
const logo = useScrollDraw({ easing: 'ease-out' });
</script>

<template>
  <!-- ref="logo", the name as a string: not :ref="logo". -->
  <div ref="logo">
    <svg viewBox="0 0 120 40" fill="none" stroke="currentColor" stroke-width="2">
      <path d="M10 30 L30 10 L50 30 L70 10 L90 30 L110 10" />
    </svg>
  </div>
</template>

Writing :ref="logo" instead binds the ref's current value, which is null, so the composable never gets an element and nothing animates. Every Vue example on this site had that mistake until 2.27.1, along with the file npx svg-scroll-draw init writes for Vue, and nothing errored; a check in CI now rejects it.

05Components

ScrollAnimate takes one options object

ScrollDraw takes each option as its own prop (easing, trigger, stagger). ScrollAnimate does not: what it animates is a map of CSS properties, which does not fit into props, so the whole configuration goes in :options:

Card.vue16 lines
<script setup lang="ts">
import { ScrollAnimate, type ScrollAnimateOptions } from 'svg-scroll-draw/vue';

const rise: ScrollAnimateOptions = {
  props: { opacity: [0, 1], transform: ['translateY(24px)', 'translateY(0)'] },
  easing: 'ease-out',
  once: true,
};
</script>

<template>
  <!-- ScrollAnimate takes one options object, not separate props. -->
  <ScrollAnimate :options="rise">
    <article class="card">Fades up as it scrolls into view.</article>
  </ScrollAnimate>
</template>

props are CSS properties, each with a start and an end value, so a slide is a transform, not an x or y.

— / 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-vue@latest add https://svg-scroll-draw.vercel.app/r/vue/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