Skip to Content
vitus·labs
Kinetic

API Reference

Complete reference for the kinetic chain factory, component props, hooks, presets, and types.

kinetic(tag)

The single public factory. Returns a renderable React component with chain methods attached.

function kinetic<Tag extends ElementType>(tag: Tag): KineticComponent<Tag, 'transition'>
ArgumentTypeDescription
tagElementTypeHTML tag name ('div', 'section', …) or any React component that forwards refs.

The returned component is a KineticComponent<Tag, Mode> — a React FC with chain methods (.preset, .enter, .collapse, .group, …). Default mode: 'transition'.

import { kinetic, fade } from '@vitus-labs/kinetic'

// Default transition mode
const FadeDiv = kinetic('div').preset(fade)

// Switch modes via chain
const Accordion = kinetic('div').collapse()
const StaggerList = kinetic('ul').preset(slideUp).stagger({ interval: 50 })
const AnimatedList = kinetic('ul').preset(fade).group()

All chain methods return a new component instance (immutable) — re-chain to extend or override.

Chain Methods

.preset(preset)

Spread a preset's style and class props onto the component. Equivalent to calling .enter(), .enterTo(), .enterTransition(), .leave(), .leaveTo(), .leaveTransition() (and/or the class equivalents) with the preset's values.

.preset(preset: StyleTransitionProps & ClassTransitionProps): KineticComponent<Tag, Mode>

Use built-in presets (fade, slideUp, scaleIn, …) or any object matching the shape.

.enter(styles) / .enterTo(styles) / .enterTransition(value)

Style-based enter definition.

.enter(styles: CSSProperties)             // Initial styles applied at enter start
.enterTo(styles: CSSProperties)           // End styles applied on next animation frame
.enterTransition(value: string)           // CSS transition shorthand for enter

.leave(styles) / .leaveTo(styles) / .leaveTransition(value)

Mirror of the enter triplet, for the leave phase.

.leave(styles: CSSProperties)
.leaveTo(styles: CSSProperties)
.leaveTransition(value: string)

.enterClass(opts) / .leaveClass(opts)

Class-based animation definition. Compatible with Tailwind, CSS modules, or any class-based system.

.enterClass({ active?: string, from?: string, to?: string })
.leaveClass({ active?: string, from?: string, to?: string })
FieldMaps to internal propLifecycle
activeenter / leaveApplied during the entire phase
fromenterFrom / leaveFromApplied at phase start, removed when the "to" state is applied
toenterTo / leaveToApplied after a double requestAnimationFrame; enterTo stays on after entering, until the leave phase starts

.config(opts)

Configure mode-agnostic and mode-specific defaults. The accepted shape narrows based on the current mode.

// Transition mode (default)
.config({ appear?: boolean; unmount?: boolean; timeout?: number })

// Collapse mode
.config({ appear?: boolean; timeout?: number; transition?: string })

// Stagger mode
.config({ appear?: boolean; timeout?: number; interval?: number; reverseLeave?: boolean })

// Group mode
.config({ appear?: boolean; timeout?: number })
FieldDefaultModesDescription
appearfalseallAnimate the initial mount as if show flipped to true
unmounttruetransitionWhen false, hide with display: none instead of removing from DOM
timeout5000allSafety timeout (ms) — completes the transition if transitionend never fires
transition'height 300ms ease'collapseCSS transition shorthand applied to the height animation
interval50staggerDelay (ms) between each child's animation start
reverseLeavefalsestaggerReverse stagger order on leave

.on(callbacks)

Set component-level default lifecycle callbacks. Per-render props override these.

.on({
  onEnter?: () => void
  onAfterEnter?: () => void
  onLeave?: () => void
  onAfterLeave?: () => void
})

.collapse(opts?) / .stagger(opts?) / .group()

Switch the component into a different mode. Returns a KineticComponent<Tag, NewMode> — the prop shape narrows accordingly.

.collapse(opts?: { appear?: boolean; timeout?: number; transition?: string }): KineticComponent<Tag, 'collapse'>
.stagger(opts?: { interval?: number; reverseLeave?: boolean }): KineticComponent<Tag, 'stagger'>
.group(): KineticComponent<Tag, 'group'>

To set appear / timeout for stagger mode, chain .config() after .stagger(). Mode switching is final for that link of the chain — the returned component reflects the new mode and accepts only that mode's props.

Component Props

The rendered component's prop shape depends on its mode. Below is the full per-mode prop list. All shapes also include the chosen tag's HTML attributes (e.g. className, onClick, id). In the types below, HTMLAttrsOf<Tag> stands for Omit<ComponentPropsWithRef<Tag>, KineticOwnKeys> — the tag's props (including ref) minus kinetic's own keys (show, appear, unmount, timeout, transition, interval, reverseLeave, the four callbacks, children).

Transition mode

type KineticTransitionProps<Tag> = HTMLAttrsOf<Tag> & {
  show: boolean
  appear?: boolean
  unmount?: boolean
  timeout?: number
  children?: ReactNode
} & Partial<TransitionCallbacks>
PropTypeDefaultDescription
showboolean—true runs enter, false runs leave + unmount
appearbooleanfalseAnimate on initial mount
unmountbooleantrueUnmount on leave; if false, keep with display: none
timeoutnumber5000Safety timeout (ms)
childrenReactNode—Content rendered inside the tag element

Collapse mode

type KineticCollapseProps<Tag> = HTMLAttrsOf<Tag> & {
  show: boolean
  appear?: boolean
  timeout?: number
  transition?: string
  children?: ReactNode
} & Partial<TransitionCallbacks>
PropTypeDefaultDescription
showboolean—true expands, false collapses
appearbooleanfalseAnimate the collapse on initial mount
timeoutnumber5000Safety timeout (ms)
transitionstring'height 300ms ease'CSS transition shorthand for the height animation
childrenReactNode—Content, rendered inside an inner display: flow-root div whose scrollHeight is measured. Unmounted while collapsed

Stagger mode

type KineticStaggerProps<Tag> = HTMLAttrsOf<Tag> & {
  show: boolean
  appear?: boolean
  timeout?: number
  interval?: number
  reverseLeave?: boolean
  children: ReactElement | ReactElement[]
} & Partial<TransitionCallbacks>
PropTypeDefaultDescription
showboolean—Controls visibility of all children
appearbooleanfalseAnimate on initial mount
timeoutnumber5000Safety timeout (ms)
intervalnumber50Delay (ms) between each child's animation start
reverseLeavebooleanfalseReverse stagger order on leave
childrenReactElement[] | ReactElement—Children to stagger (must have stable keys)

Group mode

type KineticGroupProps<Tag> = HTMLAttrsOf<Tag> & {
  appear?: boolean
  timeout?: number
  children: ReactElement | ReactElement[]
} & Partial<TransitionCallbacks>
PropTypeDefaultDescription
appearbooleanfalseAnimate the initial children on mount
timeoutnumber5000Safety timeout (ms)
childrenReactElement[] | ReactElement—Keyed children — adding triggers enter, removing triggers leave

There is no show prop in group mode — visibility is driven entirely by which children are present in the array.

Lifecycle Callbacks

type TransitionCallbacks = {
  onEnter?: () => void
  onAfterEnter?: () => void
  onLeave?: () => void
  onAfterLeave?: () => void
}
CallbackWhen it fires
onEnterEnter phase begins (start styles applied)
onAfterEnterEnter animation completes (transitionend / animationend, nothing left running, or the safety timeout)
onLeaveLeave phase begins
onAfterLeaveLeave animation completes — fires immediately before unmount

Callbacks set via .on() are component-level defaults; per-instance props on the rendered component override them.

In stagger mode onEnter / onLeave fire per child, onAfterEnter once for the last child, and onAfterLeave once all leaving children have finished. In group mode the callbacks fire per child.

In prefers-reduced-motion: reduce mode, the visible animation is skipped but callbacks still fire so analytics and focus-management side-effects remain consistent.

Animation Prop Shapes

Used internally by the chain methods and exported as types.

StyleTransitionProps

type StyleTransitionProps = {
  enterStyle?: CSSProperties
  enterToStyle?: CSSProperties
  enterTransition?: string
  leaveStyle?: CSSProperties
  leaveToStyle?: CSSProperties
  leaveTransition?: string
}

ClassTransitionProps

type ClassTransitionProps = {
  enter?: string
  enterFrom?: string
  enterTo?: string
  leave?: string
  leaveFrom?: string
  leaveTo?: string
}

TransitionStage

type TransitionStage = 'hidden' | 'entering' | 'entered' | 'leaving'

Built-in Presets

Six presets ship in the core package. Each is a Preset (StyleTransitionProps & ClassTransitionProps).

import {
  fade,
  scaleIn,
  slideUp,
  slideDown,
  slideLeft,
  slideRight,
} from '@vitus-labs/kinetic'
PresetEffect
fadeOpacity 0 → 1 on enter, reverse on leave
scaleInScale + opacity, slight zoom-in feel
slideUpTranslate from below + fade
slideDownTranslate from above + fade
slideLeftTranslate from right + fade
slideRightTranslate from left + fade

Each enter transition is opacity 300ms ease-out (plus transform 300ms ease-out for the movement/scale presets); leave is the same at 200ms ease-in. scaleIn starts at scale(0.95); the slides move 16px. The presets export is an object containing all six.

For 122 additional presets, factory generators, and composition utilities, see @vitus-labs/kinetic-presets.

Hooks

useTransitionState

The state machine that powers transition mode. Drive enter / leave manually for cases the chain factory doesn't cover.

import { useTransitionState } from '@vitus-labs/kinetic'

const { stage, ref, shouldMount, appearing, complete } = useTransitionState({
  show,
  appear: false,
})
type UseTransitionState = (options: {
  show: boolean
  appear?: boolean // default false
}) => TransitionStateResult

The state machine only tracks stages — pair it with useAnimationEnd (or your own detection) to call complete(). Initial stage is 'entered' when show && !appear, otherwise 'hidden'.

Returns:

FieldTypeDescription
stageTransitionStageCurrent lifecycle stage
refRefObject<HTMLElement | null>Attach to the element you're animating
shouldMountbooleanWhether to render the element at all (stage !== 'hidden')
appearingbooleantrue only on the first render (including SSR) of show && appear — render the enter start state so the markup exists and hydration matches
complete() => voidCall to advance to the next stage when the animation finishes

useAnimationEnd

Detect the end of a CSS transition/animation with a safety timeout fallback. Ignores bubbled events from children.

import { useAnimationEnd } from '@vitus-labs/kinetic'

useAnimationEnd({
  ref,
  onEnd: complete,
  active: stage === 'entering' || stage === 'leaving',
  timeout: 5000,
  phase: stage,
})
type UseAnimationEnd = (options: {
  ref: RefObject<HTMLElement | null>
  onEnd: () => void
  active: boolean
  timeout?: number // default 5000
  phase?: string
}) => void
OptionDescription
refElement to watch
onEndCalled once per active phase (latest callback is always used)
activeListen only while true
timeoutFallback (ms) that calls onEnd if no end event arrives
phaseCurrent phase (e.g. the stage). Changing it while active stays true (entering → leaving) restarts the listeners and the timer

Completes on animationend, on transitionend / transitioncancel once no transition is still running on the element (it waits for the longest one), when computed durations and delays are all explicitly zero, or when getAnimations() reports nothing running a few frames in. If ref.current is null, only the timeout can complete the phase (a dev warning is logged).

TransitionStateResult

Returned shape from useTransitionState:

type TransitionStateResult = {
  stage: TransitionStage
  ref: RefObject<HTMLElement | null>
  shouldMount: boolean
  appearing: boolean
  complete: () => void
}

Exports

import {
  // Factory
  kinetic,
  // Presets
  presets,
  fade,
  scaleIn,
  slideUp,
  slideDown,
  slideLeft,
  slideRight,
  // Hooks
  useTransitionState,
  useAnimationEnd,
} from '@vitus-labs/kinetic'

import type {
  KineticComponent,
  Preset,
  ClassTransitionProps,
  StyleTransitionProps,
  TransitionCallbacks,
  TransitionStage,
  TransitionStateResult,
  UseTransitionState,
  UseAnimationEnd,
} from '@vitus-labs/kinetic'
Edit on GitHub

Last updated on

On this page