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

Set div css flex-basis, PanelResizeHandle Slow #128

Closed
Link-Kou opened this issue Apr 25, 2023 · 6 comments
Closed

Set div css flex-basis, PanelResizeHandle Slow #128

Link-Kou opened this issue Apr 25, 2023 · 6 comments
Assignees

Comments

@Link-Kou
Copy link

Whether it can be supported or not can be accelerated by adjusting the number of steps.

2023-04-25_16-22-56 (1)

@bvaughn
Copy link
Owner

bvaughn commented Apr 25, 2023

I don't know what you're reporting here. If it's a bug or a performance issue, you'll need to share a repro (like Code Sandbox or something) not just a picture.

@Link-Kou
Copy link
Author

@bvaughn
Copy link
Owner

bvaughn commented Apr 26, 2023

Sorry. It's still not clear to me why you want to do this. Flex basis and grow are meant to be managed by this library. Overriding those styles will cause problems.

@Link-Kou
Copy link
Author

@bvaughn
Normally, the page is proportional.
In fact, the layout needs to be based on the actual size.
For example: left and right layout. I want the largest 300px and the smallest 200px on the left. There is no restriction on the right.

@bvaughn
Copy link
Owner

bvaughn commented Apr 26, 2023

So this essentially boils down to your wanting support for min/max sizes in pixels? That's something I've consciously chosen not to support with this library. (See #114, #46, #47, #78)

I'm going to copy-paste my response form one of those other issues showing how you could tackle your own pixel constraints (if you want to do that) but I don't think you should use flex-basis for that.


Here is a way you could basically implement this in your application code:

https://codesandbox.io/s/react-resizable-panels-forked-m5qf69?file=/src/App.js

  const MIN_SIZE_IN_PIXELS = 100;

  const [minSize, setMinSize] = useState(10);

  useLayoutEffect(() => {
    const panelGroup = document.querySelector('[data-panel-group-id="group"]');
    const resizeHandles = document.querySelectorAll(
      "[data-panel-resize-handle-id]"
    );
    const observer = new ResizeObserver(() => {
      let height = panelGroup.offsetHeight;

      resizeHandles.forEach((resizeHandle) => {
        height -= resizeHandle.offsetHeight;
      });

      // Minimum size in pixels is a percentage of the PanelGroup's height,
      // less the (fixed) height of the resize handles.
      setMinSize((MIN_SIZE_IN_PIXELS / height) * 100);
    });
    observer.observe(panelGroup);
    resizeHandles.forEach((resizeHandle) => {
      observer.observe(resizeHandle);
    });

    return () => {
      observer.disconnect();
    };
  }, []);

Here my reasoning for not wanting to add it to the NPM package directly:

I think the problem with this is similar to the problem with external width/height props– either can change without PanelGroup/Panel re-rendering or being aware. (Example: an external stylesheet could define this style.)

So handling it fully would go from being something triggered only by pointer events to something that could be triggered by a lot of external things.

I don't think I want to try to support that level of complexity 😄

@bvaughn
Copy link
Owner

bvaughn commented Aug 6, 2023

I think I have a possible solution for the pixel-based constraints; see #176


❤️ → ☕ givebrian.coffee

bvaughn added a commit that referenced this issue Aug 13, 2023
Relates to issues #46, #47, #51, #78, #114, #128, #141

This PR adds a new prop (`units`) to `PanelGroup`. This prop defaults to
"percentage" but can be set to "pixels" for static, pixel based layout
constraints.

This can be used to add enable pixel-based min/max and default size
values, e.g.:
```tsx
 <PanelGroup direction="horizontal" units="pixels">
   {/* Will be constrained to 100-200 pixels (assuming group is large enough to permit this) */}
   <Panel minSize={100} maxSize={200} />
   <PanelResizeHandle />
   <Panel />
   <PanelResizeHandle />
   <Panel />
 </PanelGroup>
```

Imperative API methods are also able to work with either pixels or
percentages now. They default to whatever units the group has been
configured to use, but can be overridden with an additional, optional
parameter, e.g.
```ts
panelRef.resize(100, "pixels");
panelGroupRef.setLayout([25, 50, 25], "percentages");

// Works for getters too, e.g.
const percentage = panelRef.getSize("percentages");
const pixels = panelRef.getSize("pixels");

const layout = panelGroupRef.getLayout("pixels");
```

See the docs for more:
[.../examples/pixel-based-layouts](https://react-resizable-panels-git-panelgroup-layout-val-2424f0-bvaughn.vercel.app/examples/pixel-based-layouts)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants