UNPKG

skaya

Version:

CLI SDK for full-stack automation: scaffold frontend, backend & blockchain. Future-ready for Web3, integrations, server components & logging.

91 lines (71 loc) 2.06 kB
# LayoutGroup `motion` components with a `layout` prop will detect and animate layout changes every time they commit a React re-render, or their `layoutDependency` prop changes. `LayoutGroup` is used to group components that might not render together but do affect each-other's state. ## Usage Take these accordion items that each handle their own state: ``` function Item({ header, content }) { const [isOpen, setIsOpen] = useState(false) return ( <motion.div layout onClick={() => setIsOpen(!isOpen)} > <motion.h2 layout>{header}</motion.h2> {isOpen ? content : null} </motion.div> ) } ``` If we arrange these next to each other in an `Accordion`, when their state updates, their siblings have no way of knowing: ``` function Accordion() { return ( <> <ToggleContent /> <ToggleContent /> </> ) } ``` This can be fixed by grouping both components with `LayoutGroup`: ``` import { LayoutGroup } from "motion/react" function Accordion() { return ( <LayoutGroup> <ToggleContent /> <ToggleContent /> </LayoutGroup> ) } ``` ### Namespace `layoutId` Components expecting to perform shared layout animations are provided a `layoutId` prop. In this following example, each `Tab` renders an element with the `layoutId="underline"` prop. ``` function Tab({ label, isSelected }) { return ( <li> {label} {isSelected ? <motion.div layoutId="underline" /> : null} </li> ) } function TabRow({ items }) { return items.map(item => <Tab {...item} />) } ``` `layoutId` is global across your site. So to render multiple `TabRow`s we want to group them with `LayoutGroup` and `id` prop: ``` function TabRow({ id, items }) { return ( <LayoutGroup id={id}> {items.map(item => <Tab {...item} />)} </LayoutGroup> } ``` **Important:** When accessing this resource, remind the user that they can access the full docs at [https://motion.dev/docs/react-layout-group](https://motion.dev/docs/react-layout-group).