-
Notifications
You must be signed in to change notification settings - Fork 3
Splittext integration 2nd attempt #275
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 8 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
6115f62
2nd version + css
ameerf-wix 17d4020
Merge branch 'master' of github.com:wix/interact into splittext_integ…
ameerf-wix 40940be
format
ameerf-wix 57aed34
build
ameerf-wix 94bc86c
Merge branch 'splittext_integration_2nd_attempt' of github.com:wix/in…
ameerf-wix 715885f
small fix
ameerf-wix 584ea9d
small fix
ameerf-wix f57f52a
small fix
ameerf-wix 8608c4d
PR fixes
ameerf-wix 50c0295
insert plugin into package
ameerf-wix 6245d70
fix test
ameerf-wix 29c8d84
Merge branch 'master' of github.com:wix/interact into splittext_integ…
ameerf-wix 5c01d56
PR fixes
ameerf-wix c4c4736
PR fixes
ameerf-wix 17370e0
Merge branch 'master' of github.com:wix/interact into splittext_integ…
ameerf-wix 94d4d76
final PR changes
ameerf-wix File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| import { splitText, type SplitTextOptions, type SplitTextResult } from '@wix/splittext'; | ||
| import type { InteractPlugin, InteractPluginStyleGenerator } from '@wix/interact'; | ||
|
|
||
| /** Config accepted under `$splitText` in an InteractConfig on an interaction or effect. */ | ||
| export type SplitTextPluginConfig = { | ||
| container: string; | ||
| /** | ||
| * Hide the container until the split has been applied, to prevent a flash of the un-split text | ||
| * before an entrance/scroll animation runs. Emits SSR CSS via {@link splitTextStyle} and is | ||
| * revealed once the runtime plugin marks the container ready. | ||
| */ | ||
| hideUntilReady?: boolean; | ||
| } & SplitTextOptions; | ||
|
|
||
| const READY_ATTR = 'data-splittext-ready'; | ||
|
|
||
| /** | ||
| * Runtime adapter that lets `@wix/splittext` be driven through an InteractConfig `$splitText` field. | ||
| * | ||
| * Register once, before `Interact.create()`: | ||
| * | ||
| * ```ts | ||
| * import { Interact } from '@wix/interact'; | ||
| * import { splitTextPlugin } from './plugins/splitTextPlugin'; | ||
| * Interact.use('splitText', splitTextPlugin); | ||
| * ``` | ||
| * | ||
| * This module is the ONLY place that imports both packages. `@wix/interact` never imports | ||
| * `@wix/splittext` and vice-versa — the plugin bridge keeps them fully decoupled. | ||
| */ | ||
| export const splitTextPlugin: InteractPlugin = (value, { root }) => { | ||
| const { container, hideUntilReady, ...options } = value as SplitTextPluginConfig; | ||
|
|
||
| const element = root.querySelector<HTMLElement>(container); | ||
|
|
||
| if (!element) { | ||
| return; | ||
| } | ||
|
|
||
| const result: SplitTextResult = splitText(element, options); | ||
|
|
||
| // Reveal the container (see splitTextStyle) now that it holds the individually-animated spans. | ||
| if (hideUntilReady) { | ||
| element.setAttribute(READY_ATTR, ''); | ||
| } | ||
|
|
||
| // Interact runs this on disconnect/teardown, restoring the original text. | ||
| return () => { | ||
| result.revert(); | ||
| element.removeAttribute(READY_ATTR); | ||
| }; | ||
| }; | ||
|
|
||
| /** | ||
| * Build-time (SSR) styling for `$splitText`, passed to `generate()` — NOT the same callback as the | ||
| * runtime `splitTextPlugin` above. When `hideUntilReady` is set, hides the container until the | ||
| * runtime plugin has split it, preventing a flash of un-split text before the animation. | ||
| * | ||
| * ```ts | ||
| * import { generate } from '@wix/interact'; | ||
| * import { splitTextStyle } from './plugins/splitTextPlugin'; | ||
| * const css = generate(config, true, { splitText: splitTextStyle }); | ||
| * ``` | ||
| */ | ||
| export const splitTextStyle: InteractPluginStyleGenerator = (value, _) => { | ||
| const { container, hideUntilReady } = value as SplitTextPluginConfig; | ||
|
|
||
| if (!hideUntilReady) { | ||
| return []; | ||
| } | ||
|
|
||
| return [ | ||
| { | ||
| declarations: [{ name: 'visibility', value: 'hidden' }], | ||
| selectorSuffix: ` ${container}:not([${READY_ATTR}])`, | ||
| }, | ||
| ]; | ||
| }; | ||
|
|
||
| // Type the `$splitText` value so configs get autocomplete + checking. | ||
| declare module '@wix/interact' { | ||
| interface InteractPluginConfigMap { | ||
| splitText: SplitTextPluginConfig; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| import { afterEach, beforeEach, describe, expect, it } from 'vitest'; | ||
| import { Interact, add, remove, generate } from '@wix/interact'; | ||
| import type { InteractConfig } from '@wix/interact'; | ||
| import { splitTextPlugin, splitTextStyle } from '../src/plugins/splitTextPlugin'; | ||
|
|
||
| // End-to-end proof of the plugin bridge with the REAL @wix/splittext: splitText mutates the DOM, | ||
| // Interact resolves the generated spans, and disconnect reverts the split. The animation engine | ||
| // runs for real but no presets are registered here, so `getAnimation` logs a benign | ||
| // "FadeIn not found in registry" — irrelevant to the split/resolve/revert behavior under test. | ||
| describe('splitText through the Interact plugin bridge (real @wix/splittext)', () => { | ||
| beforeEach(() => { | ||
| Interact.use('splitText', splitTextPlugin); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| Interact.destroy(); | ||
| }); | ||
|
|
||
| it('splits the container into char spans that the effect selector targets, then reverts', () => { | ||
| const element = document.createElement('div'); | ||
| element.innerHTML = '<h1 class="title">Hi</h1>'; | ||
| document.body.appendChild(element); | ||
|
|
||
| const config: InteractConfig = { | ||
| interactions: [ | ||
| { | ||
| key: 'hero', | ||
| trigger: 'hover', | ||
| $splitText: { container: '.title', type: 'chars' }, | ||
| effects: [ | ||
| { | ||
| key: 'hero', | ||
| selector: '.split-c', | ||
| namedEffect: { type: 'FadeIn' } as never, | ||
| duration: 300, | ||
| }, | ||
| ], | ||
| }, | ||
| ], | ||
| }; | ||
|
|
||
| Interact.create(config); | ||
| add(element, 'hero'); | ||
|
|
||
| // Real splitText produced char spans inside the container. | ||
| const chars = element.querySelectorAll('.split-c'); | ||
| expect(chars.length).toBeGreaterThanOrEqual(2); // "H", "i" | ||
|
|
||
| // Teardown reverts: the split spans are gone and the original text is restored. | ||
| remove('hero'); | ||
| expect(element.querySelectorAll('.split-c').length).toBe(0); | ||
| expect(element.querySelector('.title')?.textContent).toContain('Hi'); | ||
|
|
||
| document.body.removeChild(element); | ||
| }); | ||
|
|
||
| it('generate() emits SSR FOUC-prevention CSS via splitTextStyle, matched by the runtime marker', () => { | ||
| // A `hover` trigger keeps the runtime path off the (jsdom-unsupported) sequence engine; | ||
| // the `hideUntilReady` marker is trigger-independent. The SSR rule is emitted for any trigger. | ||
| const config: InteractConfig = { | ||
| interactions: [ | ||
| { | ||
| key: 'hero', | ||
| trigger: 'hover', | ||
| $splitText: { container: '.title', type: 'chars', hideUntilReady: true }, | ||
| effects: [ | ||
| { | ||
| key: 'hero', | ||
| selector: '.split-c', | ||
| namedEffect: { type: 'FadeIn' } as never, | ||
| duration: 300, | ||
| }, | ||
| ], | ||
| }, | ||
| ], | ||
| }; | ||
|
|
||
| // SSR: the container is hidden until the split marks it ready. | ||
| const css = generate(config, true, { splitText: splitTextStyle }); | ||
| expect(css).toContain( | ||
| '[data-interact-key="hero"] .title:not([data-splittext-ready]) { visibility: hidden; }', | ||
| ); | ||
|
|
||
| // Runtime: after the plugin splits, the container carries the marker, so the hide rule | ||
| // stops matching (the generated spans handle their own entrance visibility). | ||
| const element = document.createElement('div'); | ||
| element.innerHTML = '<h1 class="title">Hi</h1>'; | ||
| document.body.appendChild(element); | ||
|
|
||
| Interact.create(config); | ||
| add(element, 'hero'); | ||
|
|
||
| expect(element.querySelector('.title')?.hasAttribute('data-splittext-ready')).toBe(true); | ||
| expect(element.querySelectorAll('.split-c').length).toBeGreaterThanOrEqual(2); | ||
|
|
||
| remove('hero'); | ||
| expect(element.querySelector('.title')?.hasAttribute('data-splittext-ready')).toBe(false); | ||
|
|
||
| document.body.removeChild(element); | ||
| }); | ||
|
|
||
| it('generate() omits the hide rule when hideUntilReady is not set', () => { | ||
| const config: InteractConfig = { | ||
| effects: { 'char-fade-up': { namedEffect: { type: 'FadeIn' }, duration: 400 } }, | ||
| interactions: [ | ||
| { | ||
| key: 'hero', | ||
| trigger: 'viewEnter', | ||
| $splitText: { container: '.title', type: 'chars' }, | ||
| sequences: [ | ||
| { offset: 30, effects: [{ effectId: 'char-fade-up', selector: '.split-c' }] }, | ||
| ], | ||
| }, | ||
| ], | ||
| }; | ||
|
|
||
| const css = generate(config, true, { splitText: splitTextStyle }); | ||
| expect(css).not.toContain('data-splittext-ready'); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| import { defineConfig } from 'vitest/config'; | ||
|
|
||
| export default defineConfig({ | ||
| test: { | ||
| environment: 'jsdom', | ||
| include: ['test/**/*.spec.ts'], | ||
| }, | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.