Scopes
The hooks' API, a prerelease. This is
@wardic/solid/hooks, in@wardic/solid@next: npm'snexttag, built from thesolid-nextbranch. It may still change.
The hooks have three scopes, each a create* hook that returns a value with within(build), which builds build() in the scope: every ward made there, at any depth, is in it.
- A config scope,
createConfigScope: settings for wards, their default transition among them. - A layout group,
createLayoutGroup: a namespace for theirlayoutIds. - A Wardic scope,
createWardicScope: a page of its own inside the page.
Why within, and not a hook that scopes the rest of the component it's called in: a Solid 2 component has no owner of its own, so a context set from its body would land on the owner around it, and on every sibling made after it. within is Solid's own context provider around what it builds.
So a ward made in the same body as a scope isn't inside its within. A config scope and a layout group are runes too, for that ward: a config scope gives it its settings as its own keys, and a layout group puts its layoutId in the group. A Wardic scope is no rune: a ward is made in a scope, not given one.
<WardicConfig>, <LayoutGroup> and <WardicProvider>, from @wardic/solid, are utilities over them, built from the hooks' public names alone, as an app could write them.
Config scope: createConfigScope
Settings for wards, as a value an app holds. One value is two things:
- A rune. Given to a ward, it gives that ward its
transition,reducedMotion,skipAnimationsandtransformPagePoint, as the ward's own keys. within(build). Every ward made in what it builds, at any depth, reads it as its config, and aw.<tag>itsisValidProp.
import { createConfigScope, createWard } from "@wardic/solid/hooks"
const calm = createConfigScope({ transition: { type: "tween", duration: 0.6 } })
function Shelf() {
// This ward, as a rune; and every ward made in `within`.
const title = createWard(calm, lift)
return (
<section>
<h2 {...title}>New in</h2>
{calm.within(() => <Cards />)}
</section>
)
}Its settings, a ConfigSettings:
| Setting | |
|---|---|
transition | The default transition of every ward in it that gives none of its own. |
reducedMotion | How its wards honour the visitor's reduced-motion setting: "user", "always" or "never". |
skipAnimations | Its wards' animations land at their ends on the next frame: for end-to-end and visual tests. |
transformPagePoint | Where the points its wards' drags and pans use are: handed each in the viewport's coordinates. |
isValidProp | Which of the props given a w.<tag> inside its within reach the element: one it returns false for is kept off it. Asked of no prop starting with on. Not a ward's key: a hook ward spreads none of an app's props. |
A setting is a value or an accessor, read where it's used: as each animation starts, at each drag start, and as each w.<tag> is made. transformPagePoint and isValidProp are functions themselves.
How they join
- Given to
createConfigScope, other scopes and settings join left to right, as runes join: a later one's setting wins, andtransitionlays its fields over an earlier one's.createConfigScope(outer, { transition })keeps whatoutergives and lays its own over it. - Nested, a scope inside another's
withinthat isn't given it replaces each setting it gives,transitionwhole, and lets the rest fall through to the scope around, then the page's. - On a ward, its own key beats every scope it's inside. Given to a ward as a rune, a scope joins as any rune does: a rune's key after it wins, and it wins over one before it, so write the scope first:
createWard(calm, { transition: fast }).
Its members
| Member | |
|---|---|
within(build) | Builds build() under the scope: every ward made there reads it as its config. |
isValidProp() | The scope's isValidProp, as what it's given joins it: what a w.<tag> asks of its props. |
useConfigScope()
The config around the caller, as a scope: the nearest within's, every scope around it resolved in it, so a scope laid over it keeps all of it. undefined where no scope is around: the page's own settings are under every scope, and not one.
import { createConfigScope, useConfigScope } from "@wardic/solid/hooks"
function Slower() {
const slower = createConfigScope(useConfigScope() ?? {}, { transition: { duration: 1.2 } })
return slower.within(() => <Cards />)
}<WardicConfig>
A utility over createConfigScope: its props are the scope's settings, each read where the scope reads it, so a prop that changes is followed, and what it's around is built inside the scope's within. A transition given inherit: true lays its fields over the config around. See WardicConfig.
Layout group: createLayoutGroup
A layout group as a value an app holds: a namespace for the layoutIds of its wards, so two copies of a component that uses one keep their own. One value is two things:
- A rune. Given to a ward, it puts that ward's
layoutIdin the group. within(build). Every ward made in what it builds, at any depth, is in the group.
import { createLayoutGroup, createWard } from "@wardic/solid/hooks"
function TabBar() {
const tabs = createLayoutGroup("tabs")
// This ward's `layoutId` is `tabs-underline`; and every ward made in `within` is in the group.
const underline = createWard(tabs, { layoutId: "underline" })
return (
<nav>
{tabs.within(() => <Tabs />)}
<span {...underline} />
</nav>
)
}The value is the only way into a group: no key names one. id is put before every layoutId in it; with no id, the group keeps the one around. Its id is joined to the group around where it's used, a ward's where that ward is made and within's where it's called, as inherit says. Page-only: a Stage's ward takes none.
Its options, a LayoutGroupOptions:
| Option | |
|---|---|
inherit | Whether it joins the id of the group it's in: true (the default) and "id" both do, making outer-inner, or taking the outer id when it has none of its own; false starts afresh. |
layoutDependency | A value whose change has every layout ward measured again, for a change nothing else sees: an accessor, followed inside within. |
Its one member is within(build), which builds build() in the group.
<LayoutGroup> is a utility over it: its id and inherit read once, as it mounts, its layoutDependency followed, and what it's around built inside the group's within. See LayoutGroup.
Wardic scope: createWardicScope
A page of its own inside the page, for a test's Environment, or a widget that keeps its own settings whatever page it lands in. It takes what makeWard takes: an Environment, Features given at once or by a loader, and its settings.
import { domAnimation } from "@wardic/solid"
import { createWardicScope } from "@wardic/solid/hooks"
function Embedded() {
const scope = createWardicScope({ env: testEnv, features: [domAnimation] })
return scope.within(() => <Widget />)
}Its options, a WardicScopeOptions:
| Option | |
|---|---|
env | The Environment Wardic runs in: the DOM's, DomLive, unless given. Read once, and built at once: a Layer that waits as it builds fails it. |
features | The Features every w.* element in it has: at once ([domAnimation]), or loaded with a function (() => import("./features").then((m) => m.drag)). Read once. A hook ward brings its own. |
reducedMotion | How animations honour the visitor's reduced-motion setting: "user" follows it (the default), "always" reduces motion, "never" doesn't. Read as each animation starts, so a getter given for it is followed. |
appear | On a server-rendered page, entrances play from the first paint, before the JavaScript has loaded (the default, true). false makes them wait for the JavaScript; "explain" plays them, and logs on the server why each one that can't play waits. |
nonce | The nonce your page's Content Security Policy gives inline styles: the <style> a server render writes for its entrances carries it. |
scaleCorrectors | Your own corrections of a value a layout animation's scale would distort, by the key each corrects. Read once. |
strict | In development, a wardic.* element in it throws, so every element ships only its Features. |
transition | The default transition of every ward in it that gives none of its own: a value or an accessor. |
skipAnimations | Every animation in it lands at its end on the next frame: a value or an accessor. For end-to-end and visual tests. |
transformPagePoint | Where the points a drag or a pan in it uses are: handed each in the viewport's coordinates. |
- Its runtime is made where it's called, and ends with that owner: a layout animation still waiting then is rejected with an
AbortError. - Its Features are built at the browser's idle time once it has settled, and development warns of a tracer that reads no Wardic span.
- Nothing from around it reaches its wards, neither the page's settings nor a config scope's: what it doesn't set is each setting's default, and the config scopes inside it lay over it alone.
withinalone: it's no rune, since a ward is made in a scope, not given one. Its one member iswithin(build): every ward made there runs on its runtime, with its Features and settings.- Page-only: a Stage has its own.
<WardicProvider> is a utility over it: its props are the scope's options, reducedMotion read as each animation starts and the rest once, as it's made. A <WardicConfig> around a <WardicProvider> doesn't reach in. See WardicProvider.
Types
ConfigScope: whatcreateConfigScopeanduseConfigScopegive: a rune, the page's alone, that carries aConfigScopeApi.ConfigScopeApi: itswithinandisValidProp, above.ConfigSettings: the settings a config scope takes, above.HookLayoutGroup: whatcreateLayoutGroupreturns: a rune, the page's alone, that carries aLayoutGroupApi, itswithin.LayoutGroupOptions:createLayoutGroup's options,inheritandlayoutDependency.WardicScope: whatcreateWardicScopereturns: itswithin.WardicScopeOptions: what it takes:makeWard's settings, and its Features.
See also createWard, Presence, whose presence scope has the same shape, and makeWard.