solid-motion-experiments docs research solid-presence-implementation-plan.md
11 kB · 214 lines
Markdown
at main

Solid Presence Implementation Plan #

Problem #

The original gap was that presence only changed Motion state. When isPresent flipped to false, MotionState could activate the exit lane, but nothing retained the exiting subtree and nothing reported when that exit work was finished. That meant the runtime understood logical absence, but Solid still owned physical removal.

Milestone 0 no longer uses that older framing as an open design question. The repo now has a retained-child prototype built around PresenceChild and a single-slot AnimatePresence. This document now describes what that prototype proves today, what it intentionally does not prove yet, and what later milestones still need.

This is the core presence problem in Solid:

  • logical presence answers whether a child should still participate in the app state
  • physical retention answers whether its DOM and reactive owner stay alive long enough to animate out

If we do not separate those two concerns, exit animations either never run or run on already-disposed state.

Goals #

  • Keep the current MotionState contract as the place where exit intent enters the animation system.
  • Keep a retained-rendering layer that keeps exiting children alive until Motion reports completion.
  • Validate a single-slot retained-child AnimatePresence before attempting keyed list semantics.
  • Keep the first version local to AnimatePresence rather than introducing global zombie roots or reparenting.
  • Preserve Solid ownership, context, and cleanup rules for retained children.

Non-goals #

  • Do not build layout projection or shared layout transitions as part of the first presence pass.
  • Do not introduce Bloom-style global zombie parking yet.
  • Do not depend on Motion's VisualElement tree before the Solid-owned presence lifecycle is stable.
  • Do not treat keyed list diffing as part of Milestone 0.

Constraints #

Solid makes the problem sharper than React because conditional control flow disposes the subtree immediately when a branch disappears. Presence therefore cannot be implemented as a late DOM-only patch.

We also need to preserve three invariants:

  • exiting children must keep their Solid owner and context until exit completion
  • nested motion components must be able to report exit completion upward without races
  • initial render and hydration must not accidentally run exit flows

@solid-primitives/transition-group is still useful, but its primitives retain DOM elements, not full JSX ownership. That makes it a good reference for sequencing and list semantics, but not a complete ownership model for AnimatePresence.

Current Milestone 0 shape #

Presence currently resolves into four layers.

AnimatePresence
  ├─ owns one direct child slot
  ├─ retains the disappearing child until exit completion
  └─ provides presence context for that retained record
          │
          ▼
    PresenceChild
      ├─ tracks descendant registrations
      ├─ waits for all exit completions
      └─ notifies parent when this record is safe to remove
          │
          ▼
      motion.* component
        └─ forwards logical presence into MotionState
                │
                ▼
            MotionState
              ├─ activates exit lane
              └─ reports when active exit work is complete

This diagram shows the separation the current prototype keeps: AnimatePresence owns retention, PresenceChild owns aggregation, and MotionState owns animation state.

Layer 1: Keep PresenceContext as the logical contract #

The current context shape is the right seam:

  • isPresent
  • initial
  • custom
  • register
  • retainOnOwnerDispose
  • onExitComplete

That is enough to express parent-to-child presence intent and child-to-parent completion reporting. We should keep this contract small and Solid-owned.

Layer 2: Teach MotionState to report exit completion #

Right now MotionState can start exit work, but it cannot tell anyone when the work is finished. That is the next missing contract.

The minimal addition is:

  • track the currently active exit animations in _motion-value-renderer.ts
  • notify MotionState when the active exit target settles or when no exit animation is needed
  • expose a small subscription or callback registration API so the presence layer can wait for completion

This should stay narrow. MotionState does not need to know about retained children or parent lists. It only needs to say, in effect, my exit work is done.

Layer 3: Add PresenceChild as the aggregation boundary #

PresenceChild wraps one retained child record. Its job is to turn many descendant exit completions into one parent completion.

It should:

  • create one stable id per participating descendant
  • let descendants register(id) when they become exit-relevant
  • remove registrations when descendants unmount normally
  • mark ids complete when descendants call onExitComplete(id)
  • call the parent record's completion callback only when every registered descendant has completed

This keeps nested exits correct. A parent record should not disappear just because one motion node finished if another nested node is still animating out.

Layer 4: Keep retained rendering in AnimatePresence #

Milestone 0 keeps AnimatePresence deliberately narrow. It owns one direct child slot instead of trying to solve keyed list retention immediately.

The current prototype keeps:

  • the current child snapshot
  • whether that slot is logically present
  • a completion callback that releases the retained child once exit settles

When the direct child disappears from the next render:

  1. retain the previous child snapshot
  2. provide isPresent = false through PresenceChild
  3. wait for descendant exit completion
  4. release the retained child

This is the smallest Solid-native retained-presence model that proves the ownership path without pretending keyed list orchestration already exists.

Where createSwitchTransition and createListTransition fit #

These primitives are useful, but they should guide semantics more than define the architecture.

createSwitchTransition is a good match for:

  • single-child switching
  • sync, out-in, and in-out style sequencing
  • reasoning about when the entering child may appear relative to the exiting child

createListTransition remains a later-stage reference for keyed list presence. It is not part of the Milestone 0 proof target.

The limitation is that both primitives operate on retained element lists. Our harder problem is retaining Solid owners and nested presence context. Because of that, the first AnimatePresence implementation should own its keyed record store directly.

The practical recommendation remains:

  • borrow their mode semantics and list-diff expectations
  • do not make them the core ownership mechanism for the current prototype
  • revisit them later if we want a lower-level DOM-retention helper inside the implementation

Why zombie-style retention is deferred #

Zombie-style retention usually means moving disappearing nodes into a separate retained tree or parking layer so they can finish independently of their original owner path.

That buys flexibility, but it also adds complexity early:

  • cross-root context questions
  • harder mental models around ownership and cleanup
  • more complicated coordination for layout and projection later
  • more chances to desynchronize Motion state from Solid state

We do not need that yet. A local retained set inside AnimatePresence already solves the first real problem: keep exiting children alive until exit completes.

If we later need reparenting for layout projection, pop-out exits, or cross-tree coordination, we can add it on top of a stable local presence contract instead of using it as the starting point.

Milestone 0 status #

The current repo state already covers these prototype steps:

  1. MotionState can report exit completion.
  2. PresenceChild aggregates descendant completion.
  3. AnimatePresence supports one retained child slot with sync and wait sequencing.

That is enough for Milestone 0. The next rollout steps are later work, not unfinished obligations for this milestone.

Later rollout #

  1. Extend AnimatePresence from one retained child slot to keyed lists only when that behavior has a concrete Solid-native design.
  2. Revisit DOM-level helpers or zombie-style retention only if layout or projection work proves the local retained model insufficient.
  3. Fold later presence work into shared layout only after the local retained-record contract stays stable.

Risks #

  • If exit completion reporting is edge-triggered rather than state-driven, retained records can get stuck forever.
  • If descendant registration is not stable across mount and remount, nested exits can complete too early.
  • If keys are unstable, retained rendering will look wrong no matter how good the exit pipeline is.
  • If we couple presence too tightly to the current scalar renderer, the later VisualElement seam will be harder to introduce.

Open questions #

  • Should the first public AnimatePresence mode surface only sync and wait, or should it expose an in-out equivalent immediately?
  • Should presence completion be reported only for the exit lane, or for any active target that blocks removal?
  • When layout work arrives, can the local retained-record model absorb it, or does that become the point where a zombie layer is justified?

Type alignment with motion-dom #

We should build on top of motion-dom types where they describe the same runtime boundary we are already using.

That means _motion-state-options.ts should start preferring exported upstream types such as:

  • VariantLabels
  • Variants
  • TargetAndTransition
  • Transition

Those types already match the semantics we are resolving through resolveVariantFromProps, and using them reduces local drift.

We should not blindly import deeper types just because they exist. Types that pull in VisualElement, React-oriented control surfaces, or broad animation controller contracts would widen this layer faster than the Solid runtime actually supports.

The rule should be simple:

  • use upstream public types for variant labels, variant maps, targets, and transitions
  • keep Solid-owned wrapper types for accessor support, presence state, and retained-rendering contracts
  • keep the renderer's final resolved target map narrower than TargetAndTransition, because the current backend still only animates a scalar style subset

In other words, yes, the option layer should lean more on motion-dom, but only at the public animation-definition boundary, not at the full internal runtime boundary.