@elementor/locations 0.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.
- package/LICENSE +674 -0
- package/README.md +5 -0
- package/dist/index.d.ts +38 -0
- package/dist/index.js +137 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +95 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +38 -0
- package/src/__tests__/index.test.tsx +217 -0
- package/src/components/error-boundary.tsx +29 -0
- package/src/components/filler-wrapper.tsx +13 -0
- package/src/components/slot.tsx +19 -0
- package/src/hooks/use-injections-of.ts +18 -0
- package/src/index.ts +4 -0
- package/src/injections.tsx +64 -0
- package/src/types.ts +19 -0
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
import { Location, Filler, Injection, InjectionOptions, Name, Id } from './types';
|
|
3
|
+
import FillerWrapper from './components/filler-wrapper';
|
|
4
|
+
|
|
5
|
+
const DEFAULT_PRIORITY = 10;
|
|
6
|
+
|
|
7
|
+
const injections: Map<Id, Injection> = new Map();
|
|
8
|
+
|
|
9
|
+
type InjectArgs = {
|
|
10
|
+
location: Location;
|
|
11
|
+
filler: Filler;
|
|
12
|
+
name: Name;
|
|
13
|
+
options?: InjectionOptions;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function inject( { location, filler, name, options = {} }: InjectArgs ) {
|
|
17
|
+
const id = generateId( location, name );
|
|
18
|
+
|
|
19
|
+
if ( injections.has( id ) && ! options?.overwrite ) {
|
|
20
|
+
// eslint-disable-next-line no-console
|
|
21
|
+
console.error(
|
|
22
|
+
`An injection named "${ name }" under location "${ location }" already exists. Did you mean to use "options.overwrite"?`
|
|
23
|
+
);
|
|
24
|
+
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const injection = {
|
|
29
|
+
id,
|
|
30
|
+
location,
|
|
31
|
+
filler: wrapFiller( filler ),
|
|
32
|
+
priority: options.priority ?? DEFAULT_PRIORITY,
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
injections.set( id, injection );
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export function createInjectorFor( location: Location ) {
|
|
39
|
+
return ( { filler, name, options }: Omit<InjectArgs, 'location'> ) => {
|
|
40
|
+
return inject( { location, name, filler, options } );
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export function getInjectionsOf( location: string ) {
|
|
45
|
+
return [ ...injections.values() ]
|
|
46
|
+
.filter( ( injection ) => injection.location === location )
|
|
47
|
+
.sort( ( a, b ) => a.priority - b.priority );
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function flushInjections() {
|
|
51
|
+
injections.clear();
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function wrapFiller( FillerComponent: Filler ) {
|
|
55
|
+
return ( props: object ) => (
|
|
56
|
+
<FillerWrapper>
|
|
57
|
+
<FillerComponent { ...props } />
|
|
58
|
+
</FillerWrapper>
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function generateId( location: Location, name: Name ) {
|
|
63
|
+
return `${ location }::${ name }`;
|
|
64
|
+
}
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { ComponentType } from 'react';
|
|
2
|
+
|
|
3
|
+
export type Location = string;
|
|
4
|
+
export type Filler = ComponentType;
|
|
5
|
+
export type Name = string;
|
|
6
|
+
export type Id = string;
|
|
7
|
+
export type Priority = number;
|
|
8
|
+
|
|
9
|
+
export type InjectionOptions = {
|
|
10
|
+
priority?: Priority;
|
|
11
|
+
overwrite?: boolean;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export type Injection = {
|
|
15
|
+
location: Location;
|
|
16
|
+
filler: Filler;
|
|
17
|
+
priority: Priority;
|
|
18
|
+
id: Id;
|
|
19
|
+
}
|