Skip to content

Commit ded0c52

Browse files
committed
fix: refactor Drawer width and DrawerPopup sizing logic
1 parent 0759630 commit ded0c52

File tree

2 files changed

+28
-26
lines changed

2 files changed

+28
-26
lines changed

src/Drawer.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ const Drawer: React.FC<DrawerProps> = props => {
4545
placement = 'right' as Placement,
4646
autoFocus = true,
4747
keyboard = true,
48-
width = 378,
48+
width,
4949
mask = true,
5050
maskClosable = true,
5151
getContainer,
@@ -131,7 +131,12 @@ const Drawer: React.FC<DrawerProps> = props => {
131131
placement,
132132
autoFocus,
133133
keyboard,
134-
width,
134+
width:
135+
width !== undefined
136+
? width
137+
: defaultWidth !== undefined
138+
? undefined
139+
: 378,
135140
mask,
136141
maskClosable,
137142
inline: getContainer === false,

src/DrawerPopup.tsx

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -284,33 +284,31 @@ const DrawerPopup: React.ForwardRefRenderFunction<
284284
break;
285285
}
286286
}
287-
// Use currentSize if available (from resizing), otherwise use original or default width/height
287+
// Use currentSize if available (from resizing), otherwise use width/height with proper fallback to defaults
288288
if (isHorizontal) {
289289
let finalWidth: number | string;
290290
if (currentSize !== undefined) {
291+
// Highest priority: user dragged size
291292
finalWidth = currentSize;
292-
} else if (resizable && !resizable.onResize && defaultWidth !== undefined) {
293-
// Uncontrolled mode: resizable exists but no onResize callback, use defaultWidth
294-
finalWidth = defaultWidth;
295-
} else {
296-
// Controlled mode or no resizable: use width
293+
} else if (width !== undefined) {
294+
// Second priority: explicitly provided width
297295
finalWidth = width;
296+
} else {
297+
// Lowest priority: default width fallback
298+
finalWidth = defaultWidth;
298299
}
299300
wrapperStyle.width = parseWidthHeight(finalWidth);
300301
} else {
301302
let finalHeight: number | string;
302303
if (currentSize !== undefined) {
304+
// Highest priority: user dragged size
303305
finalHeight = currentSize;
304-
} else if (
305-
resizable &&
306-
!resizable.onResize &&
307-
defaultHeight !== undefined
308-
) {
309-
// Uncontrolled mode: resizable exists but no onResize callback, use defaultHeight
310-
finalHeight = defaultHeight;
311-
} else {
312-
// Controlled mode or no resizable: use height
306+
} else if (height !== undefined) {
307+
// Second priority: explicitly provided height
313308
finalHeight = height;
309+
} else {
310+
// Lowest priority: default height fallback
311+
finalHeight = defaultHeight;
314312
}
315313
wrapperStyle.height = parseWidthHeight(finalHeight);
316314
}
@@ -326,24 +324,23 @@ const DrawerPopup: React.ForwardRefRenderFunction<
326324
const [maxSize, setMaxSize] = React.useState(0);
327325
const wrapperRef = React.useRef<HTMLDivElement>(null);
328326

329-
// Update currentSize based on width/height and current placement
327+
// Update currentSize based on width/height and current placement with proper priority
330328
const updateCurrentSize = React.useCallback(() => {
331-
let targetSize = isHorizontal ? width : height;
329+
let targetSize: number | string | undefined;
332330

333-
// In uncontrolled mode, use default values if targetSize is not a number
334-
if (typeof targetSize !== 'number' && resizable && !resizable.onResize) {
335-
const defaultSize = isHorizontal ? defaultWidth : defaultHeight;
336-
if (typeof defaultSize === 'number') {
337-
targetSize = defaultSize;
338-
}
331+
// Apply priority: width/height takes precedence over defaultWidth/defaultHeight
332+
if (isHorizontal) {
333+
targetSize = width !== undefined ? width : defaultWidth;
334+
} else {
335+
targetSize = height !== undefined ? height : defaultHeight;
339336
}
340337

341338
if (typeof targetSize === 'number') {
342339
setCurrentSize(targetSize);
343340
} else {
344341
setCurrentSize(undefined);
345342
}
346-
}, [isHorizontal, width, height, resizable, defaultWidth, defaultHeight]);
343+
}, [isHorizontal, width, height, defaultWidth, defaultHeight]);
347344

348345
React.useEffect(() => {
349346
updateCurrentSize();

0 commit comments

Comments
 (0)