@elementor/editor-controls 0.9.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.9.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.0",
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 ) ?? '' }
@@ -6,7 +6,7 @@ import {
6
6
  backgroundOverlayPropTypeUtil,
7
7
  type PropKey,
8
8
  } from '@elementor/editor-props';
9
- import { Box, CardMedia, Grid, Stack, Tab, TabPanel, Tabs, UnstableColorIndicator, useTabs } from '@elementor/ui';
9
+ import { Box, CardMedia, Grid, Stack, Tab, TabPanel, Tabs, UnstableColorIndicator } from '@elementor/ui';
10
10
  import { useWpMediaAttachment } from '@elementor/wp-media';
11
11
  import { __ } from '@wordpress/i18n';
12
12
 
@@ -20,8 +20,15 @@ import { BackgroundImageOverlayAttachment } from './background-image-overlay/bac
20
20
  import { BackgroundImageOverlayPosition } from './background-image-overlay/background-image-overlay-position';
21
21
  import { BackgroundImageOverlayRepeat } from './background-image-overlay/background-image-overlay-repeat';
22
22
  import { BackgroundImageOverlaySize } from './background-image-overlay/background-image-overlay-size';
23
+ import { type BackgroundImageOverlay } from './types';
24
+ import { useBackgroundTabsHistory } from './use-background-tabs-history';
23
25
 
24
- const initialBackgroundOverlay = ( backgroundPlaceholderImage: string ): BackgroundOverlayItemPropValue => ( {
26
+ export const initialBackgroundColorOverlay: BackgroundOverlayItemPropValue = {
27
+ $$type: 'background-color-overlay',
28
+ value: '#00000033',
29
+ };
30
+
31
+ export const getInitialBackgroundOverlay = (): BackgroundOverlayItemPropValue => ( {
25
32
  $$type: 'background-image-overlay',
26
33
  value: {
27
34
  image: {
@@ -32,7 +39,7 @@ const initialBackgroundOverlay = ( backgroundPlaceholderImage: string ): Backgro
32
39
  value: {
33
40
  url: {
34
41
  $$type: 'url',
35
- value: backgroundPlaceholderImage,
42
+ value: env.background_placeholder_image,
36
43
  },
37
44
  id: null,
38
45
  },
@@ -53,31 +60,6 @@ const backgroundResolutionOptions = [
53
60
  { label: __( 'Full', 'elementor' ), value: 'full' },
54
61
  ];
55
62
 
56
- type OverlayType = 'image' | 'color';
57
-
58
- type ImageSrcAttachment = { id: { $$type: 'image-attachment-id'; value: number }; url: null };
59
-
60
- type ImageSrcUrl = { url: { $$type: 'url'; value: string }; id: null };
61
-
62
- type BackgroundImageOverlay = {
63
- $$type: 'background-image-overlay';
64
- value: {
65
- image: {
66
- $$type: 'image';
67
- value: {
68
- src: {
69
- $$type: 'image-src';
70
- value: ImageSrcAttachment | ImageSrcUrl;
71
- };
72
- size: {
73
- $$type: 'string';
74
- value: 'thumbnail' | 'medium' | 'large' | 'full';
75
- };
76
- };
77
- };
78
- };
79
- };
80
-
81
63
  export const BackgroundOverlayRepeaterControl = createControl( () => {
82
64
  const { propType, value: overlayValues, setValue } = useBoundProp( backgroundOverlayPropTypeUtil );
83
65
 
@@ -91,24 +73,26 @@ export const BackgroundOverlayRepeaterControl = createControl( () => {
91
73
  Icon: ItemIcon,
92
74
  Label: ItemLabel,
93
75
  Content: ItemContent,
94
- initialValues: initialBackgroundOverlay( env.background_placeholder_image ),
76
+ initialValues: getInitialBackgroundOverlay(),
95
77
  } }
96
78
  />
97
79
  </PropProvider>
98
80
  );
99
81
  } );
100
82
 
101
- const ItemContent = ( { bind, value }: { bind: PropKey; value: BackgroundOverlayItemPropValue } ) => {
83
+ export const ItemContent = ( { bind }: { bind: PropKey } ) => {
102
84
  return (
103
85
  <PropKeyProvider bind={ bind }>
104
- <Content value={ value } />
86
+ <Content />
105
87
  </PropKeyProvider>
106
88
  );
107
89
  };
108
90
 
109
- const Content = ( { value }: { value: BackgroundOverlayItemPropValue } ) => {
110
- const activeTab = deriveOverlayType( value.$$type );
111
- const { getTabsProps, getTabProps, getTabPanelProps } = useTabs< OverlayType >( activeTab );
91
+ const Content = () => {
92
+ const { getTabsProps, getTabProps, getTabPanelProps } = useBackgroundTabsHistory( {
93
+ image: getInitialBackgroundOverlay().value,
94
+ color: initialBackgroundColorOverlay.value,
95
+ } );
112
96
 
113
97
  return (
114
98
  <Box sx={ { width: '100%' } }>
@@ -207,18 +191,6 @@ const ImageOverlayContent = () => {
207
191
  );
208
192
  };
209
193
 
210
- const deriveOverlayType = ( type: string ): OverlayType => {
211
- if ( type === 'background-color-overlay' ) {
212
- return 'color';
213
- }
214
-
215
- if ( type === 'background-image-overlay' ) {
216
- return 'image';
217
- }
218
-
219
- throw new Error( `Invalid overlay type: ${ type }` );
220
- };
221
-
222
194
  const useImage = ( image: BackgroundImageOverlay ) => {
223
195
  let imageTitle,
224
196
  imageUrl: string | null = null;
@@ -0,0 +1,22 @@
1
+ type ImageSrcAttachment = { id: { $$type: 'image-attachment-id'; value: number }; url: null };
2
+
3
+ type ImageSrcUrl = { url: { $$type: 'url'; value: string }; id: null };
4
+
5
+ export type BackgroundImageOverlay = {
6
+ $$type: 'background-image-overlay';
7
+ value: {
8
+ image: {
9
+ $$type: 'image';
10
+ value: {
11
+ src: {
12
+ $$type: 'image-src';
13
+ value: ImageSrcAttachment | ImageSrcUrl;
14
+ };
15
+ size: {
16
+ $$type: 'string';
17
+ value: 'thumbnail' | 'medium' | 'large' | 'full';
18
+ };
19
+ };
20
+ };
21
+ };
22
+ };
@@ -0,0 +1,62 @@
1
+ import { useRef } from 'react';
2
+ import {
3
+ backgroundColorOverlayPropTypeUtil,
4
+ backgroundImageOverlayPropTypeUtil,
5
+ type BackgroundOverlayItemPropValue,
6
+ } from '@elementor/editor-props';
7
+ import { useTabs } from '@elementor/ui';
8
+
9
+ import { useBoundProp } from '../../../bound-prop-context';
10
+ import { type BackgroundImageOverlay } from './types';
11
+
12
+ type OverlayType = 'image' | 'color';
13
+
14
+ type InitialBackgroundValues = {
15
+ color: BackgroundOverlayItemPropValue[ 'value' ];
16
+ image: BackgroundImageOverlay[ 'value' ];
17
+ };
18
+
19
+ export const useBackgroundTabsHistory = ( {
20
+ color: initialBackgroundColorOverlay,
21
+ image: initialBackgroundImageOverlay,
22
+ }: InitialBackgroundValues ) => {
23
+ const { value: imageValue, setValue: setImageValue } = useBoundProp( backgroundImageOverlayPropTypeUtil );
24
+ const { value: colorValue, setValue: setColorValue } = useBoundProp( backgroundColorOverlayPropTypeUtil );
25
+
26
+ const { getTabsProps, getTabProps, getTabPanelProps } = useTabs< OverlayType >( colorValue ? 'color' : 'image' );
27
+
28
+ const valuesHistory = useRef< InitialBackgroundValues >( {
29
+ image: initialBackgroundImageOverlay,
30
+ color: initialBackgroundColorOverlay,
31
+ } );
32
+
33
+ const saveToHistory = ( key: keyof InitialBackgroundValues, value: BackgroundOverlayItemPropValue[ 'value' ] ) => {
34
+ if ( value ) {
35
+ valuesHistory.current[ key ] = value;
36
+ }
37
+ };
38
+
39
+ const onTabChange = ( e: React.SyntheticEvent, tabName: OverlayType ) => {
40
+ switch ( tabName ) {
41
+ case 'image':
42
+ setImageValue( valuesHistory.current.image );
43
+
44
+ saveToHistory( 'color', colorValue );
45
+
46
+ break;
47
+
48
+ case 'color':
49
+ setColorValue( valuesHistory.current.color );
50
+
51
+ saveToHistory( 'image', imageValue );
52
+ }
53
+
54
+ return getTabsProps().onChange( e, tabName );
55
+ };
56
+
57
+ return {
58
+ getTabProps,
59
+ getTabPanelProps,
60
+ getTabsProps: () => ( { ...getTabsProps(), onChange: onTabChange } ),
61
+ };
62
+ };
@@ -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>