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'>| Argument | Type | Description |
|---|---|---|
tag | ElementType | HTML 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 })| Field | Maps to internal prop | Lifecycle |
|---|---|---|
active | enter / leave | Applied during the entire phase |
from | enterFrom / leaveFrom | Applied at phase start, removed when the "to" state is applied |
to | enterTo / leaveTo | Applied 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 })| Field | Default | Modes | Description |
|---|---|---|---|
appear | false | all | Animate the initial mount as if show flipped to true |
unmount | true | transition | When false, hide with display: none instead of removing from DOM |
timeout | 5000 | all | Safety timeout (ms) — completes the transition if transitionend never fires |
transition | 'height 300ms ease' | collapse | CSS transition shorthand applied to the height animation |
interval | 50 | stagger | Delay (ms) between each child's animation start |
reverseLeave | false | stagger | Reverse 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>| Prop | Type | Default | Description |
|---|---|---|---|
show | boolean | — | true runs enter, false runs leave + unmount |
appear | boolean | false | Animate on initial mount |
unmount | boolean | true | Unmount on leave; if false, keep with display: none |
timeout | number | 5000 | Safety timeout (ms) |
children | ReactNode | — | 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>| Prop | Type | Default | Description |
|---|---|---|---|
show | boolean | — | true expands, false collapses |
appear | boolean | false | Animate the collapse on initial mount |
timeout | number | 5000 | Safety timeout (ms) |
transition | string | 'height 300ms ease' | CSS transition shorthand for the height animation |
children | ReactNode | — | 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>| Prop | Type | Default | Description |
|---|---|---|---|
show | boolean | — | Controls visibility of all children |
appear | boolean | false | Animate on initial mount |
timeout | number | 5000 | Safety timeout (ms) |
interval | number | 50 | Delay (ms) between each child's animation start |
reverseLeave | boolean | false | Reverse stagger order on leave |
children | ReactElement[] | ReactElement | — | Children to stagger (must have stable keys) |
Group mode
type KineticGroupProps<Tag> = HTMLAttrsOf<Tag> & {
appear?: boolean
timeout?: number
children: ReactElement | ReactElement[]
} & Partial<TransitionCallbacks>| Prop | Type | Default | Description |
|---|---|---|---|
appear | boolean | false | Animate the initial children on mount |
timeout | number | 5000 | Safety timeout (ms) |
children | ReactElement[] | 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
}| Callback | When it fires |
|---|---|
onEnter | Enter phase begins (start styles applied) |
onAfterEnter | Enter animation completes (transitionend / animationend, nothing left running, or the safety timeout) |
onLeave | Leave phase begins |
onAfterLeave | Leave 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'| Preset | Effect |
|---|---|
fade | Opacity 0 → 1 on enter, reverse on leave |
scaleIn | Scale + opacity, slight zoom-in feel |
slideUp | Translate from below + fade |
slideDown | Translate from above + fade |
slideLeft | Translate from right + fade |
slideRight | Translate 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
}) => TransitionStateResultThe 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:
| Field | Type | Description |
|---|---|---|
stage | TransitionStage | Current lifecycle stage |
ref | RefObject<HTMLElement | null> | Attach to the element you're animating |
shouldMount | boolean | Whether to render the element at all (stage !== 'hidden') |
appearing | boolean | true only on the first render (including SSR) of show && appear — render the enter start state so the markup exists and hydration matches |
complete | () => void | Call 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| Option | Description |
|---|---|
ref | Element to watch |
onEnd | Called once per active phase (latest callback is always used) |
active | Listen only while true |
timeout | Fallback (ms) that calls onEnd if no end event arrives |
phase | Current 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'Last updated on