@elementor/editor-interactions 4.0.0-545 → 4.0.0-546

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.
@@ -1,16 +1,20 @@
1
1
  import * as React from 'react';
2
2
  import { useCallback, useRef } from 'react';
3
- import { UnstableSizeField } from '@elementor/editor-controls';
3
+ import { type Unit, UnstableSizeField } from '@elementor/editor-controls';
4
4
  import { type SizePropValue } from '@elementor/editor-props';
5
5
 
6
6
  import { DEFAULT_TIME_UNIT, TIME_UNITS } from '../../configs/time-constants';
7
- import { type FieldProps, type TimeUnit, type TimeValue } from '../../types';
7
+ import { type FieldProps, type SizeStringValue } from '../../types';
8
8
  import { formatSizeValue, parseSizeValue } from '../../utils/size-transform-utils';
9
9
  import { convertTimeUnit } from '../../utils/time-conversion';
10
10
 
11
- export function TimeFrameIndicator( { value, onChange, defaultValue }: FieldProps & { defaultValue: TimeValue } ) {
12
- const sizeValue = parseSizeValue( value as TimeValue, TIME_UNITS, defaultValue, DEFAULT_TIME_UNIT );
13
- const prevUnitRef = useRef< TimeUnit >( sizeValue.unit as TimeUnit );
11
+ export function TimeFrameIndicator( {
12
+ value,
13
+ onChange,
14
+ defaultValue,
15
+ }: FieldProps & { defaultValue: SizeStringValue } ) {
16
+ const sizeValue = parseSizeValue( value as SizeStringValue, TIME_UNITS, defaultValue, DEFAULT_TIME_UNIT );
17
+ const prevUnitRef = useRef< Unit >( sizeValue.unit as Unit );
14
18
 
15
19
  const setValue = useCallback(
16
20
  ( size: SizePropValue[ 'value' ] ) => {
@@ -18,7 +22,7 @@ export function TimeFrameIndicator( { value, onChange, defaultValue }: FieldProp
18
22
 
19
23
  if ( unitChanged ) {
20
24
  const fromUnit = prevUnitRef.current;
21
- const toUnit = size.unit as TimeUnit;
25
+ const toUnit = size.unit as Unit;
22
26
 
23
27
  size.size = convertTimeUnit( Number( size.size ), fromUnit, toUnit );
24
28
  prevUnitRef.current = toUnit;
@@ -9,6 +9,7 @@ export function Trigger( { value, onChange }: FieldProps ) {
9
9
  const availableTriggers = Object.entries( {
10
10
  load: __( 'Page load', 'elementor' ),
11
11
  scrollIn: __( 'Scroll into view', 'elementor' ),
12
+ scrollOn: __( 'While Scrolling', 'elementor' ),
12
13
  } ).map( ( [ key, label ] ) => ( {
13
14
  key,
14
15
  label,
@@ -5,15 +5,15 @@ import { Divider, Grid } from '@elementor/ui';
5
5
  import { __ } from '@wordpress/i18n';
6
6
 
7
7
  import { getInteractionsControl } from '../interactions-controls-registry';
8
- import type { InteractionItemValue, TimeValue } from '../types';
8
+ import { type InteractionItemValue, type SizeStringValue } from '../types';
9
9
  import {
10
10
  createAnimationPreset,
11
11
  createString,
12
12
  extractBoolean,
13
- extractNumber,
14
13
  extractSize,
15
14
  extractString,
16
15
  } from '../utils/prop-value-utils';
16
+ import { parseSizeValue } from '../utils/size-transform-utils';
17
17
  import { Direction } from './controls/direction';
18
18
  import { Effect } from './controls/effect';
19
19
  import { EffectType } from './controls/effect-type';
@@ -59,13 +59,13 @@ type InteractionValues = {
59
59
  effect: string;
60
60
  type: string;
61
61
  direction: string;
62
- duration: TimeValue;
63
- delay: TimeValue;
62
+ duration: SizeStringValue;
63
+ delay: SizeStringValue;
64
64
  replay: boolean;
65
65
  easing: string;
66
66
  relativeTo: string;
67
- offsetTop: number;
68
- offsetBottom: number;
67
+ offsetTop: SizeStringValue;
68
+ offsetBottom: SizeStringValue;
69
69
  };
70
70
 
71
71
  type ControlVisibilityConfig = {
@@ -108,8 +108,9 @@ export const InteractionDetails = ( { interaction, onChange, onPlayInteraction }
108
108
  const replay = extractBoolean( interaction.animation.value.config?.value.replay, DEFAULT_VALUES.replay );
109
109
  const easing = extractString( interaction.animation.value.config?.value.easing, DEFAULT_VALUES.easing );
110
110
  const relativeTo = extractString( interaction.animation.value.config?.value.relativeTo, DEFAULT_VALUES.relativeTo );
111
- const offsetTop = extractNumber( interaction.animation.value.config?.value.offsetTop, DEFAULT_VALUES.offsetTop );
112
- const offsetBottom = extractNumber(
111
+
112
+ const offsetTop = extractSize( interaction.animation.value.config?.value.offsetTop, DEFAULT_VALUES.offsetTop );
113
+ const offsetBottom = extractSize(
113
114
  interaction.animation.value.config?.value.offsetBottom,
114
115
  DEFAULT_VALUES.offsetBottom
115
116
  );
@@ -159,13 +160,13 @@ export const InteractionDetails = ( { interaction, onChange, onPlayInteraction }
159
160
  effect: string;
160
161
  type: string;
161
162
  direction: string;
162
- duration: TimeValue;
163
- delay: TimeValue;
163
+ duration: SizeStringValue;
164
+ delay: SizeStringValue;
164
165
  replay: boolean;
165
166
  easing?: string;
166
167
  relativeTo: string;
167
- offsetTop: number;
168
- offsetBottom: number;
168
+ offsetTop: SizeStringValue;
169
+ offsetBottom: SizeStringValue;
169
170
  } >
170
171
  ): void => {
171
172
  const resolvedDirectionValue = resolveDirection( 'direction' in updates, updates.effect, updates.direction );
@@ -240,8 +241,8 @@ export const InteractionDetails = ( { interaction, onChange, onPlayInteraction }
240
241
  <Field label={ __( 'Duration', 'elementor' ) }>
241
242
  <TimeFrameIndicator
242
243
  value={ String( duration ) }
243
- onChange={ ( v ) => updateInteraction( { duration: v as TimeValue } ) }
244
- defaultValue={ DEFAULT_VALUES.duration }
244
+ onChange={ ( v ) => updateInteraction( { duration: v as SizeStringValue } ) }
245
+ defaultValue={ DEFAULT_VALUES.duration as SizeStringValue }
245
246
  />
246
247
  </Field>
247
248
  ) }
@@ -250,8 +251,8 @@ export const InteractionDetails = ( { interaction, onChange, onPlayInteraction }
250
251
  <Field label={ __( 'Delay', 'elementor' ) }>
251
252
  <TimeFrameIndicator
252
253
  value={ String( delay ) }
253
- onChange={ ( v ) => updateInteraction( { delay: v as TimeValue } ) }
254
- defaultValue={ DEFAULT_VALUES.delay }
254
+ onChange={ ( v ) => updateInteraction( { delay: v as SizeStringValue } ) }
255
+ defaultValue={ DEFAULT_VALUES.delay as SizeStringValue }
255
256
  />
256
257
  </Field>
257
258
  ) }
@@ -270,17 +271,19 @@ export const InteractionDetails = ( { interaction, onChange, onPlayInteraction }
270
271
  { OffsetTopControl && (
271
272
  <Field label={ __( 'Offset Top', 'elementor' ) }>
272
273
  <OffsetTopControl
273
- value={ String( offsetTop ) }
274
- onChange={ ( v: string ) => updateInteraction( { offsetTop: parseInt( v, 10 ) } ) }
274
+ value={ String( parseSizeValue( offsetTop, [ '%' ] ).size ) }
275
+ onChange={ ( v: string ) =>
276
+ updateInteraction( { offsetTop: v as SizeStringValue } )
277
+ }
275
278
  />
276
279
  </Field>
277
280
  ) }
278
281
  { OffsetBottomControl && (
279
282
  <Field label={ __( 'Offset Bottom', 'elementor' ) }>
280
283
  <OffsetBottomControl
281
- value={ String( offsetBottom ) }
284
+ value={ String( parseSizeValue( offsetBottom, [ '%' ] ).size ) }
282
285
  onChange={ ( v: string ) =>
283
- updateInteraction( { offsetBottom: parseInt( v, 10 ) } )
286
+ updateInteraction( { offsetBottom: v as SizeStringValue } )
284
287
  }
285
288
  />
286
289
  </Field>
@@ -1,5 +1,5 @@
1
- import { type TimeUnit } from '../types';
1
+ import { type Unit } from '@elementor/editor-controls';
2
2
 
3
- export const TIME_UNITS: TimeUnit[] = [ 's', 'ms' ];
3
+ export const TIME_UNITS: Unit[] = [ 's', 'ms' ];
4
4
 
5
5
  export const DEFAULT_TIME_UNIT = 'ms';
@@ -1,3 +1,4 @@
1
+ import { type Unit } from '@elementor/editor-controls';
1
2
  import { type ElementInteractions } from '@elementor/editor-elements';
2
3
  import {
3
4
  isTransformable,
@@ -7,16 +8,15 @@ import {
7
8
  type TransformablePropValue,
8
9
  } from '@elementor/editor-props';
9
10
 
10
- import { type TimeUnit } from '../../types';
11
11
  import { convertTimeUnit } from '../../utils/time-conversion';
12
12
 
13
- type Normalizer = ( value: TransformablePropValue< string, unknown > ) => PropValue;
13
+ type Normalizer = ( value: TransformablePropValue< string > ) => PropValue;
14
14
 
15
15
  const FIELD_NORMALIZERS: Record< string, Normalizer > = {
16
16
  size: ( value ) => {
17
17
  const sizeProp = value as SizePropValue;
18
18
  const { size, unit } = sizeProp.value;
19
- const numberPropValue = convertTimeUnit( size as number, unit as TimeUnit, 'ms' );
19
+ const numberPropValue = convertTimeUnit( size as number, unit as Unit, 'ms' );
20
20
 
21
21
  return numberPropTypeUtil.create( numberPropValue );
22
22
  },
package/src/types.ts CHANGED
@@ -77,6 +77,4 @@ export type InteractionsProvider = {
77
77
 
78
78
  export type InteractionItemValue = InteractionItemPropValue[ 'value' ];
79
79
 
80
- export type TimeUnit = Extract< Unit, 'ms' | 's' >;
81
-
82
- export type TimeValue = `${ number }${ TimeUnit }` | number;
80
+ export type SizeStringValue = `${ number }${ Unit }` | number;
@@ -1,3 +1,4 @@
1
+ import { type Unit } from '@elementor/editor-controls';
1
2
  import { sizePropTypeUtil, type SizePropValue } from '@elementor/editor-props';
2
3
 
3
4
  import { DEFAULT_TIME_UNIT, TIME_UNITS } from '../configs/time-constants';
@@ -11,8 +12,8 @@ import {
11
12
  type InteractionItemPropValue,
12
13
  type InteractionItemValue,
13
14
  type NumberPropValue,
15
+ type SizeStringValue,
14
16
  type StringPropValue,
15
- type TimeValue,
16
17
  type TimingConfigPropValue,
17
18
  } from '../types';
18
19
  import { formatSizeValue, parseSizeValue } from '../utils/size-transform-utils';
@@ -28,7 +29,7 @@ export const createNumber = ( value: number ): NumberPropValue => ( {
28
29
  value,
29
30
  } );
30
31
 
31
- export const createTimingConfig = ( duration: TimeValue, delay: TimeValue ): TimingConfigPropValue => ( {
32
+ export const createTimingConfig = ( duration: SizeStringValue, delay: SizeStringValue ): TimingConfigPropValue => ( {
32
33
  $$type: 'timing-config',
33
34
  value: {
34
35
  duration: sizePropTypeUtil.create( parseSizeValue( duration, TIME_UNITS, undefined, DEFAULT_TIME_UNIT ) ),
@@ -46,24 +47,32 @@ export const createConfig = ( {
46
47
  easing = 'easeIn',
47
48
  relativeTo = '',
48
49
  offsetTop = 0,
49
- offsetBottom = 100,
50
+ offsetBottom = 85,
50
51
  }: {
51
52
  replay: boolean;
52
53
  easing?: string;
53
54
  relativeTo?: string;
54
- offsetTop?: number;
55
- offsetBottom?: number;
55
+ offsetTop?: SizeStringValue;
56
+ offsetBottom?: SizeStringValue;
56
57
  } ): ConfigPropValue => ( {
57
58
  $$type: 'config',
58
59
  value: {
59
60
  replay: createBoolean( replay ),
60
61
  easing: createString( easing ),
61
62
  relativeTo: createString( relativeTo ),
62
- offsetTop: createNumber( offsetTop ),
63
- offsetBottom: createNumber( offsetBottom ),
63
+ offsetTop: createSize( offsetTop, '%' ),
64
+ offsetBottom: createSize( offsetBottom, '%' ),
64
65
  },
65
66
  } );
66
67
 
68
+ const createSize = ( value?: SizeStringValue, defaultUnit?: Unit, defaultValue?: SizeStringValue ) => {
69
+ if ( ! value ) {
70
+ return;
71
+ }
72
+
73
+ return sizePropTypeUtil.create( parseSizeValue( value, [ '%' ], defaultValue, defaultUnit ) );
74
+ };
75
+
67
76
  export const extractBoolean = ( prop: BooleanPropValue | undefined, fallback = false ): boolean => {
68
77
  return prop?.value ?? fallback;
69
78
  };
@@ -99,13 +108,13 @@ export const createAnimationPreset = ( {
99
108
  effect: string;
100
109
  type: string;
101
110
  direction?: string;
102
- duration: TimeValue;
103
- delay: TimeValue;
111
+ duration: SizeStringValue;
112
+ delay: SizeStringValue;
104
113
  replay: boolean;
105
114
  easing?: string;
106
115
  relativeTo?: string;
107
- offsetTop?: number;
108
- offsetBottom?: number;
116
+ offsetTop?: SizeStringValue;
117
+ offsetBottom?: SizeStringValue;
109
118
  } ): AnimationPresetPropValue => ( {
110
119
  $$type: 'animation-preset-props',
111
120
  value: {
@@ -142,8 +151,8 @@ export const createInteractionItem = ( {
142
151
  effect: string;
143
152
  type: string;
144
153
  direction?: string;
145
- duration: TimeValue;
146
- delay: TimeValue;
154
+ duration: SizeStringValue;
155
+ delay: SizeStringValue;
147
156
  interactionId?: string;
148
157
  replay: boolean;
149
158
  easing?: string;
@@ -197,12 +206,12 @@ export const extractString = ( prop: StringPropValue | undefined, fallback = ''
197
206
  return prop?.value ?? fallback;
198
207
  };
199
208
 
200
- export const extractSize = ( prop: SizePropValue ): TimeValue => {
201
- return formatSizeValue( prop?.value );
202
- };
209
+ export const extractSize = ( prop?: SizePropValue, defaultValue?: SizeStringValue ): SizeStringValue => {
210
+ if ( ! prop?.value ) {
211
+ return defaultValue as SizeStringValue;
212
+ }
203
213
 
204
- export const extractNumber = ( prop: NumberPropValue | undefined, fallback = 0 ): number => {
205
- return prop?.value ?? fallback;
214
+ return formatSizeValue( prop.value );
206
215
  };
207
216
 
208
217
  const TRIGGER_LABELS: Record< string, string > = {
@@ -1,6 +1,7 @@
1
+ import { type Unit } from '@elementor/editor-controls';
1
2
  import { type SizePropValue } from '@elementor/editor-props';
2
3
 
3
- import { type TimeUnit, type TimeValue } from '../types';
4
+ import { type SizeStringValue } from '../types';
4
5
 
5
6
  type SizeValue = SizePropValue[ 'value' ];
6
7
  type SizeUnit = SizeValue[ 'unit' ];
@@ -8,15 +9,15 @@ type SizeUnit = SizeValue[ 'unit' ];
8
9
  const SIZE_REGEX = /^(?:(-?\d*\.?\d+)([a-z%]+)|([a-z%]+))$/i;
9
10
 
10
11
  export const parseSizeValue = (
11
- value: TimeValue,
12
+ value: SizeStringValue,
12
13
  allowedUnits: SizeUnit[],
13
- defaultValue?: TimeValue,
14
- defaultUnit?: TimeUnit
14
+ defaultValue?: SizeStringValue,
15
+ defaultUnit?: Unit
15
16
  ): SizeValue => {
16
17
  if ( typeof value === 'number' ) {
17
18
  return {
18
19
  size: value,
19
- unit: defaultUnit as TimeUnit,
20
+ unit: defaultUnit as Unit,
20
21
  };
21
22
  }
22
23
 
@@ -37,7 +38,7 @@ export const parseSizeValue = (
37
38
  return createSizeValue( null, defaultUnit );
38
39
  };
39
40
 
40
- const tryParse = ( value: TimeValue, allowedUnits: SizeUnit[], defaultUnit?: TimeUnit ): SizeValue | null => {
41
+ const tryParse = ( value: SizeStringValue, allowedUnits: SizeUnit[], defaultUnit?: Unit ): SizeValue | null => {
41
42
  if ( typeof value === 'number' ) {
42
43
  return createSizeValue( value, defaultUnit );
43
44
  }
@@ -45,6 +46,13 @@ const tryParse = ( value: TimeValue, allowedUnits: SizeUnit[], defaultUnit?: Tim
45
46
  const match = value && value.match( SIZE_REGEX );
46
47
 
47
48
  if ( ! match ) {
49
+ if ( value ) {
50
+ return {
51
+ size: Number( value ),
52
+ unit: defaultUnit as Unit,
53
+ };
54
+ }
55
+
48
56
  return null;
49
57
  }
50
58
 
@@ -58,8 +66,8 @@ const tryParse = ( value: TimeValue, allowedUnits: SizeUnit[], defaultUnit?: Tim
58
66
  return createSizeValue( size, unit );
59
67
  };
60
68
 
61
- export const formatSizeValue = ( { size, unit }: SizeValue ): TimeValue => {
62
- return `${ size ?? '' }${ unit }` as TimeValue;
69
+ export const formatSizeValue = ( { size, unit }: SizeValue ): SizeStringValue => {
70
+ return `${ size ?? '' }${ unit }` as SizeStringValue;
63
71
  };
64
72
 
65
73
  const createSizeValue = ( size: number | null, unit?: SizeUnit ): SizeValue => {
@@ -1,10 +1,11 @@
1
- import { type TimeUnit } from '../types';
1
+ import { type Unit } from '@elementor/editor-controls';
2
2
 
3
+ type TimeUnit = Extract< Unit, 'ms' | 's' >;
3
4
  const UNIT_TO_MS: Record< TimeUnit, number > = {
4
5
  ms: 1,
5
6
  s: 1000,
6
7
  };
7
8
 
8
- export const convertTimeUnit = ( value: number, from: TimeUnit, to: TimeUnit ): number => {
9
- return ( value * UNIT_TO_MS[ from ] ) / UNIT_TO_MS[ to ];
9
+ export const convertTimeUnit = ( value: number, from: Unit, to: Unit ): number => {
10
+ return ( value * UNIT_TO_MS[ from as TimeUnit ] ) / UNIT_TO_MS[ to as TimeUnit ];
10
11
  };