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 in the default slot.. Native attributes use Vue spelling, and component refs expose element.
Quick start
Copy the component and everything it needs into your Vue 3.5+ project.
npx shadcn-vue@2.8.2 add https://kida-ui.onrender.com/r/vue/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.
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/components/ui/kida/scribble-highlight.vue
<script setup lang="ts">
import './scribble-highlight.css'
import { drawInView } from './draw-in-view.motion.js'
import { shallowRef, watch } from 'vue'
import type { ScribbleHighlightProps } from './scribble-highlight.types.js'
defineOptions({ name: 'KidaScribbleHighlight', inheritAttrs: false })
const props = withDefaults(defineProps<ScribbleHighlightProps>(), { once: undefined })
const element = shallowRef<HTMLSpanElement | null>(null)
defineExpose({ element })
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
watch(
[() => element.value, () => props.variant, () => props.once, () => props.amount],
(_, __, onCleanup) => {
if (element.value) onCleanup(drawInView(element.value, { ...props }))
},
{ flush: 'post' },
)
</script>
<template>
<span v-bind="$attrs" ref="element" data-kida-scribble="" :data-variant="props.variant ?? 'underline'" :data-state="element?.dataset.state ?? 'idle'">
<span data-kida-scribble-content=""><slot /></span>
<svg aria-hidden="true" viewBox="0 0 100 20" preserveAspectRatio="none">
<path :d="paths[props.variant ?? 'underline']" />
</svg>
</span>
</template>src/components/ui/kida/scribble-highlight.types.ts
import type { DrawInViewOptions } from './draw-in-view.motion.js';
export interface ScribbleHighlightProps extends DrawInViewOptions {
/** Shape drawn around the default slot. @default 'underline' */
variant?: 'underline' | 'circle';
}
/** Vue component refs expose the root DOM element, null when presence removes it. */
export interface KidaElement {
element: HTMLElement | null;
}src/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/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/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/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 setup lang="ts">
import ScribbleHighlight from '@/components/ui/kida/scribble-highlight.vue'
</script>
<template>
<ScribbleHighlight>imperfect detail</ScribbleHighlight>
</template>Customization
<script setup lang="ts">
import { ScribbleHighlight } from '@kida-ui/vue'
</script>
<template>
<ScribbleHighlight
variant="circle"
class="demo-scribble-circle"
:style="{ '--kida-scribble-color': '#75a98f' }"
>
worth keeping
</ScribbleHighlight>
</template>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. |
The root accepts span attributes, class, style, and a component ref exposing element.
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.