DocumentationTextBloom

TextBloom

Reveals readable text with a gentle rise or a pronounced pop and settle.

Svelte

Shared live previews stay running while you switch code examples and API documentation.

Pop by word

Make the headline bloom.

Quick start

Add to your project

Copy the component and everything it needs into your React project.

RecommendedUse the shadcn CLI
npx shadcn@latest add https://kida-ui.onrender.com/r/text-bloom.json

Run 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.

View registry JSON
Copy manually6 files

1. Install runtime dependencies

npm install motion@^13.1.0

2. Create the source files

Keep this directory structure so the component's relative imports continue to work.

src/components/ui/kida/text-bloom.tsx
'use client'

import './text-bloom.css'
import { type TextBloomOptions, textBloom, textBloomInitialStyle } from './motion/text-bloom.js'
import type { CSSProperties, ElementType, HTMLAttributes } from 'react'
import { forwardRef, useRef } from 'react'
import { composeRefs } from './compose-refs.js'
import { useIsomorphicLayoutEffect } from './use-isomorphic-layout-effect.js'

export interface TextBloomProps
  extends TextBloomOptions,
    Omit<HTMLAttributes<HTMLElement>, 'children'> {
  children: string
  /** Segment by words or Unicode characters. @default 'word' */
  by?: 'word' | 'character'
  /** Element to render. @default 'span' */
  as?: ElementType
}

function splitText(text: string, by: 'word' | 'character') {
  return by === 'character' ? Array.from(text) : text.split(/(\s+)/)
}

export const TextBloom = forwardRef<HTMLElement, TextBloomProps>(function TextBloom(
  {
    as: Tag = 'span',
    by = 'word',
    voice = 'gentle',
    stagger,
    delay,
    duration,
    once,
    amount,
    children,
    ...props
  },
  forwardedRef,
) {
  const ref = useRef<HTMLElement>(null)

  useIsomorphicLayoutEffect(() => {
    const element = ref.current
    if (!element) return
    return textBloom(element, { voice, stagger, delay, duration, once, amount })
  }, [Tag, amount, by, children, delay, duration, once, stagger, voice])

  let cursor = 0
  return (
    <Tag {...props} ref={composeRefs(ref, forwardedRef)} data-kida-text-bloom="" data-state="idle">
      <span>
        {splitText(children, by).map((segment) => {
          const key = `${cursor}-${segment}`
          cursor += segment.length
          if (/^\s+$/.test(segment)) return segment
          return (
            <span
              key={key}
              data-kida-text-bloom-segment=""
              style={textBloomInitialStyle(voice) as CSSProperties}
            >
              {segment}
            </span>
          )
        })}
      </span>
    </Tag>
  )
})
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 : useEffect
src/components/ui/kida/text-bloom.css
[data-kida-text-bloom-segment] {
  display: inline-block;
}

@media (prefers-reduced-motion: reduce) {
  [data-kida-text-bloom-segment] {
    /* biome-ignore lint/complexity/noImportantStyles: overrides the SSR inline initial state */
    opacity: 1 !important;
    /* biome-ignore lint/complexity/noImportantStyles: overrides the SSR inline initial state */
    transform: none !important;
  }
}
src/components/ui/kida/motion/text-bloom.ts
import { animate, inView, stagger } from 'motion'
import { prefersReducedMotion } from './reduced-motion.js'

export type TextBloomVoice = 'gentle' | 'pop'

export interface TextBloomOptions {
  /** Motion character. @default 'gentle' */
  voice?: TextBloomVoice
  /** Seconds between segments. @default 0.045 */
  stagger?: number
  /** Seconds before the first segment. @default 0 */
  delay?: number
  /** Seconds per segment. @default 0.55 */
  duration?: number
  /** Animate only on the first viewport entry. @default true */
  once?: boolean
  /** How much of the root must be visible. @default 0.4 */
  amount?: 'some' | 'all' | number
}

const DEFAULTS = {
  voice: 'gentle',
  stagger: 0.045,
  delay: 0,
  duration: 0.55,
  once: true,
  amount: 0.4,
} as const

export function textBloomInitialStyle(voice: TextBloomVoice = 'gentle') {
  return {
    opacity: 0,
    transform:
      voice === 'pop'
        ? 'translate3d(0, 18px, 0) scale(0.86)'
        : 'translate3d(0, 10px, 0) scale(0.98)',
    transformOrigin: '50% 80%',
    willChange: 'transform, opacity',
  } as const
}

export function textBloom(element: HTMLElement, options: TextBloomOptions = {}): () => void {
  const settings = {
    voice: options.voice ?? DEFAULTS.voice,
    stagger: options.stagger ?? DEFAULTS.stagger,
    delay: options.delay ?? DEFAULTS.delay,
    duration: options.duration ?? DEFAULTS.duration,
    once: options.once ?? DEFAULTS.once,
    amount: options.amount ?? DEFAULTS.amount,
  }
  const segments = [...element.querySelectorAll<HTMLElement>('[data-kida-text-bloom-segment]')]
  if (segments.length === 0) return () => {}

  const hide = () => {
    for (const segment of segments)
      Object.assign(segment.style, textBloomInitialStyle(settings.voice))
    element.dataset.state = 'idle'
  }
  const settle = () => {
    for (const segment of segments) {
      segment.style.opacity = '1'
      segment.style.transform = 'none'
      segment.style.transformOrigin = ''
      segment.style.willChange = 'auto'
    }
    element.dataset.state = 'settled'
  }

  if (prefersReducedMotion()) {
    settle()
    return () => {}
  }

  hide()
  let controls: ReturnType<typeof animate> | undefined
  let settleFrame = 0

  const cancelSettle = () => {
    if (!settleFrame) return
    cancelAnimationFrame(settleFrame)
    settleFrame = 0
  }

  const stop = inView(
    element,
    () => {
      cancelSettle()
      element.dataset.state = 'active'
      const pop = settings.voice === 'pop'
      controls = animate(
        segments,
        {
          opacity: pop ? [0, 1, 1, 1] : [0, 1, 1],
          transform: pop
            ? [
                'translate3d(0, 18px, 0) scale(0.86)',
                'translate3d(0, -4px, 0) scale(1.08)',
                'translate3d(0, 1px, 0) scale(0.985)',
                'translate3d(0, 0, 0) scale(1)',
              ]
            : [
                'translate3d(0, 10px, 0) scale(0.98)',
                'translate3d(0, 0, 0) scale(1)',
                'translate3d(0, 0, 0) scale(1)',
              ],
        },
        {
          duration: settings.duration,
          delay: stagger(settings.stagger, { startDelay: settings.delay }),
          // Pop reaches its visible overshoot early, then recoils once before settling.
          ease: pop
            ? [
                [0.16, 1, 0.3, 1],
                [0.4, 0, 0.6, 1],
                [0.16, 1, 0.3, 1],
              ]
            : [0.16, 1, 0.3, 1],
          times: pop ? [0, 0.45, 0.72, 1] : [0, 0.72, 1],
        },
      )

      const currentControls = controls
      void currentControls.finished
        .then(() => {
          // `animate()` creates one animation per property and segment. Wait for the whole
          // group, then move one frame past Motion's final style commits before cleaning up.
          if (controls !== currentControls) return
          settleFrame = requestAnimationFrame(() => {
            settleFrame = 0
            if (controls === currentControls) settle()
          })
        })
        .catch(() => {
          // Cancelling a pass rejects its finished promise; the exit cleanup re-arms it.
        })

      if (settings.once) stop()
      return settings.once
        ? undefined
        : () => {
            cancelSettle()
            controls?.cancel()
            hide()
          }
    },
    { amount: settings.amount },
  )

  return () => {
    stop()
    cancelSettle()
    controls?.cancel()
  }
}
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 { TextBloom } from '@/components/ui/kida/text-bloom'

export default function TextBloomExample() {
  return (
    <TextBloom as="h1" voice="pop">
      Make the headline bloom.
    </TextBloom>
  )
}

Customization

Gentle by character
Lilac, softly.
import { TextBloom } from '@kida-ui/react'

export default function TextBloomCharacters() {
  return (
    <TextBloom
      by="character"
      stagger={0.025}
      style={{ color: '#6552a8' }}
      className="demo-character-bloom"
    >
      Lilac, softly.
    </TextBloom>
  )
}

Use word segmentation for most interface copy. Character segmentation is best reserved for short display phrases, where its longer sequence will not delay comprehension.

Gentle uses a soft rise without a bounce. Pop starts smaller, rises into an 8% overshoot, then briefly recoils before settling. Both use the same default duration and finish at the text’s natural size and position.

API

Props

Prop Type Default Description
children string required The readable and animated text.
by 'word' | 'character' 'word' Segmentation unit.
voice 'gentle' | 'pop' 'gentle' Soft rise or pronounced overshoot with a brief recoil.
stagger number 0.045 Seconds between segments.
delay number 0 Seconds before the first segment.
duration number 0.55 Seconds per segment.
once boolean true Animate only on the first viewport entry.
amount 'some' | 'all' | number 0.4 Required visible portion.
as ElementType 'span' Semantic element to render.

Safe native attributes, className, style, and a forwarded ref are passed to the root.

Accessibility and input behavior

The animated segments remain in authored order as one DOM text sequence, without replacing the text with an ARIA label on a generic element. The same text is therefore available to assistive technology, selection, and copy operations without a duplicated accessible copy.

Under reduced motion, every segment renders immediately at its settled position. The server output uses deterministic segments and keys, so hydration does not change the text structure.

Troubleshooting

children must be a string so TextBloom can create deterministic, accessible segments. Refresh or change the component’s key to replay a one-time bloom that has already entered the viewport.