@wix/site-service-interactions 1.0.8 → 1.0.10
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.
- package/cjs/build/implementations/coreInteractions.js +34 -26
- package/cjs/build/implementations/coreInteractions.js.map +1 -1
- package/cjs/build/implementations/interactionsScriptContent.js +30 -16
- package/cjs/build/implementations/interactionsScriptContent.js.map +1 -1
- package/cjs/build/implementations/stylesToAdd.d.ts +1 -1
- package/cjs/build/implementations/stylesToAdd.js +1 -1
- package/cjs/build/implementations/v2/InteractionsScriptElement.d.ts +3 -0
- package/cjs/build/implementations/v2/InteractionsScriptElement.js +33 -0
- package/cjs/build/implementations/v2/InteractionsScriptElement.js.map +1 -0
- package/cjs/build/implementations/v2/cdnUrls.d.ts +2 -0
- package/cjs/build/implementations/v2/cdnUrls.js +6 -0
- package/cjs/build/implementations/v2/cdnUrls.js.map +1 -0
- package/cjs/build/implementations/v2/clickTriggerCss.d.ts +3 -0
- package/cjs/build/implementations/v2/clickTriggerCss.js +35 -0
- package/cjs/build/implementations/v2/clickTriggerCss.js.map +1 -0
- package/cjs/build/implementations/v2/configCss.d.ts +3 -0
- package/cjs/build/implementations/v2/configCss.js +15 -0
- package/cjs/build/implementations/v2/configCss.js.map +1 -0
- package/cjs/build/implementations/v2/configStyleHeadContent.d.ts +1 -0
- package/cjs/build/implementations/v2/configStyleHeadContent.js +16 -0
- package/cjs/build/implementations/v2/configStyleHeadContent.js.map +1 -0
- package/cjs/build/implementations/v2/interactionsScriptContent.d.ts +9 -0
- package/cjs/build/implementations/v2/interactionsScriptContent.js +52 -0
- package/cjs/build/implementations/v2/interactionsScriptContent.js.map +1 -0
- package/cjs/build/implementations/v2/motionPresets.d.ts +13 -0
- package/cjs/build/implementations/v2/motionPresets.js +54 -0
- package/cjs/build/implementations/v2/motionPresets.js.map +1 -0
- package/cjs/build/implementations/v2/namedEffectTypes.d.ts +2 -0
- package/cjs/build/implementations/v2/namedEffectTypes.js +42 -0
- package/cjs/build/implementations/v2/namedEffectTypes.js.map +1 -0
- package/cjs/build/implementations/v2/registerConfigEffects.d.ts +6 -0
- package/cjs/build/implementations/v2/registerConfigEffects.js +27 -0
- package/cjs/build/implementations/v2/registerConfigEffects.js.map +1 -0
- package/dist/implementations/coreInteractions.js +30 -22
- package/dist/implementations/coreInteractions.js.map +1 -1
- package/dist/implementations/interactionsScriptContent.js +30 -16
- package/dist/implementations/interactionsScriptContent.js.map +1 -1
- package/dist/implementations/stylesToAdd.d.ts +1 -1
- package/dist/implementations/stylesToAdd.js +1 -1
- package/dist/implementations/v2/InteractionsScriptElement.d.ts +3 -0
- package/dist/implementations/v2/InteractionsScriptElement.js +26 -0
- package/dist/implementations/v2/InteractionsScriptElement.js.map +1 -0
- package/dist/implementations/v2/cdnUrls.d.ts +2 -0
- package/dist/implementations/v2/cdnUrls.js +3 -0
- package/dist/implementations/v2/cdnUrls.js.map +1 -0
- package/dist/implementations/v2/clickTriggerCss.d.ts +3 -0
- package/dist/implementations/v2/clickTriggerCss.js +31 -0
- package/dist/implementations/v2/clickTriggerCss.js.map +1 -0
- package/dist/implementations/v2/configCss.d.ts +3 -0
- package/dist/implementations/v2/configCss.js +11 -0
- package/dist/implementations/v2/configCss.js.map +1 -0
- package/dist/implementations/v2/configStyleHeadContent.d.ts +1 -0
- package/dist/implementations/v2/configStyleHeadContent.js +12 -0
- package/dist/implementations/v2/configStyleHeadContent.js.map +1 -0
- package/dist/implementations/v2/interactionsScriptContent.d.ts +9 -0
- package/dist/implementations/v2/interactionsScriptContent.js +47 -0
- package/dist/implementations/v2/interactionsScriptContent.js.map +1 -0
- package/dist/implementations/v2/motionPresets.d.ts +13 -0
- package/dist/implementations/v2/motionPresets.js +16 -0
- package/dist/implementations/v2/motionPresets.js.map +1 -0
- package/dist/implementations/v2/namedEffectTypes.d.ts +2 -0
- package/dist/implementations/v2/namedEffectTypes.js +38 -0
- package/dist/implementations/v2/namedEffectTypes.js.map +1 -0
- package/dist/implementations/v2/registerConfigEffects.d.ts +6 -0
- package/dist/implementations/v2/registerConfigEffects.js +23 -0
- package/dist/implementations/v2/registerConfigEffects.js.map +1 -0
- package/package.json +8 -5
- package/src/implementations/coreInteractions.ts +36 -25
- package/src/implementations/interactionsScriptContent.ts +30 -16
- package/src/implementations/stylesToAdd.ts +1 -1
- package/src/implementations/v2/InteractionsScriptElement.tsx +42 -0
- package/src/implementations/v2/cdnUrls.ts +4 -0
- package/src/implementations/v2/clickTriggerCss.ts +44 -0
- package/src/implementations/v2/configCss.ts +13 -0
- package/src/implementations/v2/configStyleHeadContent.ts +12 -0
- package/src/implementations/v2/interactionsScriptContent.ts +61 -0
- package/src/implementations/v2/motionPresets.ts +26 -0
- package/src/implementations/v2/namedEffectTypes.ts +52 -0
- package/src/implementations/v2/registerConfigEffects.ts +31 -0
|
@@ -1,14 +1,22 @@
|
|
|
1
|
-
import { Interact } from '@wix/interact'
|
|
1
|
+
import { Interact } from '@wix/interact/web'
|
|
2
2
|
import type { InteractConfig } from '@wix/interact'
|
|
3
3
|
import { DeviceInfoDefinition } from '@wix/site-service-device-info/definition'
|
|
4
4
|
import { ExperimentsDefinition } from '@wix/site-service-experiments/definition'
|
|
5
|
-
import { RenderingContextDefinition } from '@wix/site-service-rendering-context/definition'
|
|
6
5
|
import { PagesDefinition } from '@wix/site-service-pages/definition'
|
|
6
|
+
import { HeadAppenderDefinition } from '@wix/site-service-head-appender/definition'
|
|
7
7
|
import { ProvideComponentServiceDefinition } from '@wix/services-manager-react/core-services/provide-component'
|
|
8
8
|
import type { ServicesManager } from '@wix/services-manager/types'
|
|
9
9
|
import { createInteractionsScriptElement } from './InteractionsScriptElement'
|
|
10
|
+
import { createV2InteractionsScriptElement } from './v2/InteractionsScriptElement'
|
|
11
|
+
import { getV2ConfigCss } from './v2/configCss'
|
|
12
|
+
import { getV2ConfigStyleHeadContent } from './v2/configStyleHeadContent'
|
|
13
|
+
import { getV2ModulePreloadHeadContent } from './v2/interactionsScriptContent'
|
|
14
|
+
import { registerConfigEffects } from './v2/registerConfigEffects'
|
|
10
15
|
|
|
11
16
|
const initInteract = (config: InteractConfig, isReducedMotion: boolean) => {
|
|
17
|
+
// The bundled runtime is v2, where named effects come from a separate package the loader
|
|
18
|
+
// script imports on the SSR path. Here they have to be registered before create().
|
|
19
|
+
registerConfigEffects(config)
|
|
12
20
|
Interact.forceReducedMotion = isReducedMotion
|
|
13
21
|
Interact.setup({
|
|
14
22
|
scrollOptionsGetter: () => globalThis.document.getElementById('site-root'),
|
|
@@ -20,28 +28,37 @@ const initInteract = (config: InteractConfig, isReducedMotion: boolean) => {
|
|
|
20
28
|
Interact.create(config)
|
|
21
29
|
}
|
|
22
30
|
|
|
23
|
-
const
|
|
31
|
+
const initRuntime = (
|
|
24
32
|
config: InteractConfig,
|
|
25
33
|
isReducedMotion: boolean,
|
|
26
|
-
getService: ServicesManager['getService']
|
|
34
|
+
getService: ServicesManager['getService'],
|
|
35
|
+
useV2: boolean
|
|
27
36
|
) => {
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
return false
|
|
37
|
+
if (!config.interactions?.length) {
|
|
38
|
+
return
|
|
31
39
|
}
|
|
32
40
|
|
|
33
|
-
const
|
|
34
|
-
const pageId = currentPage?.id
|
|
35
|
-
|
|
41
|
+
const pageId = getService(PagesDefinition).currentPage?.id
|
|
36
42
|
if (!pageId) {
|
|
37
|
-
return
|
|
43
|
+
return
|
|
38
44
|
}
|
|
39
45
|
|
|
40
46
|
const { addTopComponent } = getService(ProvideComponentServiceDefinition)
|
|
41
|
-
const ssrScriptElement = createInteractionsScriptElement(config, isReducedMotion, pageId)
|
|
42
|
-
addTopComponent(ssrScriptElement)
|
|
43
47
|
|
|
44
|
-
|
|
48
|
+
if (!useV2) {
|
|
49
|
+
addTopComponent(createInteractionsScriptElement(config, isReducedMotion, pageId))
|
|
50
|
+
return
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// Both belong in the head: the preload links only help while the document is still parsing,
|
|
54
|
+
// and the generated CSS hides elements before their entrance effect, so it has to apply
|
|
55
|
+
// before the body renders.
|
|
56
|
+
const css = getV2ConfigCss(config).trim()
|
|
57
|
+
getService(HeadAppenderDefinition).setHead(
|
|
58
|
+
getV2ModulePreloadHeadContent(config) + (css ? getV2ConfigStyleHeadContent(css) : '')
|
|
59
|
+
)
|
|
60
|
+
|
|
61
|
+
addTopComponent(createV2InteractionsScriptElement(config, isReducedMotion, pageId))
|
|
45
62
|
}
|
|
46
63
|
|
|
47
64
|
const getInteractiveComps = (interactions: InteractConfig['interactions']) =>
|
|
@@ -50,7 +67,7 @@ const getInteractiveComps = (interactions: InteractConfig['interactions']) =>
|
|
|
50
67
|
acc.add(key)
|
|
51
68
|
// add reaction comps
|
|
52
69
|
// there must be a key
|
|
53
|
-
effects
|
|
70
|
+
effects?.forEach(({ key }) => acc.add(key!))
|
|
54
71
|
|
|
55
72
|
return acc
|
|
56
73
|
}, new Set<string>())
|
|
@@ -58,21 +75,15 @@ const getInteractiveComps = (interactions: InteractConfig['interactions']) =>
|
|
|
58
75
|
export const coreInteractions = (config: InteractConfig, getService: ServicesManager['getService']) => {
|
|
59
76
|
const { reducedMotion: isReducedMotion } = getService(DeviceInfoDefinition)
|
|
60
77
|
const { isOpen } = getService(ExperimentsDefinition)
|
|
61
|
-
const { SSR } = getService(RenderingContextDefinition)
|
|
62
78
|
|
|
63
|
-
|
|
64
|
-
if (isSSRExperimentOpen) {
|
|
79
|
+
if (isOpen('specs.thunderbolt.interactionsInSsr')) {
|
|
65
80
|
// Emits the config and loader scripts. Works in both environments: SSR serves markup the
|
|
66
81
|
// parser executes, and on the client the loader's ref revives it into a script that runs —
|
|
67
82
|
// which is what pages rendered only on the client (password-protected / members-only,
|
|
68
83
|
// client-side navigation) need, since they never get a server-rendered document.
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
// (e.g. no pageId or no interactions).
|
|
73
|
-
if (!didInitInteractInSsr && !SSR) {
|
|
74
|
-
initInteract(config, isReducedMotion)
|
|
75
|
-
}
|
|
84
|
+
// v2 is only reachable from here: it ships as a loader script plus generated CSS, both of
|
|
85
|
+
// which exist only on this path.
|
|
86
|
+
initRuntime(config, isReducedMotion, getService, isOpen('specs.thunderbolt.vsm_interactions_v2'))
|
|
76
87
|
} else {
|
|
77
88
|
initInteract(config, isReducedMotion)
|
|
78
89
|
}
|
|
@@ -1,24 +1,38 @@
|
|
|
1
1
|
export const getInteractionsScriptContent = (configId: string, isReducedMotion: boolean) => `
|
|
2
2
|
await import(\`https://static.parastorage.com/services/interact/dist/interact.umd.min.js\`);
|
|
3
3
|
|
|
4
|
+
// Wix pages run an AMD loader (RequireJS) whose define wins the interact UMD wrapper, so the
|
|
5
|
+
// bundle registers itself as a named AMD module instead of assigning window.interact. Read the
|
|
6
|
+
// global first, and when it is absent pull the module back out of the AMD loader.
|
|
7
|
+
let interactModule = window.interact;
|
|
8
|
+
if (!interactModule?.Interact) {
|
|
9
|
+
if (typeof window.require === \`function\`) {
|
|
10
|
+
try {
|
|
11
|
+
interactModule = await new Promise(function (resolve, reject) {
|
|
12
|
+
window.require([\`interact\`], resolve, reject);
|
|
13
|
+
});
|
|
14
|
+
} catch (error) {
|
|
15
|
+
console.error(\`Failed loading Interact via the AMD loader\`, error);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const Interact = interactModule?.Interact;
|
|
4
21
|
const configElement = document.getElementById(\`${configId}\`);
|
|
5
|
-
if (
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
} else {
|
|
10
|
-
const config = JSON.parse(decodeURIComponent(configElement.textContent));
|
|
22
|
+
if (!Interact) {
|
|
23
|
+
console.error(\`Interact library is not loaded\`);
|
|
24
|
+
} else if (configElement) {
|
|
25
|
+
const config = JSON.parse(decodeURIComponent(configElement.textContent));
|
|
11
26
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
27
|
+
Interact.forceReducedMotion = ${isReducedMotion};
|
|
28
|
+
Interact.setup({
|
|
29
|
+
scrollOptionsGetter: function(){ return document.getElementById(\`site-root\`) },
|
|
30
|
+
allowA11yTriggers: true,
|
|
31
|
+
viewEnter: {
|
|
32
|
+
useSafeViewEnter: true,
|
|
33
|
+
},
|
|
34
|
+
});
|
|
20
35
|
|
|
21
|
-
|
|
22
|
-
}
|
|
36
|
+
Interact.create(config);
|
|
23
37
|
}
|
|
24
38
|
`
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import React from 'react'
|
|
2
|
+
import type { FunctionComponent } from 'react'
|
|
3
|
+
import type { InteractConfig } from '@wix/interact'
|
|
4
|
+
import { reviveScriptElement } from '../reviveScriptElement'
|
|
5
|
+
import { getV2InteractionsScriptContent } from './interactionsScriptContent'
|
|
6
|
+
|
|
7
|
+
const DANGEROUSLY_SET_INNER_HTML = ['dangerously', 'Set', 'Inner', 'HTML']
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* The config is written as raw markup, so a `</script` inside any string value would close the
|
|
11
|
+
* element early. `\u003c` is valid JSON that `JSON.parse` reads back as `<`.
|
|
12
|
+
*/
|
|
13
|
+
const serializeConfigForScriptElement = (config: InteractConfig): string =>
|
|
14
|
+
JSON.stringify(config).replace(/</g, '\\u003c')
|
|
15
|
+
|
|
16
|
+
export const createV2InteractionsScriptElement =
|
|
17
|
+
(config: InteractConfig, isReducedMotion: boolean, pageId: string): FunctionComponent =>
|
|
18
|
+
() => {
|
|
19
|
+
// This ID is also interpolated into raw module-script text. Percent-encoding removes
|
|
20
|
+
// script delimiters and template-literal syntax while keeping the ID deterministic.
|
|
21
|
+
const safePageId = encodeURIComponent(pageId)
|
|
22
|
+
const configId = `interactions-config-${safePageId}`
|
|
23
|
+
const interactionsScriptContent = getV2InteractionsScriptContent(config, configId, isReducedMotion)
|
|
24
|
+
const configProps = {
|
|
25
|
+
[DANGEROUSLY_SET_INNER_HTML.join('')]: { __html: serializeConfigForScriptElement(config) },
|
|
26
|
+
}
|
|
27
|
+
const loaderProps = {
|
|
28
|
+
[DANGEROUSLY_SET_INNER_HTML.join('')]: { __html: interactionsScriptContent },
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
return (
|
|
32
|
+
<>
|
|
33
|
+
<script type="application/json" id={configId} {...configProps} />
|
|
34
|
+
<script
|
|
35
|
+
type="module"
|
|
36
|
+
id={`interactions-loader-${safePageId}`}
|
|
37
|
+
ref={reviveScriptElement}
|
|
38
|
+
{...loaderProps}
|
|
39
|
+
/>
|
|
40
|
+
</>
|
|
41
|
+
)
|
|
42
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import type { InteractConfig, Interaction } from '@wix/interact'
|
|
2
|
+
|
|
3
|
+
const escapeCssAttr = (value: string): string => value.replace(/\\/g, '\\\\').replace(/"/g, '\\"')
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Mirrors `@wix/interact` `getSelector(interaction, { asCombinator: true, useFirstChild: true })`
|
|
7
|
+
* plus the `[data-interact-key="…"]` prefix from `CSSRuleToString`.
|
|
8
|
+
*/
|
|
9
|
+
const getInteractionSourceSelector = (
|
|
10
|
+
interaction: Pick<Interaction, 'key' | 'selector' | 'listContainer' | 'listItemSelector'>
|
|
11
|
+
): string => {
|
|
12
|
+
const { key, selector, listContainer, listItemSelector } = interaction
|
|
13
|
+
const attrSelector = `[data-interact-key="${escapeCssAttr(key)}"]`
|
|
14
|
+
|
|
15
|
+
if (listContainer) {
|
|
16
|
+
const itemFilter = listItemSelector ? ` > ${listItemSelector}` : ''
|
|
17
|
+
const inner = selector ? `${listContainer}${itemFilter} ${selector}` : `${listContainer}${itemFilter || ' > *'}`
|
|
18
|
+
|
|
19
|
+
return `${attrSelector} ${inner}`
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
if (selector) {
|
|
23
|
+
return `${attrSelector} ${selector}`
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
return `${attrSelector} > :first-child`
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** Emits `cursor: pointer` for click interaction triggers (`'activate' || 'click'`), one rule per unique source selector. */
|
|
30
|
+
export const getClickTriggerCss = (config: InteractConfig): string => {
|
|
31
|
+
const selectors = [
|
|
32
|
+
...new Set(
|
|
33
|
+
config.interactions
|
|
34
|
+
.filter(({ trigger }) => trigger === 'activate' || trigger === 'click')
|
|
35
|
+
.map(getInteractionSourceSelector)
|
|
36
|
+
),
|
|
37
|
+
]
|
|
38
|
+
|
|
39
|
+
if (!selectors.length) {
|
|
40
|
+
return ''
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return selectors.map(selector => `${selector} {\ncursor: pointer;\n}`).join('\n')
|
|
44
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { generate } from '@wix/interact'
|
|
2
|
+
import type { InteractConfig } from '@wix/interact'
|
|
3
|
+
import { getClickTriggerCss } from './clickTriggerCss'
|
|
4
|
+
import { registerConfigEffects } from './registerConfigEffects'
|
|
5
|
+
|
|
6
|
+
/** Register config-used presets, then emit animation FOUC CSS and click-trigger cursor rules. */
|
|
7
|
+
export const getV2ConfigCss = (config: InteractConfig): string => {
|
|
8
|
+
registerConfigEffects(config)
|
|
9
|
+
const animationCss = generate(config, true).trim()
|
|
10
|
+
const clickCss = getClickTriggerCss(config).trim()
|
|
11
|
+
|
|
12
|
+
return [animationCss, clickCss].filter(Boolean).join('\n')
|
|
13
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Head content is a raw HTML string, so anything tag-like in the generated CSS either closes the
|
|
3
|
+
* style element early or trips the head appender's script-tag guard, which throws. Nothing in CSS
|
|
4
|
+
* opens a tag: `<` appears only in media range queries, followed by a space or a digit. Cutting
|
|
5
|
+
* from the first `<` that could start a tag leaves valid CSS untouched.
|
|
6
|
+
*/
|
|
7
|
+
const sanitizeCssForStyleElement = (css: string): string => {
|
|
8
|
+
const tagIndex = css.search(/<[a-zA-Z/]/)
|
|
9
|
+
return tagIndex === -1 ? css : css.slice(0, tagIndex)
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export const getV2ConfigStyleHeadContent = (css: string): string => `<style>${sanitizeCssForStyleElement(css)}</style>`
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import type { InteractConfig } from '@wix/interact'
|
|
2
|
+
import { INTERACT_V2_IMPORT_URL, WIX_MOTION_PRESETS_IMPORT_URL } from './cdnUrls'
|
|
3
|
+
import { collectNamedEffectTypes } from './namedEffectTypes'
|
|
4
|
+
import { filterKnownPresetTypes } from './motionPresets'
|
|
5
|
+
|
|
6
|
+
const getPresetTypes = (config: InteractConfig): string[] => filterKnownPresetTypes(collectNamedEffectTypes(config))
|
|
7
|
+
|
|
8
|
+
const getModuleUrls = (presetTypes: string[]): string[] =>
|
|
9
|
+
presetTypes.length > 0 ? [INTERACT_V2_IMPORT_URL, WIX_MOTION_PRESETS_IMPORT_URL] : [INTERACT_V2_IMPORT_URL]
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* The loader reaches these modules through dynamic `import()`, which the preload scanner cannot
|
|
13
|
+
* see, so without the links the fetches only start once the loader runs. No `crossorigin`: a bare
|
|
14
|
+
* `modulepreload` fetches with the same credentials mode as a bare `import()`, and a mismatch
|
|
15
|
+
* would download each module twice.
|
|
16
|
+
*/
|
|
17
|
+
export const getV2ModulePreloadHeadContent = (config: InteractConfig): string =>
|
|
18
|
+
getModuleUrls(getPresetTypes(config))
|
|
19
|
+
.map(url => `<link rel="modulepreload" href="${url}">`)
|
|
20
|
+
.join('')
|
|
21
|
+
|
|
22
|
+
export const getV2InteractionsScriptContent = (
|
|
23
|
+
config: InteractConfig,
|
|
24
|
+
configId: string,
|
|
25
|
+
isReducedMotion: boolean
|
|
26
|
+
): string => {
|
|
27
|
+
const presetTypes = getPresetTypes(config)
|
|
28
|
+
const bindings = ['{ Interact }']
|
|
29
|
+
const imports = getModuleUrls(presetTypes).map(url => `import(\`${url}\`)`)
|
|
30
|
+
|
|
31
|
+
if (presetTypes.length > 0) {
|
|
32
|
+
bindings.push(`{ ${presetTypes.join(', ')} }`)
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const registerEffects =
|
|
36
|
+
presetTypes.length > 0 ? ` Interact.registerEffects({ ${presetTypes.join(', ')} })` : undefined
|
|
37
|
+
|
|
38
|
+
return [
|
|
39
|
+
`const configElement = document.getElementById(\`${configId}\`)`,
|
|
40
|
+
'if (configElement) {',
|
|
41
|
+
' const config = JSON.parse(configElement.textContent)',
|
|
42
|
+
` const [${bindings.join(', ')}] = await Promise.all([`,
|
|
43
|
+
...imports.map(importCall => ` ${importCall},`),
|
|
44
|
+
' ])',
|
|
45
|
+
registerEffects,
|
|
46
|
+
` Interact.forceReducedMotion = ${isReducedMotion}`,
|
|
47
|
+
' Interact.setup({',
|
|
48
|
+
' scrollOptionsGetter: function () {',
|
|
49
|
+
' return document.getElementById(`site-root`)',
|
|
50
|
+
' },',
|
|
51
|
+
' allowA11yTriggers: true,',
|
|
52
|
+
' viewEnter: {',
|
|
53
|
+
' useSafeViewEnter: true,',
|
|
54
|
+
' },',
|
|
55
|
+
' })',
|
|
56
|
+
' Interact.create(config)',
|
|
57
|
+
'}',
|
|
58
|
+
]
|
|
59
|
+
.filter((line): line is string => line !== undefined)
|
|
60
|
+
.join('\n')
|
|
61
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import * as allPresets from '@wix/wix-motion-presets'
|
|
2
|
+
|
|
3
|
+
type EffectModule = {
|
|
4
|
+
web: unknown
|
|
5
|
+
style?: unknown
|
|
6
|
+
getNames: unknown
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export const motionPresets: Record<string, EffectModule> = allPresets as unknown as Record<string, EffectModule>
|
|
10
|
+
|
|
11
|
+
const knownPresetTypes = new Set(Object.keys(motionPresets))
|
|
12
|
+
|
|
13
|
+
export const isKnownPresetType = (type: string): boolean => knownPresetTypes.has(type)
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Preset names become bare JavaScript identifiers in the client loader. Filtering against the
|
|
17
|
+
* installed package prevents unknown names from creating invalid source or a failed import.
|
|
18
|
+
*/
|
|
19
|
+
export const filterKnownPresetTypes = (types: string[]): string[] => {
|
|
20
|
+
const unknown = types.filter(type => !isKnownPresetType(type))
|
|
21
|
+
if (unknown.length > 0) {
|
|
22
|
+
console.warn(`Interactions: skipping unknown Wix motion preset type(s): ${unknown.join(', ')}`)
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
return types.filter(isKnownPresetType)
|
|
26
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import type { Effect, EffectRef, InteractConfig } from '@wix/interact'
|
|
2
|
+
|
|
3
|
+
type InteractionEffect = NonNullable<InteractConfig['interactions'][number]['effects']>[number]
|
|
4
|
+
type SequenceEffect = NonNullable<InteractConfig['sequences']>[string]['effects'][number]
|
|
5
|
+
type ConfigEffect = InteractionEffect | SequenceEffect
|
|
6
|
+
|
|
7
|
+
/** Prefer the effects map; fall back to an inline named effect when an effect id misses. */
|
|
8
|
+
const resolveEffect = (config: InteractConfig, effect: ConfigEffect): Effect | EffectRef => {
|
|
9
|
+
if (!effect.effectId) {
|
|
10
|
+
return effect
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
return config.effects?.[effect.effectId] ?? effect
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const readNamedEffectType = (effect: Effect | EffectRef): string | undefined => {
|
|
17
|
+
if (!('namedEffect' in effect) || !effect.namedEffect) {
|
|
18
|
+
return undefined
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const { type } = effect.namedEffect
|
|
22
|
+
return typeof type === 'string' ? type : undefined
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export const collectNamedEffectTypes = (config: InteractConfig): string[] => {
|
|
26
|
+
const types = new Set<string>()
|
|
27
|
+
|
|
28
|
+
const collectFromEffects = (effects: ConfigEffect[]) => {
|
|
29
|
+
for (const effect of effects) {
|
|
30
|
+
const type = readNamedEffectType(resolveEffect(config, effect))
|
|
31
|
+
if (type) {
|
|
32
|
+
types.add(type)
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
for (const interaction of config.interactions) {
|
|
38
|
+
collectFromEffects(interaction.effects ?? [])
|
|
39
|
+
|
|
40
|
+
for (const sequence of interaction.sequences ?? []) {
|
|
41
|
+
if ('effects' in sequence) {
|
|
42
|
+
collectFromEffects(sequence.effects)
|
|
43
|
+
continue
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const referencedSequence = config.sequences?.[sequence.sequenceId]
|
|
47
|
+
collectFromEffects(referencedSequence?.effects ?? [])
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
return [...types].sort()
|
|
52
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { Interact } from '@wix/interact'
|
|
2
|
+
import type { InteractConfig } from '@wix/interact'
|
|
3
|
+
import { collectNamedEffectTypes } from './namedEffectTypes'
|
|
4
|
+
import { filterKnownPresetTypes, motionPresets } from './motionPresets'
|
|
5
|
+
|
|
6
|
+
type PresetRegistry = Parameters<typeof Interact.registerEffects>[0]
|
|
7
|
+
type EffectModule = PresetRegistry[string]
|
|
8
|
+
|
|
9
|
+
// Interact's effect registry is process-global, so mirror it across repeated SSR renders.
|
|
10
|
+
const registeredTypes = new Set<string>()
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* A named effect only runs once its preset is registered, so every path that reaches
|
|
14
|
+
* `Interact.create` or `generate` has to register the config's presets first.
|
|
15
|
+
*/
|
|
16
|
+
export const registerConfigEffects = (config: InteractConfig) => {
|
|
17
|
+
const presetsToRegister: PresetRegistry = {}
|
|
18
|
+
|
|
19
|
+
for (const type of filterKnownPresetTypes(collectNamedEffectTypes(config))) {
|
|
20
|
+
if (registeredTypes.has(type)) {
|
|
21
|
+
continue
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
presetsToRegister[type] = motionPresets[type] as EffectModule
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
if (Object.keys(presetsToRegister).length > 0) {
|
|
28
|
+
Interact.registerEffects(presetsToRegister)
|
|
29
|
+
Object.keys(presetsToRegister).forEach(type => registeredTypes.add(type))
|
|
30
|
+
}
|
|
31
|
+
}
|