-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.tsx
183 lines (163 loc) · 5.74 KB
/
index.tsx
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import {
createContext,
FunctionComponent,
Key,
ReactElement,
useCallback,
useContext,
useEffect,
useMemo,
useState
} from 'react';
import styled from 'styled-components';
export type OpenPanelFunction<ResultType, PropType = undefined> = PropType extends undefined
? () => Promise<ResultType | null>
: (props: PropType) => Promise<ResultType | null>;
export type ClosePanelFunction<PropType = undefined> = PropType extends undefined
? () => void
: (props?: PropType) => void;
export type RegisterPanelFunction<ResultType = void, PropType = undefined> = (
props: ManagedPanel<ResultType, PropType>
) => ReactElement<ManagedPanel<ResultType, PropType>>;
export interface PanelManagerContextType {
register: <ResultType = void, PropType = undefined>(
registerPanelFunction: RegisterPanelFunction<ResultType, PropType>
) => OpenPanelFunction<ResultType, PropType>;
dismiss: () => void;
}
export type ManagedPanel<ResultType = void, PropType = undefined> = PropType extends undefined
? {
dismiss: () => void;
close: ClosePanelFunction<ResultType>;
}
: {
dismiss: () => void;
close: ClosePanelFunction<ResultType>;
props: PropType;
};
interface PromiseExecutor<ResultType> {
resolve: (value: ResultType) => void;
reject: () => void;
}
interface PanelExecutor<ResultType, PropType> {
key: Key;
panel: ReactElement<ManagedPanel<ResultType, PropType>>;
executor: PromiseExecutor<ResultType>;
}
enum PanelManagerPosition {
Left,
Right
}
interface PanelManagerContextProps {
position?: PanelManagerPosition;
}
const Container = styled.div<{ show: boolean; position: PanelManagerPosition }>`
width: 680px;
position: fixed;
z-index: 99;
transition: all 0.2s ease-in-out;
opacity: ${(props) => (props.show ? '1' : '0.25')};
height: 100%;
top: 0;
${(props) =>
props.position === PanelManagerPosition.Left && `
box-shadow: 3px 1px 4px rgba(0, 0, 0, ${props.show ? '0.15' : '0'});
transform: translateX(${props.show ? '0' : '-100%'});
left: 0;
`}
${(props) =>
props.position === PanelManagerPosition.Right && `
box-shadow: -3px 1px 4px rgba(0, 0, 0, ${props.show ? '0.15' : '0'});
transform: translateX(${props.show ? '0' : '100%'});
right: 0;
`}
`;
const PanelManagerContext = createContext<PanelManagerContextType | undefined>(undefined);
export const usePanelManagerContext = (): PanelManagerContextType => {
const panelManagerContext = useContext(PanelManagerContext);
if (!panelManagerContext) {
throw new Error('usePanelManagerContext must be used within the PanelMangerProvider.');
}
return panelManagerContext;
};
export const PanelManagerContextProvider: FunctionComponent<PanelManagerContextProps> = ({
position = PanelManagerPosition.Right,
children
}) => {
const panelRegistry = useMemo<Record<Key, OpenPanelFunction<unknown>>>(() => ({}), []);
const [panelExecutorStack, setPanelExecutorStack] = useState<PanelExecutor<unknown, unknown>[]>([]);
const [closingPanel, setClosingPanel] = useState<ReactElement<ManagedPanel> | null>();
const [panels, setPanels] = useState<ReactElement<ManagedPanel>[]>([]);
useEffect(() => {
const activePanels = panelExecutorStack.length
? panelExecutorStack.map((keyExecutor) => keyExecutor.panel)
: [closingPanel as ReactElement];
setPanels(activePanels);
}, [closingPanel, panelExecutorStack, setPanels]);
const close = useCallback(<ResultType,>(result: ResultType) => {
setPanelExecutorStack((stack) => {
const keyExecutor = stack.pop() as PanelExecutor<unknown, unknown>;
keyExecutor.executor.resolve(result || null);
return [...stack];
});
}, []);
const dismiss = useCallback(() => {
setPanelExecutorStack((stack) => {
stack.forEach((keyExecutor) => keyExecutor.executor.resolve(null));
return [];
});
}, [setPanelExecutorStack]);
const open = useCallback(
<ResultType, PropType>(
key: Key,
registerPanelFunction: RegisterPanelFunction<ResultType, PropType>,
props: PropType
): Promise<ResultType | null> => {
let executor: PromiseExecutor<unknown>;
const result = new Promise((resolve, reject) => {
executor = { reject, resolve };
});
const panel = registerPanelFunction({
close: (value: ResultType) => close(value),
dismiss,
props
} as never) as ReactElement<ManagedPanel<unknown, unknown>>;
setPanelExecutorStack((stack) => {
const panelExecutor = { executor, key, panel };
if (!stack.length) setClosingPanel(panel);
return [...stack].pop()?.key !== key ? [...stack, panelExecutor] : [...stack];
});
return result as Promise<ResultType>;
},
[close, dismiss]
);
const register = useCallback(
<ResultType, PropType = undefined>(
registerPanelFunction: RegisterPanelFunction<ResultType, PropType>
) => {
const panel = registerPanelFunction({
close: (result?: ResultType) => close(result),
dismiss,
props: {}
} as ManagedPanel<ResultType, PropType>);
if (!panel.key) {
throw new Error('Panel register requires a ReactElement with a key');
}
const openPanelFunction =
panelRegistry[panel.key] ||
((props: PropType) => open(panel.key as Key, registerPanelFunction, props));
panelRegistry[panel.key] = openPanelFunction;
return openPanelFunction as OpenPanelFunction<ResultType, PropType>;
},
[dismiss, panelRegistry, close, open]
);
const value = useMemo(() => ({ dismiss, register }), [dismiss, register]);
return (
<PanelManagerContext.Provider value={value}>
<Container position={position} show={!!panelExecutorStack.length}>
{panels}
</Container>
{children}
</PanelManagerContext.Provider>
);
};