@elementor/editor-variables 0.3.2 → 0.4.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.
@@ -16,21 +16,26 @@ import {
16
16
  import { __ } from '@wordpress/i18n';
17
17
 
18
18
  import { type Variable } from '../types';
19
- import { ColorIndicator } from './color-indicator';
20
19
 
21
20
  type Props = {
22
21
  selectedVariable: Variable;
23
22
  unlinkVariable: () => void;
24
23
  children: ( { closePopover }: { closePopover: () => void } ) => React.ReactNode;
24
+ startTagAdornment?: React.ReactNode;
25
25
  };
26
26
 
27
- export const VariablesSelectionPopover = ( { selectedVariable, unlinkVariable, children }: Props ) => {
27
+ export const VariablesSelectionPopover = ( {
28
+ selectedVariable,
29
+ unlinkVariable,
30
+ startTagAdornment,
31
+ children,
32
+ }: Props ) => {
28
33
  const id = useId();
29
34
  const popupState = usePopupState( { variant: 'popover', popupId: `elementor-variables-action-${ id }` } );
30
35
 
31
36
  const closePopover = () => popupState.close();
32
37
 
33
- const { value, label } = selectedVariable;
38
+ const { label } = selectedVariable;
34
39
 
35
40
  return (
36
41
  <Box>
@@ -40,7 +45,7 @@ export const VariablesSelectionPopover = ( { selectedVariable, unlinkVariable, c
40
45
  { ...bindTrigger( popupState ) }
41
46
  startIcon={
42
47
  <Stack spacing={ 1 } direction="row" alignItems="center">
43
- <ColorIndicator size="inherit" component="span" value={ value } />
48
+ { startTagAdornment }
44
49
  <ColorFilterIcon fontSize={ 'inherit' } sx={ { mr: 1 } } />
45
50
  </Stack>
46
51
  }
@@ -1,12 +1,13 @@
1
1
  import * as React from 'react';
2
2
  import { useBoundProp } from '@elementor/editor-controls';
3
3
 
4
- import { VariablesSelection } from '../components/variables-selection';
4
+ import { ColorIndicator } from '../components/color-indicator';
5
+ import { ColorVariablesSelection } from '../components/color-variables-selection';
5
6
  import { VariablesSelectionPopover } from '../components/variables-selection-popover';
6
7
  import { useVariable } from '../hooks/use-prop-variables';
7
8
  import { colorVariablePropTypeUtil } from '../prop-types/color-variable-prop-type';
8
9
 
9
- export const VariablesSelectionControl = () => {
10
+ export const ColorVariablesSelectionControl = () => {
10
11
  const { setValue } = useBoundProp();
11
12
  const { value: variableValue } = useBoundProp( colorVariablePropTypeUtil );
12
13
 
@@ -14,15 +15,19 @@ export const VariablesSelectionControl = () => {
14
15
  setValue( '' );
15
16
  };
16
17
 
17
- const selectedVariable = useVariable( variableValue );
18
+ const selectedVariable = useVariable( colorVariablePropTypeUtil.key, variableValue );
18
19
 
19
20
  if ( ! selectedVariable ) {
20
21
  throw new Error( `Global color variable ${ variableValue } not found` );
21
22
  }
22
23
 
23
24
  return (
24
- <VariablesSelectionPopover selectedVariable={ selectedVariable } unlinkVariable={ unlinkVariable }>
25
- { ( { closePopover } ) => <VariablesSelection onSelect={ closePopover } /> }
25
+ <VariablesSelectionPopover
26
+ selectedVariable={ selectedVariable }
27
+ unlinkVariable={ unlinkVariable }
28
+ startTagAdornment={ <ColorIndicator size="inherit" component="span" value={ selectedVariable.value } /> }
29
+ >
30
+ { ( { closePopover } ) => <ColorVariablesSelection onSelect={ closePopover } /> }
26
31
  </VariablesSelectionPopover>
27
32
  );
28
33
  };
@@ -0,0 +1,28 @@
1
+ import * as React from 'react';
2
+ import { useBoundProp } from '@elementor/editor-controls';
3
+
4
+ import { FontVariablesSelection } from '../components/font-variables-selection';
5
+ import { VariablesSelectionPopover } from '../components/variables-selection-popover';
6
+ import { useVariable } from '../hooks/use-prop-variables';
7
+ import { fontVariablePropTypeUtil } from '../prop-types/font-variable-prop-type';
8
+
9
+ export const FontVariablesSelectionControl = () => {
10
+ const { setValue } = useBoundProp();
11
+ const { value: variableValue } = useBoundProp( fontVariablePropTypeUtil );
12
+
13
+ const unlinkVariable = () => {
14
+ setValue( '' );
15
+ };
16
+
17
+ const selectedVariable = useVariable( fontVariablePropTypeUtil.key, variableValue );
18
+
19
+ if ( ! selectedVariable ) {
20
+ throw new Error( `Global font variable ${ variableValue } not found` );
21
+ }
22
+
23
+ return (
24
+ <VariablesSelectionPopover selectedVariable={ selectedVariable } unlinkVariable={ unlinkVariable }>
25
+ { ( { closePopover } ) => <FontVariablesSelection onSelect={ closePopover } /> }
26
+ </VariablesSelectionPopover>
27
+ );
28
+ };
@@ -0,0 +1,20 @@
1
+ import * as React from 'react';
2
+ import { type PopoverActionProps, useBoundProp } from '@elementor/editor-editing-panel';
3
+ import { ColorFilterIcon } from '@elementor/icons';
4
+ import { __ } from '@wordpress/i18n';
5
+
6
+ import { ColorVariablesSelection } from '../components/color-variables-selection';
7
+ import { supportsColorVariables } from '../utils';
8
+
9
+ export const usePropColorVariableAction = (): PopoverActionProps => {
10
+ const { propType } = useBoundProp();
11
+
12
+ const visible = !! propType && supportsColorVariables( propType );
13
+
14
+ return {
15
+ visible,
16
+ icon: ColorFilterIcon,
17
+ title: __( 'Variables', 'elementor' ),
18
+ popoverContent: ( { closePopover } ) => <ColorVariablesSelection onSelect={ closePopover } />,
19
+ };
20
+ };
@@ -0,0 +1,20 @@
1
+ import * as React from 'react';
2
+ import { type PopoverActionProps, useBoundProp } from '@elementor/editor-editing-panel';
3
+ import { ColorFilterIcon } from '@elementor/icons';
4
+ import { __ } from '@wordpress/i18n';
5
+
6
+ import { FontVariablesSelection } from '../components/font-variables-selection';
7
+ import { supportsFontVariables } from '../utils';
8
+
9
+ export const usePropFontVariableAction = (): PopoverActionProps => {
10
+ const { propType } = useBoundProp();
11
+
12
+ const visible = !! propType && supportsFontVariables( propType );
13
+
14
+ return {
15
+ visible,
16
+ icon: ColorFilterIcon,
17
+ title: __( 'Variables', 'elementor' ),
18
+ popoverContent: ( { closePopover } ) => <FontVariablesSelection onSelect={ closePopover } />,
19
+ };
20
+ };
@@ -1,28 +1,26 @@
1
1
  import { useMemo } from 'react';
2
2
 
3
- import { type Variable } from '../types';
3
+ type Variables = Record< string, { value: string; label: string } >;
4
4
 
5
- export const usePropVariables = () => {
6
- return useMemo( () => getVariables(), [] );
5
+ type VariablesGroup = Record< string, Variables >;
6
+
7
+ export const usePropVariables = ( propTypeKey: string ) => {
8
+ return useMemo( () => normalizeVariables( propTypeKey ), [ propTypeKey ] );
7
9
  };
8
10
 
9
- export const useVariable = ( key: string ) => {
10
- if ( ! variables[ key ] ) {
11
+ export const useVariable = ( propTypeKey: string, key: string ) => {
12
+ if ( ! variables[ propTypeKey ][ key ] ) {
11
13
  return null;
12
14
  }
13
15
 
14
16
  return {
15
- ...variables[ key ],
17
+ ...variables[ propTypeKey ][ key ],
16
18
  key,
17
19
  };
18
20
  };
19
21
 
20
- const getVariables = (): Variable[] => {
21
- return normalizeVariables();
22
- };
23
-
24
- const normalizeVariables = () => {
25
- return Object.entries( variables ).map( ( [ key, { label, value } ] ) => ( {
22
+ const normalizeVariables = ( propTypeKey: string ) => {
23
+ return Object.entries( variables[ propTypeKey ] ).map( ( [ key, { label, value } ] ) => ( {
26
24
  key,
27
25
  label,
28
26
  value,
@@ -30,46 +28,4 @@ const normalizeVariables = () => {
30
28
  };
31
29
 
32
30
  // @ts-expect-error the temporary solution to get the list of variables from the server
33
- const variables: Record< string, { value: string; label: string } > = window?.ElementorV4Variables ?? {
34
- 'e-gc-001': {
35
- value: '#ffffff',
36
- label: 'Main: white',
37
- },
38
- 'e-gc-002': {
39
- value: '#000000',
40
- label: 'Main: black',
41
- },
42
- 'e-gc-a01': {
43
- value: '#FF0000',
44
- label: 'Danger: red',
45
- },
46
- 'e-gc-a02': {
47
- value: '#0000FF',
48
- label: 'Informative: blue',
49
- },
50
- 'e-gc-a03': {
51
- value: '#FF7BE5',
52
- label: 'Elementor: pint',
53
- },
54
- 'e-gc-a04': {
55
- value: '#808080',
56
- label: 'Gray: background',
57
- },
58
-
59
- 'e-gc-b01': {
60
- value: '#213555',
61
- label: 'Navy: primary',
62
- },
63
- 'e-gc-b02': {
64
- value: '#3E5879',
65
- label: 'Navy: secondary',
66
- },
67
- 'e-gc-b03': {
68
- value: '#D8C4B6',
69
- label: 'Navy: light',
70
- },
71
- 'e-gc-b04': {
72
- value: '#F5EFE7',
73
- label: 'Navy long text variable name: background',
74
- },
75
- };
31
+ const variables: VariablesGroup = window?.ElementorV4Variables;
@@ -0,0 +1,24 @@
1
+ import { styleTransformersRegistry } from '@elementor/editor-canvas';
2
+ import { controlActionsMenu, registerControlReplacement } from '@elementor/editor-editing-panel';
3
+
4
+ import { ColorVariablesSelectionControl } from './controls/color-variables-selection-control';
5
+ import { usePropColorVariableAction } from './hooks/use-prop-color-variable-action';
6
+ import { colorVariablePropTypeUtil } from './prop-types/color-variable-prop-type';
7
+ import { variableTransformer } from './transformers/variable-transformer';
8
+ import { hasAssignedColorVariable } from './utils';
9
+
10
+ const { registerPopoverAction } = controlActionsMenu;
11
+
12
+ export function initColorVariables() {
13
+ registerControlReplacement( {
14
+ component: ColorVariablesSelectionControl,
15
+ condition: ( { value } ) => hasAssignedColorVariable( value ),
16
+ } );
17
+
18
+ registerPopoverAction( {
19
+ id: 'color-variables',
20
+ useProps: usePropColorVariableAction,
21
+ } );
22
+
23
+ styleTransformersRegistry.register( colorVariablePropTypeUtil.key, variableTransformer );
24
+ }
@@ -0,0 +1,24 @@
1
+ import { styleTransformersRegistry } from '@elementor/editor-canvas';
2
+ import { controlActionsMenu, registerControlReplacement } from '@elementor/editor-editing-panel';
3
+
4
+ import { FontVariablesSelectionControl } from './controls/font-variables-selection-control';
5
+ import { usePropFontVariableAction } from './hooks/use-prop-font-variable-action';
6
+ import { fontVariablePropTypeUtil } from './prop-types/font-variable-prop-type';
7
+ import { variableTransformer } from './transformers/variable-transformer';
8
+ import { hasAssignedFontVariable } from './utils';
9
+
10
+ const { registerPopoverAction } = controlActionsMenu;
11
+
12
+ export function initFontVariables() {
13
+ registerControlReplacement( {
14
+ component: FontVariablesSelectionControl,
15
+ condition: ( { value } ) => hasAssignedFontVariable( value ),
16
+ } );
17
+
18
+ registerPopoverAction( {
19
+ id: 'font-variables',
20
+ useProps: usePropFontVariableAction,
21
+ } );
22
+
23
+ styleTransformersRegistry.register( fontVariablePropTypeUtil.key, variableTransformer );
24
+ }
package/src/init.ts CHANGED
@@ -1,24 +1,8 @@
1
- import { styleTransformersRegistry } from '@elementor/editor-canvas';
2
- import { controlActionsMenu, registerControlReplacement } from '@elementor/editor-editing-panel';
3
-
4
- import { VariablesSelectionControl } from './controls/variables-selection-control';
5
- import { usePropVariableAction } from './hooks/use-prop-variable-action';
6
- import { colorVariablePropTypeUtil } from './prop-types/color-variable-prop-type';
7
- import { variableTransformer } from './transformers/variable-transformer';
8
- import { hasAssignedVariable } from './utils';
9
-
10
- const { registerPopoverAction } = controlActionsMenu;
1
+ import { initColorVariables } from './init-color-variables';
2
+ import { initFontVariables } from './init-font-variables';
11
3
 
12
4
  export function init() {
13
- registerControlReplacement( {
14
- component: VariablesSelectionControl,
15
- condition: ( { value } ) => hasAssignedVariable( value ),
16
- } );
17
-
18
- registerPopoverAction( {
19
- id: 'variables',
20
- useProps: usePropVariableAction,
21
- } );
5
+ initColorVariables();
22
6
 
23
- styleTransformersRegistry.register( colorVariablePropTypeUtil.key, variableTransformer );
7
+ initFontVariables();
24
8
  }
@@ -0,0 +1,4 @@
1
+ import { createPropUtils } from '@elementor/editor-props';
2
+ import { z } from '@elementor/schema';
3
+
4
+ export const fontVariablePropTypeUtil = createPropUtils( 'global-font-variable', z.string() );
package/src/utils.ts CHANGED
@@ -1,11 +1,20 @@
1
1
  import { type PropType, type PropValue } from '@elementor/editor-props';
2
2
 
3
3
  import { colorVariablePropTypeUtil } from './prop-types/color-variable-prop-type';
4
+ import { fontVariablePropTypeUtil } from './prop-types/font-variable-prop-type';
4
5
 
5
- export const hasAssignedVariable = ( propValue: PropValue ): boolean => {
6
+ export const hasAssignedColorVariable = ( propValue: PropValue ): boolean => {
6
7
  return !! colorVariablePropTypeUtil.isValid( propValue );
7
8
  };
8
9
 
9
- export const supportsVariables = ( propType: PropType ): boolean => {
10
+ export const supportsColorVariables = ( propType: PropType ): boolean => {
10
11
  return propType.kind === 'union' && colorVariablePropTypeUtil.key in propType.prop_types;
11
12
  };
13
+
14
+ export const hasAssignedFontVariable = ( propValue: PropValue ): boolean => {
15
+ return !! fontVariablePropTypeUtil.isValid( propValue );
16
+ };
17
+
18
+ export const supportsFontVariables = ( propType: PropType ): boolean => {
19
+ return propType.kind === 'union' && fontVariablePropTypeUtil.key in propType.prop_types;
20
+ };
@@ -1,20 +0,0 @@
1
- import * as React from 'react';
2
- import { type PopoverActionProps, useBoundProp } from '@elementor/editor-editing-panel';
3
- import { ColorFilterIcon } from '@elementor/icons';
4
- import { __ } from '@wordpress/i18n';
5
-
6
- import { VariablesSelection } from '../components/variables-selection';
7
- import { supportsVariables } from '../utils';
8
-
9
- export const usePropVariableAction = (): PopoverActionProps => {
10
- const { propType } = useBoundProp();
11
-
12
- const visible = !! propType && supportsVariables( propType );
13
-
14
- return {
15
- visible,
16
- icon: ColorFilterIcon,
17
- title: __( 'Variables', 'elementor' ),
18
- popoverContent: ( { closePopover } ) => <VariablesSelection onSelect={ closePopover } />,
19
- };
20
- };