|
1
|
import { ReactNode, useCallback, useContext, useMemo, useState } from "react" |
|
2
|
import { LayoutCustomizerHostContext } from "./customizer-host-context" |
|
3
|
import { LAYOUT_CONTROLS_LOCATION } from "../../components/layout-composer/constants" |
|
4
|
|
|
5
|
export type LayoutCustomizerHostValue = { |
|
6
|
/** DOM node registered for each location, or `null` when unmounted. */ |
|
7
|
hosts: Record<string, HTMLElement | null> |
|
8
|
setHost: (location: string, node: HTMLElement | null) => void |
|
9
|
/** |
|
10
|
* Id of the composer currently in edit mode, or `null` when none is. |
|
11
|
*/ |
|
12
|
activeEditor: string | null |
|
13
|
setActiveEditor: (id: string | null) => void |
|
14
|
/** |
|
15
|
* `customizeId` of the host the `CustomizerMenu` has asked to enter edit |
|
16
|
* mode, or `null`. The matching composer consumes this (and clears it) on its |
|
17
|
* next render. Acts as a one-shot command bus so the single, central trigger |
|
18
|
* can drive any composer without owning a button inside it. |
|
19
|
*/ |
|
20
|
editRequest: string | null |
|
21
|
requestEdit: (id: string | null) => void |
|
22
|
} |
|
23
|
|
|
24
|
/** |
|
25
|
* Wraps a tree that hosts the layout customizer trigger UI in one or more |
|
26
|
* fixed locations (e.g. the app shell's top bar, the sidebar header dropdown). |
|
27
|
* Each active `LayoutComposer` portals its trigger / edit controls into the |
|
28
|
* `LayoutCustomizerSlot` matching its configured `triggerLocation` |
|
29
|
*/ |
|
30
|
export const LayoutCustomizerHostProvider = ({ |
|
31
|
children, |
|
32
|
}: { |
|
33
|
children: ReactNode |
|
34
|
}) => { |
|
35
|
const [hosts, setHosts] = useState<Record<string, HTMLElement | null>>({}) |
|
36
|
const [activeEditor, setActiveEditor] = useState<string | null>(null) |
|
37
|
const [editRequest, setEditRequest] = useState<string | null>(null) |
|
38
|
|
|
39
|
const setHost = useCallback((location: string, node: HTMLElement | null) => { |
|
40
|
setHosts((prev) => { |
|
41
|
if (prev[location] === node) { |
|
42
|
return prev |
|
43
|
} |
|
44
|
return { ...prev, [location]: node } |
|
45
|
}) |
|
46
|
}, []) |
|
47
|
|
|
48
|
const value = useMemo( |
|
49
|
() => ({ |
|
50
|
hosts, |
|
51
|
setHost, |
|
52
|
activeEditor, |
|
53
|
setActiveEditor, |
|
54
|
editRequest, |
|
55
|
requestEdit: setEditRequest, |
|
56
|
}), |
|
57
|
[hosts, setHost, activeEditor, editRequest] |
|
58
|
) |
|
59
|
|
|
60
|
return ( |
|
61
|
<LayoutCustomizerHostContext.Provider value={value}> |
|
62
|
{children} |
|
63
|
</LayoutCustomizerHostContext.Provider> |
|
64
|
) |
|
65
|
} |
|
66
|
|
|
67
|
/** |
|
68
|
* Placeholder for a `LayoutComposer`'s customizer controls (trigger button at |
|
69
|
* idle, Cancel/Save while editing), identified by `location`. A composer with a |
|
70
|
* matching `triggerLocation` portals its controls here. |
|
71
|
*/ |
|
72
|
export const LayoutCustomizerSlot = ({ |
|
73
|
location = LAYOUT_CONTROLS_LOCATION, |
|
74
|
className = "contents", |
|
75
|
}: { |
|
76
|
location?: string |
|
77
|
className?: string |
|
78
|
}) => { |
|
79
|
const setHost = useContext(LayoutCustomizerHostContext)?.setHost |
|
80
|
const ref = useCallback( |
|
81
|
(el: HTMLDivElement | null) => { |
|
82
|
setHost?.(location, el) |
|
83
|
}, |
|
84
|
[setHost, location] |
|
85
|
) |
|
86
|
return <div ref={ref} className={className} /> |
|
87
|
} |