@elementor/editor-controls 4.0.0-manual → 4.0.1

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 (39) hide show
  1. package/dist/index.d.mts +138 -64
  2. package/dist/index.d.ts +138 -64
  3. package/dist/index.js +905 -233
  4. package/dist/index.js.map +1 -1
  5. package/dist/index.mjs +890 -225
  6. package/dist/index.mjs.map +1 -1
  7. package/package.json +15 -15
  8. package/src/components/inline-editor.tsx +16 -57
  9. package/src/components/promotions/attributes-control.tsx +2 -1
  10. package/src/components/promotions/display-conditions-control.tsx +2 -1
  11. package/src/components/promotions/promotion-trigger.tsx +14 -4
  12. package/src/controls/chips-control.tsx +1 -1
  13. package/src/controls/email-form-action-control.tsx +5 -5
  14. package/src/controls/number-control.tsx +8 -2
  15. package/src/controls/size-control/hooks/use-size-unit-keyboard.tsx +66 -0
  16. package/src/controls/size-control/hooks/use-size-value.ts +71 -0
  17. package/src/controls/size-control/size-component.tsx +94 -0
  18. package/src/controls/size-control/size-field.tsx +113 -0
  19. package/src/controls/size-control/sync/get-units.ts +17 -0
  20. package/src/controls/size-control/types.ts +17 -0
  21. package/src/controls/size-control/ui/size-input.tsx +68 -0
  22. package/src/controls/size-control/ui/text-field-popover.tsx +78 -0
  23. package/src/controls/size-control/ui/unit-selector.tsx +80 -0
  24. package/src/controls/size-control/unstable-size-control.tsx +86 -0
  25. package/src/controls/size-control/utils/has-size-value.ts +5 -0
  26. package/src/controls/size-control/utils/is-extended-unit.ts +8 -0
  27. package/src/controls/size-control/utils/resolve-bound-prop-value.ts +72 -0
  28. package/src/controls/size-control/utils/resolve-size-value.ts +85 -0
  29. package/src/controls/size-control/utils/settings/get-default-unit.ts +7 -0
  30. package/src/controls/size-control/utils/settings/get-prop-type-settings.ts +12 -0
  31. package/src/controls/size-control/utils/settings/get-size-units.ts +23 -0
  32. package/src/controls/size-control/utils/should-nullify-value.ts +15 -0
  33. package/src/controls/svg-media-control.tsx +5 -4
  34. package/src/controls/transition-control/data.ts +3 -3
  35. package/src/controls/transition-control/transition-repeater-control.tsx +8 -2
  36. package/src/controls/transition-control/transition-selector.tsx +7 -0
  37. package/src/hooks/use-font-families.ts +22 -25
  38. package/src/index.ts +4 -0
  39. package/src/utils/tracking.ts +61 -0
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@elementor/editor-controls",
3
3
  "description": "This package contains the controls model and utils for the Elementor editor",
4
- "version": "4.0.0-manual",
4
+ "version": "4.0.1",
5
5
  "private": false,
6
6
  "author": "Elementor Team",
7
7
  "homepage": "https://elementor.com/",
@@ -40,22 +40,22 @@
40
40
  "dev": "tsup --config=../../tsup.dev.ts"
41
41
  },
42
42
  "dependencies": {
43
- "@elementor/editor-current-user": "4.0.0-manual",
44
- "@elementor/editor-elements": "4.0.0-manual",
45
- "@elementor/editor-props": "4.0.0-manual",
46
- "@elementor/editor-responsive": "4.0.0-manual",
47
- "@elementor/editor-ui": "4.0.0-manual",
48
- "@elementor/editor-v1-adapters": "4.0.0-manual",
49
- "@elementor/env": "4.0.0-manual",
50
- "@elementor/http-client": "4.0.0-manual",
43
+ "@elementor/editor-current-user": "4.0.1",
44
+ "@elementor/editor-elements": "4.0.1",
45
+ "@elementor/editor-props": "4.0.1",
46
+ "@elementor/editor-responsive": "4.0.1",
47
+ "@elementor/editor-ui": "4.0.1",
48
+ "@elementor/editor-v1-adapters": "4.0.1",
49
+ "@elementor/env": "4.0.1",
50
+ "@elementor/http-client": "4.0.1",
51
51
  "@elementor/icons": "^1.68.0",
52
- "@elementor/locations": "4.0.0-manual",
53
- "@elementor/events": "4.0.0-manual",
54
- "@elementor/query": "4.0.0-manual",
55
- "@elementor/session": "4.0.0-manual",
52
+ "@elementor/locations": "4.0.1",
53
+ "@elementor/events": "4.0.1",
54
+ "@elementor/query": "4.0.1",
55
+ "@elementor/session": "4.0.1",
56
56
  "@elementor/ui": "1.36.17",
57
- "@elementor/utils": "4.0.0-manual",
58
- "@elementor/wp-media": "4.0.0-manual",
57
+ "@elementor/utils": "4.0.1",
58
+ "@elementor/wp-media": "4.0.1",
59
59
  "@wordpress/i18n": "^5.13.0",
60
60
  "@monaco-editor/react": "^4.7.0",
61
61
  "dayjs": "^1.11.18",
@@ -1,14 +1,6 @@
1
1
  import * as React from 'react';
2
- import {
3
- type DependencyList,
4
- type Dispatch,
5
- type PropsWithChildren,
6
- type RefObject,
7
- type SetStateAction,
8
- useEffect,
9
- useRef,
10
- } from 'react';
11
- import { Box, ClickAwayListener, type SxProps, type Theme } from '@elementor/ui';
2
+ import { type DependencyList, type Dispatch, type SetStateAction, useEffect, useRef } from 'react';
3
+ import { Box, type SxProps, type Theme } from '@elementor/ui';
12
4
  import Bold from '@tiptap/extension-bold';
13
5
  import Document from '@tiptap/extension-document';
14
6
  import HardBreak from '@tiptap/extension-hard-break';
@@ -42,16 +34,9 @@ type InlineEditorProps = {
42
34
  onEditorCreate?: Dispatch< SetStateAction< Editor | null > >;
43
35
  wrapperClassName?: string;
44
36
  onSelectionEnd?: ( view: EditorView ) => void;
37
+ mountElement?: HTMLElement | null;
45
38
  };
46
39
 
47
- type WrapperProps = PropsWithChildren< {
48
- containerRef: RefObject< HTMLDivElement >;
49
- editor: ReturnType< typeof useEditor >;
50
- sx: SxProps< Theme >;
51
- onBlur?: () => void;
52
- className?: string;
53
- } >;
54
-
55
40
  export const InlineEditor = React.forwardRef( ( props: InlineEditorProps, ref ) => {
56
41
  const {
57
42
  value,
@@ -65,9 +50,12 @@ export const InlineEditor = React.forwardRef( ( props: InlineEditorProps, ref )
65
50
  onEditorCreate,
66
51
  wrapperClassName,
67
52
  onSelectionEnd,
53
+ mountElement = null,
68
54
  } = props;
69
55
 
70
56
  const containerRef = useRef< HTMLDivElement >( null );
57
+ const onBlurRef = useRef( onBlur );
58
+ onBlurRef.current = onBlur;
71
59
  const documentContentSettings = !! expectedTag ? 'block+' : 'inline*';
72
60
 
73
61
  const onUpdate = ( { editor: updatedEditor }: { editor: Editor } ) => {
@@ -78,7 +66,7 @@ export const InlineEditor = React.forwardRef( ( props: InlineEditorProps, ref )
78
66
 
79
67
  const onKeyDown = ( _: Editor[ 'view' ], event: KeyboardEvent ) => {
80
68
  if ( event.key === 'Escape' ) {
81
- onBlur?.();
69
+ onBlurRef.current?.();
82
70
  }
83
71
 
84
72
  if ( ( ! event.metaKey && ! event.ctrlKey ) || event.altKey ) {
@@ -96,6 +84,7 @@ export const InlineEditor = React.forwardRef( ( props: InlineEditorProps, ref )
96
84
  } );
97
85
 
98
86
  const editor = useEditor( {
87
+ ...( mountElement ? { element: mountElement } : {} ),
99
88
  extensions: [
100
89
  Document.extend( {
101
90
  content: documentContentSettings,
@@ -153,6 +142,7 @@ export const InlineEditor = React.forwardRef( ( props: InlineEditorProps, ref )
153
142
  },
154
143
  },
155
144
  onCreate: onEditorCreate ? ( { editor: mountedEditor } ) => onEditorCreate( mountedEditor ) : undefined,
145
+ onBlur: mountElement ? undefined : () => onBlurRef.current?.(),
156
146
  onSelectionUpdate: onSelectionEnd
157
147
  ? ( { editor: updatedEditor } ) => onSelectionEnd( updatedEditor.view )
158
148
  : undefined,
@@ -170,47 +160,16 @@ export const InlineEditor = React.forwardRef( ( props: InlineEditorProps, ref )
170
160
  }
171
161
  }, [ editor, value ] );
172
162
 
173
- return (
174
- <>
175
- <Wrapper
176
- containerRef={ containerRef }
177
- editor={ editor }
178
- sx={ sx }
179
- onBlur={ onBlur }
180
- className={ wrapperClassName }
181
- >
182
- <EditorContent ref={ ref } editor={ editor } />
183
- </Wrapper>
184
- </>
185
- );
186
- } );
163
+ if ( mountElement ) {
164
+ return null;
165
+ }
187
166
 
188
- const Wrapper = ( { children, containerRef, editor, sx, onBlur, className }: WrapperProps ) => {
189
- const wrappedChildren = (
190
- <Box ref={ containerRef } { ...sx } className={ className }>
191
- { children }
167
+ return (
168
+ <Box ref={ containerRef } sx={ sx } className={ wrapperClassName }>
169
+ <EditorContent ref={ ref } editor={ editor } />
192
170
  </Box>
193
171
  );
194
-
195
- return onBlur ? (
196
- <ClickAwayListener
197
- onClickAway={ ( event: PointerEvent ) => {
198
- if (
199
- containerRef.current?.contains( event.target as Node ) ||
200
- editor.view.dom.contains( event.target as Node )
201
- ) {
202
- return;
203
- }
204
-
205
- onBlur?.();
206
- } }
207
- >
208
- { wrappedChildren }
209
- </ClickAwayListener>
210
- ) : (
211
- <>{ wrappedChildren }</>
212
- );
213
- };
172
+ } );
214
173
 
215
174
  const useOnUpdate = ( callback: () => void, dependencies: DependencyList ): void => {
216
175
  const hasMounted = useRef( false );
@@ -8,6 +8,7 @@ import { createControl } from '../../create-control';
8
8
  import { PromotionTrigger, type PromotionTriggerRef } from './promotion-trigger';
9
9
 
10
10
  const ARIA_LABEL = __( 'Attributes', 'elementor' );
11
+ const TRACKING_DATA = { target_name: 'attributes', location_l2: 'general' } as const;
11
12
 
12
13
  export const AttributesControl = createControl( () => {
13
14
  const triggerRef = useRef< PromotionTriggerRef >( null );
@@ -21,7 +22,7 @@ export const AttributesControl = createControl( () => {
21
22
  alignItems: 'center',
22
23
  } }
23
24
  >
24
- <PromotionTrigger ref={ triggerRef } promotionKey="attributes" />
25
+ <PromotionTrigger ref={ triggerRef } promotionKey="attributes" trackingData={ TRACKING_DATA } />
25
26
  <Tooltip title={ ARIA_LABEL } placement="top">
26
27
  <PlusIcon
27
28
  aria-label={ ARIA_LABEL }
@@ -8,6 +8,7 @@ import { createControl } from '../../create-control';
8
8
  import { PromotionTrigger, type PromotionTriggerRef } from './promotion-trigger';
9
9
 
10
10
  const ARIA_LABEL = __( 'Display Conditions', 'elementor' );
11
+ const TRACKING_DATA = { target_name: 'display_conditions', location_l2: 'general' } as const;
11
12
 
12
13
  export const DisplayConditionsControl = createControl( () => {
13
14
  const triggerRef = useRef< PromotionTriggerRef >( null );
@@ -21,7 +22,7 @@ export const DisplayConditionsControl = createControl( () => {
21
22
  alignItems: 'center',
22
23
  } }
23
24
  >
24
- <PromotionTrigger ref={ triggerRef } promotionKey="displayConditions" />
25
+ <PromotionTrigger ref={ triggerRef } promotionKey="displayConditions" trackingData={ TRACKING_DATA } />
25
26
  <Tooltip title={ ARIA_LABEL } placement="top">
26
27
  <IconButton
27
28
  size="tiny"
@@ -1,8 +1,9 @@
1
1
  import * as React from 'react';
2
- import { forwardRef, type ReactNode, useImperativeHandle, useState } from 'react';
2
+ import { forwardRef, type ReactNode, useCallback, useImperativeHandle, useState } from 'react';
3
3
  import { PromotionChip, PromotionInfotip } from '@elementor/editor-ui';
4
4
  import { Box } from '@elementor/ui';
5
5
 
6
+ import { type PromotionTrackingData, trackUpgradePromotionClick, trackViewPromotion } from '../../utils/tracking';
6
7
  import type { V4PromotionData, V4PromotionKey } from './types';
7
8
 
8
9
  function getV4Promotion( key: V4PromotionKey ): V4PromotionData | undefined {
@@ -12,6 +13,7 @@ function getV4Promotion( key: V4PromotionKey ): V4PromotionData | undefined {
12
13
  type PromotionTriggerProps = {
13
14
  promotionKey: V4PromotionKey;
14
15
  children?: ReactNode;
16
+ trackingData: PromotionTrackingData;
15
17
  };
16
18
 
17
19
  export type PromotionTriggerRef = {
@@ -19,13 +21,20 @@ export type PromotionTriggerRef = {
19
21
  };
20
22
 
21
23
  export const PromotionTrigger = forwardRef< PromotionTriggerRef, PromotionTriggerProps >(
22
- ( { promotionKey, children }, ref ) => {
24
+ ( { promotionKey, children, trackingData }, ref ) => {
23
25
  const [ isOpen, setIsOpen ] = useState( false );
24
26
  const promotion = getV4Promotion( promotionKey );
25
27
 
26
- const toggle = () => setIsOpen( ( prev ) => ! prev );
28
+ const toggle = useCallback( () => {
29
+ setIsOpen( ( prev ) => {
30
+ if ( ! prev ) {
31
+ trackViewPromotion( trackingData );
32
+ }
33
+ return ! prev;
34
+ } );
35
+ }, [ trackingData ] );
27
36
 
28
- useImperativeHandle( ref, () => ( { toggle } ), [] );
37
+ useImperativeHandle( ref, () => ( { toggle } ), [ toggle ] );
29
38
 
30
39
  return (
31
40
  <>
@@ -40,6 +49,7 @@ export const PromotionTrigger = forwardRef< PromotionTriggerRef, PromotionTrigge
40
49
  e.stopPropagation();
41
50
  setIsOpen( false );
42
51
  } }
52
+ onCtaClick={ () => trackUpgradePromotionClick( trackingData ) }
43
53
  >
44
54
  <Box
45
55
  onClick={ ( e: MouseEvent ) => {
@@ -31,7 +31,7 @@ export const ChipsControl = createControl( ( { options }: ChipsControlProps ) =>
31
31
 
32
32
  const handleChange = ( _: SyntheticEvent, newValue: ChipsOption[] ) => {
33
33
  const values = newValue.map( ( option ) => stringPropTypeUtil.create( option.value ) );
34
- setValue( values.length > 0 ? values : null );
34
+ setValue( values );
35
35
  };
36
36
 
37
37
  return (
@@ -29,7 +29,7 @@ const EmailField = ( { bind, label, placeholder }: { bind: string; label: string
29
29
  const SendToField = () => (
30
30
  <EmailField
31
31
  bind="to"
32
- label={ __( 'Send To', 'elementor' ) }
32
+ label={ __( 'Send to', 'elementor' ) }
33
33
  placeholder={ __( 'Where should we send new submissions?', 'elementor' ) }
34
34
  />
35
35
  );
@@ -37,7 +37,7 @@ const SendToField = () => (
37
37
  const SubjectField = () => (
38
38
  <EmailField
39
39
  bind="subject"
40
- label={ __( 'Email Subject', 'elementor' ) }
40
+ label={ __( 'Email subject', 'elementor' ) }
41
41
  placeholder={ __( 'New form submission', 'elementor' ) }
42
42
  />
43
43
  );
@@ -64,7 +64,7 @@ const FromEmailField = () => (
64
64
  <EmailField
65
65
  bind="from"
66
66
  label={ __( 'From email', 'elementor' ) }
67
- placeholder={ __( 'What email address should appear as the sender?', 'elementor' ) }
67
+ placeholder={ __( 'What email should appear as the sender?', 'elementor' ) }
68
68
  />
69
69
  );
70
70
 
@@ -85,7 +85,7 @@ const BccField = () => <EmailField bind="bcc" label={ __( 'Bcc', 'elementor' ) }
85
85
  const MetaDataField = () => (
86
86
  <PropKeyProvider bind="meta-data">
87
87
  <Stack gap={ 0.5 }>
88
- <ControlLabel>{ __( 'Meta data', 'elementor' ) }</ControlLabel>
88
+ <ControlLabel>{ __( 'Metadata', 'elementor' ) }</ControlLabel>
89
89
  <ChipsControl
90
90
  options={ [
91
91
  { label: __( 'Date', 'elementor' ), value: 'date' },
@@ -139,7 +139,7 @@ export const EmailFormActionControl = createControl( () => {
139
139
  return (
140
140
  <PropProvider { ...propContext } value={ value } setValue={ setValue }>
141
141
  <Stack gap={ 2 }>
142
- <ControlFormLabel>{ __( 'Email settings', 'elementor' ) }</ControlFormLabel>
142
+ <ControlLabel>{ __( 'Email settings', 'elementor' ) }</ControlLabel>
143
143
  <SendToField />
144
144
  <SubjectField />
145
145
  <MessageField />
@@ -1,6 +1,6 @@
1
1
  import * as React from 'react';
2
2
  import { numberPropTypeUtil, type PropType } from '@elementor/editor-props';
3
- import { InputAdornment } from '@elementor/ui';
3
+ import { InputAdornment, Typography } from '@elementor/ui';
4
4
 
5
5
  import { useBoundProp } from '../bound-prop-context';
6
6
  import { NumberInput } from '../components/number-input';
@@ -12,7 +12,13 @@ const isEmptyOrNaN = ( value?: string | number | null ) =>
12
12
 
13
13
  const renderSuffix = ( propType: PropType ) => {
14
14
  if ( propType.meta?.suffix ) {
15
- return <InputAdornment position="end">{ propType.meta.suffix }</InputAdornment>;
15
+ return (
16
+ <InputAdornment position="end">
17
+ <Typography variant="caption" color="text.secondary">
18
+ { propType.meta.suffix as string }
19
+ </Typography>
20
+ </InputAdornment>
21
+ );
16
22
  }
17
23
  return <></>;
18
24
  };
@@ -0,0 +1,66 @@
1
+ import type * as React from 'react';
2
+
3
+ import { useTypingBuffer } from '../../../hooks/use-typing-buffer';
4
+ import { type SizeUnit } from '../types';
5
+ import { isExtendedUnit } from '../utils/is-extended-unit';
6
+
7
+ const UNIT_KEY_PATTERN = /^[a-zA-Z%]$/;
8
+
9
+ type Props = {
10
+ unit: SizeUnit;
11
+ units: SizeUnit[];
12
+ onUnitChange: ( unit: SizeUnit ) => void;
13
+ };
14
+
15
+ export const useSizeUnitKeyboard = ( { unit, units, onUnitChange }: Props ) => {
16
+ const { appendKey, startsWith } = useTypingBuffer();
17
+
18
+ const onUnitKeyDown = ( event: React.KeyboardEvent< HTMLInputElement > ) => {
19
+ if ( units.length === 0 ) {
20
+ return;
21
+ }
22
+
23
+ const { key, altKey, ctrlKey, metaKey } = event;
24
+
25
+ if ( altKey || ctrlKey || metaKey ) {
26
+ return;
27
+ }
28
+
29
+ if ( isExtendedUnit( unit ) && isNumericValue( key ) ) {
30
+ const [ defaultUnit ] = units;
31
+
32
+ if ( defaultUnit ) {
33
+ onUnitChange( defaultUnit );
34
+ }
35
+
36
+ return;
37
+ }
38
+
39
+ if ( ! UNIT_KEY_PATTERN.test( key ) ) {
40
+ return;
41
+ }
42
+
43
+ event.preventDefault();
44
+
45
+ const updatedBuffer = appendKey( key.toLowerCase() );
46
+ const matchedUnit = units.find( ( u ) => startsWith( u, updatedBuffer ) );
47
+
48
+ if ( matchedUnit ) {
49
+ onUnitChange( matchedUnit );
50
+ }
51
+ };
52
+
53
+ return { onUnitKeyDown };
54
+ };
55
+
56
+ const isNumericValue = ( value: unknown ): boolean => {
57
+ if ( typeof value === 'number' ) {
58
+ return ! isNaN( value );
59
+ }
60
+
61
+ if ( typeof value === 'string' ) {
62
+ return value.trim() !== '' && ! isNaN( Number( value ) );
63
+ }
64
+
65
+ return false;
66
+ };
@@ -0,0 +1,71 @@
1
+ import { useMemo } from 'react';
2
+ import { type SizePropValue } from '@elementor/editor-props';
3
+
4
+ import { useSyncExternalState } from '../../../hooks/use-sync-external-state';
5
+ import { type SetSizeValue, type SizeUnit } from '../types';
6
+ import { isExtendedUnit } from '../utils/is-extended-unit';
7
+ import { createDefaultSizeValue, resolveSizeOnUnitChange, resolveSizeValue } from '../utils/resolve-size-value';
8
+
9
+ type SizeValue = SizePropValue[ 'value' ];
10
+
11
+ type UseSizeValueProps< T, U > = {
12
+ value: T | null;
13
+ setValue: SetSizeValue< T >;
14
+ units: U[];
15
+ defaultUnit?: U;
16
+ };
17
+
18
+ export const useSizeValue = < T extends SizeValue, U extends SizeUnit >( {
19
+ value,
20
+ setValue,
21
+ units,
22
+ defaultUnit,
23
+ }: UseSizeValueProps< T, U > ) => {
24
+ const resolvedValue = useMemo(
25
+ () => resolveSizeValue( value, { units, defaultUnit } ) as T,
26
+ // eslint-disable-next-line react-hooks/exhaustive-deps
27
+ [ value?.size, value?.unit, defaultUnit ]
28
+ );
29
+
30
+ const [ sizeValue, setSizeValue ] = useSyncExternalState< T >( {
31
+ external: resolvedValue,
32
+ setExternal: ( newState, options, meta ) => {
33
+ if ( newState !== null ) {
34
+ setValue( newState, options, meta );
35
+ }
36
+ },
37
+ persistWhen: ( next ) => hasChanged( next, resolvedValue ),
38
+ fallback: () => createDefaultSizeValue( units, defaultUnit ),
39
+ } );
40
+
41
+ const setSize = ( newSize: string, isInputValid = true ) => {
42
+ if ( isExtendedUnit( sizeValue.unit ) ) {
43
+ return;
44
+ }
45
+
46
+ const trimmed = newSize.trim();
47
+ const parsed = Number( trimmed );
48
+
49
+ const newState = {
50
+ ...sizeValue,
51
+ size: trimmed && ! isNaN( parsed ) ? parsed : '',
52
+ };
53
+
54
+ setSizeValue( newState, undefined, { validation: () => isInputValid } );
55
+ };
56
+
57
+ const setUnit = ( unit: SizeValue[ 'unit' ] ) => {
58
+ setSizeValue( { unit, size: resolveSizeOnUnitChange( sizeValue.size, unit ) } as T );
59
+ };
60
+
61
+ return {
62
+ size: sizeValue.size,
63
+ unit: sizeValue.unit,
64
+ setSize,
65
+ setUnit,
66
+ };
67
+ };
68
+
69
+ const hasChanged = ( next?: SizeValue | null, current?: SizeValue | null ): boolean => {
70
+ return next?.size !== current?.size || next?.unit !== current?.unit;
71
+ };
@@ -0,0 +1,94 @@
1
+ import * as React from 'react';
2
+ import { type RefObject, useEffect } from 'react';
3
+ import type { SizePropValue } from '@elementor/editor-props';
4
+ import { useActiveBreakpoint } from '@elementor/editor-responsive';
5
+ import { Box, usePopupState } from '@elementor/ui';
6
+
7
+ import { SizeField, type SizeFieldProps } from './size-field';
8
+ import { TextFieldPopover } from './ui/text-field-popover';
9
+ import { EXTENDED_UNITS } from './utils/resolve-size-value';
10
+
11
+ type SizeValue = SizePropValue[ 'value' ];
12
+
13
+ type SizeUnit = SizePropValue[ 'value' ][ 'unit' ];
14
+
15
+ type Props = SizeFieldProps< SizeValue, SizeUnit > & {
16
+ anchorRef?: RefObject< HTMLDivElement | null >;
17
+ isUnitActive?: boolean;
18
+ SizeFieldWrapper?: React.ComponentType< { children: React.ReactNode } >;
19
+ };
20
+
21
+ export const SizeComponent = ( {
22
+ anchorRef,
23
+ isUnitActive,
24
+ SizeFieldWrapper = React.Fragment,
25
+ ...sizeFieldProps
26
+ }: Props ) => {
27
+ const popupState = usePopupState( { variant: 'popover' } );
28
+ const activeBreakpoint = useActiveBreakpoint();
29
+
30
+ const isCustomUnit = sizeFieldProps?.value?.unit === EXTENDED_UNITS.custom;
31
+ const hasCustomUnitOption = sizeFieldProps.units.includes( EXTENDED_UNITS.custom );
32
+
33
+ useEffect( () => {
34
+ if ( popupState && popupState.isOpen ) {
35
+ popupState.close();
36
+ }
37
+ // eslint-disable-next-line react-hooks/exhaustive-deps
38
+ }, [ activeBreakpoint ] );
39
+
40
+ const handleCustomSizeChange = ( event: React.ChangeEvent< HTMLInputElement > ) => {
41
+ sizeFieldProps.setValue( {
42
+ size: event.target.value,
43
+ unit: EXTENDED_UNITS.custom,
44
+ } );
45
+ };
46
+
47
+ const handleSizeFieldClick = ( event: React.MouseEvent ) => {
48
+ if ( ( event.target as HTMLElement ).closest( 'input' ) && isCustomUnit ) {
49
+ popupState.open( anchorRef?.current );
50
+ }
51
+ };
52
+
53
+ const handleUnitChange = ( unit: SizeUnit ) => {
54
+ if ( unit === EXTENDED_UNITS.custom && anchorRef?.current ) {
55
+ popupState.open( anchorRef.current );
56
+ }
57
+ };
58
+
59
+ const popupAttributes = {
60
+ 'aria-controls': popupState.isOpen ? popupState.popupId : undefined,
61
+ 'aria-haspopup': true,
62
+ };
63
+
64
+ return (
65
+ <>
66
+ <SizeFieldWrapper>
67
+ <Box>
68
+ <SizeField
69
+ focused={ popupState.isOpen ? true : undefined }
70
+ onUnitChange={ handleUnitChange }
71
+ InputProps={ {
72
+ ...popupAttributes,
73
+ onClick: handleSizeFieldClick,
74
+ } }
75
+ unitSelectorProps={ {
76
+ menuItemsAttributes: hasCustomUnitOption ? { custom: popupAttributes } : undefined,
77
+ isActive: isUnitActive,
78
+ } }
79
+ { ...sizeFieldProps }
80
+ />
81
+ </Box>
82
+ </SizeFieldWrapper>
83
+ { popupState.isOpen && anchorRef?.current && (
84
+ <TextFieldPopover
85
+ popupState={ popupState }
86
+ anchorRef={ anchorRef as RefObject< HTMLDivElement > }
87
+ value={ String( sizeFieldProps?.value?.size ?? '' ) }
88
+ onChange={ handleCustomSizeChange }
89
+ onClose={ () => {} }
90
+ />
91
+ ) }
92
+ </>
93
+ );
94
+ };
@@ -0,0 +1,113 @@
1
+ import * as React from 'react';
2
+ import type { SizePropValue } from '@elementor/editor-props';
3
+ import { MathFunctionIcon } from '@elementor/icons';
4
+ import { InputAdornment, type TextFieldProps } from '@elementor/ui';
5
+
6
+ import { useSizeUnitKeyboard } from './hooks/use-size-unit-keyboard';
7
+ import { useSizeValue } from './hooks/use-size-value';
8
+ import { type SetSizeValue, type SizeUnit } from './types';
9
+ import { SizeInput } from './ui/size-input';
10
+ import { UnitSelector, type UnitSelectorProps } from './ui/unit-selector';
11
+ import { hasSizeValue } from './utils/has-size-value';
12
+ import { isExtendedUnit } from './utils/is-extended-unit';
13
+
14
+ export type SizeFieldProps< TValue extends SizePropValue[ 'value' ], TUnit extends SizeUnit > = {
15
+ units: TUnit[];
16
+ value: TValue | null;
17
+ placeholder?: string;
18
+ defaultUnit?: SizeUnit;
19
+ startIcon?: React.ReactNode;
20
+ setValue: SetSizeValue< TValue >;
21
+ onBlur?: ( event: React.FocusEvent< HTMLInputElement > ) => void;
22
+ onKeyDown?: ( event: React.KeyboardEvent< HTMLInputElement > ) => void;
23
+ onUnitChange?: ( unit: SizeUnit ) => void;
24
+ disabled?: boolean;
25
+ focused?: boolean;
26
+ ariaLabel?: string;
27
+ min?: number;
28
+ InputProps?: TextFieldProps[ 'InputProps' ];
29
+ unitSelectorProps?: Partial< UnitSelectorProps< TUnit > >;
30
+ };
31
+
32
+ const UNIT_DISPLAY_LABELS_OVERRIDES: Partial< Record< SizeUnit, React.ReactNode > > = {
33
+ custom: <MathFunctionIcon fontSize="tiny" />,
34
+ };
35
+
36
+ export const SizeField = < T extends SizePropValue[ 'value' ], U extends SizeUnit >( {
37
+ value,
38
+ focused,
39
+ disabled,
40
+ InputProps,
41
+ defaultUnit,
42
+ placeholder,
43
+ onUnitChange,
44
+ startIcon,
45
+ ariaLabel,
46
+ onKeyDown,
47
+ setValue,
48
+ onBlur,
49
+ units,
50
+ min,
51
+ unitSelectorProps,
52
+ }: SizeFieldProps< T, U > ) => {
53
+ const { size, unit, setSize, setUnit } = useSizeValue( { value, setValue, units, defaultUnit } );
54
+
55
+ const handleUnitChange = ( newUnit: SizeUnit ) => {
56
+ setUnit( newUnit );
57
+
58
+ onUnitChange?.( newUnit );
59
+ };
60
+
61
+ const { onUnitKeyDown } = useSizeUnitKeyboard( { unit, onUnitChange: handleUnitChange, units } );
62
+
63
+ const handleKeyDown = ( event: React.KeyboardEvent< HTMLInputElement > ) => {
64
+ onUnitKeyDown( event );
65
+
66
+ onKeyDown?.( event );
67
+ };
68
+
69
+ const handleChange = ( event: React.ChangeEvent< HTMLInputElement > ) => {
70
+ const newSize = event.target.value;
71
+ const isInputValid = event.target.validity.valid;
72
+
73
+ setSize( newSize, isInputValid );
74
+ };
75
+
76
+ const inputType = isExtendedUnit( unit ) ? 'text' : 'number';
77
+
78
+ return (
79
+ <SizeInput
80
+ disabled={ disabled }
81
+ focused={ focused }
82
+ type={ inputType }
83
+ value={ size }
84
+ placeholder={ placeholder }
85
+ onBlur={ onBlur }
86
+ onKeyDown={ handleKeyDown }
87
+ onChange={ handleChange }
88
+ InputProps={ {
89
+ ...InputProps,
90
+ autoComplete: 'off',
91
+ readOnly: isExtendedUnit( unit ),
92
+ startAdornment: startIcon && (
93
+ <InputAdornment position="start" disabled={ disabled }>
94
+ { startIcon }
95
+ </InputAdornment>
96
+ ),
97
+ endAdornment: (
98
+ <InputAdornment position="end">
99
+ <UnitSelector< U >
100
+ options={ units }
101
+ value={ unit as U }
102
+ onSelect={ handleUnitChange }
103
+ isActive={ unitSelectorProps?.isActive ?? hasSizeValue( size ) }
104
+ { ...unitSelectorProps }
105
+ optionLabelOverrides={ UNIT_DISPLAY_LABELS_OVERRIDES }
106
+ />
107
+ </InputAdornment>
108
+ ),
109
+ } }
110
+ inputProps={ { min, step: 'any', 'arial-label': ariaLabel } }
111
+ />
112
+ );
113
+ };