Skip to content

Commit 7ba6e5e

Browse files
authored
fix(ResizablePanel): stabilize & remove flex layout requirement (#538)
1 parent c3e963f commit 7ba6e5e

File tree

4 files changed

+39
-55
lines changed

4 files changed

+39
-55
lines changed

.changeset/silver-peaches-float.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@cube-dev/ui-kit': patch
3+
---
4+
5+
Stabilize ResizablePanel & remove requirement for the flex layout.

src/components/layout/Panel.tsx

Lines changed: 13 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
1-
import {
2-
createContext,
3-
ForwardedRef,
4-
forwardRef,
5-
ReactNode,
6-
useMemo,
7-
} from 'react';
1+
import { ForwardedRef, forwardRef, ReactNode, useMemo } from 'react';
82

93
import {
104
BASE_STYLES,
@@ -21,14 +15,6 @@ import {
2115
tasty,
2216
} from '../../tasty';
2317

24-
export interface PanelContextData {
25-
layout: 'grid' | 'flex';
26-
}
27-
28-
export const PanelContext = createContext<PanelContextData>({
29-
layout: 'grid',
30-
});
31-
3218
const PanelElement = tasty({
3319
as: 'section',
3420
qa: 'Panel',
@@ -164,25 +150,19 @@ function Panel(props: CubePanelProps, ref: ForwardedRef<HTMLDivElement>) {
164150
);
165151

166152
return (
167-
<PanelContext.Provider
168-
value={{
169-
layout: isFlex ? 'flex' : 'grid',
170-
}}
153+
<PanelElement
154+
ref={ref}
155+
qa={qa}
156+
mods={appliedMods}
157+
styles={styles}
158+
style={style}
159+
{...otherProps}
171160
>
172-
<PanelElement
173-
ref={ref}
174-
qa={qa}
175-
mods={appliedMods}
176-
styles={styles}
177-
style={style}
178-
{...otherProps}
179-
>
180-
<PanelInnerElement mods={appliedMods} styles={innerStyles}>
181-
{children}
182-
</PanelInnerElement>
183-
{extra}
184-
</PanelElement>
185-
</PanelContext.Provider>
161+
<PanelInnerElement mods={appliedMods} styles={innerStyles}>
162+
{children}
163+
</PanelInnerElement>
164+
{extra}
165+
</PanelElement>
186166
);
187167
}
188168

src/components/layout/ResizablePanel.stories.tsx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,13 @@ const TemplateControllable: StoryFn<CubeResizablePanelProps> = (args) => {
5757
);
5858
};
5959

60+
const GridTemplate: StoryFn<CubeResizablePanelProps> = (args) => (
61+
<Panel isStretched height="min 30x" fill="#white" gridColumns="auto 1fr">
62+
<ResizablePanel size={300} {...args} />
63+
<Panel fill="#light"></Panel>
64+
</Panel>
65+
);
66+
6067
export const ResizeRight = TemplateRight.bind({});
6168
ResizeRight.args = {
6269
direction: 'right',
@@ -86,3 +93,9 @@ export const Disabled = TemplateRight.bind({});
8693
Disabled.args = {
8794
isDisabled: true,
8895
};
96+
97+
export const InGridLayout = GridTemplate.bind({});
98+
InGridLayout.args = {
99+
direction: 'right',
100+
maxSize: 500,
101+
};

src/components/layout/ResizablePanel.tsx

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,10 @@
1-
import {
2-
ForwardedRef,
3-
forwardRef,
4-
useContext,
5-
useEffect,
6-
useState,
7-
} from 'react';
1+
import { ForwardedRef, forwardRef, useEffect, useState } from 'react';
82
import { useHover, useMove } from 'react-aria';
93

10-
import { useWarn } from '../../_internal/index';
114
import { BasePropsWithoutChildren, Styles, tasty } from '../../tasty/index';
125
import { mergeProps, useCombinedRefs } from '../../utils/react/index';
136

14-
import { Panel, CubePanelProps, PanelContext } from './Panel';
7+
import { Panel, CubePanelProps } from './Panel';
158

169
type Direction = 'top' | 'right' | 'bottom' | 'left';
1710

@@ -166,16 +159,6 @@ function ResizablePanel(
166159
props: CubeResizablePanelProps,
167160
ref: ForwardedRef<HTMLDivElement>,
168161
) {
169-
const panelContext = useContext(PanelContext);
170-
171-
useWarn(panelContext.layout !== 'flex', {
172-
once: true,
173-
key: 'resizable panel layout requirement',
174-
args: [
175-
'ResizablePanel can only be used inside a flex layout. Use Panel with `isFlex` property to create one.',
176-
],
177-
});
178-
179162
const isControllable = typeof props.size === 'number';
180163
const {
181164
isDisabled,
@@ -240,16 +223,19 @@ function ResizablePanel(
240223
},
241224
onMoveEnd(e) {
242225
setIsDragging(false);
226+
setSize((size) => clamp(Math.round(size)));
243227
},
244228
});
245229

246230
useEffect(() => {
247-
onSizeChange?.(size);
231+
if (providedSize == null || Math.abs(providedSize - size) > 0.5) {
232+
onSizeChange?.(size);
233+
}
248234
}, [size]);
249235

250236
useEffect(() => {
251-
if (providedSize) {
252-
setSize(providedSize);
237+
if (providedSize && Math.abs(providedSize - size) > 0.5) {
238+
setSize(clamp(providedSize));
253239
}
254240
}, [providedSize]);
255241

0 commit comments

Comments
 (0)