Skip to content

feat: change options without rebuilding the morph - #74

Open
Bewinxed wants to merge 1 commit into
lochie:mainfrom
Bewinxed:live-options
Open

Bewinxed wants to merge 1 commit into
lochie:mainfrom
Bewinxed:live-options

Conversation

@Bewinxed

Copy link
Copy Markdown

Problem

MorphController.attach() destroys the current TextMorph and builds a new one. The Svelte wrapper calls it from an $effect keyed on serializeConfig(options), and the React and Vue wrappers do the same with their own config keys. So any option change on a mounted TextMorph (disabled, duration, ease, debug and the rest) tears the morph down. destroy() cancels every animation on the element through element.getAnimations() and drops the accessible copy, the root attribute and the stylesheet reference, and the new instance replaces the element's contents and lays the text out again.

On a page with many morphs whose options follow app state, this gets expensive. In a trace of a design-system page full of morphing labels, destroy took 307 ms over about 15 s, 274 ms of it in getAnimations.

needsRecreate() looks like it was meant to prevent this, but nothing calls it.

What changed

TextMorph gets configure(options), and the constructor now calls it, so one place turns options into the instance's settings. It merges the given options over the current ones, and an option passed as undefined returns to its default, as in the constructor. The options the constructor used to apply once now update live:

  • ease and duration are resolved again, so a spring still computes its own duration.
  • respectReducedMotion adds the media-query listener when turned on and removes it when turned off.
  • debug toggles torph-debug on an instance that is already set up. One that isn't set up yet gets it in setup(), as before.
  • disabled is read by isDisabled() on each update, so the next update takes the plain-text path it already takes when reduced motion turns on mid-life. A running morph finishes and settles first, and turning disabled off sets the instance up on the next update.

locale, decimals, numbers, scale and the callbacks were already read when a morph runs.

MorphController.configure(options) passes options to the live instance and queues the last value again. That does nothing unless a new locale or decimals formats a number differently, in which case it morphs to the new format, which a rebuild would also have shown.

needsRecreate() and the controller's configKey field are removed. serializeConfig() stays because the React hook keys its effect on it.

Each wrapper now attaches once per element and sends option changes through configure(). In Svelte, one effect attaches (reading options through untrack) and another calls configure(options); callback props now reach the morph instead of staying as they were at attach time. In React, one effect attaches on mount and another calls configure() when the config key changes; callbacks still go through the ref, and onAnimationCancel now does too. In Vue, one computed options feeds attach() when the element ref changes and configure() when options changes.

The changeset is a patch, with the needsRecreate() removal listed under Breaking because MorphController is exported.

Testing

configure.test.ts covers the engine and the controller:

  • changing any option on a mounted morph doesn't call destroy() and keeps its nodes;
  • the next update uses the new duration and ease, and a spring given later resolves as the constructor would resolve it;
  • options left out are kept, and an option passed as undefined goes back to its default;
  • disabled: true makes the next update a plain-text swap with no animations, and disabled: false morphs again;
  • an instance built disabled sets itself up once enabled;
  • debug turns the outline attribute on and off;
  • respectReducedMotion adds the media-query listener once (a second call adds no other), removes it when turned off, and destroy() leaves none;
  • MorphController.configure() keeps the same instance, and a new locale reformats the current number (1,234.5 to 1.234,5).

vue/__tests__/TextMorph.test.ts mounts the Vue component, changes duration, ease and debug, and checks that the morph isn't destroyed and the next text change animates with the new duration.

All 11 new tests fail against main. The React and Svelte wrappers have no tests here: with no vitest config, the React wrapper's JSX doesn't parse in tests and .svelte files don't compile there. I compiled the Svelte component with svelte/compiler instead, with no warnings.

pnpm test (428 passed), pnpm typecheck, pnpm lint and pnpm build pass. The lint warnings are all in site/, the same ones main has.

Related

#62 stopped the React wrapper rebuilding its controller on every render. This does the same for option changes in all three wrappers.

#73 edits nearby lines: serializeConfig() in controller.ts (scale and numbers out, plugins in) and the props and options block in TextMorph.svelte. Whichever lands second needs a small rebase, and scale and numbers would come out of the option list in configure.test.ts. Under #73 a plugin sets up CSS, per-morph state and a teardown when the instance is built, so configure() shouldn't swap plugins on a live instance; a plugins change should still go through attach().

TextMorph and MorphController gain configure(), which applies new
options to the live instance: ease and duration are resolved again, the
reduced-motion listener is added or removed, and the debug attribute is
toggled. The React, Vue and Svelte wrappers attach once per element and
pass option changes through it, so a prop change no longer cancels the
running morph and rebuilds the text.

needsRecreate() had no caller and is removed.
@vercel

vercel Bot commented Nov 25, 2026

Copy link
Copy Markdown

@Bewinxed is attempting to deploy a commit to the Lochie Team on Vercel.

A member of the Team first needs to authorize it.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant