@wix/interact 1.74.0 → 1.76.0

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 (52) hide show
  1. package/README.md +33 -21
  2. package/dist/cjs/WixInteractElement.js +61 -17
  3. package/dist/cjs/WixInteractElement.js.map +1 -1
  4. package/dist/cjs/__tests__/interact.spec.js +761 -109
  5. package/dist/cjs/__tests__/interact.spec.js.map +1 -1
  6. package/dist/cjs/core/Interact.js +190 -0
  7. package/dist/cjs/core/Interact.js.map +1 -0
  8. package/dist/cjs/core/add.js +231 -0
  9. package/dist/cjs/core/add.js.map +1 -0
  10. package/dist/cjs/core/remove.js +37 -0
  11. package/dist/cjs/core/remove.js.map +1 -0
  12. package/dist/cjs/handlers/click.js +1 -1
  13. package/dist/cjs/handlers/click.js.map +1 -1
  14. package/dist/cjs/handlers/hover.js +1 -1
  15. package/dist/cjs/handlers/hover.js.map +1 -1
  16. package/dist/cjs/index.js +6 -4
  17. package/dist/cjs/index.js.map +1 -1
  18. package/dist/cjs/types.js.map +1 -1
  19. package/dist/cjs/utils.js +12 -10
  20. package/dist/cjs/utils.js.map +1 -1
  21. package/dist/esm/WixInteractElement.js +61 -17
  22. package/dist/esm/WixInteractElement.js.map +1 -1
  23. package/dist/esm/__tests__/interact.spec.js +743 -91
  24. package/dist/esm/__tests__/interact.spec.js.map +1 -1
  25. package/dist/esm/core/Interact.js +186 -0
  26. package/dist/esm/core/Interact.js.map +1 -0
  27. package/dist/esm/core/add.js +226 -0
  28. package/dist/esm/core/add.js.map +1 -0
  29. package/dist/esm/core/remove.js +32 -0
  30. package/dist/esm/core/remove.js.map +1 -0
  31. package/dist/esm/handlers/click.js +1 -1
  32. package/dist/esm/handlers/click.js.map +1 -1
  33. package/dist/esm/handlers/hover.js +1 -1
  34. package/dist/esm/handlers/hover.js.map +1 -1
  35. package/dist/esm/index.js +3 -1
  36. package/dist/esm/index.js.map +1 -1
  37. package/dist/esm/types.js.map +1 -1
  38. package/dist/esm/utils.js +12 -10
  39. package/dist/esm/utils.js.map +1 -1
  40. package/dist/types/WixInteractElement.d.ts +5 -2
  41. package/dist/types/core/Interact.d.ts +24 -0
  42. package/dist/types/core/add.d.ts +6 -0
  43. package/dist/types/core/remove.d.ts +5 -0
  44. package/dist/types/index.d.ts +3 -1
  45. package/dist/types/types.d.ts +22 -9
  46. package/dist/types/utils.d.ts +1 -1
  47. package/package.json +7 -6
  48. package/dist/cjs/interact.js +0 -308
  49. package/dist/cjs/interact.js.map +0 -1
  50. package/dist/esm/interact.js +0 -301
  51. package/dist/esm/interact.js.map +0 -1
  52. package/dist/types/interact.d.ts +0 -26
@@ -1 +1 @@
1
- {"version":3,"names":[],"sources":["../../src/types.ts"],"sourcesContent":["import type {\n NamedEffect,\n MotionKeyframeEffect,\n CustomEffect,\n RangeOffset,\n ScrubTransitionEasing,\n MotionAnimationOptions,\n} from '@wix/motion';\n\ndeclare global {\n // eslint-disable-next-line @typescript-eslint/no-namespace\n namespace JSX {\n interface IntrinsicElements {\n 'wix-interact-element': React.DetailedHTMLProps<\n React.HTMLAttributes<HTMLElement>,\n HTMLElement\n > & {\n 'data-wix-path'?: string;\n };\n }\n }\n}\n\nexport type TriggerType =\n | 'hover'\n | 'click'\n | 'viewEnter'\n | 'pageVisible'\n | 'animationEnd'\n | 'viewProgress'\n | 'pointerMove';\n\nexport type ViewEnterType = 'once' | 'repeat' | 'alternate';\n\nexport type TransitionMethod = 'add' | 'remove' | 'toggle' | 'clear';\n\nexport type StateParams = {\n method: TransitionMethod;\n};\n\nexport type PointerTriggerParams = {\n type?: ViewEnterType | 'state';\n};\n\nexport type ViewEnterParams = {\n type?: ViewEnterType;\n threshold?: number;\n inset?: string;\n};\n\nexport type PointerMoveParams = {\n hitArea?: 'root' | 'self';\n};\n\nexport type AnimationEndParams = {\n effectId: string;\n};\n\nexport type TriggerParams =\n | StateParams\n | PointerTriggerParams\n | ViewEnterParams\n | PointerMoveParams\n | AnimationEndParams;\n\ntype Fill = 'none' | 'forwards' | 'backwards' | 'both';\n\ntype EffectEffectProperty =\n | {\n keyframeEffect: MotionKeyframeEffect;\n }\n | {\n namedEffect: NamedEffect;\n }\n | {\n customEffect: CustomEffect;\n };\n\nexport type TimeEffect = {\n target?: string;\n duration: number;\n easing?: string;\n iterations?: number;\n alternate?: boolean;\n fill?: Fill;\n reversed?: boolean;\n delay?: number;\n effectId?: string;\n} & EffectEffectProperty;\n\nexport type ScrubEffect = {\n target?: string;\n easing?: string;\n iterations?: number;\n alternate?: boolean;\n fill?: Fill;\n reversed?: boolean;\n rangeStart?: RangeOffset;\n rangeEnd?: RangeOffset;\n centeredToTarget?: boolean;\n transitionDuration?: number;\n transitionDelay?: number;\n transitionEasing?: ScrubTransitionEasing;\n} & EffectEffectProperty;\n\nexport type TransitionOptions = {\n duration?: number;\n delay?: number;\n easing?: string;\n};\n\nexport type StyleProperty = {\n name: string;\n value: string;\n};\n\nexport type TransitionProperty = StyleProperty & TransitionOptions;\n\nexport type TransitionEffect = {\n target?: string;\n effectId?: string;\n} & {\n transition?: TransitionOptions & {\n styleProperties: StyleProperty[];\n };\n transitionProperties?: TransitionProperty[];\n};\n\nexport type EffectRef = {\n target?: string;\n effectId: string;\n conditions?: string[];\n};\n\nexport type Effect = (TimeEffect | ScrubEffect | TransitionEffect) & {\n conditions?: string[];\n};\n\nexport type Condition = {\n type: 'media' | 'container';\n predicate?: string;\n};\n\ntype InteractionTrigger = {\n source: string;\n trigger: TriggerType;\n params?: TriggerParams;\n conditions?: string[];\n};\n\nexport type Interaction = InteractionTrigger & {\n effects: ((Effect | EffectRef) & { interactionId?: string })[];\n};\n\nexport type InteractConfig = {\n effects: Record<string, Effect>;\n conditions?: Record<string, Condition>;\n interactions: Interaction[];\n};\n\nexport type AnimationOptions<T extends 'time' | 'scrub'> =\n MotionAnimationOptions<T> & EffectEffectProperty;\n\n/// ////////////////////////////////////////////////////////\n/// ////////////////////////////////////////////////////////\n/// ////////////////////////////////////////////////////////\n\nexport interface IWixInteractElement extends HTMLElement {\n _internals: (ElementInternals & { states: Set<string> }) | null;\n connected: boolean;\n sheet: CSSStyleSheet | null;\n connectedCallback(): void;\n disconnectedCallback(): void;\n connect(path?: string): void;\n renderStyle(cssText: string): void;\n toggleEffect(effectId: string, method: StateParams['method']): void;\n}\n\nexport type InteractionParamsTypes = {\n hover: StateParams | PointerTriggerParams;\n click: StateParams | PointerTriggerParams;\n viewEnter: ViewEnterParams;\n pageVisible: ViewEnterParams;\n animationEnd: AnimationEndParams;\n viewProgress: ViewEnterParams;\n pointerMove: PointerMoveParams;\n};\n\nexport type InteractionHandlerModule<T extends TriggerType> = {\n add: (\n source: HTMLElement,\n target: HTMLElement,\n effect: Effect,\n options: InteractionParamsTypes[T],\n reducedMotion?: boolean,\n ) => void;\n remove: (element: HTMLElement) => void;\n};\n\nexport type TriggerHandlerMap<T extends TriggerType> = {\n [K in T]: InteractionHandlerModule<K>;\n};\n\nexport type HandlerObject = {\n source: HTMLElement;\n target: HTMLElement;\n cleanup: () => void;\n handler?: () => void;\n};\n\nexport type HandlerObjectMap = WeakMap<HTMLElement, Set<HandlerObject>>;\n\nexport type InteractCache = {\n effects: {\n [effectId: string]: Effect;\n };\n conditions: {\n [conditionId: string]: Condition;\n };\n interactions: {\n [path: string]: {\n triggers: Interaction[];\n effects: Record<\n string,\n (InteractionTrigger & { effect: Effect | EffectRef })[]\n >;\n interactionIds: Set<string>;\n };\n };\n};\n\nexport type CreateTransitionCSSParams = {\n path: string;\n effectId: string;\n transition?: TransitionEffect['transition'];\n properties?: TransitionProperty[];\n childSelector?: string;\n};\n"],"mappings":"","ignoreList":[]}
1
+ {"version":3,"names":[],"sources":["../../src/types.ts"],"sourcesContent":["import type {\n NamedEffect,\n CustomEffect,\n RangeOffset,\n ScrubTransitionEasing,\n MotionAnimationOptions,\n} from '@wix/motion';\n\ndeclare global {\n // eslint-disable-next-line @typescript-eslint/no-namespace\n namespace JSX {\n interface IntrinsicElements {\n 'wix-interact-element': React.DetailedHTMLProps<\n React.HTMLAttributes<HTMLElement>,\n HTMLElement\n > & {\n 'data-wix-path'?: string;\n };\n }\n }\n}\n\nexport type TriggerType =\n | 'hover'\n | 'click'\n | 'viewEnter'\n | 'pageVisible'\n | 'animationEnd'\n | 'viewProgress'\n | 'pointerMove';\n\nexport type ViewEnterType = 'once' | 'repeat' | 'alternate';\n\nexport type TransitionMethod = 'add' | 'remove' | 'toggle' | 'clear';\n\nexport type StateParams = {\n method: TransitionMethod;\n};\n\nexport type PointerTriggerParams = {\n type?: ViewEnterType | 'state';\n};\n\nexport type ViewEnterParams = {\n type?: ViewEnterType;\n threshold?: number;\n inset?: string;\n};\n\nexport type PointerMoveParams = {\n hitArea?: 'root' | 'self';\n};\n\nexport type AnimationEndParams = {\n effectId: string;\n};\n\nexport type TriggerParams =\n | StateParams\n | PointerTriggerParams\n | ViewEnterParams\n | PointerMoveParams\n | AnimationEndParams;\n\ntype Fill = 'none' | 'forwards' | 'backwards' | 'both';\n\ntype MotionKeyframeEffect = {\n name: string;\n keyframes: Keyframe[];\n};\n\ntype EffectEffectProperty =\n | {\n keyframeEffect: MotionKeyframeEffect;\n }\n | {\n namedEffect: NamedEffect;\n }\n | {\n customEffect: CustomEffect;\n };\n\nexport type TimeEffect = {\n key?: string;\n duration: number;\n easing?: string;\n iterations?: number;\n alternate?: boolean;\n fill?: Fill;\n reversed?: boolean;\n delay?: number;\n effectId?: string;\n} & EffectEffectProperty;\n\nexport type ScrubEffect = {\n key?: string;\n easing?: string;\n iterations?: number;\n alternate?: boolean;\n fill?: Fill;\n reversed?: boolean;\n rangeStart?: RangeOffset;\n rangeEnd?: RangeOffset;\n centeredToTarget?: boolean;\n transitionDuration?: number;\n transitionDelay?: number;\n transitionEasing?: ScrubTransitionEasing;\n} & EffectEffectProperty;\n\nexport type TransitionOptions = {\n duration?: number;\n delay?: number;\n easing?: string;\n};\n\nexport type StyleProperty = {\n name: string;\n value: string;\n};\n\nexport type TransitionProperty = StyleProperty & TransitionOptions;\n\nexport type TransitionEffect = {\n key?: string;\n effectId?: string;\n} & {\n transition?: TransitionOptions & {\n styleProperties: StyleProperty[];\n };\n transitionProperties?: TransitionProperty[];\n};\n\nexport type EffectRef = {\n key?: string;\n effectId: string;\n listContainer?: string;\n conditions?: string[];\n selector?: string;\n};\n\nexport type Effect = (TimeEffect | ScrubEffect | TransitionEffect) & {\n listContainer?: string;\n conditions?: string[];\n selector?: string;\n};\n\nexport type Condition = {\n type: 'media' | 'container';\n predicate?: string;\n};\n\nexport type InteractionTrigger = {\n key: string;\n listContainer?: string;\n trigger: TriggerType;\n params?: TriggerParams;\n conditions?: string[];\n selector?: string;\n};\n\nexport type Interaction = InteractionTrigger & {\n effects: ((Effect | EffectRef) & { interactionId?: string })[];\n};\n\nexport type InteractConfig = {\n effects: Record<string, Effect>;\n conditions?: Record<string, Condition>;\n interactions: Interaction[];\n};\n\nexport type AnimationOptions<T extends 'time' | 'scrub'> =\n MotionAnimationOptions<T> & EffectEffectProperty;\n\n/// ////////////////////////////////////////////////////////\n/// ////////////////////////////////////////////////////////\n/// ////////////////////////////////////////////////////////\n\nexport interface IWixInteractElement extends HTMLElement {\n _internals: (ElementInternals & { states: Set<string> }) | null;\n connected: boolean;\n sheet: CSSStyleSheet | null;\n _observers: WeakMap<HTMLElement, MutationObserver>;\n connectedCallback(): void;\n disconnectedCallback(): void;\n connect(path?: string): void;\n renderStyle(cssRules: string[]): void;\n toggleEffect(effectId: string, method: StateParams['method']): void;\n watchChildList(listContainer: string): void;\n}\n\nexport type InteractionParamsTypes = {\n hover: StateParams | PointerTriggerParams;\n click: StateParams | PointerTriggerParams;\n viewEnter: ViewEnterParams;\n pageVisible: ViewEnterParams;\n animationEnd: AnimationEndParams;\n viewProgress: ViewEnterParams;\n pointerMove: PointerMoveParams;\n};\n\nexport type InteractionHandlerModule<T extends TriggerType> = {\n add: (\n source: HTMLElement,\n target: HTMLElement,\n effect: Effect,\n options: InteractionParamsTypes[T],\n reducedMotion?: boolean,\n ) => void;\n remove: (element: HTMLElement) => void;\n};\n\nexport type TriggerHandlerMap<T extends TriggerType> = {\n [K in T]: InteractionHandlerModule<K>;\n};\n\nexport type HandlerObject = {\n source: HTMLElement;\n target: HTMLElement;\n cleanup: () => void;\n handler?: () => void;\n};\n\nexport type HandlerObjectMap = WeakMap<HTMLElement, Set<HandlerObject>>;\n\nexport type InteractCache = {\n effects: {\n [effectId: string]: Effect;\n };\n conditions: {\n [conditionId: string]: Condition;\n };\n interactions: {\n [path: string]: {\n triggers: Interaction[];\n effects: Record<\n string,\n (InteractionTrigger & { effect: Effect | EffectRef })[]\n >;\n interactionIds: Set<string>;\n selectors: Set<string>;\n };\n };\n};\n\nexport type CreateTransitionCSSParams = {\n key: string;\n effectId: string;\n transition?: TransitionEffect['transition'];\n properties?: TransitionProperty[];\n childSelector?: string;\n};\n"],"mappings":"","ignoreList":[]}
package/dist/esm/utils.js CHANGED
@@ -6,11 +6,11 @@ export function generateId() {
6
6
  export function createTransitionCSS(_ref) {
7
7
  var _properties2;
8
8
  let {
9
- path,
9
+ key,
10
10
  effectId,
11
11
  transition,
12
12
  properties,
13
- childSelector = ':first-child'
13
+ childSelector = '> :first-child'
14
14
  } = _ref;
15
15
  let transitions = [];
16
16
  if (transition != null && transition.styleProperties) {
@@ -34,16 +34,18 @@ export function createTransitionCSS(_ref) {
34
34
  transitions = ((_properties = properties) == null ? void 0 : _properties.filter(property => property.duration).map(property => `${property.name} ${property.duration}ms ${getEasing(property.easing) || 'ease'}${property.delay ? ` ${property.delay}ms` : ''}`)) || [];
35
35
  }
36
36
  const styleProperties = ((_properties2 = properties) == null ? void 0 : _properties2.map(property => `${property.name}: ${property.value};`)) || [];
37
- const escapedPath = path.replace(/"/g, "'");
38
- return `
39
- ${transitions.length ? `@media (prefers-reduced-motion: no-preference) { [data-wix-path="${escapedPath}"] > ${childSelector} {
40
- transition: ${transitions.join(', ')};
41
- } }` : ''}
42
- :is(:state(${effectId}), :--${effectId}) > ${childSelector},
43
- [data-wix-interact-effect~="${effectId}"] > ${childSelector} {
37
+ const escapedKey = key.replace(/"/g, "'");
38
+ const result = [`:is(:state(${effectId}), :--${effectId}) ${childSelector},
39
+ [data-wix-interact-effect~="${effectId}"] ${childSelector} {
44
40
  ${styleProperties.join(`
45
41
  `)}
46
- }`;
42
+ }`];
43
+ if (transitions.length) {
44
+ result.push(`@media (prefers-reduced-motion: no-preference) { [data-wix-path="${escapedKey}"] ${childSelector} {
45
+ transition: ${transitions.join(', ')};
46
+ } }`);
47
+ }
48
+ return result;
47
49
  }
48
50
  export function getMediaQuery(conditionNames, conditions) {
49
51
  const conditionContent = (conditionNames || []).filter(conditionName => {
@@ -1 +1 @@
1
- {"version":3,"names":["getEasing","generateId","replace","c","String","fromCharCode","crypto","getRandomValues","Uint8Array","createTransitionCSS","_ref","_properties2","path","effectId","transition","properties","childSelector","transitions","styleProperties","duration","easing","delay","hasCustomPropertiesTransition","some","styleProperty","name","startsWith","map","_properties","filter","property","value","escapedPath","length","join","getMediaQuery","conditionNames","conditions","conditionContent","conditionName","_conditions$condition","type","predicate","condition","mql","window","matchMedia"],"sources":["../../src/utils.ts"],"sourcesContent":["import { getEasing } from '@wix/motion';\nimport type { Condition, CreateTransitionCSSParams } from './types';\n\nexport function generateId() {\n return 'wi-12343210'.replace(\n /\\d/g,\n (c) =>\n String.fromCharCode(\n (+c ^\n (crypto.getRandomValues(new Uint8Array(1))[0] & (15 >> (+c / 4)))) +\n 97,\n ), // 97 for \"a\"\n );\n}\n\nexport function createTransitionCSS({\n path,\n effectId,\n transition,\n properties,\n childSelector = ':first-child',\n}: CreateTransitionCSSParams): string {\n let transitions: string[] = [];\n\n if (transition?.styleProperties) {\n const { duration, easing, delay } = transition;\n\n if (duration) {\n const hasCustomPropertiesTransition = transition.styleProperties.some(\n (styleProperty) => styleProperty.name.startsWith('--'),\n );\n\n if (hasCustomPropertiesTransition) {\n // If there are custom properties in the transition, we need to fall back to Viewer's legacy implementation\n transitions = [\n `all ${duration}ms ${getEasing(easing || 'ease')}${\n delay ? ` ${delay}ms` : ''\n }`,\n 'visibility 0s',\n ];\n } else {\n transitions = transition.styleProperties.map(\n (styleProperty) =>\n `${styleProperty.name} ${duration}ms ${getEasing(\n easing || 'ease',\n )}${delay ? ` ${delay}ms` : ''}`,\n );\n }\n }\n\n properties = transition.styleProperties;\n } else {\n transitions =\n properties\n ?.filter((property) => property.duration)\n .map(\n (property) =>\n `${property.name} ${property.duration}ms ${\n getEasing(property.easing) || 'ease'\n }${property.delay ? ` ${property.delay}ms` : ''}`,\n ) || [];\n }\n\n const styleProperties =\n properties?.map((property) => `${property.name}: ${property.value};`) || [];\n const escapedPath = path.replace(/\"/g, \"'\");\n\n return `\n ${\n transitions.length\n ? `@media (prefers-reduced-motion: no-preference) { [data-wix-path=\"${escapedPath}\"] > ${childSelector} {\n transition: ${transitions.join(', ')};\n } }`\n : ''\n }\n :is(:state(${effectId}), :--${effectId}) > ${childSelector},\n [data-wix-interact-effect~=\"${effectId}\"] > ${childSelector} {\n ${styleProperties.join(`\n `)}\n }`;\n}\n\nexport function getMediaQuery(\n conditionNames: string[] | undefined,\n conditions: Record<string, Condition>,\n) {\n const conditionContent = (conditionNames || [])\n .filter((conditionName) => {\n return (\n conditions[conditionName]?.type === 'media' &&\n conditions[conditionName].predicate\n );\n })\n .map((conditionName) => {\n return conditions[conditionName].predicate;\n })\n .join(') and (');\n\n const condition = conditionContent && `(${conditionContent})`;\n const mql = condition && window.matchMedia(condition);\n\n if (mql) {\n return mql;\n }\n}\n"],"mappings":"AAAA,SAASA,SAAS,QAAQ,aAAa;AAGvC,OAAO,SAASC,UAAUA,CAAA,EAAG;EAC3B,OAAO,aAAa,CAACC,OAAO,CAC1B,KAAK,EACJC,CAAC,IACAC,MAAM,CAACC,YAAY,CACjB,CAAC,CAACF,CAAC,GACAG,MAAM,CAACC,eAAe,CAAC,IAAIC,UAAU,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,GAAI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,IAAI,CAAC,SAAS,CAAC;EAElB,MAAMS,SAAS,GAAGL,gBAAgB,IAAI,IAAIA,gBAAgB,GAAG;EAC7D,MAAMM,GAAG,GAAGD,SAAS,IAAIE,MAAM,CAACC,UAAU,CAACH,SAAS,CAAC;EAErD,IAAIC,GAAG,EAAE;IACP,OAAOA,GAAG;EACZ;AACF","ignoreList":[]}
1
+ {"version":3,"names":["getEasing","generateId","replace","c","String","fromCharCode","crypto","getRandomValues","Uint8Array","createTransitionCSS","_ref","_properties2","key","effectId","transition","properties","childSelector","transitions","styleProperties","duration","easing","delay","hasCustomPropertiesTransition","some","styleProperty","name","startsWith","map","_properties","filter","property","value","escapedKey","result","join","length","push","getMediaQuery","conditionNames","conditions","conditionContent","conditionName","_conditions$condition","type","predicate","condition","mql","window","matchMedia"],"sources":["../../src/utils.ts"],"sourcesContent":["import { getEasing } from '@wix/motion';\nimport type { Condition, CreateTransitionCSSParams } from './types';\n\nexport function generateId() {\n return 'wi-12343210'.replace(\n /\\d/g,\n (c) =>\n String.fromCharCode(\n (+c ^\n (crypto.getRandomValues(new Uint8Array(1))[0] & (15 >> (+c / 4)))) +\n 97,\n ), // 97 for \"a\"\n );\n}\n\nexport function createTransitionCSS({\n key,\n effectId,\n transition,\n properties,\n childSelector = '> :first-child',\n}: CreateTransitionCSSParams): string[] {\n let transitions: string[] = [];\n\n if (transition?.styleProperties) {\n const { duration, easing, delay } = transition;\n\n if (duration) {\n const hasCustomPropertiesTransition = transition.styleProperties.some(\n (styleProperty) => styleProperty.name.startsWith('--'),\n );\n\n if (hasCustomPropertiesTransition) {\n // If there are custom properties in the transition, we need to fall back to Viewer's legacy implementation\n transitions = [\n `all ${duration}ms ${getEasing(easing || 'ease')}${\n delay ? ` ${delay}ms` : ''\n }`,\n 'visibility 0s',\n ];\n } else {\n transitions = transition.styleProperties.map(\n (styleProperty) =>\n `${styleProperty.name} ${duration}ms ${getEasing(\n easing || 'ease',\n )}${delay ? ` ${delay}ms` : ''}`,\n );\n }\n }\n\n properties = transition.styleProperties;\n } else {\n transitions =\n properties\n ?.filter((property) => property.duration)\n .map(\n (property) =>\n `${property.name} ${property.duration}ms ${\n getEasing(property.easing) || 'ease'\n }${property.delay ? ` ${property.delay}ms` : ''}`,\n ) || [];\n }\n\n const styleProperties =\n properties?.map((property) => `${property.name}: ${property.value};`) || [];\n const escapedKey = key.replace(/\"/g, \"'\");\n\n const result = [\n `:is(:state(${effectId}), :--${effectId}) ${childSelector},\n [data-wix-interact-effect~=\"${effectId}\"] ${childSelector} {\n ${styleProperties.join(`\n `)}\n }`,\n ];\n\n if (transitions.length) {\n result.push(`@media (prefers-reduced-motion: no-preference) { [data-wix-path=\"${escapedKey}\"] ${childSelector} {\n transition: ${transitions.join(', ')};\n } }`);\n }\n return result;\n}\n\nexport function getMediaQuery(\n conditionNames: string[] | undefined,\n conditions: Record<string, Condition>,\n) {\n const conditionContent = (conditionNames || [])\n .filter((conditionName) => {\n return (\n conditions[conditionName]?.type === 'media' &&\n conditions[conditionName].predicate\n );\n })\n .map((conditionName) => {\n return conditions[conditionName].predicate;\n })\n .join(') and (');\n\n const condition = conditionContent && `(${conditionContent})`;\n const mql = condition && window.matchMedia(condition);\n\n if (mql) {\n return mql;\n }\n}\n"],"mappings":"AAAA,SAASA,SAAS,QAAQ,aAAa;AAGvC,OAAO,SAASC,UAAUA,CAAA,EAAG;EAC3B,OAAO,aAAa,CAACC,OAAO,CAC1B,KAAK,EACJC,CAAC,IACAC,MAAM,CAACC,YAAY,CACjB,CAAC,CAACF,CAAC,GACAG,MAAM,CAACC,eAAe,CAAC,IAAIC,UAAU,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,GAAI,EAAE,IAAK,CAACL,CAAC,GAAG,CAAI,IACjE,EACJ,CAAC,CAAE;EACP,CAAC;AACH;AAEA,OAAO,SAASM,mBAAmBA,CAAAC,IAAA,EAMK;EAAA,IAAAC,YAAA;EAAA,IANJ;IAClCC,GAAG;IACHC,QAAQ;IACRC,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,SAAS,IAAIE,MAAM,CAACC,UAAU,CAACH,SAAS,CAAC;EAErD,IAAIC,GAAG,EAAE;IACP,OAAOA,GAAG;EACZ;AACF","ignoreList":[]}
@@ -7,11 +7,14 @@ export declare function getWixInteractElement(): {
7
7
  }) | null;
8
8
  connected: boolean;
9
9
  sheet: CSSStyleSheet | null;
10
+ _observers: WeakMap<HTMLElement, MutationObserver>;
10
11
  connectedCallback(): void;
11
12
  disconnectedCallback(): void;
12
- connect(path?: string): void;
13
- renderStyle(cssText: string): void;
13
+ connect(key?: string): void;
14
+ renderStyle(cssRules: string[]): void;
14
15
  toggleEffect(effectId: string, method: StateParams['method']): void;
16
+ watchChildList(listContainer: string): void;
17
+ _childListChangeHandler(listContainer: string, entries: MutationRecord[]): void;
15
18
  accessKey: string;
16
19
  readonly accessKeyLabel: string;
17
20
  autocapitalize: string;
@@ -0,0 +1,24 @@
1
+ import { InteractCache, IWixInteractElement, InteractConfig, Effect, Interaction } from '../types';
2
+ export declare class Interact {
3
+ dataCache: InteractCache;
4
+ addedInteractions: {
5
+ [interactionId: string]: boolean;
6
+ };
7
+ listInteractionsCache: {
8
+ [listContainer: string]: {
9
+ [interactionId: string]: boolean;
10
+ };
11
+ };
12
+ static forceReducedMotion: boolean;
13
+ static instances: Interact[];
14
+ static elementCache: Map<string, IWixInteractElement>;
15
+ constructor();
16
+ init(config: InteractConfig): void;
17
+ has(key: string): boolean;
18
+ clearInteractionStateForKey(key: string): void;
19
+ static create(config: InteractConfig): Interact;
20
+ static getInstance(key: string): Interact | undefined;
21
+ static getElement(key: string): IWixInteractElement | undefined;
22
+ static setElement(key: string, element: IWixInteractElement): void;
23
+ }
24
+ export declare function getSelector(d: Interaction | Effect, asCombinator?: boolean): string;
@@ -0,0 +1,6 @@
1
+ import type { IWixInteractElement } from '../types';
2
+ /**
3
+ * Adds all events and effects to an element based on config
4
+ */
5
+ export declare function add(element: IWixInteractElement, key: string): boolean;
6
+ export declare function addListItems(root: IWixInteractElement, key: string, listContainer: string, elements: HTMLElement[]): void;
@@ -0,0 +1,5 @@
1
+ /**
2
+ * Removes all events and effects from an element based on config
3
+ */
4
+ export declare function remove(key: string): void;
5
+ export declare function removeListItems(elements: HTMLElement[]): void;
@@ -1,2 +1,4 @@
1
- export { Interact, add, remove } from './interact';
1
+ export { Interact } from './core/Interact';
2
+ export { add } from './core/add';
3
+ export { remove } from './core/remove';
2
4
  export * from './types';
@@ -1,5 +1,5 @@
1
1
  /// <reference types="react" />
2
- import type { NamedEffect, MotionKeyframeEffect, CustomEffect, RangeOffset, ScrubTransitionEasing, MotionAnimationOptions } from '@wix/motion';
2
+ import type { NamedEffect, CustomEffect, RangeOffset, ScrubTransitionEasing, MotionAnimationOptions } from '@wix/motion';
3
3
  declare global {
4
4
  namespace JSX {
5
5
  interface IntrinsicElements {
@@ -31,6 +31,10 @@ export type AnimationEndParams = {
31
31
  };
32
32
  export type TriggerParams = StateParams | PointerTriggerParams | ViewEnterParams | PointerMoveParams | AnimationEndParams;
33
33
  type Fill = 'none' | 'forwards' | 'backwards' | 'both';
34
+ type MotionKeyframeEffect = {
35
+ name: string;
36
+ keyframes: Keyframe[];
37
+ };
34
38
  type EffectEffectProperty = {
35
39
  keyframeEffect: MotionKeyframeEffect;
36
40
  } | {
@@ -39,7 +43,7 @@ type EffectEffectProperty = {
39
43
  customEffect: CustomEffect;
40
44
  };
41
45
  export type TimeEffect = {
42
- target?: string;
46
+ key?: string;
43
47
  duration: number;
44
48
  easing?: string;
45
49
  iterations?: number;
@@ -50,7 +54,7 @@ export type TimeEffect = {
50
54
  effectId?: string;
51
55
  } & EffectEffectProperty;
52
56
  export type ScrubEffect = {
53
- target?: string;
57
+ key?: string;
54
58
  easing?: string;
55
59
  iterations?: number;
56
60
  alternate?: boolean;
@@ -74,7 +78,7 @@ export type StyleProperty = {
74
78
  };
75
79
  export type TransitionProperty = StyleProperty & TransitionOptions;
76
80
  export type TransitionEffect = {
77
- target?: string;
81
+ key?: string;
78
82
  effectId?: string;
79
83
  } & {
80
84
  transition?: TransitionOptions & {
@@ -83,22 +87,28 @@ export type TransitionEffect = {
83
87
  transitionProperties?: TransitionProperty[];
84
88
  };
85
89
  export type EffectRef = {
86
- target?: string;
90
+ key?: string;
87
91
  effectId: string;
92
+ listContainer?: string;
88
93
  conditions?: string[];
94
+ selector?: string;
89
95
  };
90
96
  export type Effect = (TimeEffect | ScrubEffect | TransitionEffect) & {
97
+ listContainer?: string;
91
98
  conditions?: string[];
99
+ selector?: string;
92
100
  };
93
101
  export type Condition = {
94
102
  type: 'media' | 'container';
95
103
  predicate?: string;
96
104
  };
97
- type InteractionTrigger = {
98
- source: string;
105
+ export type InteractionTrigger = {
106
+ key: string;
107
+ listContainer?: string;
99
108
  trigger: TriggerType;
100
109
  params?: TriggerParams;
101
110
  conditions?: string[];
111
+ selector?: string;
102
112
  };
103
113
  export type Interaction = InteractionTrigger & {
104
114
  effects: ((Effect | EffectRef) & {
@@ -117,11 +127,13 @@ export interface IWixInteractElement extends HTMLElement {
117
127
  }) | null;
118
128
  connected: boolean;
119
129
  sheet: CSSStyleSheet | null;
130
+ _observers: WeakMap<HTMLElement, MutationObserver>;
120
131
  connectedCallback(): void;
121
132
  disconnectedCallback(): void;
122
133
  connect(path?: string): void;
123
- renderStyle(cssText: string): void;
134
+ renderStyle(cssRules: string[]): void;
124
135
  toggleEffect(effectId: string, method: StateParams['method']): void;
136
+ watchChildList(listContainer: string): void;
125
137
  }
126
138
  export type InteractionParamsTypes = {
127
139
  hover: StateParams | PointerTriggerParams;
@@ -160,11 +172,12 @@ export type InteractCache = {
160
172
  effect: Effect | EffectRef;
161
173
  })[]>;
162
174
  interactionIds: Set<string>;
175
+ selectors: Set<string>;
163
176
  };
164
177
  };
165
178
  };
166
179
  export type CreateTransitionCSSParams = {
167
- path: string;
180
+ key: string;
168
181
  effectId: string;
169
182
  transition?: TransitionEffect['transition'];
170
183
  properties?: TransitionProperty[];
@@ -1,4 +1,4 @@
1
1
  import type { Condition, CreateTransitionCSSParams } from './types';
2
2
  export declare function generateId(): string;
3
- export declare function createTransitionCSS({ path, effectId, transition, properties, childSelector, }: CreateTransitionCSSParams): string;
3
+ export declare function createTransitionCSS({ key, effectId, transition, properties, childSelector, }: CreateTransitionCSSParams): string[];
4
4
  export declare function getMediaQuery(conditionNames: string[] | undefined, conditions: Record<string, Condition>): MediaQueryList | undefined;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wix/interact",
3
- "version": "1.74.0",
3
+ "version": "1.76.0",
4
4
  "author": {
5
5
  "name": "wow!Team",
6
6
  "email": "wow-dev@wix.com"
@@ -23,6 +23,7 @@
23
23
  "build": "yoshi-library build",
24
24
  "start": "yoshi-library start",
25
25
  "test": "yoshi-library test",
26
+ "test:coverage": "yoshi-library test --coverage",
26
27
  "playground": "yarn workspace @wix/interact-playground dev",
27
28
  "lint": "yoshi-library lint"
28
29
  },
@@ -31,16 +32,16 @@
31
32
  },
32
33
  "dependencies": {
33
34
  "@babel/runtime": "^7.26.0",
34
- "@wix/motion": "1.606.0",
35
+ "@wix/motion": "1.616.0",
35
36
  "fizban": "^0.7.0",
36
37
  "kuliso": "^0.4.13"
37
38
  },
38
39
  "devDependencies": {
39
40
  "@types/jest": "^27.5.2",
40
41
  "@types/node": "^16.18.123",
41
- "@wix/eslint-config-yoshi": "^6.158.0",
42
- "@wix/jest-yoshi-preset": "^6.158.0",
43
- "@wix/yoshi-flow-library": "^6.158.0",
42
+ "@wix/eslint-config-yoshi": "^6.160.0",
43
+ "@wix/jest-yoshi-preset": "^6.160.0",
44
+ "@wix/yoshi-flow-library": "^6.160.0",
44
45
  "ts-jest": "^29.2.5",
45
46
  "typescript": "~4.9.5"
46
47
  },
@@ -67,5 +68,5 @@
67
68
  "wallaby": {
68
69
  "autoDetect": true
69
70
  },
70
- "falconPackageHash": "79b58e789386f0d9344f376b1779f782a5e2e32fe7e6a2aeade5aa18"
71
+ "falconPackageHash": "29f21fca1d7aa915ccd4ea1064cb917658e67ed2e333594929a7a111"
71
72
  }
@@ -1,308 +0,0 @@
1
- "use strict";
2
-
3
- var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
- exports.__esModule = true;
5
- exports.Interact = void 0;
6
- exports.add = add;
7
- exports.remove = remove;
8
- var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
9
- var _WixInteractElement = require("./WixInteractElement");
10
- var _utils = require("./utils");
11
- var _handlers = _interopRequireDefault(require("./handlers"));
12
- var _Interact;
13
- function registerWixInteractElement() {
14
- if (!customElements.get('wix-interact-element')) {
15
- const wixInteractElement = (0, _WixInteractElement.getWixInteractElement)();
16
- customElements.define('wix-interact-element', wixInteractElement);
17
- return true;
18
- }
19
- return false;
20
- }
21
- class Interact {
22
- constructor() {
23
- (0, _defineProperty2.default)(this, "dataCache", void 0);
24
- (0, _defineProperty2.default)(this, "addedInteractions", void 0);
25
- this.dataCache = {
26
- effects: {},
27
- conditions: {},
28
- interactions: {}
29
- };
30
- this.addedInteractions = {};
31
- }
32
- init(config) {
33
- if (typeof window === 'undefined' || !window.customElements) {
34
- return;
35
- }
36
- this.dataCache = parseConfig(config);
37
- const didRegister = registerWixInteractElement();
38
- if (!didRegister) {
39
- Interact.elementCache.forEach((element, path) => element.connect(path));
40
- }
41
- }
42
- has(path) {
43
- return !!this.dataCache.interactions[path];
44
- }
45
- clearInteractionStateForPath(path) {
46
- var _this$dataCache$inter;
47
- const interactionIds = ((_this$dataCache$inter = this.dataCache.interactions[path]) == null ? void 0 : _this$dataCache$inter.interactionIds) || [];
48
- interactionIds.forEach(interactionId => {
49
- // eslint-disable-next-line @typescript-eslint/no-dynamic-delete
50
- delete this.addedInteractions[interactionId];
51
- });
52
- }
53
- static create(config) {
54
- const instance = new Interact();
55
- Interact.instances.push(instance);
56
- instance.init(config);
57
- return instance;
58
- }
59
- static getInstance(path) {
60
- return Interact.instances.find(instance => instance.has(path));
61
- }
62
- static getElement(path) {
63
- return Interact.elementCache.get(path);
64
- }
65
- static setElement(path, element) {
66
- Interact.elementCache.set(path, element);
67
- }
68
- }
69
- exports.Interact = Interact;
70
- _Interact = Interact;
71
- (0, _defineProperty2.default)(Interact, "forceReducedMotion", false);
72
- (0, _defineProperty2.default)(Interact, "instances", []);
73
- (0, _defineProperty2.default)(Interact, "elementCache", new Map());
74
- let interactionIdCounter = 0;
75
-
76
- /**
77
- * Parses the config object and caches interactions, effects, and conditions
78
- */
79
- function parseConfig(config) {
80
- var _config$interactions;
81
- const conditions = config.conditions || {};
82
- const interactions = {};
83
- (_config$interactions = config.interactions) == null || _config$interactions.forEach(interaction => {
84
- var _interaction$effects, _interaction$effects2;
85
- const source = interaction.source;
86
- const interactionIdx = ++interactionIdCounter;
87
- if (!source) {
88
- console.error(`Interaction ${interactionIdx} is missing a source element.`);
89
- return;
90
- }
91
- if (!interactions[source]) {
92
- interactions[source] = {
93
- triggers: [],
94
- effects: {},
95
- interactionIds: new Set()
96
- };
97
- }
98
-
99
- /*
100
- * Cache interaction trigger by source element
101
- */
102
- (_interaction$effects = interaction.effects) == null || _interaction$effects.reverse(); // reverse to ensure the first effect is the one that will be applied first
103
-
104
- interactions[source].triggers.push(interaction);
105
- (_interaction$effects2 = interaction.effects) == null || _interaction$effects2.forEach(effect => {
106
- /*
107
- * Target cascade order is the first of:
108
- * -> Config.interactions.effects.effect.target
109
- * -> Config.effects.effect.target
110
- * -> Config.interactions.interaction.source
111
- */
112
- let target = effect.target;
113
- if (!target && effect.effectId) {
114
- const referencedEffect = config.effects[effect.effectId];
115
- if (referencedEffect) {
116
- target = referencedEffect.target;
117
- }
118
- }
119
- if (!effect.effectId) {
120
- effect.effectId = (0, _utils.generateId)();
121
- }
122
-
123
- // if no target is specified, use the source element as the target
124
- target = target || source;
125
- effect.target = target;
126
- const effectId = effect.effectId;
127
- const interactionId = `${target}::${effectId}::${interactionIdx}`;
128
- effect.interactionId = interactionId;
129
- interactions[source].interactionIds.add(interactionId);
130
- if (target === source) {
131
- // if target is the source element, no need to add an interaction to `effects`
132
- return;
133
- }
134
-
135
- /*
136
- * Cache interaction effect by target element
137
- */
138
- if (!interactions[target]) {
139
- interactions[target] = {
140
- triggers: [],
141
- effects: {
142
- [interactionId]: []
143
- },
144
- interactionIds: new Set()
145
- };
146
- } else if (!interactions[target].effects[interactionId]) {
147
- interactions[target].effects[interactionId] = [];
148
- interactions[target].interactionIds.add(interactionId);
149
- }
150
- const {
151
- effects,
152
- ...rest
153
- } = interaction;
154
- interactions[target].effects[interactionId].push({
155
- ...rest,
156
- effect
157
- });
158
- });
159
- });
160
- return {
161
- effects: config.effects || {},
162
- conditions,
163
- interactions
164
- };
165
- }
166
- function _addInteraction(instance, interaction, sourceElement) {
167
- const interactionVariations = {};
168
- interaction.effects.forEach(effect => {
169
- const effectId = effect.effectId;
170
- const interactionId = effect.interactionId;
171
- if (interactionVariations[interactionId]) {
172
- // Skip this effect if it has already been added
173
- return;
174
- }
175
- const effectOptions = {
176
- ...(instance.dataCache.effects[effectId] || {}),
177
- ...effect,
178
- effectId
179
- };
180
- if (instance.addedInteractions[effectOptions.interactionId]) {
181
- // Skip this interaction if it has already been added
182
- return;
183
- }
184
-
185
- // TODO: implement watching for condition `change` events and add/remove interactions accordingly
186
- const mql = (0, _utils.getMediaQuery)(effectOptions.conditions || [], instance.dataCache.conditions);
187
- if (!mql || mql.matches) {
188
- var _instance$dataCache$e, _Interact$getElement;
189
- interactionVariations[effect.interactionId] = true;
190
- const target = effect.target || effectId && ((_instance$dataCache$e = instance.dataCache.effects[effect.effectId]) == null ? void 0 : _instance$dataCache$e.target) || interaction.source;
191
- const targetElement = (_Interact$getElement = Interact.getElement(target)) == null ? void 0 : _Interact$getElement.firstElementChild;
192
- if (!targetElement) {
193
- // Bail out :: no target element in cache
194
- return;
195
- }
196
- instance.addedInteractions[effectOptions.interactionId] = true;
197
- addInteraction(target, sourceElement, interaction.trigger, targetElement, effectOptions, interaction.params);
198
- }
199
- });
200
- }
201
-
202
- /**
203
- * Adds all events and effects to an element based on config
204
- */
205
- function add(element, path) {
206
- const instance = Interact.getInstance(path);
207
- const {
208
- triggers = []
209
- } = (instance == null ? void 0 : instance.dataCache.interactions[path]) || {};
210
- const hasTriggers = triggers.length > 0;
211
-
212
- // even if we don't find a matching instance, we still want to cache the element
213
- Interact.setElement(path, element);
214
- const triggerSourceElement = element.firstElementChild;
215
- triggers.forEach(interaction => {
216
- const mql = (0, _utils.getMediaQuery)(interaction.conditions, instance.dataCache.conditions);
217
-
218
- // TODO: implement watching for condition `change` events and add/remove interactions accordingly
219
- if (!mql || mql.matches) {
220
- _addInteraction(instance, interaction, triggerSourceElement);
221
- }
222
- });
223
- let hasEffects = false;
224
- if (instance) {
225
- hasEffects = addEffectsForTarget(path, element, instance);
226
- }
227
- return hasTriggers || hasEffects;
228
- }
229
- function addEffectsForTarget(path, element, instance) {
230
- var _instance$dataCache$i;
231
- const effects = ((_instance$dataCache$i = instance.dataCache.interactions[path]) == null ? void 0 : _instance$dataCache$i.effects) || {};
232
- const interactionIds = Object.keys(effects);
233
- interactionIds.forEach(interactionId => {
234
- if (instance.addedInteractions[interactionId]) {
235
- // Skip this interaction if it has already been added
236
- return;
237
- }
238
- const effectVariations = effects[interactionId];
239
-
240
- // use `some` to short-circuit after the first effect that matches the conditions
241
- // eslint-disable-next-line array-callback-return
242
- effectVariations.some(({
243
- effect,
244
- ...options
245
- }) => {
246
- const effectId = effect.effectId;
247
- const effectOptions = {
248
- ...(instance.dataCache.effects[effectId] || {}),
249
- ...effect,
250
- effectId
251
- };
252
-
253
- // TODO: implement watching for condition `change` events and add/remove interactions accordingly
254
- const mql = (0, _utils.getMediaQuery)(effectOptions.conditions || [], instance.dataCache.conditions);
255
- if (!mql || mql.matches) {
256
- var _Interact$getElement2;
257
- const targetElement = element.firstElementChild;
258
- const sourceElement = (_Interact$getElement2 = Interact.getElement(options.source)) == null ? void 0 : _Interact$getElement2.firstElementChild;
259
- if (!sourceElement) {
260
- // Bail out :: no source or target elements in cache
261
- return true;
262
- }
263
- instance.addedInteractions[interactionId] = true;
264
- addInteraction(path, sourceElement, options.trigger, targetElement, effectOptions, options.params);
265
-
266
- // short-circuit the loop since we have a match
267
- return true;
268
- }
269
- });
270
- });
271
- return interactionIds.length > 0;
272
- }
273
-
274
- /**
275
- * Registers a handler to an event on a given element.
276
- */
277
- function addInteraction(path, source, trigger, target, effect, options) {
278
- var _TRIGGER_TO_HANDLER_M;
279
- if (effect.transition || effect.transitionProperties) {
280
- const args = {
281
- path,
282
- effectId: effect.effectId,
283
- transition: effect.transition,
284
- properties: effect.transitionProperties
285
- };
286
- if (target.id) {
287
- // temporarily allow using the target's id as the element selector to override default styles in cases ids are used
288
- args.childSelector = `#${target.id}`;
289
- }
290
- target.parentElement.renderStyle((0, _utils.createTransitionCSS)(args));
291
- }
292
- (_TRIGGER_TO_HANDLER_M = _handlers.default[trigger]) == null || _TRIGGER_TO_HANDLER_M.add(source, target, effect, options, Interact.forceReducedMotion);
293
- }
294
-
295
- /**
296
- * Removes all events and effects from an element based on config
297
- */
298
- function remove(path) {
299
- var _Interact$getElement3, _Interact$getInstance;
300
- const element = (_Interact$getElement3 = Interact.getElement(path)) == null ? void 0 : _Interact$getElement3.firstElementChild;
301
- if (!element) {
302
- return;
303
- }
304
- Object.values(_handlers.default).forEach(module => module.remove(element));
305
- (_Interact$getInstance = Interact.getInstance(path)) == null || _Interact$getInstance.clearInteractionStateForPath(path);
306
- Interact.elementCache.delete(path);
307
- }
308
- //# sourceMappingURL=interact.js.map