DocumentationScribbleHighlight
ScribbleHighlight
Draws an informal underline or circle around existing content.
Shared live previews stay running while you switch code examples and API documentation.
Keep the layout precise, then add one imperfect detail.
Place content inside the component. Native attributes and events use Svelte spelling, such as onclick, and bind:element gives
you the root DOM element.
Quick start
Copy the component and everything it needs into your Svelte 5 project.
npx shadcn-svelte@1.8.0 add https://kida-ui.onrender.com/r/svelte/scribble-highlight.jsonRun this from a project with components.json. The CLI creates all required files and installs the declared runtime dependencies. New to shadcn? Follow theinstallation guide first.
Files land in src/lib/components/ui/kida by default. SvelteKit 3 projects import them through #lib; SvelteKit 2 and Vite projects use $lib, as in the examples here.
Copy manually6 files
1. Install runtime dependencies
npm install motion@^13.1.02. Create the source files
Keep this directory structure so the component's relative imports continue to work.
src/lib/components/ui/kida/scribble-highlight.svelte
<script lang="ts">
import './scribble-highlight.css'
import { drawInView } from './draw-in-view.motion.js'
import { untrack } from 'svelte'
import type { ScribbleHighlightProps } from './scribble-highlight.types.js'
let {
variant = 'underline',
once,
amount,
element = $bindable(null),
children,
...rest
}: ScribbleHighlightProps = $props()
const paths = {
underline: 'M2 12.5 C18 8.5 34 15.5 50 11 C66 6.5 82 14 98 9.5',
circle: 'M6 10 C16 1 78 0 94 8 C104 15 88 20 50 19 C14 19 -3 15 6 10',
} as const
const root = $derived(element)
const optionsKey = $derived(JSON.stringify([variant, once, amount]))
$effect(() => {
const node = root
void optionsKey
if (!node) return
return untrack(() => drawInView(node, { once, amount }))
})
</script>
<span {...rest} bind:this={element} data-kida-scribble="" data-variant={variant} data-state="idle"><span data-kida-scribble-content="">{@render children?.()}</span><svg aria-hidden="true" viewBox="0 0 100 20" preserveAspectRatio="none"><path d={paths[variant]} /></svg></span>src/lib/components/ui/kida/scribble-highlight.types.ts
import type { DrawInViewOptions } from './draw-in-view.motion.js';
import type { HTMLAttributes } from 'svelte/elements';
export interface ScribbleHighlightProps extends DrawInViewOptions, HTMLAttributes<HTMLSpanElement> {
variant?: 'underline' | 'circle';
element?: HTMLSpanElement | null;
}src/lib/components/ui/kida/scribble-highlight.css
@import './tokens.css';
[data-kida-scribble] {
--kida-scribble-color: #e94f86;
--kida-scribble-width: 0.14em;
--kida-scribble-duration: 650ms;
position: relative;
display: inline-block;
isolation: isolate;
}
[data-kida-scribble-content] {
position: relative;
z-index: 1;
}
[data-kida-scribble] svg {
position: absolute;
z-index: 0;
left: -4%;
width: 108%;
overflow: visible;
pointer-events: none;
}
[data-kida-scribble][data-variant="underline"] svg {
top: 78%;
height: 0.55em;
}
[data-kida-scribble][data-variant="circle"] svg {
inset: -12% -7%;
width: 114%;
height: 124%;
}
[data-kida-scribble] path {
fill: none;
stroke: var(--kida-scribble-color);
stroke-width: var(--kida-scribble-width);
stroke-linecap: round;
stroke-linejoin: round;
vector-effect: non-scaling-stroke;
}
[data-kida-scribble][data-state="idle"] path {
visibility: hidden;
}
/*
* A non-scaling stroke is dashed in screen pixels, so `pathLength` cannot describe the whole
* stroke once the SVG stretches past about 100px. drawInView sets --kida-draw-length to the
* stroke's measured length. One dash covers it; the longer gap and the extra stroke width at the
* start keep the dash's round cap out of sight until drawing begins. Without a measured length
* these declarations are invalid, and the stroke appears complete.
*/
[data-kida-scribble][data-state="active"] path {
stroke-dasharray: var(--kida-draw-length) calc(var(--kida-draw-length) * 2);
animation: kida-scribble-draw var(--kida-scribble-duration) var(--kida-ease-out) forwards;
}
@keyframes kida-scribble-draw {
from {
stroke-dashoffset: calc(var(--kida-draw-length) + var(--kida-scribble-width));
}
to {
stroke-dashoffset: 0;
}
}
@media (prefers-reduced-motion: reduce) {
[data-kida-scribble][data-state] path {
visibility: inherit;
animation: none;
stroke-dashoffset: 0;
}
}src/lib/components/ui/kida/tokens.css
/*
* Kida UI — motion tokens.
* Plain CSS custom properties (ADR D7). No preprocessor, no Tailwind, no build step.
*/
:root {
/* Durations */
--kida-duration-xs: 100ms;
--kida-duration-sm: 150ms;
--kida-duration-md: 300ms;
--kida-duration-lg: 500ms;
--kida-duration-xl: 800ms;
/* Easings */
--kida-ease-out: cubic-bezier(0.16, 1, 0.3, 1);
--kida-ease-in: cubic-bezier(0.7, 0, 0.84, 0);
--kida-ease-in-out: cubic-bezier(0.65, 0, 0.35, 1);
--kida-ease-spring: linear(0, 0.5, 0.9, 1.02, 1);
/* Travel distances used by the enter/exit keyframes */
--kida-distance-sm: 4px;
--kida-distance-md: 8px;
--kida-distance-lg: 16px;
/* Stagger step between siblings */
--kida-stagger: 60ms;
}
/*
* The shared reduced-motion guard for CSS-driven animation in the library.
* Durations collapse to ~0 by default. Components whose presence lifecycle cannot safely observe
* a 1ms animation, such as Collapse, disable it explicitly and complete presence in their adapter.
*/
@media (prefers-reduced-motion: reduce) {
:root {
--kida-duration-xs: 1ms;
--kida-duration-sm: 1ms;
--kida-duration-md: 1ms;
--kida-duration-lg: 1ms;
--kida-duration-xl: 1ms;
--kida-distance-sm: 0px;
--kida-distance-md: 0px;
--kida-distance-lg: 0px;
--kida-stagger: 0ms;
}
}src/lib/components/ui/kida/draw-in-view.motion.ts
import { inView } from 'motion'
import { prefersReducedMotion } from './reduced-motion.motion.js'
export interface DrawInViewOptions {
/** Animate only on the first viewport entry. @default true */
once?: boolean
/** How much of the element must be visible. @default 0.4 */
amount?: 'some' | 'all' | number
}
/** Custom property set on each shape: its drawn length in pixels, for dash-based drawing. */
const LENGTH = '--kida-draw-length'
/**
* Length of an SVG shape as drawn, in pixels of its `<svg>` viewport.
*
* A stroke with `vector-effect: non-scaling-stroke` is dashed in these pixels, not in user units
* or `pathLength`, and a stretched viewBox (`preserveAspectRatio="none"`) changes the drawn
* length with the element's size. Sampling the shape through its viewport matrix measures that
* length for any stretch.
*/
export function drawnLength(shape: SVGGeometryElement, samples = 48): number {
const matrix = shape.getCTM()
const total = shape.getTotalLength()
if (!matrix || !total) return 0
let length = 0
let previous: DOMPoint | undefined
for (let index = 0; index <= samples; index++) {
const point = shape.getPointAtLength((total * index) / samples).matrixTransform(matrix)
if (previous) length += Math.hypot(point.x - previous.x, point.y - previous.y)
previous = point
}
return length
}
export function drawInView(element: HTMLElement, options: DrawInViewOptions = {}): () => void {
if (prefersReducedMotion()) {
element.dataset.state = 'settled'
return () => {}
}
const { once = true, amount = 0.4 } = options
element.dataset.state = 'idle'
// Each shape's dash spans its whole drawn length, so it reveals in one stroke at any size.
// Rounding up and one extra pixel cover sampling error and keep the end cap hidden until drawn.
const shapes = [...element.querySelectorAll<SVGGeometryElement>('svg path')]
const measure = () => {
for (const shape of shapes) {
const length = drawnLength(shape)
if (length) shape.style.setProperty(LENGTH, `${Math.ceil(length) + 1}px`)
else shape.style.removeProperty(LENGTH)
}
}
measure()
// Font loading, content, and container changes resize the element, including after drawing.
const resize = new ResizeObserver(measure)
resize.observe(element)
const stop = inView(
element,
() => {
element.dataset.state = 'active'
if (once) stop()
return once
? undefined
: () => {
element.dataset.state = 'idle'
}
},
{ amount },
)
return () => {
stop()
resize.disconnect()
for (const shape of shapes) shape.style.removeProperty(LENGTH)
}
}src/lib/components/ui/kida/reduced-motion.motion.ts
const QUERY = '(prefers-reduced-motion: reduce)'
/**
* Whether the user has asked for reduced motion.
*
* SSR-safe: returns `false` when there is no `window`, matching the CSS default
* (the `prefers-reduced-motion` media query in `@kida-ui/styles` is the real guard;
* this is for the JS-driven paths that CSS cannot express).
*/
export function prefersReducedMotion(): boolean {
if (typeof window === 'undefined' || typeof window.matchMedia !== 'function') return false
return window.matchMedia(QUERY).matches
}
/**
* Subscribe to changes of the reduced-motion preference.
* Returns an unsubscribe function. No-op (and immediately unsubscribable) on the server.
*/
export function watchReducedMotion(onChange: (reduced: boolean) => void): () => void {
if (typeof window === 'undefined' || typeof window.matchMedia !== 'function') return () => {}
const list = window.matchMedia(QUERY)
const handler = (event: MediaQueryListEvent) => onChange(event.matches)
list.addEventListener('change', handler)
return () => list.removeEventListener('change', handler)
}<script lang="ts">
import ScribbleHighlight from '$lib/components/ui/kida/scribble-highlight.svelte'
</script>
<ScribbleHighlight>imperfect detail</ScribbleHighlight>Customization
<script lang="ts">
import { ScribbleHighlight } from '@kida-ui/svelte'
</script>
<ScribbleHighlight variant="circle" class="demo-scribble-circle" style="--kida-scribble-color: #75a98f">
worth keeping
</ScribbleHighlight>Use variant="circle" for a surrounding mark. Set --kida-scribble-color, --kida-scribble-width, and --kida-scribble-duration on the root to customize the stroke.
API
Props
| Prop | Type | Default | Description |
|---|---|---|---|
variant |
'underline' | 'circle' |
'underline' |
Shape drawn around the content. |
once |
boolean |
true |
Draw only on the first viewport entry. |
amount |
'some' | 'all' | number |
0.4 |
Required visible portion. |
element |
HTMLSpanElement | null |
null |
Use bind:element to read the root DOM element. |
The root also accepts span attributes, class, style, and event handlers.
CSS custom properties
| Property | Default | Description |
|---|---|---|
--kida-scribble-color |
#e94f86 |
Stroke color. |
--kida-scribble-width |
0.14em |
Stroke width. |
--kida-scribble-duration |
650ms |
Draw duration. |
Accessibility and input behavior
The SVG is decorative and hidden from assistive technology; the wrapped text remains ordinary, readable content. Static paths avoid generated IDs and remain deterministic during server rendering. Reduced motion displays the completed mark without running the draw animation.
Troubleshooting
The mark is positioned around the component’s inline box. Wrap only the words you want marked, and check reduced-motion settings if the completed stroke appears without drawing.