import type { MotionStyle, MotionNodeOptions, OnPressEndEvent, OnPressStartEvent, PressGestureInfo, TargetAndTransition, TargetResolver, VariantLabels, ViewportOptions, } from "motion-dom"; import { solid1Adapter } from "../adapter/solid1.ts"; import type { SolidMotionAdapter } from "../adapter/types.ts"; import type { MaybeAccessor } from "./resolve-motion-prop.ts"; /** * Upstream Motion prop names that belong to `MotionNodeOptions` rather than to the host element. * * Solid prop objects stay reactive through property reads. If we separate * Motion props from DOM props with normal destructuring, we risk freezing access to values like * `animate`, `transition`, or `layoutDependency` too early. `splitProps` preserves those reads so * later effects can subscribe lazily. * * Keeping the upstream-owned slice separate makes the Solid-specific additions below easier to * audit. When the adapter gains more Motion features later, this list is the first place to check * whether a prop should be treated as part of the motion-dom contract or as Solid-local glue. * * @example Split animation props from DOM props * ```ts * const [motionProps, domProps] = splitProps(rawProps, MOTION_PROP_KEYS); * ``` * * @example Keep `class` reactive while reading `animate` lazily * ```ts * const [motionProps, domProps] = splitMotionProps(rawProps); * createEffect(() => updateTarget(resolveMaybeAccessor(motionProps.animate))); * ``` */ export const MOTION_NODE_PROP_KEYS = [ "initial", "animate", "exit", "transition", "variants", "custom", "inherit", "transformTemplate", "layout", "layoutId", "layoutDependency", "layoutScroll", "layoutRoot", "layoutAnchor", "whileHover", "whileTap", "whileFocus", "whileDrag", "whileInView", "drag", "dragConstraints", "dragElastic", "dragMomentum", "dragSnapToOrigin", "dragDirectionLock", "dragPropagation", "dragTransition", "dragControls", "dragListener", "onUpdate", "onAnimationStart", "onAnimationComplete", "onLayoutAnimationStart", "onLayoutAnimationComplete", "onHoverStart", "onHoverEnd", "onPan", "onPanStart", "onPanEnd", "onDrag", "onDragStart", "onDragEnd", "onDirectionLock", "onViewportEnter", "onViewportLeave", "propagate", ] as const satisfies readonly (keyof MotionNodeOptions)[]; /** * Solid-local prop names that the current adapter owns outside upstream `MotionNodeOptions`. * * These props still need to be stripped from the rendered host because they affect Motion's * wrapper/runtime boundary, not the underlying DOM element or custom component contract. */ export const SOLID_MOTION_PROP_KEYS = [ "style", "presenceKey", "whilePress", "inViewOptions", "onPressStart", "onPress", "onPressCancel", ] as const; /** * Complete list of prop names that the Solid wrapper currently treats as Motion-owned. * * Exporting this list keeps the split boundary inspectable for callers and tests. It also * ensures the same prop vocabulary drives both `splitMotionProps()` and any future checks that * need to answer "does this prop belong to Motion or to the rendered element?". * * @example Reuse the shared split vocabulary * ```ts * const [motionProps, domProps] = splitProps(rawProps, MOTION_PROP_KEYS); * ``` * * @example Check whether one prop name is Motion-owned * ```ts * const isMotionProp = MOTION_PROP_KEYS.includes("animate"); * ``` */ export const MOTION_PROP_KEYS = [ ...MOTION_NODE_PROP_KEYS, ...SOLID_MOTION_PROP_KEYS, ] as const; /** * Target definition forms that the current Solid adapter forwards into MotionState. */ export type MotionAnimationDefinition = VariantLabels | TargetAndTransition | TargetResolver; /** * Subset of upstream `MotionNodeOptions` that the current Solid adapter forwards as plain props. * * The adapter intentionally names this slice because the Solid runtime does not yet implement the * full upstream feature surface. Keeping the supported subset explicit makes that boundary honest. */ export type SupportedMotionNodeOptions = Pick< MotionNodeOptions, | "inherit" | "transformTemplate" | "layout" | "layoutId" | "layoutDependency" | "layoutScroll" | "layoutRoot" | "layoutAnchor" | "drag" | "dragConstraints" | "dragElastic" | "dragMomentum" | "dragSnapToOrigin" | "dragDirectionLock" | "dragPropagation" | "dragTransition" | "dragControls" | "dragListener" | "onUpdate" | "onAnimationStart" | "onAnimationComplete" | "onLayoutAnimationStart" | "onLayoutAnimationComplete" | "onHoverStart" | "onHoverEnd" | "onPan" | "onPanStart" | "onPanEnd" | "onDrag" | "onDragStart" | "onDragEnd" | "onDirectionLock" | "onViewportEnter" | "onViewportLeave" | "propagate" >; /** * Motion props that stay accessor-friendly because callers often express them through signals. */ export type MotionAccessorProps = { initial?: MaybeAccessor; animate?: MaybeAccessor; exit?: MaybeAccessor; transition?: MaybeAccessor>; variants?: MaybeAccessor>; custom?: MaybeAccessor; whileHover?: MaybeAccessor>; whileTap?: MaybeAccessor>; whileFocus?: MaybeAccessor>; whileDrag?: MaybeAccessor>; whileInView?: MaybeAccessor>; }; /** * Solid-local additions that do not exist on upstream `MotionNodeOptions` in this exact shape. */ export type SolidMotionProps = { /** * Base Motion style layer that sits under animated targets. */ style?: MaybeAccessor; /** * Stable identity hint reserved for retained presence workflows. */ presenceKey?: string | number; /** * Solid-local press target until the adapter grows Motion's full press feature layer. */ whilePress?: MaybeAccessor; /** * Temporary Solid-local alias for Motion's viewport options. */ inViewOptions?: MaybeAccessor; /** * Press start callback shape reused from Motion's exported press event types. */ onPressStart?: (event: OnPressStartEvent, info: PressGestureInfo) => void; /** * Press success callback shape reused from Motion's exported press event types. */ onPress?: (event: OnPressEndEvent, info: PressGestureInfo) => void; /** * Press cancel callback shape reused from Motion's exported press gesture payload. */ onPressCancel?: (event: PointerEvent, info: PressGestureInfo) => void; }; /** * Remaining supported upstream props that can pass through without Solid accessor wrapping. */ export type StaticMotionProps = Omit; /** * Combined Motion prop surface currently supported by the Solid adapter. * * This is deliberately narrower than `MotionNodeOptions`. The adapter already reuses * motion-dom's public target, variant, transition, and gesture types where the current * Solid runtime actually matches them, but it does not yet claim the full upstream * feature surface. * * Read the combined shape as three layers: * 1. plain supported upstream props that can pass through unchanged * 2. accessor-friendly animated props that Solid callers often express as signals * 3. Solid-local extensions such as `presenceKey`, `whilePress`, and `inViewOptions` * * @example Motion target props with Solid accessors * ```ts * const props: MotionProps = { * animate: () => ({ opacity: 1, x: 24 }), * whileHover: { scale: 1.05 }, * }; * ``` * * @example Solid-local presence and press props * ```ts * const props: MotionProps = { * presenceKey: "card-a", * whilePress: { scale: 0.96 }, * }; * ``` */ export type MotionProps = StaticMotionProps & MotionAccessorProps & SolidMotionProps; /** * Splits a Solid props object into Motion-owned props and DOM-owned props without * breaking reactive property access. * * This is a Solid-specific correctness helper. Motion needs to observe animation props * in tracked effects later, while still forwarding the remaining props directly to the * underlying DOM element. `splitProps` is the mechanism Solid provides for exactly that * shape. * * @example Split a div component's props * ```ts * const [motionProps, domProps] = splitMotionProps(rawProps); * ``` * * @example Read animation intent lazily * ```ts * createEffect(() => { * const next = motionProps.animate; * void next; * }); * ``` */ export function splitMotionProps>( rawProps: T, adapter: Pick = solid1Adapter, ) { // The returned objects are still reactive views over the original props bag. That lets the // wrapper forward DOM props immediately while Motion keeps reading animation props lazily later. return adapter.splitProps(rawProps, MOTION_PROP_KEYS); }