-
-
Notifications
You must be signed in to change notification settings - Fork 142
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
Add isCollapsed boolean or size value to the Imperative API #71
Comments
I see why this could be convenient in some cases, but I'm not sure it's worth adding to the library since it's not new functionality. It seems pretty straight forward to implement this using the import {
ImperativePanelHandle,
Panel,
PanelGroup,
PanelResizeHandle
} from "react-resizable-panels";
const ref = useRef < ImperativePanelHandle > null;
const [isCollapsed, setIsCollapsed] = useState(false);
const collapsePanel = () => {
const panel = ref.current;
if (isCollapased) {
panel.expand();
} else {
panel.collapse();
}
};
<PanelGroup direction="horizontal">
<Panel collapsible ref={ref} onCollapse={setIsCollapsed}>
left
</Panel>
<PanelResizeHandle />
<Panel>right</Panel>
</PanelGroup>; |
@bvaughn I was just about to ask the same question. In the snippet you posted above, you assume that the panel is expanded to begin with, which is not necessarily true. When the panel is collapsed and the user reloads the page, Btw, I really enjoy using your library, great work 👍🏻 |
Ah, that's an interesting point. I had assumed Seems like maybe that's something that we could change. |
Add getSize and getCollapsed to imperative Panel API
|
Even though the imperative API change has landed, I'm going to fix this too, because I think it's unexpected ~> #75 |
I must admit, I would not have expected |
I think it's important to synchronize any external state. How else would you know if the panel group had saved a previously collapsed state? |
Totally agree with you, sync errors should be avoided at all cost. It's possible to get the initial state using the |
Yes but not reliably bc of the server rendering approach to mount with an initial size of 1 for every panel. You would have to know to ignore the mount effect and read the second render- and I think this is too much an implementation detail to make for a good api. The hook suggestion is an interesting one, but I would like to keep the api small if possible (avoid redundant ways of doing something) just to make maintenance more manageable for me. |
Good point 👍🏻
I have to admit that I'm pretty short on time at the moment. I don't need lazy loading at this moment, but possibly in the future. If my solution can be generalized, I would be happy to contribute back. |
Fair! I think my suggestion at this point would be to use the import { lazy } from "react";
import { Component } from "wherever";
const LazyComponent = lazy(Component);
function App() {
const [shouldLoad, setShouldLoad] = useState(false);
const onCollapse = (collapsed: boolean) => {
// Wait to load panel contents until it's been shown (at least once)
setShouldLoad(shouldLoad || !collapsed);
};
return (
<PanelGroup autoSaveId="example" {...otherProps}>
<Panel collapsible {...panelProps} onCollapse={onCollapse}>
{shouldLoad && <LazyComponent />}
</Panel>
{/* ... */}
</PanelGroup>
);
} |
It would be handy to include either a status boolean or the current size of a panel in the Imperative API to use alongside the methods it provides.
Scenario:
You have a panel you want to toggle open or closed via another button component:
The text was updated successfully, but these errors were encountered: