<!-- Source: https://motion.svelte.page/examples/layout-root -->

# layoutRoot & layoutAnchor

> Animate shared layoutId elements relative to a layout root, and anchor relative layout projection to any point on the parent.

**Source:** [https://motion.svelte.page/examples/layout-root](https://motion.svelte.page/examples/layout-root)

**Markdown mirror:** [https://motion.svelte.page/examples/layout-root.md](https://motion.svelte.page/examples/layout-root.md)

---

This mirror preserves the prose, implementation notes, and runnable Svelte source behind the live example page.

## FIG-001: the tab bar jumps, the underline glides.

Selecting Lettuce opens a sidebar, so the centred tab bar jumps left in the same update as the shared `layoutId` underline moves. The list is a `layoutRoot`: the underline animates relative to it and slides exactly one tab. Turn the root off and it flies in from where Tomato used to be.

**Metadata:** tag: `LAYOUT` | api: `layoutRoot` | input: `layoutId` | mode: `live`

### Notes

- The sidebar and the selection change in one update, so the list moves 110px left at the same moment the underline changes tab.
- `layoutRoot` makes the list's own layout change instant and animates the underline relative to it, because the previous underline was inside the list too.
- `layoutRoot` is read when the list mounts; the toggle remounts it with `{#key}`.

### Source

#### SidebarTabs.svelte

Source file: [src/lib/examples/layout-root/demos/SidebarTabs.svelte](https://github.com/humanspeak/svelte-motion/blob/main/docs/src/lib/examples/layout-root/demos/SidebarTabs.svelte)

```svelte
<script lang="ts">
    import { PanelLeft, Target } from '@lucide/svelte'
    import { AnimatePresence, motion } from '@humanspeak/svelte-motion'

    // #1935 (Motion 14.7.1): a centred tab bar whose Lettuce tab opens a
    // sidebar, so the whole bar jumps left in the same update as the
    // underline moves. With `layoutRoot` on the list, the underline animates
    // relative to the list: it glides exactly one tab. Without it, it flies
    // in from where the old tab used to be on the page.
    const tabs = ['🍅 Tomato', '🥬 Lettuce', '🧀 Cheese']

    let selected = $state(tabs[0])
    let layoutRoot = $state(true)

    const transition = { duration: 0.6, ease: [0.22, 1, 0.36, 1] } as const
    const showSidebar = $derived(selected === tabs[1])
</script>

<!-- dk-strip: docs-kit positioning shell - stripped from the published code. -->
<div class="dk-demo-shell">
    <div class="toolbar" aria-label="layoutRoot controls">
        <button
            type="button"
            class:active={layoutRoot}
            aria-pressed={layoutRoot}
            onclick={() => {
                layoutRoot = !layoutRoot
                selected = tabs[0]
            }}
        >
            <Target size={15} />
            layoutRoot: {layoutRoot ? 'on' : 'off'}
        </button>
        <span class="hint"><PanelLeft size={15} /> Lettuce opens a sidebar</span>
    </div>

    <div class="window">
        <!-- layoutRoot is read when the list mounts, so remount it to toggle -->
        {#key layoutRoot}
            <motion.ul class="tabs" layout={layoutRoot} {layoutRoot}>
                {#each tabs as item (item)}
                    <li>
                        <button
                            type="button"
                            class="tab"
                            class:selected={item === selected}
                            onclick={() => (selected = item)}
                        >
                            {item}
                        </button>
                        <AnimatePresence>
                            {#if item === selected}
                                <motion.div
                                    key="underline"
                                    class="underline"
                                    layoutId="layout-root-underline"
                                    {transition}
                                />
                            {/if}
                        </AnimatePresence>
                    </li>
                {/each}
            </motion.ul>
        {/key}
        {#if showSidebar}
            <aside>Sidebar</aside>
        {/if}
    </div>
</div>

<style>
    .dk-demo-shell {
        width: 100%;
        height: clamp(320px, calc(100vh - 160px), 380px);
        display: grid;
        grid-template-rows: auto minmax(0, 1fr);
        gap: 16px;
        padding: 20px 26px;
        background: var(--brut-bg, #f8fcfb);
        color: var(--brut-ink, #0a0a0a);
    }

    .toolbar {
        display: flex;
        flex-wrap: wrap;
        align-items: center;
        justify-content: center;
        gap: 12px;
    }

    .toolbar button {
        height: 36px;
        display: inline-flex;
        align-items: center;
        gap: 7px;
        padding: 0 12px;
        border: 1px solid var(--brut-rule-2, #bbc4c0);
        background: var(--brut-bg-2, #eef4f1);
        color: var(--brut-ink, #0a0a0a);
        font-family: var(--brut-mono, monospace);
        font-size: 12px;
        font-weight: 700;
        letter-spacing: 0.04em;
        text-transform: uppercase;
        cursor: pointer;
    }

    .toolbar button.active {
        border-color: var(--brut-accent, #247768);
        background: var(--brut-accent-soft, rgba(36, 119, 104, 0.1));
        color: var(--brut-accent, #247768);
    }

    .hint {
        display: inline-flex;
        align-items: center;
        gap: 6px;
        color: var(--brut-ink-2, #4a5552);
        font-family: var(--brut-mono, monospace);
        font-size: 12px;
    }

    .window {
        display: flex;
        align-items: flex-start;
        justify-content: center;
        gap: 0;
        padding-top: 40px;
        border: 1px solid var(--brut-rule-2, #bbc4c0);
        background: var(--brut-bg-2, #eef4f1);
    }

    .window :global(.tabs) {
        display: flex;
        margin: 0;
        padding: 0;
        list-style: none;
    }

    li {
        position: relative;
        width: 120px;
    }

    .tab {
        width: 100%;
        height: 48px;
        border: 0;
        background: transparent;
        color: var(--brut-ink-2, #4a5552);
        font-size: 15px;
        cursor: pointer;
    }

    .tab.selected {
        color: var(--brut-ink, #0a0a0a);
        font-weight: 700;
    }

    .window :global(.underline) {
        position: absolute;
        right: 0;
        bottom: 0;
        left: 0;
        height: 3px;
        background: var(--brut-accent, #247768);
    }

    aside {
        width: 220px;
        height: 160px;
        display: grid;
        place-items: center;
        border-left: 1px solid var(--brut-rule-2, #bbc4c0);
        color: var(--brut-ink-2, #4a5552);
        font-family: var(--brut-mono, monospace);
        font-size: 12px;
        text-transform: uppercase;
    }
</style>
```

## FIG-002: a resizing parent, a child that stays centred.

The parent grows and shrinks over one second; the child's own layout animation starts half a second later. Until then it only follows its parent. Anchored at `{ x: 0.5, y: 0.5 }` it never leaves the centre; with the default top-left anchor it drifts.

**Metadata:** tag: `LAYOUT` | api: `layoutAnchor` | input: `layout` | mode: `live`

### Notes

- `layoutAnchor={{ x: 0.5, y: 0.5 }}` measures the child's position from the parent's centre, so a flexbox-centred child stays centred while the parent resizes.
- The default anchor is the parent's top-left corner: the child keeps its offset from that corner and drifts off centre.
- `layoutAnchor={'{false}'}` turns relative projection off: the child animates in page space on its own.

### Source

#### CenteredAnchor.svelte

Source file: [src/lib/examples/layout-root/demos/CenteredAnchor.svelte](https://github.com/humanspeak/svelte-motion/blob/main/docs/src/lib/examples/layout-root/demos/CenteredAnchor.svelte)

```svelte
<script lang="ts">
    import { Maximize2 } from '@lucide/svelte'
    import { motion } from '@humanspeak/svelte-motion'

    // `layoutAnchor` picks the point on the parent a child's relative layout
    // projection is anchored to. The child's own layout animation is delayed,
    // so for the first half of each resize it only follows its parent: with
    // the centre anchor it stays centred, with the default top-left it drifts.
    type Anchor = { label: string; value: { x: number; y: number } | false | undefined }
    const anchors: Anchor[] = [
        { label: 'default (top-left)', value: undefined },
        { label: '{ x: 0.5, y: 0.5 }', value: { x: 0.5, y: 0.5 } },
        { label: 'false', value: false }
    ]

    let expanded = $state(false)
    let anchorIndex = $state(1)
    const anchor = $derived(anchors[anchorIndex].value)

    const parentTransition = { duration: 1, ease: 'linear' } as const
    const childTransition = { duration: 1, ease: 'linear', delay: 0.5 } as const
</script>

<!-- dk-strip: docs-kit positioning shell - stripped from the published code. -->
<div class="dk-demo-shell">
    <div class="toolbar" aria-label="layoutAnchor controls">
        {#each anchors as option, i (option.label)}
            <button
                type="button"
                class:active={anchorIndex === i}
                aria-pressed={anchorIndex === i}
                onclick={() => (anchorIndex = i)}
            >
                {option.label}
            </button>
        {/each}
        <button type="button" class="primary" onclick={() => (expanded = !expanded)}>
            <Maximize2 size={15} />
            {expanded ? 'Shrink' : 'Grow'}
        </button>
    </div>

    <div class="stage">
        <motion.div
            class="parent"
            layout
            transition={parentTransition}
            style="width: {expanded ? 320 : 160}px; height: {expanded ? 240 : 120}px"
        >
            <motion.div class="child" layout layoutAnchor={anchor} transition={childTransition} />
        </motion.div>
    </div>
</div>

<style>
    .dk-demo-shell {
        width: 100%;
        height: clamp(380px, calc(100vh - 160px), 440px);
        display: grid;
        grid-template-rows: auto minmax(0, 1fr);
        gap: 16px;
        padding: 20px 26px;
        background: var(--brut-bg, #f8fcfb);
        color: var(--brut-ink, #0a0a0a);
    }

    .toolbar {
        display: flex;
        flex-wrap: wrap;
        justify-content: center;
        gap: 8px;
    }

    button {
        height: 36px;
        display: inline-flex;
        align-items: center;
        gap: 7px;
        padding: 0 12px;
        border: 1px solid var(--brut-rule-2, #bbc4c0);
        background: var(--brut-bg-2, #eef4f1);
        color: var(--brut-ink, #0a0a0a);
        font-family: var(--brut-mono, monospace);
        font-size: 12px;
        font-weight: 700;
        cursor: pointer;
    }

    button.active {
        border-color: var(--brut-ink, #0a0a0a);
        background: var(--brut-ink, #0a0a0a);
        color: var(--brut-accent-ink, #f8fcfb);
    }

    button.primary {
        border-color: var(--brut-accent, #247768);
        background: var(--brut-accent-soft, rgba(36, 119, 104, 0.1));
        color: var(--brut-accent, #247768);
    }

    .stage {
        display: grid;
        place-items: center;
        border: 1px solid var(--brut-rule-2, #bbc4c0);
        background: var(--brut-bg-2, #eef4f1);
    }

    .stage :global(.parent) {
        display: flex;
        align-items: center;
        justify-content: center;
        border: 1px dashed var(--brut-ink-2, #4a5552);
    }

    .stage :global(.child) {
        width: 48px;
        height: 48px;
        background: var(--brut-accent, #247768);
    }
</style>
```
