Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/okikio/solid-motion-experiments.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384import type { JSX, ValidComponent } from "solid-js";
import { createMotionComponent } from "./create-motion-component.tsx";
import type { MotionComponent, MotionCreateOptions, MotionNamespace,} from "./motion-component-types.ts";
// Custom components default to filtering Motion props at the wrapper boundary.const filteredComponentCache = new Map<ValidComponent, unknown>();// Forwarded wrappers need their own cache because their prop contract is different.const forwardedComponentCache = new Map<ValidComponent, unknown>();
/** * Keep separate caches for the two public forwarding modes. * * `forwardMotionProps` changes the wrapper's prop-filtering contract, so a cached * component built for the filtered mode cannot be safely reused for the forwarded mode. */function getComponentCache(options?: MotionCreateOptions): Map<ValidComponent, unknown> { return options?.forwardMotionProps ? forwardedComponentCache : filteredComponentCache;}
/** * Return a stable Motion wrapper for one intrinsic tag or custom component. * * The wrapper factory itself is cheap, but repeated property reads like `motion.div` are * part of the public API surface. This cache keeps those reads stable and makes the Proxy * behave like a namespace of reusable components rather than a namespace of fresh factory * calls. */function getCachedMotionComponent<T extends ValidComponent>( component: T, options?: MotionCreateOptions,): MotionComponent<T> { const cache = getComponentCache(options); const cached = cache.get(component);
if (cached) return cached as MotionComponent<T>;
const nextComponent = createMotionComponent(component, options); cache.set(component, nextComponent); return nextComponent;}
/** * Exposes Motion's Solid component namespace through a cached Proxy. * * The cache matters because `motion.div` and `motion.create(Card)` should return stable * wrapper component identities. Recreating those factories on every property read would * make animation state harder to preserve and would drift away from Motion's established * API behavior. * * @example Primary member component API * ```tsx * <motion.div class="box" /> * ``` * * @example Dynamic intrinsic creation * ```tsx * const MotionDialog = motion.create("dialog"); * ``` */export const motion = new Proxy({} as MotionNamespace, { get(_, property: string) { if (property === "create") { // `motion.create(...)` is the explicit factory form for custom components or // dynamic intrinsic tags. return function create<T extends ValidComponent>( component: T, options?: MotionCreateOptions, ): MotionComponent<T> { return getCachedMotionComponent(component, options); }; }
// Member access like `motion.div` is just shorthand for asking the cache for the // wrapper around a specific intrinsic element. return getCachedMotionComponent(property as keyof JSX.IntrinsicElements); },});