@loykin/side-panel 0.0.1-dev.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 +21 -0
- package/README.md +25 -0
- package/dist/index.cjs +217 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +41 -0
- package/dist/index.d.ts +41 -0
- package/dist/index.js +211 -0
- package/dist/index.js.map +1 -0
- package/dist/styles.css +41 -0
- package/dist/styles.d.ts +1 -0
- package/package.json +67 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 LeeSuk
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# @loykin/side-panel
|
|
2
|
+
|
|
3
|
+
Non-blocking, resizable React side panels with per-provider Zustand state,
|
|
4
|
+
close guards, snap points, and support for all four screen edges.
|
|
5
|
+
|
|
6
|
+
```bash
|
|
7
|
+
pnpm add @loykin/side-panel
|
|
8
|
+
```
|
|
9
|
+
|
|
10
|
+
```tsx
|
|
11
|
+
import { SidePanelProvider, useSidePanel } from '@loykin/side-panel'
|
|
12
|
+
import '@loykin/side-panel/styles'
|
|
13
|
+
|
|
14
|
+
function OpenDetails() {
|
|
15
|
+
const { open } = useSidePanel()
|
|
16
|
+
return <button onClick={() => open(<Details />, { side: 'right' })}>Open</button>
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
<SidePanelProvider>
|
|
20
|
+
<OpenDetails />
|
|
21
|
+
</SidePanelProvider>
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
React 18 and React 19 are supported. See the
|
|
25
|
+
[BaseKit repository](https://github.com/loykin/basekit) for all panel options.
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var react = require('react');
|
|
4
|
+
var zustand = require('zustand');
|
|
5
|
+
var reResizable = require('re-resizable');
|
|
6
|
+
var clsx = require('clsx');
|
|
7
|
+
var jsxRuntime = require('react/jsx-runtime');
|
|
8
|
+
|
|
9
|
+
// src/SidePanelProvider.tsx
|
|
10
|
+
var DEFAULTS = {
|
|
11
|
+
side: "right",
|
|
12
|
+
size: 560,
|
|
13
|
+
minSize: 400,
|
|
14
|
+
maxSize: 1e3,
|
|
15
|
+
resizable: true,
|
|
16
|
+
snapGap: 20,
|
|
17
|
+
closeOnOutsideClick: true,
|
|
18
|
+
closeOnEsc: true
|
|
19
|
+
};
|
|
20
|
+
var createSidePanelStore = (providerDefaults = {}) => zustand.createStore()((set) => ({
|
|
21
|
+
content: null,
|
|
22
|
+
isOpen: false,
|
|
23
|
+
size: providerDefaults.size ?? DEFAULTS.size,
|
|
24
|
+
options: {},
|
|
25
|
+
_open: (content, options) => {
|
|
26
|
+
const merged = { ...DEFAULTS, ...providerDefaults, ...options };
|
|
27
|
+
set({ content, isOpen: true, size: merged.size, options: merged });
|
|
28
|
+
},
|
|
29
|
+
_close: () => set({ isOpen: false }),
|
|
30
|
+
_setSize: (size) => set({ size })
|
|
31
|
+
}));
|
|
32
|
+
function cn(...inputs) {
|
|
33
|
+
return clsx.clsx(inputs);
|
|
34
|
+
}
|
|
35
|
+
var SidePanelContext = react.createContext(null);
|
|
36
|
+
function useSidePanelContext() {
|
|
37
|
+
const ctx = react.useContext(SidePanelContext);
|
|
38
|
+
if (!ctx) throw new Error("useSidePanelContext: must be used within <SidePanelProvider>");
|
|
39
|
+
return ctx;
|
|
40
|
+
}
|
|
41
|
+
function SidePanelProvider({
|
|
42
|
+
children,
|
|
43
|
+
defaultSide = "right",
|
|
44
|
+
defaultSize = 560,
|
|
45
|
+
defaultMinSize = 400,
|
|
46
|
+
defaultMaxSize = 1e3,
|
|
47
|
+
className,
|
|
48
|
+
style
|
|
49
|
+
}) {
|
|
50
|
+
const storeRef = react.useRef(null);
|
|
51
|
+
if (!storeRef.current) {
|
|
52
|
+
storeRef.current = createSidePanelStore({
|
|
53
|
+
side: defaultSide,
|
|
54
|
+
size: defaultSize,
|
|
55
|
+
minSize: defaultMinSize,
|
|
56
|
+
maxSize: defaultMaxSize
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
const store = storeRef.current;
|
|
60
|
+
const guardRefs = react.useRef([]);
|
|
61
|
+
const containerRef = react.useRef(null);
|
|
62
|
+
const registerGuard = react.useCallback((ref) => {
|
|
63
|
+
guardRefs.current.push(ref);
|
|
64
|
+
}, []);
|
|
65
|
+
const unregisterGuard = react.useCallback((ref) => {
|
|
66
|
+
const idx = guardRefs.current.indexOf(ref);
|
|
67
|
+
if (idx > -1) guardRefs.current.splice(idx, 1);
|
|
68
|
+
}, []);
|
|
69
|
+
const close = react.useCallback(async () => {
|
|
70
|
+
const { options, _close } = store.getState();
|
|
71
|
+
if (options.onBeforeClose) {
|
|
72
|
+
const canClose = await options.onBeforeClose();
|
|
73
|
+
if (!canClose) return;
|
|
74
|
+
}
|
|
75
|
+
_close();
|
|
76
|
+
}, [store]);
|
|
77
|
+
const open = react.useCallback(
|
|
78
|
+
(content, options) => {
|
|
79
|
+
store.getState()._open(content, options ?? {});
|
|
80
|
+
},
|
|
81
|
+
[store]
|
|
82
|
+
);
|
|
83
|
+
return /* @__PURE__ */ jsxRuntime.jsx(SidePanelContext.Provider, { value: { store, open, close, registerGuard, unregisterGuard }, children: /* @__PURE__ */ jsxRuntime.jsxs("div", { ref: containerRef, className: cn("sp-root", className), style, children: [
|
|
84
|
+
children,
|
|
85
|
+
/* @__PURE__ */ jsxRuntime.jsx(
|
|
86
|
+
SidePanelSlot,
|
|
87
|
+
{
|
|
88
|
+
store,
|
|
89
|
+
close,
|
|
90
|
+
guardRefs,
|
|
91
|
+
containerRef,
|
|
92
|
+
defaultMinSize,
|
|
93
|
+
defaultMaxSize
|
|
94
|
+
}
|
|
95
|
+
)
|
|
96
|
+
] }) });
|
|
97
|
+
}
|
|
98
|
+
function SidePanelSlot({
|
|
99
|
+
store,
|
|
100
|
+
close,
|
|
101
|
+
guardRefs,
|
|
102
|
+
containerRef,
|
|
103
|
+
defaultMinSize,
|
|
104
|
+
defaultMaxSize
|
|
105
|
+
}) {
|
|
106
|
+
const panelRef = react.useRef(null);
|
|
107
|
+
const [isResizing, setIsResizing] = react.useState(false);
|
|
108
|
+
const [containerWidth, setContainerWidth] = react.useState(Infinity);
|
|
109
|
+
const [containerHeight, setContainerHeight] = react.useState(Infinity);
|
|
110
|
+
const isOpen = zustand.useStore(store, (s) => s.isOpen);
|
|
111
|
+
const size = zustand.useStore(store, (s) => s.size);
|
|
112
|
+
const content = zustand.useStore(store, (s) => s.content);
|
|
113
|
+
const options = zustand.useStore(store, (s) => s.options);
|
|
114
|
+
react.useEffect(() => {
|
|
115
|
+
const el = containerRef.current;
|
|
116
|
+
if (!el) return;
|
|
117
|
+
setContainerWidth(el.offsetWidth);
|
|
118
|
+
setContainerHeight(el.offsetHeight);
|
|
119
|
+
const ro = new ResizeObserver(() => {
|
|
120
|
+
setContainerWidth(el.offsetWidth);
|
|
121
|
+
setContainerHeight(el.offsetHeight);
|
|
122
|
+
});
|
|
123
|
+
ro.observe(el);
|
|
124
|
+
return () => ro.disconnect();
|
|
125
|
+
}, [containerRef]);
|
|
126
|
+
const side = options.side ?? "right";
|
|
127
|
+
const isHorizontal = side === "left" || side === "right";
|
|
128
|
+
const containerSize = isHorizontal ? containerWidth : containerHeight;
|
|
129
|
+
const resizable = options.resizable ?? true;
|
|
130
|
+
const minSize = options.minSize ?? defaultMinSize;
|
|
131
|
+
const maxSize = Math.min(options.maxSize ?? defaultMaxSize, containerSize);
|
|
132
|
+
const snapPoints = options.snapPoints;
|
|
133
|
+
const snapGap = options.snapGap ?? 20;
|
|
134
|
+
const closeOnOutsideClick = options.closeOnOutsideClick ?? true;
|
|
135
|
+
const closeOnEsc = options.closeOnEsc ?? true;
|
|
136
|
+
const positionStyle = isHorizontal ? { position: "absolute", [side]: 0, top: 0, bottom: 0 } : { position: "absolute", [side]: 0, left: 0, right: 0 };
|
|
137
|
+
const resizeEnable = {
|
|
138
|
+
top: resizable && side === "bottom",
|
|
139
|
+
right: resizable && side === "left",
|
|
140
|
+
bottom: resizable && side === "top",
|
|
141
|
+
left: resizable && side === "right",
|
|
142
|
+
topRight: false,
|
|
143
|
+
bottomRight: false,
|
|
144
|
+
bottomLeft: false,
|
|
145
|
+
topLeft: false
|
|
146
|
+
};
|
|
147
|
+
react.useEffect(() => {
|
|
148
|
+
if (!isOpen || !closeOnOutsideClick || isResizing) return;
|
|
149
|
+
const handler = (e) => {
|
|
150
|
+
if (panelRef.current?.contains(e.target)) return;
|
|
151
|
+
if (guardRefs.current.some((ref) => ref.current?.contains(e.target))) return;
|
|
152
|
+
if (document.querySelector('[role="dialog"][data-state="open"], [role="alertdialog"][data-state="open"]')) return;
|
|
153
|
+
close();
|
|
154
|
+
};
|
|
155
|
+
document.addEventListener("click", handler, true);
|
|
156
|
+
return () => document.removeEventListener("click", handler, true);
|
|
157
|
+
}, [isOpen, closeOnOutsideClick, isResizing, close, guardRefs]);
|
|
158
|
+
react.useEffect(() => {
|
|
159
|
+
if (!isOpen || !closeOnEsc) return;
|
|
160
|
+
const handler = (e) => {
|
|
161
|
+
if (e.key === "Escape") close();
|
|
162
|
+
};
|
|
163
|
+
document.addEventListener("keydown", handler);
|
|
164
|
+
return () => document.removeEventListener("keydown", handler);
|
|
165
|
+
}, [isOpen, closeOnEsc, close]);
|
|
166
|
+
if (!isOpen) return null;
|
|
167
|
+
return /* @__PURE__ */ jsxRuntime.jsx(
|
|
168
|
+
reResizable.Resizable,
|
|
169
|
+
{
|
|
170
|
+
style: positionStyle,
|
|
171
|
+
size: isHorizontal ? { width: size } : { height: size },
|
|
172
|
+
minWidth: isHorizontal ? minSize : void 0,
|
|
173
|
+
maxWidth: isHorizontal ? maxSize : void 0,
|
|
174
|
+
minHeight: !isHorizontal ? minSize : void 0,
|
|
175
|
+
maxHeight: !isHorizontal ? maxSize : void 0,
|
|
176
|
+
snap: isHorizontal ? { x: snapPoints } : { y: snapPoints },
|
|
177
|
+
snapGap,
|
|
178
|
+
onResizeStart: () => setIsResizing(true),
|
|
179
|
+
onResizeStop: (_e, _dir, _el, delta) => {
|
|
180
|
+
store.getState()._setSize(size + (isHorizontal ? delta.width : delta.height));
|
|
181
|
+
setIsResizing(false);
|
|
182
|
+
},
|
|
183
|
+
enable: resizeEnable,
|
|
184
|
+
className: "sp-panel",
|
|
185
|
+
"data-side": side,
|
|
186
|
+
children: /* @__PURE__ */ jsxRuntime.jsx("div", { ref: panelRef, className: "sp-panel-content", children: content })
|
|
187
|
+
}
|
|
188
|
+
);
|
|
189
|
+
}
|
|
190
|
+
function useSidePanel() {
|
|
191
|
+
const { store, open, close } = useSidePanelContext();
|
|
192
|
+
const isOpen = zustand.useStore(store, (s) => s.isOpen);
|
|
193
|
+
return { open, close, isOpen };
|
|
194
|
+
}
|
|
195
|
+
function usePanelClose() {
|
|
196
|
+
return useSidePanelContext().close;
|
|
197
|
+
}
|
|
198
|
+
function usePanelGuard(ref) {
|
|
199
|
+
const { registerGuard, unregisterGuard } = useSidePanelContext();
|
|
200
|
+
react.useEffect(() => {
|
|
201
|
+
registerGuard(ref);
|
|
202
|
+
return () => unregisterGuard(ref);
|
|
203
|
+
}, [ref, registerGuard, unregisterGuard]);
|
|
204
|
+
}
|
|
205
|
+
function PanelGuard({ children }) {
|
|
206
|
+
const ref = react.useRef(null);
|
|
207
|
+
usePanelGuard(ref);
|
|
208
|
+
return /* @__PURE__ */ jsxRuntime.jsx("div", { ref, children });
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
exports.PanelGuard = PanelGuard;
|
|
212
|
+
exports.SidePanelProvider = SidePanelProvider;
|
|
213
|
+
exports.usePanelClose = usePanelClose;
|
|
214
|
+
exports.usePanelGuard = usePanelGuard;
|
|
215
|
+
exports.useSidePanel = useSidePanel;
|
|
216
|
+
//# sourceMappingURL=index.cjs.map
|
|
217
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/sidePanelStore.ts","../src/SidePanelProvider.tsx","../src/useSidePanel.ts","../src/usePanelGuard.ts","../src/PanelGuard.tsx"],"names":["createStore","clsx","createContext","useContext","useRef","useCallback","jsx","jsxs","useState","useStore","useEffect","Resizable"],"mappings":";;;;;;;;;AAmBA,IAAM,QAAA,GAAyE;AAAA,EAC7E,IAAA,EAAM,OAAA;AAAA,EACN,IAAA,EAAM,GAAA;AAAA,EACN,OAAA,EAAS,GAAA;AAAA,EACT,OAAA,EAAS,GAAA;AAAA,EACT,SAAA,EAAW,IAAA;AAAA,EACX,OAAA,EAAS,EAAA;AAAA,EAC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A,EAAW,QAAA,EAAS,CAAA;AAClC","file":"index.cjs","sourcesContent":["import { createStore } from 'zustand'\nimport type React from 'react'\n\nexport type Side = 'left' | 'right' | 'top' | 'bottom'\n\nexport type PanelOptions = {\n side?: Side\n // size = width for left/right, height for top/bottom\n size?: number\n minSize?: number\n maxSize?: number\n resizable?: boolean\n snapPoints?: number[]\n snapGap?: number\n closeOnOutsideClick?: boolean\n closeOnEsc?: boolean\n onBeforeClose?: () => boolean | Promise<boolean>\n}\n\nconst DEFAULTS: Required<Omit<PanelOptions, 'onBeforeClose' | 'snapPoints'>> = {\n side: 'right',\n size: 560,\n minSize: 400,\n maxSize: 1000,\n resizable: true,\n snapGap: 20,\n closeOnOutsideClick: true,\n closeOnEsc: true,\n}\n\ntype SidePanelState = {\n content: React.ReactNode\n isOpen: boolean\n size: number\n options: PanelOptions\n}\n\ntype SidePanelActions = {\n _open: (content: React.ReactNode, options: PanelOptions) => void\n _close: () => void\n _setSize: (size: number) => void\n}\n\nexport type SidePanelStore = ReturnType<typeof createSidePanelStore>\n\nexport const createSidePanelStore = (providerDefaults: PanelOptions = {}) =>\n createStore<SidePanelState & SidePanelActions>()((set) => ({\n content: null,\n isOpen: false,\n size: providerDefaults.size ?? DEFAULTS.size,\n options: {},\n _open: (content, options) => {\n const merged: PanelOptions = { ...DEFAULTS, ...providerDefaults, ...options }\n set({ content, isOpen: true, size: merged.size!, options: merged })\n },\n _close: () => set({ isOpen: false }),\n _setSize: (size) => set({ size }),\n }))\n","'use client'\n\nimport React, {\n createContext,\n useCallback,\n useContext,\n useEffect,\n useRef,\n useState,\n} from 'react'\nimport { useStore } from 'zustand'\nimport { Resizable } from 're-resizable'\nimport { clsx } from 'clsx'\nimport { createSidePanelStore, SidePanelStore, PanelOptions, Side } from './sidePanelStore'\n\nfunction cn(...inputs: Parameters<typeof clsx>) {\n return clsx(inputs)\n}\n\ntype GuardRef = React.RefObject<HTMLDivElement | null>\n\nexport type SidePanelContextValue = {\n store: SidePanelStore\n open: (content: React.ReactNode, options?: PanelOptions) => void\n close: () => Promise<void>\n registerGuard: (ref: GuardRef) => void\n unregisterGuard: (ref: GuardRef) => void\n}\n\nexport const SidePanelContext = createContext<SidePanelContextValue | null>(null)\n\nexport function useSidePanelContext(): SidePanelContextValue {\n const ctx = useContext(SidePanelContext)\n if (!ctx) throw new Error('useSidePanelContext: must be used within <SidePanelProvider>')\n return ctx\n}\n\nexport type SidePanelProviderProps = {\n children: React.ReactNode\n defaultSide?: Side\n defaultSize?: number\n defaultMinSize?: number\n defaultMaxSize?: number\n className?: string\n style?: React.CSSProperties\n}\n\nexport function SidePanelProvider({\n children,\n defaultSide = 'right',\n defaultSize = 560,\n defaultMinSize = 400,\n defaultMaxSize = 1000,\n className,\n style,\n}: SidePanelProviderProps) {\n const storeRef = useRef<SidePanelStore | null>(null)\n if (!storeRef.current) {\n storeRef.current = createSidePanelStore({\n side: defaultSide,\n size: defaultSize,\n minSize: defaultMinSize,\n maxSize: defaultMaxSize,\n })\n }\n const store = storeRef.current\n const guardRefs = useRef<GuardRef[]>([])\n const containerRef = useRef<HTMLDivElement>(null)\n\n const registerGuard = useCallback((ref: GuardRef) => {\n guardRefs.current.push(ref)\n }, [])\n\n const unregisterGuard = useCallback((ref: GuardRef) => {\n const idx = guardRefs.current.indexOf(ref)\n if (idx > -1) guardRefs.current.splice(idx, 1)\n }, [])\n\n const close = useCallback(async () => {\n const { options, _close } = store.getState()\n if (options.onBeforeClose) {\n const canClose = await options.onBeforeClose()\n if (!canClose) return\n }\n _close()\n }, [store])\n\n const open = useCallback(\n (content: React.ReactNode, options?: PanelOptions) => {\n store.getState()._open(content, options ?? {})\n },\n [store],\n )\n\n return (\n <SidePanelContext.Provider value={{ store, open, close, registerGuard, unregisterGuard }}>\n <div ref={containerRef} className={cn('sp-root', className)} style={style}>\n {children}\n <SidePanelSlot\n store={store}\n close={close}\n guardRefs={guardRefs}\n containerRef={containerRef}\n defaultMinSize={defaultMinSize}\n defaultMaxSize={defaultMaxSize}\n />\n </div>\n </SidePanelContext.Provider>\n )\n}\n\ntype SidePanelSlotProps = {\n store: SidePanelStore\n close: () => Promise<void>\n guardRefs: React.RefObject<GuardRef[]>\n containerRef: React.RefObject<HTMLDivElement | null>\n defaultMinSize: number\n defaultMaxSize: number\n}\n\nfunction SidePanelSlot({\n store,\n close,\n guardRefs,\n containerRef,\n defaultMinSize,\n defaultMaxSize,\n}: SidePanelSlotProps) {\n const panelRef = useRef<HTMLDivElement>(null)\n const [isResizing, setIsResizing] = useState(false)\n const [containerWidth, setContainerWidth] = useState(Infinity)\n const [containerHeight, setContainerHeight] = useState(Infinity)\n\n const isOpen = useStore(store, (s) => s.isOpen)\n const size = useStore(store, (s) => s.size)\n const content = useStore(store, (s) => s.content)\n const options = useStore(store, (s) => s.options)\n\n // Clamp panel size within the container's actual bounds.\n useEffect(() => {\n const el = containerRef.current\n if (!el) return\n setContainerWidth(el.offsetWidth)\n setContainerHeight(el.offsetHeight)\n const ro = new ResizeObserver(() => {\n setContainerWidth(el.offsetWidth)\n setContainerHeight(el.offsetHeight)\n })\n ro.observe(el)\n return () => ro.disconnect()\n }, [containerRef])\n\n const side: Side = options.side ?? 'right'\n const isHorizontal = side === 'left' || side === 'right'\n const containerSize = isHorizontal ? containerWidth : containerHeight\n\n const resizable = options.resizable ?? true\n const minSize = options.minSize ?? defaultMinSize\n const maxSize = Math.min(options.maxSize ?? defaultMaxSize, containerSize)\n const snapPoints = options.snapPoints\n const snapGap = options.snapGap ?? 20\n const closeOnOutsideClick = options.closeOnOutsideClick ?? true\n const closeOnEsc = options.closeOnEsc ?? true\n\n const positionStyle: React.CSSProperties = isHorizontal\n ? { position: 'absolute', [side]: 0, top: 0, bottom: 0 }\n : { position: 'absolute', [side]: 0, left: 0, right: 0 }\n\n // Only the handle facing the main content area is enabled.\n const resizeEnable = {\n top: resizable && side === 'bottom',\n right: resizable && side === 'left',\n bottom: resizable && side === 'top',\n left: resizable && side === 'right',\n topRight: false,\n bottomRight: false,\n bottomLeft: false,\n topLeft: false,\n }\n\n // Capture phase fires before React's onClick. If the same click also calls\n // open(), _open() runs afterward and overrides _close() via React 18 batching.\n useEffect(() => {\n if (!isOpen || !closeOnOutsideClick || isResizing) return\n\n const handler = (e: MouseEvent) => {\n if (panelRef.current?.contains(e.target as Node)) return\n if (guardRefs.current.some((ref) => ref.current?.contains(e.target as Node))) return\n if (document.querySelector('[role=\"dialog\"][data-state=\"open\"], [role=\"alertdialog\"][data-state=\"open\"]')) return\n close()\n }\n\n document.addEventListener('click', handler, true)\n return () => document.removeEventListener('click', handler, true)\n }, [isOpen, closeOnOutsideClick, isResizing, close, guardRefs])\n\n useEffect(() => {\n if (!isOpen || !closeOnEsc) return\n const handler = (e: KeyboardEvent) => {\n if (e.key === 'Escape') close()\n }\n document.addEventListener('keydown', handler)\n return () => document.removeEventListener('keydown', handler)\n }, [isOpen, closeOnEsc, close])\n\n if (!isOpen) return null\n\n return (\n <Resizable\n style={positionStyle}\n size={isHorizontal ? { width: size } : { height: size }}\n minWidth={isHorizontal ? minSize : undefined}\n maxWidth={isHorizontal ? maxSize : undefined}\n minHeight={!isHorizontal ? minSize : undefined}\n maxHeight={!isHorizontal ? maxSize : undefined}\n snap={isHorizontal ? { x: snapPoints } : { y: snapPoints }}\n snapGap={snapGap}\n onResizeStart={() => setIsResizing(true)}\n onResizeStop={(_e, _dir, _el, delta) => {\n store.getState()._setSize(size + (isHorizontal ? delta.width : delta.height))\n setIsResizing(false)\n }}\n enable={resizeEnable}\n className=\"sp-panel\"\n data-side={side}\n >\n <div ref={panelRef} className=\"sp-panel-content\">\n {content}\n </div>\n </Resizable>\n )\n}\n","import { useStore } from 'zustand'\nimport type React from 'react'\nimport { useSidePanelContext } from './SidePanelProvider'\nimport type { PanelOptions } from './sidePanelStore'\n\nexport function useSidePanel() {\n const { store, open, close } = useSidePanelContext()\n const isOpen = useStore(store, (s) => s.isOpen)\n return { open, close, isOpen }\n}\n\nexport function usePanelClose(): () => Promise<void> {\n return useSidePanelContext().close\n}\n\nexport type { PanelOptions }\nexport type OpenFn = (content: React.ReactNode, options?: PanelOptions) => void\n","import { useEffect, type RefObject } from 'react'\nimport { useSidePanelContext } from './SidePanelProvider'\n\nexport function usePanelGuard(ref: RefObject<HTMLDivElement | null>) {\n const { registerGuard, unregisterGuard } = useSidePanelContext()\n\n useEffect(() => {\n registerGuard(ref)\n return () => unregisterGuard(ref)\n }, [ref, registerGuard, unregisterGuard])\n}\n","import React, { useRef, type PropsWithChildren } from 'react'\nimport { usePanelGuard } from './usePanelGuard'\n\nexport function PanelGuard({ children }: PropsWithChildren) {\n const ref = useRef<HTMLDivElement>(null)\n usePanelGuard(ref)\n return <div ref={ref}>{children}</div>\n}\n"]}
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import React, { RefObject, PropsWithChildren } from 'react';
|
|
2
|
+
|
|
3
|
+
type Side = 'left' | 'right' | 'top' | 'bottom';
|
|
4
|
+
type PanelOptions = {
|
|
5
|
+
side?: Side;
|
|
6
|
+
size?: number;
|
|
7
|
+
minSize?: number;
|
|
8
|
+
maxSize?: number;
|
|
9
|
+
resizable?: boolean;
|
|
10
|
+
snapPoints?: number[];
|
|
11
|
+
snapGap?: number;
|
|
12
|
+
closeOnOutsideClick?: boolean;
|
|
13
|
+
closeOnEsc?: boolean;
|
|
14
|
+
onBeforeClose?: () => boolean | Promise<boolean>;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
type SidePanelProviderProps = {
|
|
18
|
+
children: React.ReactNode;
|
|
19
|
+
defaultSide?: Side;
|
|
20
|
+
defaultSize?: number;
|
|
21
|
+
defaultMinSize?: number;
|
|
22
|
+
defaultMaxSize?: number;
|
|
23
|
+
className?: string;
|
|
24
|
+
style?: React.CSSProperties;
|
|
25
|
+
};
|
|
26
|
+
declare function SidePanelProvider({ children, defaultSide, defaultSize, defaultMinSize, defaultMaxSize, className, style, }: SidePanelProviderProps): React.JSX.Element;
|
|
27
|
+
|
|
28
|
+
declare function useSidePanel(): {
|
|
29
|
+
open: (content: React.ReactNode, options?: PanelOptions) => void;
|
|
30
|
+
close: () => Promise<void>;
|
|
31
|
+
isOpen: boolean;
|
|
32
|
+
};
|
|
33
|
+
declare function usePanelClose(): () => Promise<void>;
|
|
34
|
+
|
|
35
|
+
type OpenFn = (content: React.ReactNode, options?: PanelOptions) => void;
|
|
36
|
+
|
|
37
|
+
declare function usePanelGuard(ref: RefObject<HTMLDivElement | null>): void;
|
|
38
|
+
|
|
39
|
+
declare function PanelGuard({ children }: PropsWithChildren): React.JSX.Element;
|
|
40
|
+
|
|
41
|
+
export { type OpenFn, PanelGuard, type PanelOptions, type Side, SidePanelProvider, type SidePanelProviderProps, usePanelClose, usePanelGuard, useSidePanel };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import React, { RefObject, PropsWithChildren } from 'react';
|
|
2
|
+
|
|
3
|
+
type Side = 'left' | 'right' | 'top' | 'bottom';
|
|
4
|
+
type PanelOptions = {
|
|
5
|
+
side?: Side;
|
|
6
|
+
size?: number;
|
|
7
|
+
minSize?: number;
|
|
8
|
+
maxSize?: number;
|
|
9
|
+
resizable?: boolean;
|
|
10
|
+
snapPoints?: number[];
|
|
11
|
+
snapGap?: number;
|
|
12
|
+
closeOnOutsideClick?: boolean;
|
|
13
|
+
closeOnEsc?: boolean;
|
|
14
|
+
onBeforeClose?: () => boolean | Promise<boolean>;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
type SidePanelProviderProps = {
|
|
18
|
+
children: React.ReactNode;
|
|
19
|
+
defaultSide?: Side;
|
|
20
|
+
defaultSize?: number;
|
|
21
|
+
defaultMinSize?: number;
|
|
22
|
+
defaultMaxSize?: number;
|
|
23
|
+
className?: string;
|
|
24
|
+
style?: React.CSSProperties;
|
|
25
|
+
};
|
|
26
|
+
declare function SidePanelProvider({ children, defaultSide, defaultSize, defaultMinSize, defaultMaxSize, className, style, }: SidePanelProviderProps): React.JSX.Element;
|
|
27
|
+
|
|
28
|
+
declare function useSidePanel(): {
|
|
29
|
+
open: (content: React.ReactNode, options?: PanelOptions) => void;
|
|
30
|
+
close: () => Promise<void>;
|
|
31
|
+
isOpen: boolean;
|
|
32
|
+
};
|
|
33
|
+
declare function usePanelClose(): () => Promise<void>;
|
|
34
|
+
|
|
35
|
+
type OpenFn = (content: React.ReactNode, options?: PanelOptions) => void;
|
|
36
|
+
|
|
37
|
+
declare function usePanelGuard(ref: RefObject<HTMLDivElement | null>): void;
|
|
38
|
+
|
|
39
|
+
declare function PanelGuard({ children }: PropsWithChildren): React.JSX.Element;
|
|
40
|
+
|
|
41
|
+
export { type OpenFn, PanelGuard, type PanelOptions, type Side, SidePanelProvider, type SidePanelProviderProps, usePanelClose, usePanelGuard, useSidePanel };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
import { createContext, useRef, useCallback, useState, useEffect, useContext } from 'react';
|
|
2
|
+
import { useStore, createStore } from 'zustand';
|
|
3
|
+
import { Resizable } from 're-resizable';
|
|
4
|
+
import { clsx } from 'clsx';
|
|
5
|
+
import { jsx, jsxs } from 'react/jsx-runtime';
|
|
6
|
+
|
|
7
|
+
// src/SidePanelProvider.tsx
|
|
8
|
+
var DEFAULTS = {
|
|
9
|
+
side: "right",
|
|
10
|
+
size: 560,
|
|
11
|
+
minSize: 400,
|
|
12
|
+
maxSize: 1e3,
|
|
13
|
+
resizable: true,
|
|
14
|
+
snapGap: 20,
|
|
15
|
+
closeOnOutsideClick: true,
|
|
16
|
+
closeOnEsc: true
|
|
17
|
+
};
|
|
18
|
+
var createSidePanelStore = (providerDefaults = {}) => createStore()((set) => ({
|
|
19
|
+
content: null,
|
|
20
|
+
isOpen: false,
|
|
21
|
+
size: providerDefaults.size ?? DEFAULTS.size,
|
|
22
|
+
options: {},
|
|
23
|
+
_open: (content, options) => {
|
|
24
|
+
const merged = { ...DEFAULTS, ...providerDefaults, ...options };
|
|
25
|
+
set({ content, isOpen: true, size: merged.size, options: merged });
|
|
26
|
+
},
|
|
27
|
+
_close: () => set({ isOpen: false }),
|
|
28
|
+
_setSize: (size) => set({ size })
|
|
29
|
+
}));
|
|
30
|
+
function cn(...inputs) {
|
|
31
|
+
return clsx(inputs);
|
|
32
|
+
}
|
|
33
|
+
var SidePanelContext = createContext(null);
|
|
34
|
+
function useSidePanelContext() {
|
|
35
|
+
const ctx = useContext(SidePanelContext);
|
|
36
|
+
if (!ctx) throw new Error("useSidePanelContext: must be used within <SidePanelProvider>");
|
|
37
|
+
return ctx;
|
|
38
|
+
}
|
|
39
|
+
function SidePanelProvider({
|
|
40
|
+
children,
|
|
41
|
+
defaultSide = "right",
|
|
42
|
+
defaultSize = 560,
|
|
43
|
+
defaultMinSize = 400,
|
|
44
|
+
defaultMaxSize = 1e3,
|
|
45
|
+
className,
|
|
46
|
+
style
|
|
47
|
+
}) {
|
|
48
|
+
const storeRef = useRef(null);
|
|
49
|
+
if (!storeRef.current) {
|
|
50
|
+
storeRef.current = createSidePanelStore({
|
|
51
|
+
side: defaultSide,
|
|
52
|
+
size: defaultSize,
|
|
53
|
+
minSize: defaultMinSize,
|
|
54
|
+
maxSize: defaultMaxSize
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
const store = storeRef.current;
|
|
58
|
+
const guardRefs = useRef([]);
|
|
59
|
+
const containerRef = useRef(null);
|
|
60
|
+
const registerGuard = useCallback((ref) => {
|
|
61
|
+
guardRefs.current.push(ref);
|
|
62
|
+
}, []);
|
|
63
|
+
const unregisterGuard = useCallback((ref) => {
|
|
64
|
+
const idx = guardRefs.current.indexOf(ref);
|
|
65
|
+
if (idx > -1) guardRefs.current.splice(idx, 1);
|
|
66
|
+
}, []);
|
|
67
|
+
const close = useCallback(async () => {
|
|
68
|
+
const { options, _close } = store.getState();
|
|
69
|
+
if (options.onBeforeClose) {
|
|
70
|
+
const canClose = await options.onBeforeClose();
|
|
71
|
+
if (!canClose) return;
|
|
72
|
+
}
|
|
73
|
+
_close();
|
|
74
|
+
}, [store]);
|
|
75
|
+
const open = useCallback(
|
|
76
|
+
(content, options) => {
|
|
77
|
+
store.getState()._open(content, options ?? {});
|
|
78
|
+
},
|
|
79
|
+
[store]
|
|
80
|
+
);
|
|
81
|
+
return /* @__PURE__ */ jsx(SidePanelContext.Provider, { value: { store, open, close, registerGuard, unregisterGuard }, children: /* @__PURE__ */ jsxs("div", { ref: containerRef, className: cn("sp-root", className), style, children: [
|
|
82
|
+
children,
|
|
83
|
+
/* @__PURE__ */ jsx(
|
|
84
|
+
SidePanelSlot,
|
|
85
|
+
{
|
|
86
|
+
store,
|
|
87
|
+
close,
|
|
88
|
+
guardRefs,
|
|
89
|
+
containerRef,
|
|
90
|
+
defaultMinSize,
|
|
91
|
+
defaultMaxSize
|
|
92
|
+
}
|
|
93
|
+
)
|
|
94
|
+
] }) });
|
|
95
|
+
}
|
|
96
|
+
function SidePanelSlot({
|
|
97
|
+
store,
|
|
98
|
+
close,
|
|
99
|
+
guardRefs,
|
|
100
|
+
containerRef,
|
|
101
|
+
defaultMinSize,
|
|
102
|
+
defaultMaxSize
|
|
103
|
+
}) {
|
|
104
|
+
const panelRef = useRef(null);
|
|
105
|
+
const [isResizing, setIsResizing] = useState(false);
|
|
106
|
+
const [containerWidth, setContainerWidth] = useState(Infinity);
|
|
107
|
+
const [containerHeight, setContainerHeight] = useState(Infinity);
|
|
108
|
+
const isOpen = useStore(store, (s) => s.isOpen);
|
|
109
|
+
const size = useStore(store, (s) => s.size);
|
|
110
|
+
const content = useStore(store, (s) => s.content);
|
|
111
|
+
const options = useStore(store, (s) => s.options);
|
|
112
|
+
useEffect(() => {
|
|
113
|
+
const el = containerRef.current;
|
|
114
|
+
if (!el) return;
|
|
115
|
+
setContainerWidth(el.offsetWidth);
|
|
116
|
+
setContainerHeight(el.offsetHeight);
|
|
117
|
+
const ro = new ResizeObserver(() => {
|
|
118
|
+
setContainerWidth(el.offsetWidth);
|
|
119
|
+
setContainerHeight(el.offsetHeight);
|
|
120
|
+
});
|
|
121
|
+
ro.observe(el);
|
|
122
|
+
return () => ro.disconnect();
|
|
123
|
+
}, [containerRef]);
|
|
124
|
+
const side = options.side ?? "right";
|
|
125
|
+
const isHorizontal = side === "left" || side === "right";
|
|
126
|
+
const containerSize = isHorizontal ? containerWidth : containerHeight;
|
|
127
|
+
const resizable = options.resizable ?? true;
|
|
128
|
+
const minSize = options.minSize ?? defaultMinSize;
|
|
129
|
+
const maxSize = Math.min(options.maxSize ?? defaultMaxSize, containerSize);
|
|
130
|
+
const snapPoints = options.snapPoints;
|
|
131
|
+
const snapGap = options.snapGap ?? 20;
|
|
132
|
+
const closeOnOutsideClick = options.closeOnOutsideClick ?? true;
|
|
133
|
+
const closeOnEsc = options.closeOnEsc ?? true;
|
|
134
|
+
const positionStyle = isHorizontal ? { position: "absolute", [side]: 0, top: 0, bottom: 0 } : { position: "absolute", [side]: 0, left: 0, right: 0 };
|
|
135
|
+
const resizeEnable = {
|
|
136
|
+
top: resizable && side === "bottom",
|
|
137
|
+
right: resizable && side === "left",
|
|
138
|
+
bottom: resizable && side === "top",
|
|
139
|
+
left: resizable && side === "right",
|
|
140
|
+
topRight: false,
|
|
141
|
+
bottomRight: false,
|
|
142
|
+
bottomLeft: false,
|
|
143
|
+
topLeft: false
|
|
144
|
+
};
|
|
145
|
+
useEffect(() => {
|
|
146
|
+
if (!isOpen || !closeOnOutsideClick || isResizing) return;
|
|
147
|
+
const handler = (e) => {
|
|
148
|
+
if (panelRef.current?.contains(e.target)) return;
|
|
149
|
+
if (guardRefs.current.some((ref) => ref.current?.contains(e.target))) return;
|
|
150
|
+
if (document.querySelector('[role="dialog"][data-state="open"], [role="alertdialog"][data-state="open"]')) return;
|
|
151
|
+
close();
|
|
152
|
+
};
|
|
153
|
+
document.addEventListener("click", handler, true);
|
|
154
|
+
return () => document.removeEventListener("click", handler, true);
|
|
155
|
+
}, [isOpen, closeOnOutsideClick, isResizing, close, guardRefs]);
|
|
156
|
+
useEffect(() => {
|
|
157
|
+
if (!isOpen || !closeOnEsc) return;
|
|
158
|
+
const handler = (e) => {
|
|
159
|
+
if (e.key === "Escape") close();
|
|
160
|
+
};
|
|
161
|
+
document.addEventListener("keydown", handler);
|
|
162
|
+
return () => document.removeEventListener("keydown", handler);
|
|
163
|
+
}, [isOpen, closeOnEsc, close]);
|
|
164
|
+
if (!isOpen) return null;
|
|
165
|
+
return /* @__PURE__ */ jsx(
|
|
166
|
+
Resizable,
|
|
167
|
+
{
|
|
168
|
+
style: positionStyle,
|
|
169
|
+
size: isHorizontal ? { width: size } : { height: size },
|
|
170
|
+
minWidth: isHorizontal ? minSize : void 0,
|
|
171
|
+
maxWidth: isHorizontal ? maxSize : void 0,
|
|
172
|
+
minHeight: !isHorizontal ? minSize : void 0,
|
|
173
|
+
maxHeight: !isHorizontal ? maxSize : void 0,
|
|
174
|
+
snap: isHorizontal ? { x: snapPoints } : { y: snapPoints },
|
|
175
|
+
snapGap,
|
|
176
|
+
onResizeStart: () => setIsResizing(true),
|
|
177
|
+
onResizeStop: (_e, _dir, _el, delta) => {
|
|
178
|
+
store.getState()._setSize(size + (isHorizontal ? delta.width : delta.height));
|
|
179
|
+
setIsResizing(false);
|
|
180
|
+
},
|
|
181
|
+
enable: resizeEnable,
|
|
182
|
+
className: "sp-panel",
|
|
183
|
+
"data-side": side,
|
|
184
|
+
children: /* @__PURE__ */ jsx("div", { ref: panelRef, className: "sp-panel-content", children: content })
|
|
185
|
+
}
|
|
186
|
+
);
|
|
187
|
+
}
|
|
188
|
+
function useSidePanel() {
|
|
189
|
+
const { store, open, close } = useSidePanelContext();
|
|
190
|
+
const isOpen = useStore(store, (s) => s.isOpen);
|
|
191
|
+
return { open, close, isOpen };
|
|
192
|
+
}
|
|
193
|
+
function usePanelClose() {
|
|
194
|
+
return useSidePanelContext().close;
|
|
195
|
+
}
|
|
196
|
+
function usePanelGuard(ref) {
|
|
197
|
+
const { registerGuard, unregisterGuard } = useSidePanelContext();
|
|
198
|
+
useEffect(() => {
|
|
199
|
+
registerGuard(ref);
|
|
200
|
+
return () => unregisterGuard(ref);
|
|
201
|
+
}, [ref, registerGuard, unregisterGuard]);
|
|
202
|
+
}
|
|
203
|
+
function PanelGuard({ children }) {
|
|
204
|
+
const ref = useRef(null);
|
|
205
|
+
usePanelGuard(ref);
|
|
206
|
+
return /* @__PURE__ */ jsx("div", { ref, children });
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
export { PanelGuard, SidePanelProvider, usePanelClose, usePanelGuard, useSidePanel };
|
|
210
|
+
//# sourceMappingURL=index.js.map
|
|
211
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/sidePanelStore.ts","../src/SidePanelProvider.tsx","../src/useSidePanel.ts","../src/usePanelGuard.ts","../src/PanelGuard.tsx"],"names":["useStore","useEffect","useRef","jsx"],"mappings":";;;;;;;AAmBA,IAAM,QAAA,GAAyE;AAAA,EAC7E,IAAA,EAAM,OAAA;AAAA,EACN,IAAA,EAAM,GAAA;AAAA,EACN,OAAA,EAAS,GAAA;AAAA,EACT,OAAA,EAAS,GAAA;AAAA,EACT,SAAA,EAAW,IAAA;AAAA,EACX,OAAA,EAAS,EAAA;AAAA,EACT,mBAAA,EAAqB,IAAA;AAAA,EACrB,UAAA,EAAY;AACd,CAAA;AAiBO,IAAM,oBAAA,GAAuB,CAAC,gBAAA,GAAiC,OACpE,WAAA,EAA+C,CAAE,CAAC,GAAA,MAAS;AAAA,EACzD,OAAA,EAAS,IAAA;AAAA,EACT,MAAA,EAAQ,KAAA;AAAA,EACR,IAAA,EAAM,gBAAA,CAAiB,IAAA,IAAQ,QAAA,CAAS,IAAA;AAAA,EACxC,SAAS,EAAC;AAAA,EACV,KAAA,EAAO,CAAC,OAAA,EAAS,OAAA,KAAY;AAC3B,IAAA,MAAM,SAAuB,EAAE,GAAG,UAAU,GAAG,gBAAA,EAAkB,GAAG,OAAA,EAAQ;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AAO,IAAA,EAAM,KAAA,KAAU,mBAAA,EAAoB;AACnD,EAAA,MAAM,SAASA,QAAAA,CAAS,KAAA,EAAO,CAAC,CAAA,KAAM,EAAE,MAAM,CAAA;AAC9C,EAAA,OAAO,EAAE,IAAA,EAAM,KAAA,EAAO,MAAA,EAAO;AAC/B;AAEO,SAAS,aAAA,GAAqC;AACnD,EAAA,OAAO,qBAAoB,CAAE,KAAA;AAC/B;ACVO,SAAS,cAAc,GAAA,EAAuC;AACnE,EAAA,MAAM,EAAE,aAAA,EAAe,eAAA,EAAgB,GAAI,mBAAA,EAAoB;AAE/D,EAAAC,UAAU,MAAM;AACd,IAAA,aAAA,CAAc,GAAG,CAAA;AACjB,IAAA,OAAO,MAAM,gBAAgB,GAAG,CAAA;AAAA,EAClC,CAAA,EAAG,CAAC,GAAA,EAAK,aAAA,EAAe,eAAe,CAAC,CAAA;AAC1C;ACPO,SAAS,UAAA,CAAW,EAAE,QAAA,EAAS,EAAsB;AAC1D,EAAA,MAAM,GAAA,GAAMC,OAAuB,IAAI,CAAA;AACvC,EAAA,aAAA,CAAc,GAAG,CAAA;AACjB,EAAA,uBAAOC,GAAAA,CAAC,KAAA,EAAA,EAAI,GAAA,EAAW,QAAA,EAAS,CAAA;AAClC","file":"index.js","sourcesContent":["import { createStore } from 'zustand'\nimport type React from 'react'\n\nexport type Side = 'left' | 'right' | 'top' | 'bottom'\n\nexport type PanelOptions = {\n side?: Side\n // size = width for left/right, height for top/bottom\n size?: number\n minSize?: number\n maxSize?: number\n resizable?: boolean\n snapPoints?: number[]\n snapGap?: number\n closeOnOutsideClick?: boolean\n closeOnEsc?: boolean\n onBeforeClose?: () => boolean | Promise<boolean>\n}\n\nconst DEFAULTS: Required<Omit<PanelOptions, 'onBeforeClose' | 'snapPoints'>> = {\n side: 'right',\n size: 560,\n minSize: 400,\n maxSize: 1000,\n resizable: true,\n snapGap: 20,\n closeOnOutsideClick: true,\n closeOnEsc: true,\n}\n\ntype SidePanelState = {\n content: React.ReactNode\n isOpen: boolean\n size: number\n options: PanelOptions\n}\n\ntype SidePanelActions = {\n _open: (content: React.ReactNode, options: PanelOptions) => void\n _close: () => void\n _setSize: (size: number) => void\n}\n\nexport type SidePanelStore = ReturnType<typeof createSidePanelStore>\n\nexport const createSidePanelStore = (providerDefaults: PanelOptions = {}) =>\n createStore<SidePanelState & SidePanelActions>()((set) => ({\n content: null,\n isOpen: false,\n size: providerDefaults.size ?? DEFAULTS.size,\n options: {},\n _open: (content, options) => {\n const merged: PanelOptions = { ...DEFAULTS, ...providerDefaults, ...options }\n set({ content, isOpen: true, size: merged.size!, options: merged })\n },\n _close: () => set({ isOpen: false }),\n _setSize: (size) => set({ size }),\n }))\n","'use client'\n\nimport React, {\n createContext,\n useCallback,\n useContext,\n useEffect,\n useRef,\n useState,\n} from 'react'\nimport { useStore } from 'zustand'\nimport { Resizable } from 're-resizable'\nimport { clsx } from 'clsx'\nimport { createSidePanelStore, SidePanelStore, PanelOptions, Side } from './sidePanelStore'\n\nfunction cn(...inputs: Parameters<typeof clsx>) {\n return clsx(inputs)\n}\n\ntype GuardRef = React.RefObject<HTMLDivElement | null>\n\nexport type SidePanelContextValue = {\n store: SidePanelStore\n open: (content: React.ReactNode, options?: PanelOptions) => void\n close: () => Promise<void>\n registerGuard: (ref: GuardRef) => void\n unregisterGuard: (ref: GuardRef) => void\n}\n\nexport const SidePanelContext = createContext<SidePanelContextValue | null>(null)\n\nexport function useSidePanelContext(): SidePanelContextValue {\n const ctx = useContext(SidePanelContext)\n if (!ctx) throw new Error('useSidePanelContext: must be used within <SidePanelProvider>')\n return ctx\n}\n\nexport type SidePanelProviderProps = {\n children: React.ReactNode\n defaultSide?: Side\n defaultSize?: number\n defaultMinSize?: number\n defaultMaxSize?: number\n className?: string\n style?: React.CSSProperties\n}\n\nexport function SidePanelProvider({\n children,\n defaultSide = 'right',\n defaultSize = 560,\n defaultMinSize = 400,\n defaultMaxSize = 1000,\n className,\n style,\n}: SidePanelProviderProps) {\n const storeRef = useRef<SidePanelStore | null>(null)\n if (!storeRef.current) {\n storeRef.current = createSidePanelStore({\n side: defaultSide,\n size: defaultSize,\n minSize: defaultMinSize,\n maxSize: defaultMaxSize,\n })\n }\n const store = storeRef.current\n const guardRefs = useRef<GuardRef[]>([])\n const containerRef = useRef<HTMLDivElement>(null)\n\n const registerGuard = useCallback((ref: GuardRef) => {\n guardRefs.current.push(ref)\n }, [])\n\n const unregisterGuard = useCallback((ref: GuardRef) => {\n const idx = guardRefs.current.indexOf(ref)\n if (idx > -1) guardRefs.current.splice(idx, 1)\n }, [])\n\n const close = useCallback(async () => {\n const { options, _close } = store.getState()\n if (options.onBeforeClose) {\n const canClose = await options.onBeforeClose()\n if (!canClose) return\n }\n _close()\n }, [store])\n\n const open = useCallback(\n (content: React.ReactNode, options?: PanelOptions) => {\n store.getState()._open(content, options ?? {})\n },\n [store],\n )\n\n return (\n <SidePanelContext.Provider value={{ store, open, close, registerGuard, unregisterGuard }}>\n <div ref={containerRef} className={cn('sp-root', className)} style={style}>\n {children}\n <SidePanelSlot\n store={store}\n close={close}\n guardRefs={guardRefs}\n containerRef={containerRef}\n defaultMinSize={defaultMinSize}\n defaultMaxSize={defaultMaxSize}\n />\n </div>\n </SidePanelContext.Provider>\n )\n}\n\ntype SidePanelSlotProps = {\n store: SidePanelStore\n close: () => Promise<void>\n guardRefs: React.RefObject<GuardRef[]>\n containerRef: React.RefObject<HTMLDivElement | null>\n defaultMinSize: number\n defaultMaxSize: number\n}\n\nfunction SidePanelSlot({\n store,\n close,\n guardRefs,\n containerRef,\n defaultMinSize,\n defaultMaxSize,\n}: SidePanelSlotProps) {\n const panelRef = useRef<HTMLDivElement>(null)\n const [isResizing, setIsResizing] = useState(false)\n const [containerWidth, setContainerWidth] = useState(Infinity)\n const [containerHeight, setContainerHeight] = useState(Infinity)\n\n const isOpen = useStore(store, (s) => s.isOpen)\n const size = useStore(store, (s) => s.size)\n const content = useStore(store, (s) => s.content)\n const options = useStore(store, (s) => s.options)\n\n // Clamp panel size within the container's actual bounds.\n useEffect(() => {\n const el = containerRef.current\n if (!el) return\n setContainerWidth(el.offsetWidth)\n setContainerHeight(el.offsetHeight)\n const ro = new ResizeObserver(() => {\n setContainerWidth(el.offsetWidth)\n setContainerHeight(el.offsetHeight)\n })\n ro.observe(el)\n return () => ro.disconnect()\n }, [containerRef])\n\n const side: Side = options.side ?? 'right'\n const isHorizontal = side === 'left' || side === 'right'\n const containerSize = isHorizontal ? containerWidth : containerHeight\n\n const resizable = options.resizable ?? true\n const minSize = options.minSize ?? defaultMinSize\n const maxSize = Math.min(options.maxSize ?? defaultMaxSize, containerSize)\n const snapPoints = options.snapPoints\n const snapGap = options.snapGap ?? 20\n const closeOnOutsideClick = options.closeOnOutsideClick ?? true\n const closeOnEsc = options.closeOnEsc ?? true\n\n const positionStyle: React.CSSProperties = isHorizontal\n ? { position: 'absolute', [side]: 0, top: 0, bottom: 0 }\n : { position: 'absolute', [side]: 0, left: 0, right: 0 }\n\n // Only the handle facing the main content area is enabled.\n const resizeEnable = {\n top: resizable && side === 'bottom',\n right: resizable && side === 'left',\n bottom: resizable && side === 'top',\n left: resizable && side === 'right',\n topRight: false,\n bottomRight: false,\n bottomLeft: false,\n topLeft: false,\n }\n\n // Capture phase fires before React's onClick. If the same click also calls\n // open(), _open() runs afterward and overrides _close() via React 18 batching.\n useEffect(() => {\n if (!isOpen || !closeOnOutsideClick || isResizing) return\n\n const handler = (e: MouseEvent) => {\n if (panelRef.current?.contains(e.target as Node)) return\n if (guardRefs.current.some((ref) => ref.current?.contains(e.target as Node))) return\n if (document.querySelector('[role=\"dialog\"][data-state=\"open\"], [role=\"alertdialog\"][data-state=\"open\"]')) return\n close()\n }\n\n document.addEventListener('click', handler, true)\n return () => document.removeEventListener('click', handler, true)\n }, [isOpen, closeOnOutsideClick, isResizing, close, guardRefs])\n\n useEffect(() => {\n if (!isOpen || !closeOnEsc) return\n const handler = (e: KeyboardEvent) => {\n if (e.key === 'Escape') close()\n }\n document.addEventListener('keydown', handler)\n return () => document.removeEventListener('keydown', handler)\n }, [isOpen, closeOnEsc, close])\n\n if (!isOpen) return null\n\n return (\n <Resizable\n style={positionStyle}\n size={isHorizontal ? { width: size } : { height: size }}\n minWidth={isHorizontal ? minSize : undefined}\n maxWidth={isHorizontal ? maxSize : undefined}\n minHeight={!isHorizontal ? minSize : undefined}\n maxHeight={!isHorizontal ? maxSize : undefined}\n snap={isHorizontal ? { x: snapPoints } : { y: snapPoints }}\n snapGap={snapGap}\n onResizeStart={() => setIsResizing(true)}\n onResizeStop={(_e, _dir, _el, delta) => {\n store.getState()._setSize(size + (isHorizontal ? delta.width : delta.height))\n setIsResizing(false)\n }}\n enable={resizeEnable}\n className=\"sp-panel\"\n data-side={side}\n >\n <div ref={panelRef} className=\"sp-panel-content\">\n {content}\n </div>\n </Resizable>\n )\n}\n","import { useStore } from 'zustand'\nimport type React from 'react'\nimport { useSidePanelContext } from './SidePanelProvider'\nimport type { PanelOptions } from './sidePanelStore'\n\nexport function useSidePanel() {\n const { store, open, close } = useSidePanelContext()\n const isOpen = useStore(store, (s) => s.isOpen)\n return { open, close, isOpen }\n}\n\nexport function usePanelClose(): () => Promise<void> {\n return useSidePanelContext().close\n}\n\nexport type { PanelOptions }\nexport type OpenFn = (content: React.ReactNode, options?: PanelOptions) => void\n","import { useEffect, type RefObject } from 'react'\nimport { useSidePanelContext } from './SidePanelProvider'\n\nexport function usePanelGuard(ref: RefObject<HTMLDivElement | null>) {\n const { registerGuard, unregisterGuard } = useSidePanelContext()\n\n useEffect(() => {\n registerGuard(ref)\n return () => unregisterGuard(ref)\n }, [ref, registerGuard, unregisterGuard])\n}\n","import React, { useRef, type PropsWithChildren } from 'react'\nimport { usePanelGuard } from './usePanelGuard'\n\nexport function PanelGuard({ children }: PropsWithChildren) {\n const ref = useRef<HTMLDivElement>(null)\n usePanelGuard(ref)\n return <div ref={ref}>{children}</div>\n}\n"]}
|
package/dist/styles.css
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* @loykin/side-panel
|
|
3
|
+
*
|
|
4
|
+
* Plain CSS, no Tailwind dependency. Consumers can override with:
|
|
5
|
+
* - CSS custom properties: --bk-*
|
|
6
|
+
* - Semantic class names: .sp-*
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
:root {
|
|
10
|
+
--bk-background: oklch(1 0 0);
|
|
11
|
+
--bk-card: oklch(1 0 0);
|
|
12
|
+
--bk-border: oklch(0.922 0 0);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
.dark {
|
|
16
|
+
--bk-background: oklch(0.145 0 0);
|
|
17
|
+
--bk-card: oklch(0.205 0 0);
|
|
18
|
+
--bk-border: oklch(1 0 0 / 10%);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
.sp-root {
|
|
22
|
+
position: relative;
|
|
23
|
+
overflow: hidden;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
.sp-panel {
|
|
27
|
+
z-index: 50;
|
|
28
|
+
overflow: hidden;
|
|
29
|
+
background: var(--bk-card);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
.sp-panel[data-side="right"] { border-left: 1px solid var(--bk-border); }
|
|
33
|
+
.sp-panel[data-side="left"] { border-right: 1px solid var(--bk-border); }
|
|
34
|
+
.sp-panel[data-side="top"] { border-bottom: 1px solid var(--bk-border); }
|
|
35
|
+
.sp-panel[data-side="bottom"] { border-top: 1px solid var(--bk-border); }
|
|
36
|
+
|
|
37
|
+
.sp-panel-content {
|
|
38
|
+
height: 100%;
|
|
39
|
+
overflow: auto;
|
|
40
|
+
background: var(--bk-background);
|
|
41
|
+
}
|
package/dist/styles.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
declare const styles: string; export default styles;
|
package/package.json
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@loykin/side-panel",
|
|
3
|
+
"version": "0.0.1-dev.0",
|
|
4
|
+
"description": "Non-blocking resizable side panel with Zustand state management.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.cjs",
|
|
7
|
+
"module": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"import": "./dist/index.js",
|
|
13
|
+
"require": "./dist/index.cjs"
|
|
14
|
+
},
|
|
15
|
+
"./styles": {
|
|
16
|
+
"types": "./dist/styles.d.ts",
|
|
17
|
+
"default": "./dist/styles.css"
|
|
18
|
+
}
|
|
19
|
+
},
|
|
20
|
+
"files": [
|
|
21
|
+
"dist"
|
|
22
|
+
],
|
|
23
|
+
"sideEffects": [
|
|
24
|
+
"./dist/styles.css"
|
|
25
|
+
],
|
|
26
|
+
"publishConfig": {
|
|
27
|
+
"access": "public"
|
|
28
|
+
},
|
|
29
|
+
"dependencies": {
|
|
30
|
+
"clsx": "^2.1.1",
|
|
31
|
+
"re-resizable": "^6.11.2",
|
|
32
|
+
"zustand": "^5.0.9"
|
|
33
|
+
},
|
|
34
|
+
"peerDependencies": {
|
|
35
|
+
"react": "^18.0.0 || ^19.0.0",
|
|
36
|
+
"react-dom": "^18.0.0 || ^19.0.0"
|
|
37
|
+
},
|
|
38
|
+
"devDependencies": {
|
|
39
|
+
"@types/react": "^19.0.0",
|
|
40
|
+
"@types/react-dom": "^19.0.0",
|
|
41
|
+
"tsup": "^8.4.0",
|
|
42
|
+
"typescript": "^5.9.3"
|
|
43
|
+
},
|
|
44
|
+
"keywords": [
|
|
45
|
+
"react",
|
|
46
|
+
"side-panel",
|
|
47
|
+
"sheet",
|
|
48
|
+
"zustand",
|
|
49
|
+
"resizable"
|
|
50
|
+
],
|
|
51
|
+
"repository": {
|
|
52
|
+
"type": "git",
|
|
53
|
+
"url": "https://github.com/loykin/basekit.git",
|
|
54
|
+
"directory": "packages/side-panel"
|
|
55
|
+
},
|
|
56
|
+
"homepage": "https://loykin.github.io/basekit/playground/",
|
|
57
|
+
"bugs": {
|
|
58
|
+
"url": "https://github.com/loykin/basekit/issues"
|
|
59
|
+
},
|
|
60
|
+
"license": "MIT",
|
|
61
|
+
"scripts": {
|
|
62
|
+
"build": "tsc --noEmit && tsup && node scripts/build-css.mjs && echo 'declare const styles: string; export default styles;' > dist/styles.d.ts",
|
|
63
|
+
"build:css": "node scripts/build-css.mjs",
|
|
64
|
+
"dev": "tsup --watch",
|
|
65
|
+
"type-check": "tsc --noEmit"
|
|
66
|
+
}
|
|
67
|
+
}
|