@@ -8,6 +8,8 @@ import * as React from 'react'
88export type ResizablePanelEdge = 'left' | 'right' | 'top' | 'bottom'
99
1010type UseResizablePanelParams = {
11+ /** When set, read/write this CSS custom property instead of `width` / `height`. */
12+ cssVariable ?: string
1113 /** Width restored on a double-click reset. */
1214 defaultValuePx : number
1315 /** When `true`, pointer and keyboard gestures are ignored. */
@@ -65,19 +67,32 @@ function getElementValuePx(
6567 element : HTMLElement | null ,
6668 edge : ResizablePanelEdge ,
6769 fallbackValuePx : number ,
70+ cssVariable ?: string ,
6871) : number {
6972 if ( ! element ) return fallbackValuePx
7073
7174 const dimension = getDimension ( edge )
72- const inlineValuePx = Number . parseFloat ( element . style [ dimension ] )
75+ const inlineValuePx = Number . parseFloat (
76+ cssVariable ? element . style . getPropertyValue ( cssVariable ) : element . style [ dimension ] ,
77+ )
7378 if ( Number . isFinite ( inlineValuePx ) && inlineValuePx > 0 ) return inlineValuePx
7479
7580 const measuredValuePx = element . getBoundingClientRect ( ) [ dimension ]
7681 return measuredValuePx > 0 ? measuredValuePx : fallbackValuePx
7782}
7883
79- function setElementValuePx ( element : HTMLElement | null , edge : ResizablePanelEdge , valuePx : number ) {
80- if ( element ) element . style [ getDimension ( edge ) ] = `${ valuePx } px`
84+ function setElementValuePx (
85+ element : HTMLElement | null ,
86+ edge : ResizablePanelEdge ,
87+ valuePx : number ,
88+ cssVariable ?: string ,
89+ ) {
90+ if ( ! element ) return
91+ if ( cssVariable ) {
92+ element . style . setProperty ( cssVariable , `${ valuePx } px` )
93+ } else {
94+ element . style [ getDimension ( edge ) ] = `${ valuePx } px`
95+ }
8196}
8297
8398function getActiveElementForRestore ( ) : HTMLElement | null {
@@ -121,6 +136,7 @@ function getKeyboardDeltaPx(edge: ResizablePanelEdge, key: string, stepPx: numbe
121136 * event handlers, the animation-frame callback, or an effect, never during render.
122137 */
123138export function useResizablePanel ( {
139+ cssVariable,
124140 defaultValuePx,
125141 disabled = false ,
126142 edge,
@@ -150,7 +166,7 @@ export function useResizablePanel({
150166 function flushPreview ( ) {
151167 if ( pendingValueRef . current === null ) return
152168
153- setElementValuePx ( panelRef . current , edge , pendingValueRef . current )
169+ setElementValuePx ( panelRef . current , edge , pendingValueRef . current , cssVariable )
154170 pendingValueRef . current = null
155171 }
156172
@@ -174,7 +190,7 @@ export function useResizablePanel({
174190 function commitValue ( nextValuePx : number ) {
175191 const clampedValuePx = clamp ( nextValuePx , minValuePx , maxValuePx )
176192
177- setElementValuePx ( panelRef . current , edge , clampedValuePx )
193+ setElementValuePx ( panelRef . current , edge , clampedValuePx , cssVariable )
178194 onValueCommit ( clampedValuePx )
179195 }
180196
@@ -206,7 +222,7 @@ export function useResizablePanel({
206222 endDrag ( )
207223
208224 const startValuePx = clamp (
209- getElementValuePx ( panelRef . current , edge , currentValuePx ) ,
225+ getElementValuePx ( panelRef . current , edge , currentValuePx , cssVariable ) ,
210226 minValuePx ,
211227 maxValuePx ,
212228 )
@@ -256,15 +272,16 @@ export function useResizablePanel({
256272 event . preventDefault ( )
257273 commitValue (
258274 nextValuePx ??
259- getElementValuePx ( panelRef . current , edge , currentValuePx ) + ( deltaPx ?? 0 ) ,
275+ getElementValuePx ( panelRef . current , edge , currentValuePx , cssVariable ) +
276+ ( deltaPx ?? 0 ) ,
260277 )
261278 }
262279
263280 React . useEffect (
264281 function reapplyCommittedValue ( ) {
265- setElementValuePx ( panelRef . current , edge , currentValuePx )
282+ setElementValuePx ( panelRef . current , edge , currentValuePx , cssVariable )
266283 } ,
267- [ currentValuePx , edge , panelRef ] ,
284+ [ currentValuePx , edge , panelRef , cssVariable ] ,
268285 )
269286
270287 React . useEffect ( function cleanupOnUnmount ( ) {
0 commit comments