-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathLayoutContext.tsx
More file actions
306 lines (267 loc) · 9.32 KB
/
LayoutContext.tsx
File metadata and controls
306 lines (267 loc) · 9.32 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
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
import {
createContext,
MutableRefObject,
ReactNode,
useContext,
useMemo,
useRef,
useState,
} from 'react';
import { useEvent } from '../../../_internal/hooks';
export type Side = 'left' | 'top' | 'right' | 'bottom';
/**
* Refs context - stable refs that don't change and don't trigger re-renders.
* Provides portal container ref for panels to render into.
*/
export interface LayoutRefsContextValue {
/** Container element ref for panels to portal into */
panelContainerRef: MutableRefObject<HTMLDivElement | null>;
/** Whether the panel container is mounted and ready for portals */
isPanelContainerReady: boolean;
/** Callback ref to set on the panel container element */
setPanelContainer: (element: HTMLDivElement | null) => void;
}
export const LayoutRefsContext = createContext<LayoutRefsContextValue | null>(
null,
);
export function useLayoutRefsContext(): LayoutRefsContextValue | null {
return useContext(LayoutRefsContext);
}
/** Callback to dismiss an overlay panel */
export type OverlayDismissCallback = () => void;
/**
* Actions context - stable functions and configuration that don't change.
* Separating these from state prevents unnecessary re-renders when only state changes.
*/
export interface LayoutActionsContextValue {
/** Register a panel on a specific side with initial size */
registerPanel: (side: Side, size: number) => void;
/** Unregister a panel from a specific side */
unregisterPanel: (side: Side) => void;
/** Update the size of a registered panel */
updatePanelSize: (side: Side, size: number) => void;
/** Set global dragging state (when any panel is being resized) */
setDragging: (isDragging: boolean) => void;
/** Mark the layout as ready (after initial mount) */
markReady: () => void;
/** Update the container dimensions (called from ResizeObserver) */
updateContainerSize: (width: number, height: number) => void;
/** Register an overlay panel's dismiss callback. Returns unregister function. */
registerOverlayPanel: (dismiss: OverlayDismissCallback) => () => void;
/** Dismiss all overlay panels */
dismissOverlayPanels: () => void;
/** Whether transitions are enabled for panels (stable config value) */
hasTransition: boolean;
/** Minimum size reserved for the content area between panels */
minContentSize: number;
}
/** State context - reactive state that triggers re-renders */
export interface LayoutStateContextValue {
panelSizes: Record<Side, number>;
isDragging: boolean;
isReady: boolean;
hasOverlayPanels: boolean;
containerWidth: number;
containerHeight: number;
}
export const LayoutActionsContext =
createContext<LayoutActionsContextValue | null>(null);
export const LayoutStateContext = createContext<LayoutStateContextValue | null>(
null,
);
export function useLayoutActionsContext(): LayoutActionsContextValue | null {
return useContext(LayoutActionsContext);
}
export function useLayoutStateContext(): LayoutStateContextValue | null {
return useContext(LayoutStateContext);
}
/** Combined layout context value for convenience */
export interface LayoutContextValue
extends LayoutActionsContextValue,
LayoutStateContextValue,
LayoutRefsContextValue {}
/**
* Combined hook that returns all layout context values.
* Convenience wrapper around the individual context hooks.
* Returns null if used outside of a Layout component.
*/
export function useLayoutContext(): LayoutContextValue | null {
const actions = useLayoutActionsContext();
const state = useLayoutStateContext();
const refs = useLayoutRefsContext();
if (!actions || !state || !refs) {
return null;
}
return { ...actions, ...state, ...refs };
}
export interface LayoutProviderProps {
children: ReactNode;
/** Whether transitions are enabled for panels */
hasTransition?: boolean;
/** Minimum size reserved for the content area between panels. Default: 320 */
minContentSize?: number;
}
export function LayoutProvider({
children,
hasTransition = false,
minContentSize = 320,
}: LayoutProviderProps) {
const registeredPanels = useRef<Set<Side>>(new Set());
const overlayPanelCallbacks = useRef<Set<OverlayDismissCallback>>(new Set());
const panelContainerRef = useRef<HTMLDivElement | null>(null);
const [isPanelContainerReady, setIsPanelContainerReady] = useState(false);
// Callback ref for panel container - triggers re-render when container mounts
const setPanelContainer = useEvent((element: HTMLDivElement | null) => {
panelContainerRef.current = element;
setIsPanelContainerReady(element !== null);
});
const [panelSizes, setPanelSizes] = useState<Record<Side, number>>({
left: 0,
top: 0,
right: 0,
bottom: 0,
});
const [isDragging, setIsDragging] = useState(false);
const [isReady, setIsReady] = useState(false);
const [hasOverlayPanels, setHasOverlayPanels] = useState(false);
const [containerWidth, setContainerWidth] = useState(0);
const [containerHeight, setContainerHeight] = useState(0);
const updatePanelSize = useEvent((side: Side, size: number) => {
setPanelSizes((prev) => {
if (prev[side] === size) return prev;
return { ...prev, [side]: size };
});
});
const registerPanel = useEvent((side: Side, size: number) => {
if (registeredPanels.current.has(side)) {
throw new Error(
`Layout: Only one panel per side is allowed. ` +
`A panel is already registered on the "${side}" side.`,
);
}
// Check for axis conflict
const isHorizontal = side === 'left' || side === 'right';
const conflictingSides: Side[] = isHorizontal
? ['top', 'bottom']
: ['left', 'right'];
for (const conflictSide of conflictingSides) {
if (registeredPanels.current.has(conflictSide)) {
throw new Error(
`Layout: Panels from different axes cannot be combined. ` +
`Cannot register "${side}" panel when "${conflictSide}" panel exists. ` +
`Use either horizontal (left/right) or vertical (top/bottom) panels.`,
);
}
}
registeredPanels.current.add(side);
updatePanelSize(side, size);
});
const unregisterPanel = useEvent((side: Side) => {
registeredPanels.current.delete(side);
updatePanelSize(side, 0);
});
const setDragging = useEvent((dragging: boolean) => {
setIsDragging(dragging);
});
const markReady = useEvent(() => {
setIsReady(true);
});
const registerOverlayPanel = useEvent((dismiss: OverlayDismissCallback) => {
overlayPanelCallbacks.current.add(dismiss);
setHasOverlayPanels(true);
// Return unregister function
return () => {
overlayPanelCallbacks.current.delete(dismiss);
setHasOverlayPanels(overlayPanelCallbacks.current.size > 0);
};
});
const dismissOverlayPanels = useEvent(() => {
overlayPanelCallbacks.current.forEach((dismiss) => dismiss());
});
const updateContainerSize = useEvent((width: number, height: number) => {
setContainerWidth((prev) => (prev === width ? prev : width));
setContainerHeight((prev) => (prev === height ? prev : height));
});
// Actions context - stable because all callbacks use useEvent
const actionsValue = useMemo(
() => ({
registerPanel,
unregisterPanel,
updatePanelSize,
setDragging,
markReady,
updateContainerSize,
hasTransition,
minContentSize,
registerOverlayPanel,
dismissOverlayPanels,
}),
// Only hasTransition and minContentSize can change - all other values are stable useEvent callbacks
[hasTransition, minContentSize],
);
// State context - changes when state updates
const stateValue = useMemo(
() => ({
panelSizes,
isDragging,
isReady,
hasOverlayPanels,
containerWidth,
containerHeight,
}),
[
panelSizes,
isDragging,
isReady,
hasOverlayPanels,
containerWidth,
containerHeight,
],
);
// Refs context - includes container ready state
const refsValue = useMemo(
() => ({
panelContainerRef,
isPanelContainerReady,
setPanelContainer,
}),
[isPanelContainerReady],
);
return (
<LayoutRefsContext.Provider value={refsValue}>
<LayoutActionsContext.Provider value={actionsValue}>
<LayoutStateContext.Provider value={stateValue}>
{children}
</LayoutStateContext.Provider>
</LayoutActionsContext.Provider>
</LayoutRefsContext.Provider>
);
}
/**
* Provider that resets the layout context for sub-components.
* Used to prevent panels in nested Layouts from affecting parent layouts.
*/
export function LayoutContextReset({ children }: { children: ReactNode }) {
return (
<LayoutRefsContext.Provider value={null}>
<LayoutActionsContext.Provider value={null}>
<LayoutStateContext.Provider value={null}>
{children}
</LayoutStateContext.Provider>
</LayoutActionsContext.Provider>
</LayoutRefsContext.Provider>
);
}
// Panel context - provides panel-level callbacks to child components
export interface LayoutPanelContextValue {
/** Callback to change the panel's open state */
onOpenChange: (isOpen: boolean) => void;
/** Current open state of the panel */
isOpen: boolean;
}
export const LayoutPanelContext = createContext<LayoutPanelContextValue | null>(
null,
);
export function useLayoutPanelContext(): LayoutPanelContextValue | null {
return useContext(LayoutPanelContext);
}