MotionHighlight
Tabs, segmented controls or navigation from data, with one highlight that travels to the selection.
WebReact NativeSelection and paging
Try it
Preparing the example…
Shortest usage
<MotionHighlight
items={['Overview', 'Activity', 'Settings']}
value={tab}
onValueChange={setTab}
hover
/>;What it handles for you
- One highlight moves and resizes between items; style it with
highlightClassName. - Items come from
items, keys fromgetKeyand labels fromrenderItem. hovermakes it follow the pointer and return to the selection.- Items can wrap onto several rows and the highlight follows them.
- Font loading and layout changes are measured again automatically.
Use it for
- Page tabs.
- A filter bar.
- A segmented control in a settings form.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
items | readonly T[] | —required | Your data array. |
value | key | null | —required | Key of the highlighted item; null hides the highlight. |
onValueChange | (key, item) => void | Runs with the key and the item when an item is pressed. | |
getKey | (item) => string | number | item.id | Returns a unique, stable key for an item. Defaults to item.id, or the item itself for strings and numbers. |
renderItem | (item, { selected, index }) => ReactNode | item.label | Draws an item; receives whether it is selected. |
hover | boolean | false | Follow the pointer over items and return to value on leave (web). |
itemAs | element | 'button' | The element for each item. |
itemClassName | string | Class for each item element; itemStyle sets its inline style. | |
highlightClassName | string | Class for the highlight; highlightStyle sets its inline style. | |
transition | Transition | spring | A timing curve { duration, easing } or a spring { tempo: 'spring', spring }. Springs keep their momentum when interrupted. |
Other props go to the rendered element, so className, style and event handlers work as usual.
Web and React Native
Each item renders as a button by default (itemAs changes it); pressing it calls onValueChange(key, item).
On React Native items are Pressables; position runs on the native driver and size on a nested view driven by JavaScript.