@elementor/editor-app-bar 0.14.0 → 0.14.2
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/CHANGELOG.md +16 -0
- package/dist/index.d.mts +78 -196
- package/dist/index.d.ts +78 -196
- package/dist/index.js +774 -201
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +734 -162
- package/dist/index.mjs.map +1 -1
- package/package.json +9 -8
- package/src/components/__tests__/app-bar.test.tsx +23 -0
- package/src/components/actions/action.tsx +33 -0
- package/src/components/actions/actions-group.tsx +69 -0
- package/src/components/actions/link.tsx +33 -0
- package/src/components/actions/toggle-action.tsx +35 -0
- package/src/components/app-bar.tsx +42 -0
- package/src/components/locations/__tests__/locations-components.test.tsx +37 -0
- package/src/components/locations/__tests__/menus.test.tsx +158 -0
- package/src/components/locations/integrations-menu-location.tsx +44 -0
- package/src/components/locations/main-menu-location.tsx +57 -0
- package/src/components/locations/page-indication-location.tsx +8 -0
- package/src/components/locations/primary-action-location.tsx +8 -0
- package/src/components/locations/responsive-location.tsx +8 -0
- package/src/components/locations/tools-menu-location.tsx +28 -0
- package/src/components/locations/utilities-menu-location.tsx +35 -0
- package/src/components/ui/popover-menu-item.tsx +47 -0
- package/src/components/ui/popover-menu.tsx +26 -0
- package/src/components/ui/popover-sub-menu.tsx +29 -0
- package/src/components/ui/toolbar-logo.tsx +84 -0
- package/src/components/ui/toolbar-menu-item.tsx +45 -0
- package/src/components/ui/toolbar-menu-more.tsx +29 -0
- package/src/components/ui/toolbar-menu-toggle-item.tsx +34 -0
- package/src/components/ui/toolbar-menu.tsx +15 -0
- package/src/contexts/menu-context.tsx +24 -0
- package/src/extensions/documents-indicator/index.ts +1 -1
- package/src/extensions/documents-preview/index.ts +1 -1
- package/src/extensions/documents-save/components/primary-action-menu.tsx +1 -1
- package/src/extensions/documents-save/hooks/use-document-copy-and-share-props.ts +1 -1
- package/src/extensions/documents-save/hooks/use-document-save-draft-props.ts +1 -1
- package/src/extensions/documents-save/hooks/use-document-save-template-props.ts +1 -1
- package/src/extensions/documents-save/index.ts +1 -1
- package/src/extensions/documents-save/locations.ts +1 -1
- package/src/extensions/elements/index.ts +1 -1
- package/src/extensions/finder/index.ts +1 -1
- package/src/extensions/help/index.ts +1 -1
- package/src/extensions/history/index.ts +1 -1
- package/src/extensions/keyboard-shortcuts/hooks/use-action-props.ts +1 -2
- package/src/extensions/keyboard-shortcuts/index.ts +1 -1
- package/src/extensions/site-settings/hooks/use-action-props.ts +1 -2
- package/src/extensions/site-settings/index.ts +1 -1
- package/src/extensions/structure/hooks/use-action-props.ts +1 -2
- package/src/extensions/structure/index.ts +1 -1
- package/src/extensions/theme-builder/hooks/use-action-props.ts +1 -2
- package/src/extensions/theme-builder/index.ts +1 -1
- package/src/extensions/user-preferences/hooks/use-action-props.ts +1 -2
- package/src/extensions/user-preferences/index.ts +1 -1
- package/src/extensions/wordpress/index.ts +1 -1
- package/src/index.ts +14 -10
- package/src/init.ts +1 -1
- package/src/locations/__tests__/menus.test.tsx +212 -0
- package/src/locations/index.ts +27 -0
- package/src/locations/menus.tsx +172 -0
- package/src/types.ts +4 -0
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
import { ComponentPropsWithoutRef, ElementType, useMemo } from 'react';
|
|
3
|
+
import { createLocation, Location } from '@elementor/locations';
|
|
4
|
+
import Action from '../components/actions/action';
|
|
5
|
+
import ToggleAction from '../components/actions/toggle-action';
|
|
6
|
+
import Link from '../components/actions/link';
|
|
7
|
+
import ActionsGroup from '../components/actions/actions-group';
|
|
8
|
+
|
|
9
|
+
type GroupName = string;
|
|
10
|
+
|
|
11
|
+
type MenuGroup<TGroup extends GroupName> = TGroup | 'default';
|
|
12
|
+
|
|
13
|
+
export type ItemsArray = Array<{ id: string, MenuItem: ElementType }>;
|
|
14
|
+
type GroupedItems<TGroup extends GroupName> = Record<MenuGroup<TGroup>, ItemsArray>;
|
|
15
|
+
|
|
16
|
+
type GroupLocationMap<TGroup extends GroupName> = Record<MenuGroup<TGroup>, Location>;
|
|
17
|
+
|
|
18
|
+
type ItemConfigProps<TComponent extends ElementType> = Omit<ComponentPropsWithoutRef<TComponent>, 'items'>;
|
|
19
|
+
|
|
20
|
+
type MenuItem<
|
|
21
|
+
TGroup extends GroupName,
|
|
22
|
+
TComponent extends ElementType,
|
|
23
|
+
> = {
|
|
24
|
+
id: string,
|
|
25
|
+
group?: TGroup,
|
|
26
|
+
priority?: number,
|
|
27
|
+
overwrite?: boolean,
|
|
28
|
+
} & (
|
|
29
|
+
{ props: ItemConfigProps<TComponent>, useProps?: never } |
|
|
30
|
+
{ useProps: () => ItemConfigProps<TComponent>, props?: never }
|
|
31
|
+
)
|
|
32
|
+
|
|
33
|
+
export type MenuActions<TGroup extends GroupName> = {
|
|
34
|
+
registerAction: ( args: MenuItem<TGroup, typeof Action> ) => void,
|
|
35
|
+
registerToggleAction: ( args: MenuItem<TGroup, typeof ToggleAction> ) => void,
|
|
36
|
+
registerLink: ( args: MenuItem<TGroup, typeof Link> ) => void,
|
|
37
|
+
registerSubMenu: ( args: MenuItem<'default', typeof ActionsGroup> ) => MenuActions<'default'>,
|
|
38
|
+
useMenuItems: () => GroupedItems<TGroup>,
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function createMenu<
|
|
42
|
+
TGroup extends GroupName = 'default'
|
|
43
|
+
>( groups: TGroup[] = [] ): MenuActions<MenuGroup<TGroup>> {
|
|
44
|
+
const menuGroups: MenuGroup<TGroup>[] = [
|
|
45
|
+
...groups,
|
|
46
|
+
'default' as const,
|
|
47
|
+
];
|
|
48
|
+
|
|
49
|
+
const locations = menuGroups.reduce(
|
|
50
|
+
( carry, group ) => ( {
|
|
51
|
+
...carry,
|
|
52
|
+
[ group ]: createLocation(),
|
|
53
|
+
} ),
|
|
54
|
+
{} as GroupLocationMap<TGroup>
|
|
55
|
+
);
|
|
56
|
+
|
|
57
|
+
const [
|
|
58
|
+
registerAction,
|
|
59
|
+
registerToggleAction,
|
|
60
|
+
registerLink,
|
|
61
|
+
] = [ Action, ToggleAction, Link ].map(
|
|
62
|
+
( Component ) => createRegisterMenuItem( {
|
|
63
|
+
locations,
|
|
64
|
+
menuGroups,
|
|
65
|
+
component: Component,
|
|
66
|
+
} )
|
|
67
|
+
);
|
|
68
|
+
const useMenuItems = createUseMenuItems( locations );
|
|
69
|
+
|
|
70
|
+
const registerSubMenu = createRegisterSubMenu( {
|
|
71
|
+
locations,
|
|
72
|
+
menuGroups,
|
|
73
|
+
} );
|
|
74
|
+
|
|
75
|
+
return {
|
|
76
|
+
registerAction,
|
|
77
|
+
registerToggleAction,
|
|
78
|
+
registerLink,
|
|
79
|
+
registerSubMenu,
|
|
80
|
+
useMenuItems,
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function createRegisterMenuItem<
|
|
85
|
+
TGroup extends GroupName,
|
|
86
|
+
TComponent extends ElementType,
|
|
87
|
+
>( { locations, menuGroups, component, useMenuItems }: {
|
|
88
|
+
locations: GroupLocationMap<TGroup>
|
|
89
|
+
menuGroups: MenuGroup<TGroup>[],
|
|
90
|
+
component: TComponent,
|
|
91
|
+
useMenuItems?: () => ItemsArray
|
|
92
|
+
} ) {
|
|
93
|
+
return ( { group = 'default', id, overwrite, priority, ...args }: MenuItem<MenuGroup<TGroup>, TComponent> ) => {
|
|
94
|
+
if ( ! menuGroups.includes( group ) ) {
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
const useProps = 'props' in args ? () => args.props : args.useProps;
|
|
99
|
+
|
|
100
|
+
const Component = component as ElementType;
|
|
101
|
+
|
|
102
|
+
const InjectedComponent = ( props: object ) => {
|
|
103
|
+
const componentProps = useProps();
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* if passing useMenuItems - it's to use the menu item as a group parent
|
|
107
|
+
* in such case we'd also pass the id in case it's needed to wrap the group by
|
|
108
|
+
*/
|
|
109
|
+
const items = useMenuItems?.();
|
|
110
|
+
if ( items?.length ) {
|
|
111
|
+
return <Component
|
|
112
|
+
{ ...props }
|
|
113
|
+
{ ...componentProps }
|
|
114
|
+
items={ items }
|
|
115
|
+
/>;
|
|
116
|
+
}
|
|
117
|
+
return <Component { ...props } { ...componentProps } />;
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
locations[ group ].inject( {
|
|
121
|
+
id,
|
|
122
|
+
component: InjectedComponent,
|
|
123
|
+
options: {
|
|
124
|
+
priority,
|
|
125
|
+
overwrite,
|
|
126
|
+
},
|
|
127
|
+
} );
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function createRegisterSubMenu<
|
|
132
|
+
TGroup extends GroupName,
|
|
133
|
+
>( { locations, menuGroups }: {
|
|
134
|
+
locations: GroupLocationMap<TGroup>
|
|
135
|
+
menuGroups: MenuGroup<TGroup>[],
|
|
136
|
+
} ) {
|
|
137
|
+
return ( args: MenuItem<MenuGroup<'default'>, typeof ActionsGroup> ) => {
|
|
138
|
+
const menu = createMenu();
|
|
139
|
+
|
|
140
|
+
createRegisterMenuItem( {
|
|
141
|
+
locations,
|
|
142
|
+
menuGroups,
|
|
143
|
+
component: ActionsGroup,
|
|
144
|
+
useMenuItems: () => menu.useMenuItems().default,
|
|
145
|
+
} )( args );
|
|
146
|
+
|
|
147
|
+
return menu;
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
function createUseMenuItems<TGroup extends GroupName>( locations : GroupLocationMap<TGroup> ) {
|
|
152
|
+
return () => {
|
|
153
|
+
// Normalize the injections groups to an object with the groups as keys.
|
|
154
|
+
return useMemo( () => {
|
|
155
|
+
return Object.entries( locations ).reduce<GroupedItems<TGroup>>(
|
|
156
|
+
( carry, [ groupName, location ] ) => {
|
|
157
|
+
const items = location.getInjections()
|
|
158
|
+
.map( ( injection ) => ( {
|
|
159
|
+
id: injection.id,
|
|
160
|
+
MenuItem: injection.component,
|
|
161
|
+
} ) );
|
|
162
|
+
|
|
163
|
+
return {
|
|
164
|
+
...carry,
|
|
165
|
+
[ groupName ]: items,
|
|
166
|
+
};
|
|
167
|
+
},
|
|
168
|
+
{} as GroupedItems<TGroup>
|
|
169
|
+
);
|
|
170
|
+
}, [] );
|
|
171
|
+
};
|
|
172
|
+
}
|
package/src/types.ts
CHANGED
|
@@ -14,3 +14,7 @@ export type ExtendedWindow = Window & {
|
|
|
14
14
|
}
|
|
15
15
|
}
|
|
16
16
|
}
|
|
17
|
+
|
|
18
|
+
export type { Props as ActionProps } from './components/actions/action';
|
|
19
|
+
export type { Props as ToggleActionProps } from './components/actions/toggle-action';
|
|
20
|
+
export type { Props as LinkProps } from './components/actions/link';
|