@elementor/editor-controls 4.0.0-682 → 4.0.0-beta5
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/dist/index.d.mts +138 -74
- package/dist/index.d.ts +138 -74
- package/dist/index.js +822 -203
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +799 -185
- package/dist/index.mjs.map +1 -1
- package/package.json +15 -15
- package/src/components/inline-editor.tsx +16 -57
- package/src/controls/email-form-action-control.tsx +5 -5
- package/src/controls/size-control/hooks/use-size-unit-keyboard.tsx +12 -1
- package/src/controls/size-control/hooks/use-size-value.ts +12 -16
- package/src/controls/size-control/size-component.tsx +38 -17
- package/src/controls/size-control/size-field.tsx +37 -13
- package/src/controls/size-control/sync/get-units.ts +15 -1
- package/src/controls/size-control/types.ts +8 -0
- package/src/controls/size-control/unstable-size-control.tsx +86 -0
- package/src/controls/size-control/utils/has-size-value.ts +5 -0
- package/src/controls/size-control/utils/resolve-bound-prop-value.ts +72 -0
- package/src/controls/size-control/utils/resolve-size-value.ts +4 -3
- package/src/controls/size-control/utils/settings/get-default-unit.ts +7 -0
- package/src/controls/size-control/utils/settings/get-prop-type-settings.ts +12 -0
- package/src/controls/size-control/utils/settings/get-size-units.ts +23 -0
- package/src/controls/size-control/utils/should-nullify-value.ts +15 -0
- package/src/controls/svg-media-control.tsx +5 -4
- package/src/controls/transition-control/data.ts +3 -3
- package/src/controls/transition-control/transition-repeater-control.tsx +8 -2
- package/src/index.ts +2 -0
- package/src/controls/size-control/utils/is-numeric-value.ts +0 -11
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-
|
|
4
|
+
"version": "4.0.0-beta5",
|
|
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-
|
|
44
|
-
"@elementor/editor-elements": "4.0.0-
|
|
45
|
-
"@elementor/editor-props": "4.0.0-
|
|
46
|
-
"@elementor/editor-responsive": "4.0.0-
|
|
47
|
-
"@elementor/editor-ui": "4.0.0-
|
|
48
|
-
"@elementor/editor-v1-adapters": "4.0.0-
|
|
49
|
-
"@elementor/env": "4.0.0-
|
|
50
|
-
"@elementor/http-client": "4.0.0-
|
|
43
|
+
"@elementor/editor-current-user": "4.0.0-beta5",
|
|
44
|
+
"@elementor/editor-elements": "4.0.0-beta5",
|
|
45
|
+
"@elementor/editor-props": "4.0.0-beta5",
|
|
46
|
+
"@elementor/editor-responsive": "4.0.0-beta5",
|
|
47
|
+
"@elementor/editor-ui": "4.0.0-beta5",
|
|
48
|
+
"@elementor/editor-v1-adapters": "4.0.0-beta5",
|
|
49
|
+
"@elementor/env": "4.0.0-beta5",
|
|
50
|
+
"@elementor/http-client": "4.0.0-beta5",
|
|
51
51
|
"@elementor/icons": "^1.68.0",
|
|
52
|
-
"@elementor/locations": "4.0.0-
|
|
53
|
-
"@elementor/events": "4.0.0-
|
|
54
|
-
"@elementor/query": "4.0.0-
|
|
55
|
-
"@elementor/session": "4.0.0-
|
|
52
|
+
"@elementor/locations": "4.0.0-beta5",
|
|
53
|
+
"@elementor/events": "4.0.0-beta5",
|
|
54
|
+
"@elementor/query": "4.0.0-beta5",
|
|
55
|
+
"@elementor/session": "4.0.0-beta5",
|
|
56
56
|
"@elementor/ui": "1.36.17",
|
|
57
|
-
"@elementor/utils": "4.0.0-
|
|
58
|
-
"@elementor/wp-media": "4.0.0-
|
|
57
|
+
"@elementor/utils": "4.0.0-beta5",
|
|
58
|
+
"@elementor/wp-media": "4.0.0-beta5",
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
174
|
-
|
|
175
|
-
|
|
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
|
-
|
|
189
|
-
|
|
190
|
-
|
|
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 );
|
|
@@ -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
|
|
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
|
|
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
|
|
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>{ __( '
|
|
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
|
-
<
|
|
142
|
+
<ControlLabel>{ __( 'Email settings', 'elementor' ) }</ControlLabel>
|
|
143
143
|
<SendToField />
|
|
144
144
|
<SubjectField />
|
|
145
145
|
<MessageField />
|
|
@@ -3,7 +3,6 @@ import type * as React from 'react';
|
|
|
3
3
|
import { useTypingBuffer } from '../../../hooks/use-typing-buffer';
|
|
4
4
|
import { type SizeUnit } from '../types';
|
|
5
5
|
import { isExtendedUnit } from '../utils/is-extended-unit';
|
|
6
|
-
import { isNumericValue } from '../utils/is-numeric-value';
|
|
7
6
|
|
|
8
7
|
const UNIT_KEY_PATTERN = /^[a-zA-Z%]$/;
|
|
9
8
|
|
|
@@ -53,3 +52,15 @@ export const useSizeUnitKeyboard = ( { unit, units, onUnitChange }: Props ) => {
|
|
|
53
52
|
|
|
54
53
|
return { onUnitKeyDown };
|
|
55
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
|
+
};
|
|
@@ -2,7 +2,7 @@ import { useMemo } from 'react';
|
|
|
2
2
|
import { type SizePropValue } from '@elementor/editor-props';
|
|
3
3
|
|
|
4
4
|
import { useSyncExternalState } from '../../../hooks/use-sync-external-state';
|
|
5
|
-
import { type SizeUnit } from '../types';
|
|
5
|
+
import { type SetSizeValue, type SizeUnit } from '../types';
|
|
6
6
|
import { isExtendedUnit } from '../utils/is-extended-unit';
|
|
7
7
|
import { createDefaultSizeValue, resolveSizeOnUnitChange, resolveSizeValue } from '../utils/resolve-size-value';
|
|
8
8
|
|
|
@@ -10,39 +10,35 @@ type SizeValue = SizePropValue[ 'value' ];
|
|
|
10
10
|
|
|
11
11
|
type UseSizeValueProps< T, U > = {
|
|
12
12
|
value: T | null;
|
|
13
|
-
|
|
13
|
+
setValue: SetSizeValue< T >;
|
|
14
14
|
units: U[];
|
|
15
15
|
defaultUnit?: U;
|
|
16
16
|
};
|
|
17
17
|
|
|
18
18
|
export const useSizeValue = < T extends SizeValue, U extends SizeUnit >( {
|
|
19
19
|
value,
|
|
20
|
-
|
|
20
|
+
setValue,
|
|
21
21
|
units,
|
|
22
22
|
defaultUnit,
|
|
23
23
|
}: UseSizeValueProps< T, U > ) => {
|
|
24
24
|
const resolvedValue = useMemo(
|
|
25
|
-
() => resolveSizeValue( value, { units, defaultUnit } ),
|
|
25
|
+
() => resolveSizeValue( value, { units, defaultUnit } ) as T,
|
|
26
26
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
27
27
|
[ value?.size, value?.unit, defaultUnit ]
|
|
28
28
|
);
|
|
29
29
|
|
|
30
30
|
const [ sizeValue, setSizeValue ] = useSyncExternalState< T >( {
|
|
31
|
-
external: resolvedValue
|
|
32
|
-
setExternal: ( newState ) => {
|
|
33
|
-
// TODO we need to check behaviour that low level doesn't set to null only the high level components size component
|
|
34
|
-
// This will fix the issue of if size is empty string '' it gets sends to the model
|
|
35
|
-
// but on blur the size component set to null.
|
|
36
|
-
// But we need to test this behaviour
|
|
31
|
+
external: resolvedValue,
|
|
32
|
+
setExternal: ( newState, options, meta ) => {
|
|
37
33
|
if ( newState !== null ) {
|
|
38
|
-
|
|
34
|
+
setValue( newState, options, meta );
|
|
39
35
|
}
|
|
40
|
-
},
|
|
41
|
-
persistWhen: ( next ) => hasChanged( next, resolvedValue
|
|
42
|
-
fallback: () => createDefaultSizeValue
|
|
36
|
+
},
|
|
37
|
+
persistWhen: ( next ) => hasChanged( next, resolvedValue ),
|
|
38
|
+
fallback: () => createDefaultSizeValue( units, defaultUnit ),
|
|
43
39
|
} );
|
|
44
40
|
|
|
45
|
-
const setSize = ( newSize: string ) => {
|
|
41
|
+
const setSize = ( newSize: string, isInputValid = true ) => {
|
|
46
42
|
if ( isExtendedUnit( sizeValue.unit ) ) {
|
|
47
43
|
return;
|
|
48
44
|
}
|
|
@@ -55,7 +51,7 @@ export const useSizeValue = < T extends SizeValue, U extends SizeUnit >( {
|
|
|
55
51
|
size: trimmed && ! isNaN( parsed ) ? parsed : '',
|
|
56
52
|
};
|
|
57
53
|
|
|
58
|
-
setSizeValue( newState );
|
|
54
|
+
setSizeValue( newState, undefined, { validation: () => isInputValid } );
|
|
59
55
|
};
|
|
60
56
|
|
|
61
57
|
const setUnit = ( unit: SizeValue[ 'unit' ] ) => {
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import * as React from 'react';
|
|
2
2
|
import { type RefObject, useEffect } from 'react';
|
|
3
3
|
import type { SizePropValue } from '@elementor/editor-props';
|
|
4
|
-
import {
|
|
4
|
+
import { useActiveBreakpoint } from '@elementor/editor-responsive';
|
|
5
|
+
import { Box, usePopupState } from '@elementor/ui';
|
|
5
6
|
|
|
6
7
|
import { SizeField, type SizeFieldProps } from './size-field';
|
|
7
8
|
import { TextFieldPopover } from './ui/text-field-popover';
|
|
@@ -13,24 +14,31 @@ type SizeUnit = SizePropValue[ 'value' ][ 'unit' ];
|
|
|
13
14
|
|
|
14
15
|
type Props = SizeFieldProps< SizeValue, SizeUnit > & {
|
|
15
16
|
anchorRef?: RefObject< HTMLDivElement | null >;
|
|
17
|
+
isUnitActive?: boolean;
|
|
18
|
+
SizeFieldWrapper?: React.ComponentType< { children: React.ReactNode } >;
|
|
16
19
|
};
|
|
17
20
|
|
|
18
|
-
export const SizeComponent = ( {
|
|
21
|
+
export const SizeComponent = ( {
|
|
22
|
+
anchorRef,
|
|
23
|
+
isUnitActive,
|
|
24
|
+
SizeFieldWrapper = React.Fragment,
|
|
25
|
+
...sizeFieldProps
|
|
26
|
+
}: Props ) => {
|
|
19
27
|
const popupState = usePopupState( { variant: 'popover' } );
|
|
28
|
+
const activeBreakpoint = useActiveBreakpoint();
|
|
20
29
|
|
|
21
30
|
const isCustomUnit = sizeFieldProps?.value?.unit === EXTENDED_UNITS.custom;
|
|
22
31
|
const hasCustomUnitOption = sizeFieldProps.units.includes( EXTENDED_UNITS.custom );
|
|
23
32
|
|
|
24
33
|
useEffect( () => {
|
|
25
|
-
if (
|
|
26
|
-
popupState.
|
|
34
|
+
if ( popupState && popupState.isOpen ) {
|
|
35
|
+
popupState.close();
|
|
27
36
|
}
|
|
28
|
-
|
|
29
37
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
30
|
-
}, [
|
|
38
|
+
}, [ activeBreakpoint ] );
|
|
31
39
|
|
|
32
40
|
const handleCustomSizeChange = ( event: React.ChangeEvent< HTMLInputElement > ) => {
|
|
33
|
-
sizeFieldProps.
|
|
41
|
+
sizeFieldProps.setValue( {
|
|
34
42
|
size: event.target.value,
|
|
35
43
|
unit: EXTENDED_UNITS.custom,
|
|
36
44
|
} );
|
|
@@ -42,6 +50,12 @@ export const SizeComponent = ( { anchorRef, ...sizeFieldProps }: Props ) => {
|
|
|
42
50
|
}
|
|
43
51
|
};
|
|
44
52
|
|
|
53
|
+
const handleUnitChange = ( unit: SizeUnit ) => {
|
|
54
|
+
if ( unit === EXTENDED_UNITS.custom && anchorRef?.current ) {
|
|
55
|
+
popupState.open( anchorRef.current );
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
|
|
45
59
|
const popupAttributes = {
|
|
46
60
|
'aria-controls': popupState.isOpen ? popupState.popupId : undefined,
|
|
47
61
|
'aria-haspopup': true,
|
|
@@ -49,16 +63,23 @@ export const SizeComponent = ( { anchorRef, ...sizeFieldProps }: Props ) => {
|
|
|
49
63
|
|
|
50
64
|
return (
|
|
51
65
|
<>
|
|
52
|
-
<
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
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>
|
|
62
83
|
{ popupState.isOpen && anchorRef?.current && (
|
|
63
84
|
<TextFieldPopover
|
|
64
85
|
popupState={ popupState }
|
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
import * as React from 'react';
|
|
2
|
-
import type {
|
|
2
|
+
import type { SizePropValue } from '@elementor/editor-props';
|
|
3
3
|
import { MathFunctionIcon } from '@elementor/icons';
|
|
4
4
|
import { InputAdornment, type TextFieldProps } from '@elementor/ui';
|
|
5
5
|
|
|
6
6
|
import { useSizeUnitKeyboard } from './hooks/use-size-unit-keyboard';
|
|
7
7
|
import { useSizeValue } from './hooks/use-size-value';
|
|
8
|
-
import { type SizeUnit } from './types';
|
|
8
|
+
import { type SetSizeValue, type SizeUnit } from './types';
|
|
9
9
|
import { SizeInput } from './ui/size-input';
|
|
10
10
|
import { UnitSelector, type UnitSelectorProps } from './ui/unit-selector';
|
|
11
|
+
import { hasSizeValue } from './utils/has-size-value';
|
|
11
12
|
import { isExtendedUnit } from './utils/is-extended-unit';
|
|
12
13
|
|
|
13
14
|
export type SizeFieldProps< TValue extends SizePropValue[ 'value' ], TUnit extends SizeUnit > = {
|
|
@@ -16,10 +17,14 @@ export type SizeFieldProps< TValue extends SizePropValue[ 'value' ], TUnit exten
|
|
|
16
17
|
placeholder?: string;
|
|
17
18
|
defaultUnit?: SizeUnit;
|
|
18
19
|
startIcon?: React.ReactNode;
|
|
19
|
-
|
|
20
|
+
setValue: SetSizeValue< TValue >;
|
|
20
21
|
onBlur?: ( event: React.FocusEvent< HTMLInputElement > ) => void;
|
|
21
22
|
onKeyDown?: ( event: React.KeyboardEvent< HTMLInputElement > ) => void;
|
|
23
|
+
onUnitChange?: ( unit: SizeUnit ) => void;
|
|
22
24
|
disabled?: boolean;
|
|
25
|
+
focused?: boolean;
|
|
26
|
+
ariaLabel?: string;
|
|
27
|
+
min?: number;
|
|
23
28
|
InputProps?: TextFieldProps[ 'InputProps' ];
|
|
24
29
|
unitSelectorProps?: Partial< UnitSelectorProps< TUnit > >;
|
|
25
30
|
};
|
|
@@ -30,18 +35,30 @@ const UNIT_DISPLAY_LABELS_OVERRIDES: Partial< Record< SizeUnit, React.ReactNode
|
|
|
30
35
|
|
|
31
36
|
export const SizeField = < T extends SizePropValue[ 'value' ], U extends SizeUnit >( {
|
|
32
37
|
value,
|
|
38
|
+
focused,
|
|
33
39
|
disabled,
|
|
34
40
|
InputProps,
|
|
35
41
|
defaultUnit,
|
|
42
|
+
placeholder,
|
|
43
|
+
onUnitChange,
|
|
36
44
|
startIcon,
|
|
45
|
+
ariaLabel,
|
|
37
46
|
onKeyDown,
|
|
38
|
-
|
|
47
|
+
setValue,
|
|
39
48
|
onBlur,
|
|
40
49
|
units,
|
|
50
|
+
min,
|
|
41
51
|
unitSelectorProps,
|
|
42
52
|
}: SizeFieldProps< T, U > ) => {
|
|
43
|
-
const { size, unit, setSize, setUnit } = useSizeValue( { value,
|
|
44
|
-
|
|
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 } );
|
|
45
62
|
|
|
46
63
|
const handleKeyDown = ( event: React.KeyboardEvent< HTMLInputElement > ) => {
|
|
47
64
|
onUnitKeyDown( event );
|
|
@@ -49,15 +66,25 @@ export const SizeField = < T extends SizePropValue[ 'value' ], U extends SizeUni
|
|
|
49
66
|
onKeyDown?.( event );
|
|
50
67
|
};
|
|
51
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
|
+
|
|
52
76
|
const inputType = isExtendedUnit( unit ) ? 'text' : 'number';
|
|
53
77
|
|
|
54
78
|
return (
|
|
55
79
|
<SizeInput
|
|
80
|
+
disabled={ disabled }
|
|
81
|
+
focused={ focused }
|
|
56
82
|
type={ inputType }
|
|
57
83
|
value={ size }
|
|
84
|
+
placeholder={ placeholder }
|
|
58
85
|
onBlur={ onBlur }
|
|
59
86
|
onKeyDown={ handleKeyDown }
|
|
60
|
-
onChange={
|
|
87
|
+
onChange={ handleChange }
|
|
61
88
|
InputProps={ {
|
|
62
89
|
...InputProps,
|
|
63
90
|
autoComplete: 'off',
|
|
@@ -72,18 +99,15 @@ export const SizeField = < T extends SizePropValue[ 'value' ], U extends SizeUni
|
|
|
72
99
|
<UnitSelector< U >
|
|
73
100
|
options={ units }
|
|
74
101
|
value={ unit as U }
|
|
75
|
-
onSelect={
|
|
76
|
-
isActive={
|
|
102
|
+
onSelect={ handleUnitChange }
|
|
103
|
+
isActive={ unitSelectorProps?.isActive ?? hasSizeValue( size ) }
|
|
77
104
|
{ ...unitSelectorProps }
|
|
78
105
|
optionLabelOverrides={ UNIT_DISPLAY_LABELS_OVERRIDES }
|
|
79
106
|
/>
|
|
80
107
|
</InputAdornment>
|
|
81
108
|
),
|
|
82
109
|
} }
|
|
110
|
+
inputProps={ { min, step: 'any', 'arial-label': ariaLabel } }
|
|
83
111
|
/>
|
|
84
112
|
);
|
|
85
113
|
};
|
|
86
|
-
|
|
87
|
-
const hasValue = ( value: PropValue ): boolean => {
|
|
88
|
-
return Boolean( value ) || value === 0;
|
|
89
|
-
};
|
|
@@ -1,3 +1,17 @@
|
|
|
1
|
+
import { type SizeUnit } from '../types';
|
|
2
|
+
|
|
3
|
+
export const getLengthUnits = () => {
|
|
4
|
+
return ( window.elementor?.config?.size_units?.length ?? [] ) as SizeUnit[];
|
|
5
|
+
};
|
|
6
|
+
|
|
7
|
+
export const getAngleUnits = () => {
|
|
8
|
+
return ( window.elementor?.config?.size_units?.angle ?? [] ) as SizeUnit[];
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
export const getTimeUnits = () => {
|
|
12
|
+
return ( window.elementor?.config?.size_units?.time ?? [] ) as SizeUnit[];
|
|
13
|
+
};
|
|
14
|
+
|
|
1
15
|
export const getExtendedUnits = () => {
|
|
2
|
-
return window.elementor?.config?.size_units?.extended_units ?? [];
|
|
16
|
+
return ( window.elementor?.config?.size_units?.extended_units ?? [] ) as SizeUnit[];
|
|
3
17
|
};
|
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
import type { CreateOptions } from '@elementor/editor-props';
|
|
2
|
+
|
|
3
|
+
import { type SetValueMeta } from '../../bound-prop-context';
|
|
4
|
+
|
|
1
5
|
type LengthUnit = 'px' | '%' | 'em' | 'rem' | 'vw' | 'vh' | 'ch';
|
|
2
6
|
type AngleUnit = 'deg' | 'rad' | 'grad' | 'turn';
|
|
3
7
|
type TimeUnit = 's' | 'ms';
|
|
@@ -7,3 +11,7 @@ type ExtendedSizeOption = 'auto' | 'custom';
|
|
|
7
11
|
type Unit = LengthUnit | AngleUnit | TimeUnit;
|
|
8
12
|
|
|
9
13
|
export type SizeUnit = Unit | ExtendedSizeOption;
|
|
14
|
+
|
|
15
|
+
export type SizeVariant = 'length' | 'angle' | 'time';
|
|
16
|
+
|
|
17
|
+
export type SetSizeValue< T > = ( value: T, options?: CreateOptions, meta?: SetValueMeta ) => void;
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
import type { RefObject } from 'react';
|
|
3
|
+
import { type CreateOptions, sizePropTypeUtil, type SizePropValue } from '@elementor/editor-props';
|
|
4
|
+
|
|
5
|
+
import { type SetValueMeta, useBoundProp } from '../../bound-prop-context';
|
|
6
|
+
import ControlActions from '../../control-actions/control-actions';
|
|
7
|
+
import { createControl } from '../../create-control';
|
|
8
|
+
import { SizeComponent } from './size-component';
|
|
9
|
+
import { type SizeVariant } from './types';
|
|
10
|
+
import { resolveBoundPropValue } from './utils/resolve-bound-prop-value';
|
|
11
|
+
import { getDefaultUnit } from './utils/settings/get-default-unit';
|
|
12
|
+
import { getSizeUnits } from './utils/settings/get-size-units';
|
|
13
|
+
import { shouldNullifyValue } from './utils/should-nullify-value';
|
|
14
|
+
|
|
15
|
+
type Props = {
|
|
16
|
+
placeholder?: string;
|
|
17
|
+
variant?: SizeVariant;
|
|
18
|
+
anchorRef?: RefObject< HTMLDivElement | null >;
|
|
19
|
+
startIcon?: React.ReactNode;
|
|
20
|
+
ariaLabel?: string;
|
|
21
|
+
min?: number;
|
|
22
|
+
id?: string;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
export const UnstableSizeControl = createControl(
|
|
26
|
+
( { variant = 'length', placeholder: propPlaceholder, anchorRef, startIcon, ariaLabel, min }: Props ) => {
|
|
27
|
+
const {
|
|
28
|
+
value,
|
|
29
|
+
setValue,
|
|
30
|
+
propType,
|
|
31
|
+
placeholder: boundPropPlaceholder,
|
|
32
|
+
restoreValue,
|
|
33
|
+
} = useBoundProp( sizePropTypeUtil );
|
|
34
|
+
|
|
35
|
+
const { sizeValue, isUnitHighlighted, placeholder } = resolveBoundPropValue(
|
|
36
|
+
value,
|
|
37
|
+
boundPropPlaceholder,
|
|
38
|
+
propPlaceholder
|
|
39
|
+
);
|
|
40
|
+
|
|
41
|
+
const units = getSizeUnits( propType, variant );
|
|
42
|
+
const defaultUnit = getDefaultUnit( propType );
|
|
43
|
+
|
|
44
|
+
const handleBlur = () => {
|
|
45
|
+
const isRequired = propType.settings.required;
|
|
46
|
+
|
|
47
|
+
if ( shouldNullifyValue( value ) && ! isRequired ) {
|
|
48
|
+
setValue( null );
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
if ( isRequired ) {
|
|
52
|
+
restoreValue();
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
const handleChange = ( newValue: SizePropValue[ 'value' ], options?: CreateOptions, meta?: SetValueMeta ) => {
|
|
57
|
+
setValue( newValue, options, {
|
|
58
|
+
...meta,
|
|
59
|
+
validation: () => {
|
|
60
|
+
if ( propType.settings.required ) {
|
|
61
|
+
return newValue.size !== '';
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
return meta?.validation ? meta.validation( newValue ) : true;
|
|
65
|
+
},
|
|
66
|
+
} );
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
return (
|
|
70
|
+
<SizeComponent
|
|
71
|
+
units={ units }
|
|
72
|
+
value={ sizeValue }
|
|
73
|
+
anchorRef={ anchorRef }
|
|
74
|
+
placeholder={ placeholder }
|
|
75
|
+
defaultUnit={ defaultUnit }
|
|
76
|
+
isUnitActive={ isUnitHighlighted }
|
|
77
|
+
onBlur={ handleBlur }
|
|
78
|
+
setValue={ handleChange }
|
|
79
|
+
SizeFieldWrapper={ ControlActions }
|
|
80
|
+
startIcon={ startIcon }
|
|
81
|
+
ariaLabel={ ariaLabel }
|
|
82
|
+
min={ min }
|
|
83
|
+
/>
|
|
84
|
+
);
|
|
85
|
+
}
|
|
86
|
+
);
|