Presence
The hooks' API, from
@wardic/solid/hooks: a prerelease, on npm'snexttag, built from thesolid-nextbranch. It may still change.
What keeps a Subject on the page while it leaves. A hook covers a thing whose coming and going you write yourself, and its ward is made where that's decided:
- One Subject that comes and goes: a ward's
show. SeecreateWard. - A list:
createWards, a ward for each item. - One value at a time:
createPresence(ward, value), below.
A router's page, children handed to a component, another library's component: these are ended by someone else, and everything made inside them with it, so what keeps them on the page while they leave has to be above them. That's <Presence>, or a presence scope, below.
createPresence
One ward at a time, for a value that changes. Its motion is a ward's, made by createWard as any other, and given first. The ward of the value that was leaves, and only once it has gone does the new value's ward enter. A value of undefined, null or false shows nothing.
import { createPresence, createWard } from "@wardic/solid/hooks"
const panel = createWard({ initial: { opacity: 0 }, animate: { opacity: 1 }, exit: { opacity: 0 } })
const shown = createPresence(panel, tab)
<Show when={shown()} keyed>{(tab) => <div {...panel}><Panel tab={tab} /></div>}</Show>Not the main entry's
createPresence().@wardic/solidhas acreatePresence()of its own: it takes nothing, and makes a handle that hears a presence's exits, for<Presence ref>. The hooks'createPresence(ward, value)shows a value, and isn't that handle. On the hooks, the handle iscreatePresenceRef().
The ward given is what's spread, and shown() is the value on the page. It's the one ward throughout: the presence takes it, and says when its value is on the page, so it leaves as a show ward does, and the next value's Subject, a new one, enters afresh from its initial. A value that comes back before its exit has landed turns the ward round. A sensor or a listener made on the ward hears every value's Subject.
- The ward is one
createWardmade, with noshowof its own, and not a list's row: any other throws. It's made before its presence is, so it says how it starts itself:enter: falsegives the value there at first no entrance. A value that isn't there as the page opens enters when it comes, whateverenter: falsesays. - The value is an accessor, or
{ value, key, affectsLayout }.keyis what a value is matched by when it's made again (a refetch), the value itself if left out: a value made again under the same key is shown without leaving.affectsLayout: falseis a list's: seecreateWards. - Its exits are its ward's.
shown.next(name)andshown.when(name)are the ward's ownnextandwhen, of"exitStart"and"exitComplete".
import { createPresence, createWard } from "@wardic/solid/hooks"
const frame = createWard({ initial: "closed", animate: "open" })
const showing = createPresence(frame, open)
createEffect(frame.when("exitComplete"), () => trigger.focus())createWards({ value }) has both at once instead: the value now beside the one it replaced, until that has gone.
<Presence>
<Presence> is the hooks' one boundary, for what its author doesn't take away themselves: each child that leaves stays on the page until its wards' exits have landed. A ward under it, a hook's or a component's, leaves by its own exit.
import { createWard, Presence } from "@wardic/solid/hooks"
// The layout: a navigation ends the page it hands over.
<Presence pop>{props.children}</Presence>
// A page: its motion is its own, a ward on its own root.
const page = createWard(fade)
<ul {...page}>…</ul>A ward made inside what it takes away never hears its own exit: it ends as its exit starts. Those exits are the boundary's to tell, by its ref. <Presence> is a utility over createPresenceScope, built from the hooks' public names alone, and its props are a scope's options:
| Prop | |
|---|---|
pop | A leaving child is taken out of the flow where it was, so the next is in its place at once: a ward's pop, said for whatever leaves here. true pins it by its top left; a corner, "top-right", "bottom-left" or "bottom-right", by that. |
wait | A child enters once the one before it has gone: one thing at a time. |
enter | false: what's there as the boundary is made has no entrance, and starts at its animate. What comes later enters from its initial. A ward's own enter: false, said for everything inside. |
custom | What a leaving child's dynamic exit resolves with: it has left, and can't be handed anything new. |
propagate | Inside another boundary's child: as that child leaves, this one's children leave too, each by its exit. |
affectsLayout | false: its children's coming and going moves no layout ward around them. |
ref | Its exits, heard: a createPresenceRef() handle, or a callback handed the boundary's own. Read once. |
scope | A scope made elsewhere, a module's for one: this boundary is its within, and its options are its own, so the ones here go unread. Read once. |
With neither pop nor wait, a child enters at once, beside the one leaving. It takes one or the other, not both. Coming from <AnimatePresence>, see its page for each prop's new name.
createPresenceScope
The same boundary as a value an app holds: createPresenceScope(options), with the options above.
import { createPresenceScope } from "@wardic/solid/hooks"
const pages = createPresenceScope({ pop: true })
// The layout: a navigation ends the page it hands over.
pages.within(() => props.children)
// Anywhere:
createEffect(pages.when("exitComplete"), () => window.scrollTo(0, 0))within(build) builds what it's given under the scope, and keeps a child that leaves on the page until the exits of the wards inside it have landed. Each ward made there, at any depth, registers its exit with the scope as it mounts; once its child leaves, the scope runs that exit itself, so it runs after Solid has ended the child and the ward with it. Code inside a leaving child can't hear its own leave, then: the scope does, by exiting() and its "exitStart" and "exitComplete", as a ward hears its own.
A scope is one boundary. In development, a within round a second while the first is on the page warns: the scope's exiting(), next and when would tell of both as one. Make a scope for each boundary.
The scope is a presence handle, so it can be a <Presence>'s own, by scope, or another's ref.
import { createPresenceScope, Presence } from "@wardic/solid/hooks"
// src/lib/pages.ts
export const pages = createPresenceScope({ pop: true })
// The layout:
<Presence scope={pages}>{props.children}</Presence>createPresenceRef
A handle for a <Presence>'s ref, made before the boundary is: it hears that boundary's exits from then on. A presence scope is one already. It's the main entry's createPresence(), under the hooks' name.
import { createPresenceRef, Presence } from "@wardic/solid/hooks"
const presence = createPresenceRef()
<Presence ref={presence}>…</Presence>
<button disabled={presence.exiting()} onClick={async () => {
setOpen(false)
await presence.next("exitComplete")
await save()
}}>Close and save</button>| Member | |
|---|---|
exiting() | Whether a child is leaving: true from an exit's start until no leaving child is left. |
next(name) | A promise of the next "exitStart", a child's exit starting, or "exitComplete", every leaving child gone. An exit a returning child interrupts doesn't complete. |
each(name) | Every one, for for await. It ends when the handle's owner is disposed. |
when(name) | Every one, as a memo for an effect. |
ref also takes a callback, ref={(p) => (presence = p)}, handed the boundary's own handle. On a server nothing leaves: exiting() is false, and next never settles.
Types
PresenceProps:<Presence>'s props: a presence scope's options, withchildren,refandscope.PresenceScopeOptions: whatcreatePresenceScopetakes: the props above, lesschildren,refandscope.PresenceScope: a presence scope: aPresenceRef, withwithin.PresenceRef: a presence's exits, heard: the handle above, the main entry'sPresencetype.PresenceSource<T>: the valuecreatePresencetakes, with how it's matched:{ value, key }.StagePresenceSource<T>: the value a Stage'screatePresencetakes: aPresenceSourcewithinitial: false, since a Stage's presence makes a ward for each value and so can say how the first starts.HookPresenceOf:createPresence's type:(ward, value), returning the value on the page, with its exits (Leaving, oncreateWards).