-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathStylesProvider.tsx
More file actions
61 lines (49 loc) · 1.63 KB
/
StylesProvider.tsx
File metadata and controls
61 lines (49 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import type { Sector } from 'grapesjs';
import React, { memo, useEffect, useState } from 'react';
import { useEditorInstance } from './context/EditorInstance';
import { isFunction } from './utils';
import { PortalContainerResult, portalContainer } from './utils/react';
import { useEditorOptions } from './context/EditorOptions';
export type StylesState = {
/**
* Array of visible sectors.
*/
sectors: Sector[],
/**
* Default Styles container.
*/
Container: PortalContainerResult,
};
export type StylesResultProps = StylesState;
export interface StylesProviderProps {
children: (props: StylesResultProps) => React.JSX.Element,
}
const StylesProvider = memo(function ({ children }: StylesProviderProps) {
const { editor } = useEditorInstance();
const options = useEditorOptions();
const [propState, setPropState] = useState<StylesState>(() => ({
sectors: [],
Container: () => null,
}));
useEffect(() => {
if (!editor) return;
const { Styles } = editor;
const event = Styles.events.custom;
const up = ({ container }: { container: HTMLElement }) => {
setPropState({
sectors: Styles.getSectors({ visible: true }),
Container: portalContainer(container),
});
}
editor.on(event, up);
Styles.__trgCustom();
return () => {
editor.off(event, up);
};
}, [editor]);
useEffect(() => options.setCustomStyles(true), []);
return editor ?
(isFunction(children) ? children(propState) : null)
: null;
});
export default StylesProvider;