@elementor/editor-controls 0.10.0 → 0.11.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.
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": "0.10.0",
4
+ "version": "0.11.0",
5
5
  "private": false,
6
6
  "author": "Elementor Team",
7
7
  "homepage": "https://elementor.com/",
@@ -40,7 +40,7 @@
40
40
  "dev": "tsup --config=../../tsup.dev.ts"
41
41
  },
42
42
  "dependencies": {
43
- "@elementor/editor-props": "0.9.1",
43
+ "@elementor/editor-props": "0.9.2",
44
44
  "@elementor/env": "0.3.5",
45
45
  "@elementor/http": "0.1.3",
46
46
  "@elementor/icons": "1.31.0",
@@ -0,0 +1,181 @@
1
+ import * as React from 'react';
2
+ import { forwardRef } from 'react';
3
+ import { XIcon } from '@elementor/icons';
4
+ import {
5
+ Autocomplete as AutocompleteBase,
6
+ type AutocompleteRenderInputParams,
7
+ Box,
8
+ IconButton,
9
+ InputAdornment,
10
+ TextField,
11
+ } from '@elementor/ui';
12
+
13
+ export type FlatOption = {
14
+ id: string;
15
+ label: string;
16
+ groupLabel?: never;
17
+ };
18
+
19
+ export type CategorizedOption = {
20
+ id: string;
21
+ label: string;
22
+ groupLabel: string;
23
+ };
24
+
25
+ export type Props = {
26
+ options: FlatOption[] | CategorizedOption[];
27
+ value?: number | string | null;
28
+ onOptionChange: ( newValue: number | null ) => void;
29
+ onTextChange?: ( newValue: string | null ) => void;
30
+ allowCustomValues?: boolean;
31
+ placeholder?: string;
32
+ minInputLength?: number;
33
+ };
34
+
35
+ export const Autocomplete = forwardRef( ( props: Props, ref ) => {
36
+ const {
37
+ options,
38
+ onOptionChange,
39
+ onTextChange,
40
+ allowCustomValues = false,
41
+ placeholder = '',
42
+ minInputLength = 2,
43
+ value = '',
44
+ ...restProps
45
+ } = props;
46
+
47
+ const optionKeys = _factoryFilter( value, options, minInputLength ).map( ( { id } ) => id );
48
+ const allowClear = !! value;
49
+
50
+ // Prevents MUI warning when freeSolo/allowCustomValues is false
51
+ const muiWarningPreventer = allowCustomValues || !! value?.toString()?.length;
52
+
53
+ const isOptionEqualToValue = muiWarningPreventer ? undefined : () => true;
54
+
55
+ const isValueFromOptions = typeof value === 'number' && !! findMatchingOption( options, value );
56
+
57
+ return (
58
+ <AutocompleteBase
59
+ { ...restProps }
60
+ ref={ ref }
61
+ forcePopupIcon={ false }
62
+ disableClearable={ true } // Disabled component's auto clear icon to use our custom one instead
63
+ freeSolo={ allowCustomValues }
64
+ value={ value?.toString() || '' }
65
+ size={ 'tiny' }
66
+ onChange={ ( _, newValue ) => onOptionChange( Number( newValue ) ) }
67
+ readOnly={ isValueFromOptions }
68
+ options={ optionKeys }
69
+ getOptionKey={ ( optionId ) => findMatchingOption( options, optionId )?.id || optionId }
70
+ getOptionLabel={ ( optionId ) => findMatchingOption( options, optionId )?.label || optionId.toString() }
71
+ groupBy={
72
+ isCategorizedOptionPool( options )
73
+ ? ( optionId: string ) => findMatchingOption( options, optionId )?.groupLabel || optionId
74
+ : undefined
75
+ }
76
+ isOptionEqualToValue={ isOptionEqualToValue }
77
+ filterOptions={ () => optionKeys }
78
+ renderOption={ ( optionProps, optionId ) => (
79
+ <Box component="li" { ...optionProps } key={ optionProps.id }>
80
+ { findMatchingOption( options, optionId )?.label ?? optionId }
81
+ </Box>
82
+ ) }
83
+ renderInput={ ( params ) => (
84
+ <TextInput
85
+ params={ params }
86
+ handleChange={ ( newValue ) => onTextChange?.( newValue ) }
87
+ allowClear={ allowClear }
88
+ placeholder={ placeholder }
89
+ hasSelectedValue={ isValueFromOptions }
90
+ />
91
+ ) }
92
+ />
93
+ );
94
+ } );
95
+
96
+ const TextInput = ( {
97
+ params,
98
+ allowClear,
99
+ placeholder,
100
+ handleChange,
101
+ hasSelectedValue,
102
+ }: {
103
+ params: AutocompleteRenderInputParams;
104
+ allowClear: boolean;
105
+ handleChange: ( newValue: string | null ) => void;
106
+ placeholder: string;
107
+ hasSelectedValue: boolean;
108
+ } ) => {
109
+ const onChange = ( event: React.ChangeEvent< HTMLInputElement > ) => {
110
+ handleChange( event.target.value );
111
+ };
112
+
113
+ return (
114
+ <TextField
115
+ { ...params }
116
+ placeholder={ placeholder }
117
+ onChange={ onChange }
118
+ sx={ {
119
+ '& .MuiInputBase-input': {
120
+ cursor: hasSelectedValue ? 'default' : undefined,
121
+ },
122
+ } }
123
+ InputProps={ {
124
+ ...params.InputProps,
125
+ endAdornment: <ClearButton params={ params } allowClear={ allowClear } handleChange={ handleChange } />,
126
+ } }
127
+ />
128
+ );
129
+ };
130
+
131
+ const ClearButton = ( {
132
+ allowClear,
133
+ handleChange,
134
+ params,
135
+ }: {
136
+ params: AutocompleteRenderInputParams;
137
+ allowClear: boolean;
138
+ handleChange: ( newValue: string | null ) => void;
139
+ } ) => (
140
+ <InputAdornment position="end">
141
+ { allowClear && (
142
+ <IconButton size={ params.size } onClick={ () => handleChange( null ) } sx={ { cursor: 'pointer' } }>
143
+ <XIcon fontSize={ params.size } />
144
+ </IconButton>
145
+ ) }
146
+ </InputAdornment>
147
+ );
148
+
149
+ export function findMatchingOption(
150
+ options: FlatOption[] | CategorizedOption[],
151
+ optionId: string | number | null = null
152
+ ) {
153
+ const formattedOption = ( optionId || '' ).toString();
154
+
155
+ return options.find( ( { id } ) => formattedOption === id.toString() );
156
+ }
157
+
158
+ export function isCategorizedOptionPool( options: FlatOption[] | CategorizedOption[] ): options is CategorizedOption[] {
159
+ return options.every( ( option ) => 'groupLabel' in option );
160
+ }
161
+ function _factoryFilter< T extends FlatOption[] | CategorizedOption[] >(
162
+ newValue: string | number | null,
163
+ options: T,
164
+ minInputLength: number
165
+ ): T {
166
+ if ( null === newValue ) {
167
+ return options;
168
+ }
169
+
170
+ const formattedValue = String( newValue || '' )?.toLowerCase();
171
+
172
+ if ( formattedValue.length < minInputLength ) {
173
+ return new Array( 0 ) as T;
174
+ }
175
+
176
+ return options.filter(
177
+ ( option ) =>
178
+ String( option.id ).toLowerCase().includes( formattedValue ) ||
179
+ option.label.toLowerCase().includes( formattedValue )
180
+ ) as T;
181
+ }
@@ -30,7 +30,7 @@ export const BackgroundImageOverlayAttachment = () => {
30
30
  <Grid item xs={ 6 }>
31
31
  <ControlLabel>{ __( 'Attachment', 'elementor' ) }</ControlLabel>
32
32
  </Grid>
33
- <Grid item xs={ 6 } sx={ { display: 'flex', justifyContent: 'flex-end' } }>
33
+ <Grid item xs={ 6 } sx={ { display: 'flex', justifyContent: 'flex-end', overflow: 'hidden' } }>
34
34
  <ToggleControl options={ attachmentControlOptions } />
35
35
  </Grid>
36
36
  </Grid>
@@ -21,15 +21,15 @@ type Positions =
21
21
  | 'custom';
22
22
 
23
23
  const backgroundPositionOptions = [
24
- { label: __( 'Center Center', 'elementor' ), value: 'center center' },
25
- { label: __( 'Center Left', 'elementor' ), value: 'center left' },
26
- { label: __( 'Center Right', 'elementor' ), value: 'center right' },
27
- { label: __( 'Top Center', 'elementor' ), value: 'top center' },
28
- { label: __( 'Top Left', 'elementor' ), value: 'top left' },
29
- { label: __( 'Top Right', 'elementor' ), value: 'top right' },
30
- { label: __( 'Bottom Center', 'elementor' ), value: 'bottom center' },
31
- { label: __( 'Bottom Left', 'elementor' ), value: 'bottom left' },
32
- { label: __( 'Bottom Right', 'elementor' ), value: 'bottom right' },
24
+ { label: __( 'Center center', 'elementor' ), value: 'center center' },
25
+ { label: __( 'Center left', 'elementor' ), value: 'center left' },
26
+ { label: __( 'Center right', 'elementor' ), value: 'center right' },
27
+ { label: __( 'Top center', 'elementor' ), value: 'top center' },
28
+ { label: __( 'Top left', 'elementor' ), value: 'top left' },
29
+ { label: __( 'Top right', 'elementor' ), value: 'top right' },
30
+ { label: __( 'Bottom center', 'elementor' ), value: 'bottom center' },
31
+ { label: __( 'Bottom left', 'elementor' ), value: 'bottom left' },
32
+ { label: __( 'Bottom right', 'elementor' ), value: 'bottom right' },
33
33
  { label: __( 'Custom', 'elementor' ), value: 'custom' },
34
34
  ];
35
35
 
@@ -56,7 +56,7 @@ export const BackgroundImageOverlayPosition = () => {
56
56
  <Grid item xs={ 6 }>
57
57
  <ControlLabel>{ __( 'Position', 'elementor' ) }</ControlLabel>
58
58
  </Grid>
59
- <Grid item xs={ 6 } sx={ { display: 'flex', justifyContent: 'flex-end' } }>
59
+ <Grid item xs={ 6 } sx={ { display: 'flex', justifyContent: 'flex-end', overflow: 'hidden' } }>
60
60
  <Select
61
61
  size="tiny"
62
62
  value={ ( backgroundImageOffsetContext.value ? 'custom' : stringPropContext.value ) ?? '' }
@@ -98,7 +98,7 @@ const Content = ( { anchorEl }: { anchorEl: HTMLElement | null } ) => {
98
98
 
99
99
  const Control = ( { label, bind, children }: { bind: string; label: string; children: React.ReactNode } ) => (
100
100
  <PropKeyProvider bind={ bind }>
101
- <Grid item xs={ 6 }>
101
+ <Grid item xs={ 6 } sx={ { overflow: 'hidden' } }>
102
102
  <Grid container gap={ 1 } alignItems="center">
103
103
  <Grid item xs={ 12 }>
104
104
  <Typography component="label" variant="caption" color="text.secondary">
@@ -40,7 +40,7 @@ export const GapControl = createControl( ( { label }: { label: string } ) => {
40
40
  <Stack direction="row" gap={ 2 } flexWrap="nowrap">
41
41
  <ControlLabel>{ label }</ControlLabel>
42
42
  <ToggleButton
43
- aria-label={ __( 'Link Inputs', 'elementor' ) }
43
+ aria-label={ __( 'Link inputs', 'elementor' ) }
44
44
  size={ 'tiny' }
45
45
  value={ 'check' }
46
46
  selected={ isLinked }
@@ -30,7 +30,7 @@ export const ImageControl = createControl(
30
30
  <Grid item xs={ 6 }>
31
31
  <ControlLabel> { resolutionLabel } </ControlLabel>
32
32
  </Grid>
33
- <Grid item xs={ 6 }>
33
+ <Grid item xs={ 6 } sx={ { overflow: 'hidden' } }>
34
34
  <SelectControl options={ sizes } />
35
35
  </Grid>
36
36
  </Grid>
@@ -57,7 +57,7 @@ export const ImageMediaControl = createControl( ( props: ImageMediaControlProps
57
57
  variant="outlined"
58
58
  onClick={ () => open( { mode: 'browse' } ) }
59
59
  >
60
- { __( 'Select Image', 'elementor' ) }
60
+ { __( 'Select image', 'elementor' ) }
61
61
  </Button>
62
62
  <Button
63
63
  size="tiny"
@@ -66,7 +66,7 @@ export const ImageMediaControl = createControl( ( props: ImageMediaControlProps
66
66
  startIcon={ <UploadIcon /> }
67
67
  onClick={ () => open( { mode: 'upload' } ) }
68
68
  >
69
- { __( 'Upload Image', 'elementor' ) }
69
+ { __( 'Upload image', 'elementor' ) }
70
70
  </Button>
71
71
  </Stack>
72
72
  </CardOverlay>
@@ -1,6 +1,13 @@
1
1
  import * as React from 'react';
2
- import { useState } from 'react';
3
- import { booleanPropTypeUtil, linkPropTypeUtil, type LinkPropValue, numberPropTypeUtil } from '@elementor/editor-props';
2
+ import { useMemo, useState } from 'react';
3
+ import {
4
+ booleanPropTypeUtil,
5
+ linkPropTypeUtil,
6
+ type LinkPropValue,
7
+ numberPropTypeUtil,
8
+ stringPropTypeUtil,
9
+ urlPropTypeUtil,
10
+ } from '@elementor/editor-props';
4
11
  import { type HttpResponse, httpService } from '@elementor/http';
5
12
  import { MinusIcon, PlusIcon } from '@elementor/icons';
6
13
  import { useSessionStorage } from '@elementor/session';
@@ -8,19 +15,20 @@ import { Collapse, Divider, Grid, IconButton, Stack, Switch } from '@elementor/u
8
15
  import { __ } from '@wordpress/i18n';
9
16
 
10
17
  import { PropKeyProvider, PropProvider, useBoundProp } from '../bound-prop-context';
11
- import { ControlLabel } from '../components/control-label';
12
- import { createControl } from '../create-control';
13
18
  import {
14
- AutocompleteControl,
19
+ Autocomplete,
15
20
  type CategorizedOption,
16
21
  findMatchingOption,
17
22
  type FlatOption,
18
23
  isCategorizedOptionPool,
19
- } from './autocomplete-control';
24
+ } from '../components/autocomplete';
25
+ import { ControlLabel } from '../components/control-label';
26
+ import ControlActions from '../control-actions/control-actions';
27
+ import { createControl } from '../create-control';
20
28
 
21
29
  type Props = {
22
30
  queryOptions: {
23
- requestParams: object;
31
+ requestParams: Record< string, unknown >;
24
32
  endpoint: string;
25
33
  };
26
34
  allowCustomValues?: boolean;
@@ -29,7 +37,7 @@ type Props = {
29
37
  };
30
38
 
31
39
  type LinkSessionValue = {
32
- value?: LinkPropValue[ 'value' ];
40
+ value?: LinkPropValue[ 'value' ] | null;
33
41
  meta?: {
34
42
  isEnabled?: boolean;
35
43
  };
@@ -42,6 +50,7 @@ const SIZE = 'tiny';
42
50
  export const LinkControl = createControl( ( props: Props ) => {
43
51
  const { value, path, setValue, ...propContext } = useBoundProp( linkPropTypeUtil );
44
52
  const [ linkSessionValue, setLinkSessionValue ] = useSessionStorage< LinkSessionValue >( path.join( '/' ) );
53
+ const [ isEnabled, setIsEnabled ] = useState( !! value );
45
54
 
46
55
  const {
47
56
  allowCustomValues,
@@ -55,38 +64,39 @@ export const LinkControl = createControl( ( props: Props ) => {
55
64
  );
56
65
 
57
66
  const onEnabledChange = () => {
58
- const { meta } = linkSessionValue ?? {};
59
- const { isEnabled } = meta ?? {};
60
-
61
- setValue( isEnabled ? null : linkSessionValue?.value ?? { destination: null } );
67
+ setIsEnabled( ( prevState ) => ! prevState );
68
+ setValue( isEnabled ? null : linkSessionValue?.value ?? null );
62
69
  setLinkSessionValue( { value, meta: { isEnabled: ! isEnabled } } );
63
70
  };
64
71
 
65
72
  const onOptionChange = ( newValue: number | null ) => {
66
- const valueToSave = {
67
- ...value,
68
- destination: { $$type: 'number', value: newValue },
69
- label: {
70
- $$type: 'string',
71
- value: findMatchingOption( options, newValue?.toString() )?.label || '',
72
- },
73
- };
73
+ const valueToSave: LinkPropValue[ 'value' ] | null = newValue
74
+ ? {
75
+ ...value,
76
+ destination: numberPropTypeUtil.create( newValue ),
77
+ label: stringPropTypeUtil.create( findMatchingOption( options, newValue )?.label || null ),
78
+ }
79
+ : null;
74
80
 
75
81
  onSaveNewValue( valueToSave );
76
82
  };
77
83
 
78
84
  const onTextChange = ( newValue: string | null ) => {
79
- const valueToSave = {
80
- ...value,
81
- destination: { $$type: 'url', value: newValue },
82
- label: null,
83
- };
85
+ newValue = newValue?.trim() || '';
86
+
87
+ const valueToSave: LinkPropValue[ 'value' ] | null = newValue
88
+ ? {
89
+ ...value,
90
+ destination: urlPropTypeUtil.create( newValue ),
91
+ label: null,
92
+ }
93
+ : null;
84
94
 
85
95
  onSaveNewValue( valueToSave );
86
96
  updateOptions( newValue );
87
97
  };
88
98
 
89
- const onSaveNewValue = ( newValue: typeof value ) => {
99
+ const onSaveNewValue = ( newValue: LinkPropValue[ 'value' ] | null ) => {
90
100
  setValue( newValue );
91
101
  setLinkSessionValue( { ...linkSessionValue, value: newValue } );
92
102
  };
@@ -98,17 +108,20 @@ export const LinkControl = createControl( ( props: Props ) => {
98
108
  return;
99
109
  }
100
110
 
101
- debounceFetch( newValue )();
111
+ debounceFetch( { ...requestParams, term: newValue } );
102
112
  };
103
113
 
104
- const debounceFetch = ( newValue: string ) =>
105
- debounce(
106
- () =>
107
- fetchOptions( endpoint, { ...requestParams, term: newValue } ).then( ( newOptions ) => {
108
- setOptions( formatOptions( newOptions ) );
109
- } ),
110
- 400
111
- );
114
+ const debounceFetch = useMemo(
115
+ () =>
116
+ debounce(
117
+ ( params: FetchOptionsParams ) =>
118
+ fetchOptions( endpoint, params ).then( ( newOptions ) => {
119
+ setOptions( formatOptions( newOptions ) );
120
+ } ),
121
+ 400
122
+ ),
123
+ [ endpoint ]
124
+ );
112
125
 
113
126
  return (
114
127
  <PropProvider { ...propContext } value={ value } setValue={ setValue }>
@@ -123,24 +136,25 @@ export const LinkControl = createControl( ( props: Props ) => {
123
136
  >
124
137
  <ControlLabel>{ __( 'Link', 'elementor' ) }</ControlLabel>
125
138
  <ToggleIconControl
126
- enabled={ linkSessionValue?.meta?.isEnabled ?? false }
139
+ enabled={ isEnabled }
127
140
  onIconClick={ onEnabledChange }
128
- label={ __( 'Toggle Link', 'elementor' ) }
141
+ label={ __( 'Toggle link', 'elementor' ) }
129
142
  />
130
143
  </Stack>
131
- <Collapse in={ linkSessionValue?.meta?.isEnabled } timeout="auto" unmountOnExit>
144
+ <Collapse in={ isEnabled } timeout="auto" unmountOnExit>
132
145
  <Stack gap={ 1.5 }>
133
146
  <PropKeyProvider bind={ 'destination' }>
134
- <AutocompleteControl
135
- options={ options }
136
- allowCustomValues={ allowCustomValues }
137
- placeholder={ placeholder }
138
- optionRestrictedPropTypeUtil={ numberPropTypeUtil }
139
- onOptionChangeCallback={ onOptionChange }
140
- onTextChangeCallback={ onTextChange }
141
- minInputLength={ minInputLength }
142
- customValue={ value?.destination?.value?.toString() }
143
- />
147
+ <ControlActions>
148
+ <Autocomplete
149
+ options={ options }
150
+ allowCustomValues={ allowCustomValues }
151
+ placeholder={ placeholder }
152
+ value={ value?.destination?.value }
153
+ onOptionChange={ onOptionChange }
154
+ onTextChange={ onTextChange }
155
+ minInputLength={ minInputLength }
156
+ />
157
+ </ControlActions>
144
158
  </PropKeyProvider>
145
159
  <PropKeyProvider bind={ 'isTargetBlank' }>
146
160
  <SwitchControl />
@@ -186,7 +200,9 @@ const SwitchControl = () => {
186
200
  );
187
201
  };
188
202
 
189
- async function fetchOptions( ajaxUrl: string, params: object ) {
203
+ type FetchOptionsParams = Record< string, unknown > & { term: string };
204
+
205
+ async function fetchOptions( ajaxUrl: string, params: FetchOptionsParams ) {
190
206
  if ( ! params || ! ajaxUrl ) {
191
207
  return [];
192
208
  }
@@ -38,7 +38,7 @@ export const LinkedDimensionsControl = createControl( ( { label }: { label: stri
38
38
  <Stack direction="row" gap={ 2 } flexWrap="nowrap">
39
39
  <ControlLabel>{ label }</ControlLabel>
40
40
  <ToggleButton
41
- aria-label={ __( 'Link Inputs', 'elementor' ) }
41
+ aria-label={ __( 'Link inputs', 'elementor' ) }
42
42
  size={ 'tiny' }
43
43
  value={ 'check' }
44
44
  selected={ isLinked }
@@ -23,7 +23,14 @@ export const SelectControl = createControl( ( { options, onChange }: Props ) =>
23
23
 
24
24
  return (
25
25
  <ControlActions>
26
- <Select displayEmpty size="tiny" value={ value ?? '' } onChange={ handleChange } fullWidth>
26
+ <Select
27
+ sx={ { overflow: 'hidden' } }
28
+ displayEmpty
29
+ size="tiny"
30
+ value={ value ?? '' }
31
+ onChange={ handleChange }
32
+ fullWidth
33
+ >
27
34
  { options.map( ( { label, ...props } ) => (
28
35
  <MenuItem key={ props.value } { ...props } value={ props.value ?? '' }>
29
36
  { label }
@@ -23,10 +23,10 @@ export const StrokeControl = createControl( () => {
23
23
  return (
24
24
  <PropProvider { ...propContext }>
25
25
  <Stack gap={ 1.5 }>
26
- <Control bind="width" label={ __( 'Stroke Width', 'elementor' ) }>
26
+ <Control bind="width" label={ __( 'Stroke width', 'elementor' ) }>
27
27
  <SizeControl units={ units } />
28
28
  </Control>
29
- <Control bind="color" label={ __( 'Stroke Color', 'elementor' ) }>
29
+ <Control bind="color" label={ __( 'Stroke color', 'elementor' ) }>
30
30
  <ColorControl />
31
31
  </Control>
32
32
  </Stack>
package/src/index.ts CHANGED
@@ -1,5 +1,4 @@
1
1
  // control types
2
- export { AutocompleteControl } from './controls/autocomplete-control';
3
2
  export { ImageControl } from './controls/image-control';
4
3
  export { TextControl } from './controls/text-control';
5
4
  export { TextAreaControl } from './controls/text-area-control';