@elementor/editor-controls 1.0.0 → 1.1.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.
@@ -24,6 +24,7 @@ type SizeControlProps = {
24
24
  extendedOptions?: ExtendedOption[];
25
25
  disableCustom?: boolean;
26
26
  anchorRef?: MutableRefObject< HTMLElement | undefined >;
27
+ defaultUnit?: Unit;
27
28
  };
28
29
 
29
30
  type State = {
@@ -33,9 +34,10 @@ type State = {
33
34
  };
34
35
 
35
36
  export const SizeControl = createControl( ( props: SizeControlProps ) => {
37
+ const defaultUnit = props.defaultUnit ?? DEFAULT_UNIT;
36
38
  const { units = [ ...defaultUnits ], placeholder, startIcon, anchorRef } = props;
37
39
  const { value: sizeValue, setValue: setSizeValue, disabled, restoreValue } = useBoundProp( sizePropTypeUtil );
38
- const [ internalState, setInternalState ] = useState( createStateFromSizeProp( sizeValue ) );
40
+ const [ internalState, setInternalState ] = useState( createStateFromSizeProp( sizeValue, defaultUnit ) );
39
41
  const activeBreakpoint = useActiveBreakpoint();
40
42
 
41
43
  const extendedOptions = useSizeExtendedOptions( props.extendedOptions || [], props.disableCustom ?? false );
@@ -56,7 +58,7 @@ export const SizeControl = createControl( ( props: SizeControlProps ) => {
56
58
  return !! newState?.numeric || newState?.numeric === 0;
57
59
  },
58
60
  fallback: ( newState ) => ( {
59
- unit: newState?.unit ?? DEFAULT_UNIT,
61
+ unit: newState?.unit ?? props.defaultUnit ?? DEFAULT_UNIT,
60
62
  numeric: newState?.numeric ?? DEFAULT_SIZE,
61
63
  custom: newState?.custom ?? '',
62
64
  } ),
@@ -101,7 +103,7 @@ export const SizeControl = createControl( ( props: SizeControlProps ) => {
101
103
  };
102
104
 
103
105
  useEffect( () => {
104
- const newState = createStateFromSizeProp( sizeValue );
106
+ const newState = createStateFromSizeProp( sizeValue, defaultUnit );
105
107
  const currentUnit = isUnitExtendedOption( state.unit ) ? 'custom' : 'numeric';
106
108
  const mergedStates = { ...state, [ currentUnit ]: newState[ currentUnit ] };
107
109
 
@@ -120,7 +122,7 @@ export const SizeControl = createControl( ( props: SizeControlProps ) => {
120
122
  }, [ sizeValue ] );
121
123
 
122
124
  useEffect( () => {
123
- const newState = createStateFromSizeProp( sizeValue );
125
+ const newState = createStateFromSizeProp( sizeValue, defaultUnit );
124
126
 
125
127
  if ( activeBreakpoint && ! areStatesEqual( newState, state ) ) {
126
128
  setState( newState );
@@ -165,8 +167,8 @@ function formatSize< TSize extends string | number >( size: TSize, unit: Unit |
165
167
  return size || size === 0 ? ( Number( size ) as TSize ) : ( NaN as TSize );
166
168
  }
167
169
 
168
- function createStateFromSizeProp( sizeValue: SizeValue | null ): State {
169
- const unit = sizeValue?.unit ?? DEFAULT_UNIT;
170
+ function createStateFromSizeProp( sizeValue: SizeValue | null, defaultUnit: Unit ): State {
171
+ const unit = sizeValue?.unit ?? defaultUnit;
170
172
  const size = sizeValue?.size ?? '';
171
173
 
172
174
  return {
@@ -0,0 +1,24 @@
1
+ import { createContext, useContext } from 'react';
2
+ import { type PropTypeUtil } from '@elementor/editor-props';
3
+
4
+ export type ChildControlConfig = {
5
+ component: React.ComponentType;
6
+ props?: Record< string, unknown >;
7
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
8
+ propTypeUtil: PropTypeUtil< string, any >;
9
+ label?: string;
10
+ };
11
+
12
+ const RepeatableControlContext = createContext< ChildControlConfig | undefined >( undefined );
13
+
14
+ const useRepeatableControlContext = () => {
15
+ const context = useContext( RepeatableControlContext );
16
+
17
+ if ( ! context ) {
18
+ throw new Error( 'useRepeatableControlContext must be used within RepeatableControl' );
19
+ }
20
+
21
+ return context;
22
+ };
23
+
24
+ export { RepeatableControlContext, useRepeatableControlContext };
package/src/index.ts CHANGED
@@ -19,6 +19,9 @@ export { AspectRatioControl } from './controls/aspect-ratio-control';
19
19
  export { SvgMediaControl } from './controls/svg-media-control';
20
20
  export { BackgroundControl } from './controls/background-control/background-control';
21
21
  export { SwitchControl } from './controls/switch-control';
22
+ export { RepeatableControl } from './controls/repeatable-control';
23
+ export { KeyValueControl } from './controls/key-value-control';
24
+ export { PositionControl } from './controls/position-control';
22
25
 
23
26
  // components
24
27
  export { ControlFormLabel } from './components/control-form-label';