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.
Quick start
Copy the component and everything it needs into your React project.
npx shadcn@latest add https://kida-ui.onrender.com/r/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 manually7 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.tsx
'use client'
import './scribble-highlight.css'
import { type DrawInViewOptions, drawInView } from './motion/draw-in-view.js'
import type { HTMLAttributes, ReactNode } from 'react'
import { forwardRef, useRef } from 'react'
import { composeRefs } from './compose-refs.js'
import { useIsomorphicLayoutEffect } from './use-isomorphic-layout-effect.js'
export interface ScribbleHighlightProps
extends DrawInViewOptions,
Omit<HTMLAttributes<HTMLSpanElement>, 'children'> {
children?: ReactNode
/** Shape drawn around the content. @default 'underline' */
variant?: 'underline' | 'circle'
}
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
export const ScribbleHighlight = forwardRef<HTMLSpanElement, ScribbleHighlightProps>(
function ScribbleHighlight(
{ children, variant = 'underline', once, amount, ...props },
forwardedRef,
) {
const ref = useRef<HTMLSpanElement>(null)
useIsomorphicLayoutEffect(() => {
const element = ref.current
if (!element) return
return drawInView(element, { once, amount })
}, [amount, once, variant])
return (
<span
{...props}
ref={composeRefs(ref, forwardedRef)}
data-kida-scribble=""
data-variant={variant}
data-state="idle"
>
<span data-kida-scribble-content="">{children}</span>
<svg aria-hidden="true" viewBox="0 0 100 20" preserveAspectRatio="none">
<path d={paths[variant]} />
</svg>
</span>
)
},
)src/components/ui/kida/compose-refs.ts
import type { Ref } from 'react'
/** Feed one node to several refs — ours for measuring, Zag's for animation detection. */
export function composeRefs<T>(...refs: (Ref<T> | undefined)[]) {
return (node: T | null) => {
for (const ref of refs) {
if (typeof ref === 'function') ref(node)
else if (ref) (ref as { current: T | null }).current = node
}
}
}src/components/ui/kida/use-isomorphic-layout-effect.ts
'use client'
import { useEffect, useLayoutEffect } from 'react'
/**
* `useLayoutEffect` in the browser, `useEffect` on the server.
*
* Motion has to be applied before paint or the user sees an unstyled frame, but React
* warns when `useLayoutEffect` runs during SSR — where it does nothing anyway.
*/
export const useIsomorphicLayoutEffect = typeof window !== 'undefined' ? useLayoutEffect : useEffectsrc/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/motion/draw-in-view.ts
import { inView } from 'motion'
import { prefersReducedMotion } from './reduced-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/motion/reduced-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)
}import { ScribbleHighlight } from '@/components/ui/kida/scribble-highlight'
export default function ScribbleHighlightExample() {
return <ScribbleHighlight>imperfect detail</ScribbleHighlight>
}Customization
import { ScribbleHighlight } from '@kida-ui/react'
export default function ScribbleHighlightCircle() {
return (
<ScribbleHighlight
variant="circle"
className="demo-scribble-circle"
style={{ '--kida-scribble-color': '#75a98f' } as React.CSSProperties}
>
worth keeping
</ScribbleHighlight>
)
}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, className, style, and a forwarded ref.
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.