@elementor/editor-editing-panel 1.30.0 → 1.31.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.
@@ -0,0 +1,22 @@
1
+ import * as React from 'react';
2
+ import { SizeControl } from '@elementor/editor-controls';
3
+ import { Grid } from '@elementor/ui';
4
+ import { __ } from '@wordpress/i18n';
5
+
6
+ import { StylesField } from '../../../controls-registry/styles-field';
7
+ import { ControlLabel } from '../../control-label';
8
+
9
+ export const OffsetField = () => {
10
+ return (
11
+ <StylesField bind="scroll-margin-top">
12
+ <Grid container gap={ 2 } alignItems="center" flexWrap="nowrap">
13
+ <Grid item xs={ 6 }>
14
+ <ControlLabel>{ __( 'Anchor offset', 'elementor' ) }</ControlLabel>
15
+ </Grid>
16
+ <Grid item xs={ 6 }>
17
+ <SizeControl units={ [ 'px', 'em', 'rem', 'vw', 'vh' ] } />
18
+ </Grid>
19
+ </Grid>
20
+ </StylesField>
21
+ );
22
+ };
@@ -5,8 +5,10 @@ import { useSessionStorage } from '@elementor/session';
5
5
  import { useStyle } from '../../../contexts/style-context';
6
6
  import { useStylesField } from '../../../hooks/use-styles-field';
7
7
  import { useStylesFields } from '../../../hooks/use-styles-fields';
8
+ import { PanelDivider } from '../../panel-divider';
8
9
  import { SectionContent } from '../../section-content';
9
10
  import { DimensionsField } from './dimensions-field';
11
+ import { OffsetField } from './offset-field';
10
12
  import { PositionField } from './position-field';
11
13
  import { ZIndexField } from './z-index-field';
12
14
 
@@ -66,6 +68,8 @@ export const PositionSection = () => {
66
68
  <ZIndexField />
67
69
  </>
68
70
  ) : null }
71
+ <PanelDivider />
72
+ <OffsetField />
69
73
  </SectionContent>
70
74
  );
71
75
  };
@@ -4,11 +4,12 @@ import { CLASSES_PROP_KEY } from '@elementor/editor-props';
4
4
  import { useActiveBreakpoint } from '@elementor/editor-responsive';
5
5
  import { type StyleDefinitionID, type StyleDefinitionState } from '@elementor/editor-styles';
6
6
  import { SessionStorageProvider } from '@elementor/session';
7
- import { Divider } from '@elementor/ui';
7
+ import { Divider, Stack } from '@elementor/ui';
8
8
  import { __ } from '@wordpress/i18n';
9
9
 
10
10
  import { ClassesPropProvider } from '../contexts/classes-prop-context';
11
11
  import { useElement } from '../contexts/element-context';
12
+ import { useScrollDirection } from '../contexts/scroll-context';
12
13
  import { StyleProvider } from '../contexts/style-context';
13
14
  import { StyleInheritanceProvider } from '../contexts/styles-inheritance-context';
14
15
  import { useActiveStyleDefId } from '../hooks/use-active-style-def-id';
@@ -24,6 +25,16 @@ import { SizeSection } from './style-sections/size-section/size-section';
24
25
  import { SpacingSection } from './style-sections/spacing-section/spacing-section';
25
26
  import { TypographySection } from './style-sections/typography-section/typography-section';
26
27
 
28
+ const TABS_HEADER_HEIGHT = '37px';
29
+
30
+ export const stickyHeaderStyles = {
31
+ position: 'sticky',
32
+ zIndex: 1,
33
+ opacity: 1,
34
+ backgroundColor: 'background.default',
35
+ transition: 'top 300ms ease',
36
+ };
37
+
27
38
  export const StyleTab = () => {
28
39
  const currentClassesProp = useCurrentClassesProp();
29
40
  const [ activeStyleDefId, setActiveStyleDefId ] = useActiveStyleDefId( currentClassesProp );
@@ -43,8 +54,10 @@ export const StyleTab = () => {
43
54
  >
44
55
  <SessionStorageProvider prefix={ activeStyleDefId ?? '' }>
45
56
  <StyleInheritanceProvider>
46
- <CssClassSelector />
47
- <Divider />
57
+ <ClassesHeader>
58
+ <CssClassSelector />
59
+ <Divider />
60
+ </ClassesHeader>
48
61
  <SectionsList>
49
62
  <Section title={ __( 'Layout', 'elementor' ) }>
50
63
  <LayoutSection />
@@ -78,6 +91,16 @@ export const StyleTab = () => {
78
91
  );
79
92
  };
80
93
 
94
+ function ClassesHeader( { children }: { children: React.ReactNode } ) {
95
+ const scrollDirection = useScrollDirection();
96
+
97
+ return (
98
+ <Stack sx={ { ...stickyHeaderStyles, top: scrollDirection === 'up' ? TABS_HEADER_HEIGHT : 0 } }>
99
+ { children }
100
+ </Stack>
101
+ );
102
+ }
103
+
81
104
  function useCurrentClassesProp(): string {
82
105
  const { elementType } = useElement();
83
106
 
@@ -0,0 +1,60 @@
1
+ import * as React from 'react';
2
+ import { createContext, type ReactNode, useContext, useEffect, useRef, useState } from 'react';
3
+ import { styled } from '@elementor/ui';
4
+
5
+ type ScrollDirection = 'up' | 'down';
6
+
7
+ type ScrollContextValue = {
8
+ direction: ScrollDirection;
9
+ };
10
+
11
+ const ScrollContext = createContext< ScrollContextValue | undefined >( undefined );
12
+
13
+ const ScrollPanel = styled( 'div' )`
14
+ height: 100%;
15
+ overflow-y: auto;
16
+ `;
17
+
18
+ const DEFAULT_SCROLL_DIRECTION: ScrollDirection = 'up';
19
+
20
+ export function ScrollProvider( { children }: { children: ReactNode } ) {
21
+ const [ direction, setDirection ] = useState< ScrollDirection >( DEFAULT_SCROLL_DIRECTION );
22
+ const ref = useRef< HTMLDivElement >( null );
23
+ const scrollPos = useRef< number >( 0 );
24
+
25
+ useEffect( () => {
26
+ const scrollElement = ref.current;
27
+
28
+ if ( ! scrollElement ) {
29
+ return;
30
+ }
31
+
32
+ const handleScroll = () => {
33
+ const { scrollTop } = scrollElement;
34
+
35
+ if ( scrollTop > scrollPos.current ) {
36
+ setDirection( 'down' );
37
+ } else if ( scrollTop < scrollPos.current ) {
38
+ setDirection( 'up' );
39
+ }
40
+
41
+ scrollPos.current = scrollTop;
42
+ };
43
+
44
+ scrollElement.addEventListener( 'scroll', handleScroll );
45
+
46
+ return () => {
47
+ scrollElement.removeEventListener( 'scroll', handleScroll );
48
+ };
49
+ } );
50
+
51
+ return (
52
+ <ScrollContext.Provider value={ { direction } }>
53
+ <ScrollPanel ref={ ref }>{ children }</ScrollPanel>
54
+ </ScrollContext.Provider>
55
+ );
56
+ }
57
+
58
+ export function useScrollDirection() {
59
+ return useContext( ScrollContext )?.direction ?? DEFAULT_SCROLL_DIRECTION;
60
+ }
package/src/index.ts CHANGED
@@ -3,6 +3,7 @@ export type { PopoverActionProps } from './popover-action';
3
3
  export { registerControlReplacement } from './control-replacement';
4
4
  export { injectIntoClassSelectorActions } from './components/css-classes/css-class-selector';
5
5
  export { usePanelActions, usePanelStatus } from './panel';
6
+ export { type ValidationResult, type ValidationEvent } from './components/creatable-autocomplete';
6
7
  export { controlActionsMenu } from './controls-actions';
7
8
 
8
9
  export { init } from './init';
@@ -1,165 +0,0 @@
1
- import * as React from 'react';
2
- import { useState } from 'react';
3
- import { Autocomplete, type AutocompleteProps, createFilterOptions, TextField, type Theme } from '@elementor/ui';
4
-
5
- export type Option = {
6
- label: string;
7
- value: string | null;
8
- fixed?: boolean;
9
- group?: string;
10
- key?: string;
11
- };
12
-
13
- export type Action< TOption extends Option > = {
14
- label: ( value: string ) => string;
15
- apply: ( value: string ) => void | Promise< void >;
16
- condition: ( options: TOption[], value: string ) => boolean;
17
- group?: string;
18
- };
19
-
20
- type ActionAsOption< TOption extends Option > = TOption & {
21
- apply: Action< TOption >[ 'apply' ];
22
- condition: Action< TOption >[ 'condition' ];
23
- };
24
-
25
- type Props< TOption extends Option > = Omit<
26
- AutocompleteProps< TOption, true, true, true >,
27
- 'renderInput' | 'onSelect'
28
- > & {
29
- actions?: Action< TOption >[];
30
- selected: TOption[];
31
- options: TOption[];
32
- onSelect?: ( value: TOption[] ) => void;
33
- placeholder?: string;
34
- };
35
-
36
- export function MultiCombobox< TOption extends Option >( {
37
- actions = [],
38
- selected,
39
- options,
40
- onSelect,
41
- placeholder,
42
- ...props
43
- }: Props< TOption > ) {
44
- const filter = useFilterOptions< TOption >();
45
- const { run, loading } = useActionRunner< TOption >();
46
-
47
- return (
48
- <Autocomplete
49
- { ...props }
50
- freeSolo
51
- multiple
52
- clearOnBlur
53
- selectOnFocus
54
- disableClearable
55
- handleHomeEndKeys
56
- disabled={ loading }
57
- value={ selected }
58
- options={ options }
59
- renderInput={ ( params ) => (
60
- <TextField
61
- { ...params }
62
- placeholder={ placeholder }
63
- sx={ ( theme: Theme ) => ( {
64
- '.MuiAutocomplete-inputRoot.MuiInputBase-adornedStart': {
65
- paddingLeft: theme.spacing( 0.25 ),
66
- paddingRight: theme.spacing( 0.25 ),
67
- },
68
- } ) }
69
- />
70
- ) }
71
- onChange={ ( _, selectedOrInputValue, reason ) => {
72
- const inputValue = selectedOrInputValue.find( ( option ) => typeof option === 'string' );
73
- const optionsAndActions = selectedOrInputValue.filter( ( option ) => typeof option !== 'string' );
74
-
75
- // Handles user input when Enter is pressed
76
- if ( reason === 'createOption' ) {
77
- const [ firstAction ] = filterActions( actions, { options, inputValue: inputValue ?? '' } );
78
-
79
- if ( firstAction?.value ) {
80
- return run( firstAction.apply, firstAction.value );
81
- }
82
- }
83
-
84
- // Handles the user's action selection when triggered.
85
- const action = optionsAndActions.find( ( value ) => isAction( value ) );
86
-
87
- if ( reason === 'selectOption' && action?.value ) {
88
- return run( action.apply, action.value );
89
- }
90
-
91
- // Every other case, we update the selected values.
92
- const fixedValues = options.filter( ( option ) => !! option.fixed );
93
-
94
- onSelect?.( [ ...new Set( [ ...optionsAndActions, ...fixedValues ] ) ] );
95
- } }
96
- getOptionLabel={ ( option ) => ( typeof option === 'string' ? option : option.label ) }
97
- getOptionKey={ ( option ) => {
98
- if ( typeof option === 'string' ) {
99
- return option;
100
- }
101
-
102
- return option.key ?? option.value ?? option.label;
103
- } }
104
- filterOptions={ ( optionList, params ) => {
105
- const selectedValues = selected.map( ( option ) => option.value );
106
-
107
- return [
108
- ...filterActions( actions, { options: optionList, inputValue: params.inputValue } ),
109
- ...filter(
110
- optionList.filter( ( option ) => ! selectedValues.includes( option.value ) ),
111
- params
112
- ),
113
- ];
114
- } }
115
- groupBy={ ( option ) => option.group ?? '' }
116
- renderOption={ ( optionProps, { label, group } ) => (
117
- <li { ...optionProps } style={ { display: 'block', textOverflow: 'ellipsis' } } data-group={ group }>
118
- { label }
119
- </li>
120
- ) }
121
- />
122
- );
123
- }
124
-
125
- function useFilterOptions< TOption extends Option >() {
126
- return useState( () => createFilterOptions< TOption >() )[ 0 ];
127
- }
128
-
129
- function useActionRunner< TOption extends Option >() {
130
- const [ loading, setLoading ] = useState( false );
131
-
132
- const run = async ( apply: Action< TOption >[ 'apply' ], value: string ) => {
133
- setLoading( true );
134
-
135
- try {
136
- await apply( value );
137
- } catch {
138
- // TODO: Do something with the error.
139
- }
140
-
141
- setLoading( false );
142
- };
143
-
144
- return { run, loading };
145
- }
146
-
147
- function filterActions< TOption extends Option >(
148
- actions: Action< TOption >[],
149
- { options, inputValue }: { options: TOption[]; inputValue: string }
150
- ) {
151
- return actions
152
- .filter( ( action ) => action.condition( options, inputValue ) )
153
- .map( ( action, index ) => ( {
154
- label: action.label( inputValue ),
155
- value: inputValue,
156
- group: action.group,
157
- apply: action.apply,
158
- condition: action.condition,
159
- key: index.toString(),
160
- } ) ) as ActionAsOption< TOption >[];
161
- }
162
-
163
- function isAction< TOption extends Option >( option: TOption ): option is ActionAsOption< TOption > {
164
- return 'apply' in option && 'condition' in option;
165
- }