@wix/interact 2.8.3 → 2.8.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (38) hide show
  1. package/dist/cjs/index.js +1 -1
  2. package/dist/cjs/react.js +1 -1
  3. package/dist/cjs/web.js +1 -1
  4. package/dist/es/index.js +1 -1
  5. package/dist/es/react.js +2 -2
  6. package/dist/es/web.js +2 -2
  7. package/dist/index-D8zv4Q-3.js +29 -0
  8. package/dist/index-D8zv4Q-3.js.map +1 -0
  9. package/dist/{index-CXd-XY1L.mjs → index-DSREj0qw.mjs} +1631 -1432
  10. package/dist/index-DSREj0qw.mjs.map +1 -0
  11. package/dist/tsconfig.build.tsbuildinfo +1 -1
  12. package/dist/types/core/Interact.d.ts +2 -2
  13. package/dist/types/core/Interact.d.ts.map +1 -1
  14. package/dist/types/core/InteractionController.d.ts +6 -1
  15. package/dist/types/core/InteractionController.d.ts.map +1 -1
  16. package/dist/types/core/add.d.ts.map +1 -1
  17. package/dist/types/handlers/animationEnd.d.ts +15 -0
  18. package/dist/types/handlers/animationEnd.d.ts.map +1 -1
  19. package/dist/types/handlers/viewEnter.d.ts +8 -0
  20. package/dist/types/handlers/viewEnter.d.ts.map +1 -1
  21. package/dist/types/react/Interaction.d.ts +1 -1
  22. package/dist/types/types/controller.d.ts +15 -2
  23. package/dist/types/types/controller.d.ts.map +1 -1
  24. package/dist/types/types/handlers.d.ts +18 -0
  25. package/dist/types/types/handlers.d.ts.map +1 -1
  26. package/dist/types/types/internal.d.ts +2 -0
  27. package/dist/types/types/internal.d.ts.map +1 -1
  28. package/dist/types/types/plugins.d.ts +12 -4
  29. package/dist/types/types/plugins.d.ts.map +1 -1
  30. package/docs/api/interact-class.md +1 -1
  31. package/docs/api/types.md +3 -1
  32. package/docs/guides/plugins.md +28 -3
  33. package/package.json +2 -2
  34. package/rules/full-lean.md +2 -1
  35. package/rules/plugins.md +5 -1
  36. package/dist/index-CXd-XY1L.mjs.map +0 -1
  37. package/dist/index-CeIuoLxG.js +0 -29
  38. package/dist/index-CeIuoLxG.js.map +0 -1
@@ -50,13 +50,34 @@ type InteractPlugin = (
50
50
  scope: 'interaction' | 'effect';
51
51
  config: Record<string, unknown>; // the interaction/effect object the field was on
52
52
  },
53
- ) => void | (() => void); // optionally return a cleanup function
53
+ ) => void | (() => void) | PromiseLike<void | (() => void)>; // optionally return a cleanup function, or a promise of one
54
54
  ```
55
55
 
56
56
  - Plugins run at **connect** time (when the element enters the DOM / is added).
57
+ - A plugin may be **async** — see [Async plugins](#async-plugins).
57
58
  - A returned **cleanup** runs on disconnect, on teardown (`Interact.destroy()`), and when an interaction re-connects after a media-query change — so plugins can fully undo their work.
58
59
  - Each distinct plugin value is applied **once per connect**, even if referenced by multiple triggers on the same element. That dedup relies on the value's identity, so plugin values should be objects.
59
60
 
61
+ ### Async plugins
62
+
63
+ A plugin that prepares the DOM asynchronously (e.g. after loading a font or a module) can return a promise, optionally resolving to its cleanup:
64
+
65
+ ```ts
66
+ Interact.use('myPlugin', async (value, { root }) => {
67
+ await document.fonts.ready;
68
+ const undo = prepare(root, value);
69
+ return undo;
70
+ });
71
+ ```
72
+
73
+ Until the promise settles, Interact holds back the interaction that carries the field — exactly what [deferring](#deferring-plugins) holds back — and applies it once the plugin's DOM is in place, so selectors never resolve against unprepared markup. Other interactions are applied immediately.
74
+
75
+ - An async plugin starts only once its element is connected, so an effect whose target connects after its siblings were attached is held back on its own.
76
+
77
+ - The plugin runs once per connect: other resolution passes wait for the same promise instead of calling it again. Like the dedup, this relies on the value's identity: a primitive value (e.g. `$fonts: true`) is not awaited, so it holds nothing back.
78
+ - If the element disconnects (or re-connects after a media-query change) while the promise is pending, the resolved cleanup runs right away and nothing is applied for the stale connection.
79
+ - A rejected promise is logged with `console.error`, and the held-back parts are applied without the plugin.
80
+
60
81
  ### Type-safe config (optional)
61
82
 
62
83
  Augment `InteractPluginConfigMap` — keyed by the **unprefixed** plugin name — so the `$<name>` field is type-checked:
@@ -158,11 +179,15 @@ While a plugin is deferred, Interact also defers everything that may resolve aga
158
179
  | Where the `$<name>` field sits | What is deferred |
159
180
  | --------------------------------------------------------------------------------- | ------------------------------------------------ |
160
181
  | On an interaction | The whole interaction: its effects and sequences |
161
- | On an effect in `effects` (or in its definition under the config's `effects` map) | Only that effect |
162
- | On any effect of a sequence | The whole sequence |
182
+ | On an effect in `effects` (or in its definition under the config's `effects` map) | The whole interaction: its effects and sequences |
183
+ | On any effect of a sequence | The whole interaction: its effects and sequences |
184
+
185
+ An interaction's effects and sequences wait for each other, so they attach (and play) together — whatever plugin holds one of them back, and even if its target is a different element, connected or not. Releasing only that element's key (`Interact.ready(name, key)`) attaches all of them.
163
186
 
164
187
  Everything else in the config is applied immediately, as usual. Once released, deferred parts are applied exactly as they would have been on connect.
165
188
 
189
+ A `viewEnter` entrance is not lost while deferred (or while an [async plugin](#async-plugins) is pending): Interact keeps observing the source element, and if it entered the viewport meanwhile, a `once` entrance plays as soon as its trigger attaches — even if the element has already scrolled out of view. Likewise, an `animationEnd` chain is not broken: Interact records the source's `animationend` events meanwhile, and if one of them matches the trigger's `effectId`, its effect plays as soon as the trigger attaches (once, however many matched). This holds even for an interaction-level field on an interaction whose sources come from its own `selector` / `listContainer` — the interaction still attaches once the plugin is done, to the sources it finds then. Its `animationend` events are recorded for sources the plugin creates too; a `viewEnter` entry, though, only for sources that already existed before the plugin ran and that it kept — one it creates or replaces cannot be observed before it exists, so its entrance plays when it next enters the viewport. Other triggers (`click`, `hover`, …) are not replayed: events fired while held back are ignored. Repeating `viewEnter` types (`alternate`, `repeat`, `state`) pick up the element's current visibility when they attach.
190
+
166
191
  - `Interact.ready(name, key)` releases a name for a single element only — useful when parts of the page hydrate separately (e.g. Suspense boundaries).
167
192
  - `defer` accepts several names; the plugin stays deferred until all are released. A name can stand for any event, not just hydration — for example, `@wix/splittext`'s `lines` mode measures text, so it should wait for fonts:
168
193
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wix/interact",
3
- "version": "2.8.3",
3
+ "version": "2.8.5",
4
4
  "description": "Declarative, configuration-driven interaction library — web-native, AI-ready, and framework-agnostic.",
5
5
  "license": "MIT",
6
6
  "main": "dist/cjs/index.js",
@@ -68,7 +68,7 @@
68
68
  "url": "https://github.com/wix/interact/issues"
69
69
  },
70
70
  "dependencies": {
71
- "@wix/motion": "^2.2.1",
71
+ "@wix/motion": "^2.2.2",
72
72
  "fastdom": "^1.0.12",
73
73
  "fizban": "^0.7.2",
74
74
  "kuliso": "^0.4.13"
@@ -750,9 +750,10 @@ Interact can route config to external plugins registered with `Interact.use(name
750
750
  - Reference with a `$<name>` field on an **interaction** or **effect**: `$splitText: { container: '.title', type: 'chars' }`.
751
751
  - Plugins run at connect time, **before** target resolution — so DOM they create (e.g. `.split-c` spans) is visible to `selector` queries that follow.
752
752
  - A plugin may return a cleanup function; Interact runs it on disconnect/teardown.
753
+ - A plugin may be async (return a promise of an optional cleanup): what carries its field is held back until it settles, like a deferred plugin.
753
754
  - A `$<name>` field with no registered plugin is ignored.
754
755
  - Plugin fields MUST be `$`-prefixed — a non-prefixed unknown key on an interaction/effect is rejected by `@wix/interact-validate`.
755
- - **Deferring (hydration):** in apps that hydrate server-rendered markup (e.g. React), register DOM-changing plugins with `Interact.use('splitText', splitTextPlugin, { defer: 'hydrated' })` and call `Interact.ready('hydrated')` once hydration is done (e.g. in a root `useEffect`). While deferred, the plugin and what carries its field don't run: the whole interaction (interaction-level field), only that effect (effect-level field), or the whole sequence (field on a sequence effect). `Interact.ready(name, key)` releases a single element; `defer` may list several names, all of which must be released.
756
+ - **Deferring (hydration):** in apps that hydrate server-rendered markup (e.g. React), register DOM-changing plugins with `Interact.use('splitText', splitTextPlugin, { defer: 'hydrated' })` and call `Interact.ready('hydrated')` once hydration is done (e.g. in a root `useEffect`). While deferred, the plugin and what carries its field don't run: the whole interaction (interaction-level field), only that effect (effect-level field), or the whole sequence (field on a sequence effect). `Interact.ready(name, key)` releases a single element; `defer` may list several names, all of which must be released. A `viewEnter` `once` entry or a matching source `animationend` that happens while held back is replayed once the trigger attaches (except a `viewEnter` entry of a source the plugin itself creates or replaces, via the interaction's own `selector` / `listContainer`); other trigger events are not.
756
757
  - **SSR styling:** for FOUC prevention (e.g. hiding un-split text before an entrance animation), pass a **separate** per-plugin callback in the `plugins` option of `generate()`'s options bag: `generate(config, { useFirstChild: true, plugins: { myPlugin: (value, context) => { declarations, selectorSuffix }[] } })`. `generate()` does not inspect the value; the callback returns CSS rule(s) data and scopes them under the base selector (`[data-interact-key="<key>"]`). It is NOT the `use()` callback. If the plugin package ships a generator (e.g. `splitTextStyle` from `@wix/splittext/plugin`), pass that instead of writing one.
757
758
 
758
759
  **Example — split text, then stagger the generated char spans.** Use the adapter shipped from `@wix/splittext/plugin` (`splitTextPlugin` + its SSR counterpart `splitTextStyle`); do NOT hand-roll it:
package/rules/plugins.md CHANGED
@@ -20,9 +20,12 @@ Interact.use('<plugin-name>', (value, context) => {
20
20
  // value: the value of the `$<plugin-name>` field
21
21
  // context: { root, key, scope: 'interaction' | 'effect', config }
22
22
  // return: optional cleanup () => void, run on disconnect/teardown
23
+ // — or a promise of one (async plugin)
23
24
  });
24
25
  ```
25
26
 
27
+ - An async plugin (returning a promise) holds back the interaction carrying its field — same scope as deferring, below — until the promise settles; it starts only once its element connects, so siblings already attached by then are not held. It runs once per connect; a primitive value (no identity to dedup by) is not awaited; a stale resolution (element disconnected meanwhile) only runs its cleanup; a rejection is logged and the held-back parts apply without it.
28
+
26
29
  - `Interact.getPlugin(name) / Interact.getPluginsNames()` inspect the registry.
27
30
 
28
31
  ### Deferring (hydration)
@@ -37,8 +40,9 @@ Interact.create(config);
37
40
  useEffect(() => Interact.ready('hydrated'), []);
38
41
  ```
39
42
 
40
- - While deferred, the plugin does not run, and neither does what carries its `$<name>` field: the whole interaction (interaction-level field), only that effect (effect-level field), or the whole sequence (field on any sequence effect). Everything else applies immediately.
43
+ - While deferred, the plugin does not run, and neither does what carries its `$<name>` field: the whole interaction — its effects and sequences wait for each other, wherever the field sits (interaction, effect, or sequence effect) and even when a sibling's target is another element, connected or not; releasing only that element's key attaches all of them. Everything else applies immediately.
41
44
  - `Interact.ready(name, key)` releases a name for one element only; `Interact.ready(name)` releases it for all.
45
+ - A `viewEnter` `once` entry, or a source `animationend` matching an `animationEnd` trigger, that happens while held back (deferred or async) is replayed when the trigger attaches (it plays once) — also for an interaction-level field on an interaction with its own `selector` / `listContainer`, which attaches once the plugin is done; there, only a `viewEnter` source the plugin creates or replaces is not replayed (it plays when it next enters the viewport). `click` / `hover` events fired while held back are NOT replayed.
42
46
  - `defer` may list several names (e.g. `['hydrated', 'fonts']`); all must be released. Release `'fonts'` with `document.fonts.ready.then(() => Interact.ready('fonts'))`.
43
47
  - Released names persist until `Interact.destroy()`.
44
48