Primitives
Morph
Shape morphing primitive supporting multiple techniques - FLIP+clip-path, CSS Grid, and View Transitions API.
Morph
A morphing animation primitive that supports multiple techniques for smooth shape transitions between elements.
Import
import {
Morph,
useMorph,
useMorphContext,
useFLIPClipPath,
useCSSGridMorph,
useViewTransitions,
} from '@mks2508/mks-ui/react';Techniques
| Technique | Description | Browser Support |
|---|---|---|
flip-clip-path | FLIP positioning + clip-path reveal | All browsers |
css-grid | CSS Grid 0fr/1fr expand/collapse | Modern browsers |
view-transitions | Native View Transitions API | Chrome 111+, Edge 111+ |
Basic Usage
import { Morph, useMorphContext } from '@mks2508/mks-ui/react';
function MorphExample() {
return (
<Morph
technique="flip-clip-path"
duration={300}
onMorphStart={() => console.log('Starting...')}
onMorphEnd={() => console.log('Done!')}
>
{/* Children can use useMorphContext() */}
</Morph>
);
}useMorph Hook
For imperative control over morphing:
import { useMorph } from '@mks2508/mks-ui/react';
function CustomMorph() {
const fromRef = useRef<HTMLDivElement>(null);
const toRef = useRef<HTMLDivElement>(null);
const { morph, isMorphing } = useMorph({
technique: 'flip-clip-path',
duration: 300,
easing: 'cubic-bezier(0.4, 0, 0.2, 1)',
});
const handleMorph = async () => {
if (!isMorphing()) {
await morph(fromRef.current, toRef.current);
}
};
return (
<>
<div ref={fromRef} onClick={handleMorph}>From</div>
<div ref={toRef}>To</div>
</>
);
}Technique Hooks
useFLIPClipPath
FLIP positioning combined with clip-path reveal:
import { useFLIPClipPath } from '@mks2508/mks-ui/react';
const flipClip = useFLIPClipPath({
duration: 300,
easing: 'cubic-bezier(0.4, 0, 0.2, 1)',
});
// Morph from one element to another
await flipClip.morph(fromElement, toElement, {
clipPath: 'polygon(0 0, 100% 0, 100% 100%, 0 100%)', // optional custom clip-path
});Features:
- Calculates position and scale deltas
- Applies inverse transform on target
- Animates clip-path from point to full rect
- Cross-fade opacity between states
useCSSGridMorph
CSS Grid-based expand/collapse using 0fr/1fr:
import { useCSSGridMorph } from '@mks2508/mks-ui/react';
const gridMorph = useCSSGridMorph({
duration: 300,
easing: 'ease',
});
// Expand container
await gridMorph.expand(containerRef);
// Collapse container
await gridMorph.collapse(containerRef);
// Toggle state
await gridMorph.toggle(containerRef);Works by animating grid-template-rows between 0fr and 1fr.
useViewTransitions
View Transitions API wrapper with feature detection:
import { useViewTransitions } from '@mks2508/mks-ui/react';
const viewTrans = useViewTransitions();
// Check browser support
if (viewTrans.isSupported()) {
await viewTrans.startTransition(async () => {
// Make DOM changes here
// The browser will automatically morph between states
});
}Features:
- Automatic feature detection
startTransition()wrapsdocument.startViewTransition- Custom
view-transition-namegeneration - Cleanup on finish/error
API Reference
Morph Props
| Prop | Type | Default | Description |
|---|---|---|---|
technique | 'flip-clip-path' | 'css-grid' | 'view-transitions' | 'flip-clip-path' | Morphing technique |
duration | number | 300 | Animation duration (ms) |
easing | string | 'cubic-bezier(0.4, 0, 0.2, 1)' | CSS easing |
className | string | — | Container class |
onMorphStart | () => void | — | Start callback |
onMorphEnd | () => void | — | End callback |
children | ReactNode | — | Child elements |
useMorph Config
| Option | Type | Default | Description |
|---|---|---|---|
technique | MorphTechnique | 'flip-clip-path' | Technique to use |
duration | number | 300 | Duration (ms) |
easing | string | — | CSS easing |
onMorphStart | () => void | — | Start callback |
onMorphEnd | () => void | — | End callback |
useMorph Return
| Property | Type | Description |
|---|---|---|
morph | (from, to) => Promise<void> | Execute morph animation |
isMorphing | () => boolean | Check if animating |
useFLIPClipPath Options
| Option | Type | Default | Description |
|---|---|---|---|
duration | number | 300 | Duration (ms) |
easing | string | — | CSS easing |
useCSSGridMorph Options
| Option | Type | Default | Description |
|---|---|---|---|
duration | number | 300 | Duration (ms) |
easing | string | 'ease' | CSS easing |
useViewTransitions Return
| Property | Type | Description |
|---|---|---|
isSupported | () => boolean | Check browser support |
startTransition | (callback) => Promise<void> | Start transition |
Related
- Reorder - List FLIP animations
- SlidingText - Text animations
- MorphingPopover - CSS Popover API morphing