Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Skip the lifecycle-related code outside of the browser env #169

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
143 changes: 73 additions & 70 deletions packages/react-resizable-panels/src/Panel.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { isBrowser } from "#is-browser";
import useIsomorphicLayoutEffect from "./hooks/useIsomorphicEffect";
import useUniqueId from "./hooks/useUniqueId";
import {
Expand Down Expand Up @@ -81,16 +82,6 @@
unregisterPanel,
} = context;

// Use a ref to guard against users passing inline props
const callbacksRef = useRef<{
onCollapse: PanelOnCollapse | null;
onResize: PanelOnResize | null;
}>({ onCollapse, onResize });
useEffect(() => {
callbacksRef.current.onCollapse = onCollapse;
callbacksRef.current.onResize = onResize;
});

// Basic props validation
if (minSize < 0 || minSize > 100) {
throw Error(`Panel minSize must be between 0 and 100, but was ${minSize}`);
Expand All @@ -114,66 +105,78 @@

const style = getPanelStyle(panelId, defaultSize);

const committedValuesRef = useRef<{
size: number;
}>({
size: parseSizeFromStyle(style),
});
const panelDataRef = useRef<{
callbacksRef: PanelCallbackRef;
collapsedSize: number;
collapsible: boolean;
defaultSize: number | null;
id: string;
maxSize: number;
minSize: number;
order: number | null;
}>({
callbacksRef,
collapsedSize,
collapsible,
defaultSize,
id: panelId,
maxSize,
minSize,
order,
});
useIsomorphicLayoutEffect(() => {
committedValuesRef.current.size = parseSizeFromStyle(style);

panelDataRef.current.callbacksRef = callbacksRef;
panelDataRef.current.collapsedSize = collapsedSize;
panelDataRef.current.collapsible = collapsible;
panelDataRef.current.defaultSize = defaultSize;
panelDataRef.current.id = panelId;
panelDataRef.current.maxSize = maxSize;
panelDataRef.current.minSize = minSize;
panelDataRef.current.order = order;
});

useIsomorphicLayoutEffect(() => {
registerPanel(panelId, panelDataRef as PanelData);

return () => {
unregisterPanel(panelId);
};
}, [order, panelId, registerPanel, unregisterPanel]);

useImperativeHandle(
forwardedRef,
() => ({
collapse: () => collapsePanel(panelId),
expand: () => expandPanel(panelId),
getCollapsed() {
return committedValuesRef.current.size === 0;
},
getSize() {
return committedValuesRef.current.size;
},
resize: (percentage: number) => resizePanel(panelId, percentage),
}),
[collapsePanel, expandPanel, panelId, resizePanel]
);
if (isBrowser) {
// Use a ref to guard against users passing inline props
const callbacksRef = useRef<{

Check failure on line 110 in packages/react-resizable-panels/src/Panel.ts

View workflow job for this annotation

GitHub Actions / tests-e2e

React Hook "useRef" is called conditionally. React Hooks must be called in the exact same order in every component render. Did you accidentally call a React Hook after an early return?
onCollapse: PanelOnCollapse | null;
onResize: PanelOnResize | null;
}>({ onCollapse, onResize });
useEffect(() => {

Check failure on line 114 in packages/react-resizable-panels/src/Panel.ts

View workflow job for this annotation

GitHub Actions / tests-e2e

React Hook "useEffect" is called conditionally. React Hooks must be called in the exact same order in every component render. Did you accidentally call a React Hook after an early return?
callbacksRef.current.onCollapse = onCollapse;
callbacksRef.current.onResize = onResize;
});

const committedValuesRef = useRef<{

Check failure on line 119 in packages/react-resizable-panels/src/Panel.ts

View workflow job for this annotation

GitHub Actions / tests-e2e

React Hook "useRef" is called conditionally. React Hooks must be called in the exact same order in every component render. Did you accidentally call a React Hook after an early return?
size: number;
}>({
size: parseSizeFromStyle(style),
});
const panelDataRef = useRef<{

Check failure on line 124 in packages/react-resizable-panels/src/Panel.ts

View workflow job for this annotation

GitHub Actions / tests-e2e

React Hook "useRef" is called conditionally. React Hooks must be called in the exact same order in every component render. Did you accidentally call a React Hook after an early return?
callbacksRef: PanelCallbackRef;
collapsedSize: number;
collapsible: boolean;
defaultSize: number | null;
id: string;
maxSize: number;
minSize: number;
order: number | null;
}>({
callbacksRef,
collapsedSize,
collapsible,
defaultSize,
id: panelId,
maxSize,
minSize,
order,
});
useIsomorphicLayoutEffect(() => {

Check failure on line 143 in packages/react-resizable-panels/src/Panel.ts

View workflow job for this annotation

GitHub Actions / tests-e2e

React Hook "useIsomorphicLayoutEffect" is called conditionally. React Hooks must be called in the exact same order in every component render. Did you accidentally call a React Hook after an early return?
committedValuesRef.current.size = parseSizeFromStyle(style);

panelDataRef.current.callbacksRef = callbacksRef;
panelDataRef.current.collapsedSize = collapsedSize;
panelDataRef.current.collapsible = collapsible;
panelDataRef.current.defaultSize = defaultSize;
panelDataRef.current.id = panelId;
panelDataRef.current.maxSize = maxSize;
panelDataRef.current.minSize = minSize;
panelDataRef.current.order = order;
});

useIsomorphicLayoutEffect(() => {

Check failure on line 156 in packages/react-resizable-panels/src/Panel.ts

View workflow job for this annotation

GitHub Actions / tests-e2e

React Hook "useIsomorphicLayoutEffect" is called conditionally. React Hooks must be called in the exact same order in every component render. Did you accidentally call a React Hook after an early return?
registerPanel(panelId, panelDataRef as PanelData);

return () => {
unregisterPanel(panelId);
};
}, [order, panelId, registerPanel, unregisterPanel]);

useImperativeHandle(

Check failure on line 164 in packages/react-resizable-panels/src/Panel.ts

View workflow job for this annotation

GitHub Actions / tests-e2e

React Hook "useImperativeHandle" is called conditionally. React Hooks must be called in the exact same order in every component render. Did you accidentally call a React Hook after an early return?
forwardedRef,
() => ({
collapse: () => collapsePanel(panelId),
expand: () => expandPanel(panelId),
getCollapsed() {
return committedValuesRef.current.size === 0;
},
getSize() {
return committedValuesRef.current.size;
},
resize: (percentage: number) => resizePanel(panelId, percentage),
}),
[collapsePanel, expandPanel, panelId, resizePanel]
);
}

return createElement(Type, {
children,
Expand Down
Loading