@humanspeak/svelte-motion
Version:
Framer Motion for Svelte 5. Declarative motion.<tag> components with AnimatePresence exit animations, gestures (hover, tap, drag, focus, in-view), variants, FLIP layout animations, shared-layout transitions, spring physics, and scroll-linked motion values
42 lines (41 loc) • 1.42 kB
JavaScript
import { getContext, setContext } from 'svelte';
const LAYOUT_GROUP_CONTEXT_KEY = Symbol('layout-group');
/**
* Publish a LayoutGroup id for descendants. Called by `<LayoutGroup>`
* after computing its own (possibly inherited and chained) id.
*/
export const setLayoutGroupContext = (id) => {
setContext(LAYOUT_GROUP_CONTEXT_KEY, id);
};
/**
* Read the nearest LayoutGroup id, or `undefined` if not inside one.
*
* `_MotionContainer.svelte` reads this to prefix `layoutId` when
* snapshotting and consuming against the registry, so shared-layout
* animations stay scoped to the surrounding group.
*/
export const getLayoutGroupContext = () => {
return getContext(LAYOUT_GROUP_CONTEXT_KEY);
};
/**
* Combine a parent group's id with a descendant LayoutGroup's own id
* to produce the effective scope id. Mirrors framer-motion's chaining
* (`"parent-id"` + `"-"` + `"own-id"`).
*
* Either side can be `undefined`; the result is the other one, or
* `undefined` if both are absent.
*/
export const chainLayoutGroupId = (parent, own) => {
if (!parent)
return own;
if (!own)
return parent;
return `${parent}-${own}`;
};
/**
* Apply a LayoutGroup scope to a raw `layoutId` for registry lookups.
* Returns the un-prefixed id when no group is in scope.
*/
export const scopeLayoutId = (groupId, layoutId) => {
return groupId ? `${groupId}::${layoutId}` : layoutId;
};