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

Add minSize/maxSize, but in pixels #46

Closed
NepeinAV opened this issue Dec 29, 2022 · 9 comments
Closed

Add minSize/maxSize, but in pixels #46

NepeinAV opened this issue Dec 29, 2022 · 9 comments

Comments

@NepeinAV
Copy link

Hi! Can you add min/max panel size restrictions, but in pixels? Sometimes, if the window size becomes too small, panel content may shrink too much, which causes UI issues (overflows, etc.).

@bvaughn
Copy link
Owner

bvaughn commented Dec 29, 2022

I understand why that would be convenient in some cases but I don’t think it’s something I want to support- because it would require changing the public api in a non obvious way: This library doesn’t control its own outer dimensions, so it couldn’t prevent panels from expanding/contracting past a certain point as its own width/height changed.

You should be able to accomplish this using minSize/maxSize and a min/max external height/width though!

@bvaughn bvaughn closed this as completed Dec 29, 2022
@bvaughn
Copy link
Owner

bvaughn commented Dec 30, 2022

Copying from #48 (comment) in case others see this issue.


Here is an example of what I mean:
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();
    };
  }, []);

@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)
@bvaughn bvaughn mentioned this issue Dec 13, 2023
7 tasks
@sleeyax
Copy link

sleeyax commented Aug 14, 2024

If anyone is looking for a recent example to simply get the height of a panel to React state, you can use this hook I came up with (which is based on the original answer):

// usePanelHeight.ts

import { useLayoutEffect, useState } from "react";
import { getPanelElement } from "react-resizable-panels";

/**
 * Custom hook to get the height of a panel from `react-resizable-panels`.
 * @param id The id of the panel to get the height of.
 */
export function usePanelHeight(id: string) {
  const [height, setHeight] = useState<number>();

  useLayoutEffect(() => {
    const panelElement = getPanelElement(id);

    if (panelElement) {
      const observer = new ResizeObserver(() =>
        setHeight(panelElement.offsetHeight),
      );

      observer.observe(panelElement);

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

  return height;
}

Usage:

export function MyPanelsComponent() {
  const height = usePanelHeight("my-panel-id"); // the same id must be specified in the ResizablePanel below.

  return (
    <ResizablePanelGroup direction="vertical">
      <ResizablePanel id="my-panel-id">
        <MyComponent panelHeight={height} />
      </ResizablePanel>
      <ResizableHandle />
      <ResizablePanel>
        <SomeOtherComponent />
      </ResizablePanel>
    </ResizablePanelGroup>
  );
}

@Albinzr
Copy link

Albinzr commented Sep 19, 2024

@NepeinAV I think this can help you guys
a simple css

<ResizablePanel
minSize={2}
defaultSize={2}
style={{
"min-width": "40px",
"max-height":"400px"
}}
>

@johnstack94
Copy link

If anyone is looking for a recent example to simply get the height of a panel to React state, you can use this hook I came up with (which is based on the original answer):

// usePanelHeight.ts

import { useLayoutEffect, useState } from "react";
import { getPanelElement } from "react-resizable-panels";

/**
 * Custom hook to get the height of a panel from `react-resizable-panels`.
 * @param id The id of the panel to get the height of.
 */
export function usePanelHeight(id: string) {
  const [height, setHeight] = useState<number>();

  useLayoutEffect(() => {
    const panelElement = getPanelElement(id);

    if (panelElement) {
      const observer = new ResizeObserver(() =>
        setHeight(panelElement.offsetHeight),
      );

      observer.observe(panelElement);

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

  return height;
}

Usage:

export function MyPanelsComponent() {
  const height = usePanelHeight("my-panel-id"); // the same id must be specified in the ResizablePanel below.

  return (
    <ResizablePanelGroup direction="vertical">
      <ResizablePanel id="my-panel-id">
        <MyComponent panelHeight={height} />
      </ResizablePanel>
      <ResizableHandle />
      <ResizablePanel>
        <SomeOtherComponent />
      </ResizablePanel>
    </ResizablePanelGroup>
  );
}

How do you set the minSize/maxSize using this pattern?

@NepeinAV
Copy link
Author

@NepeinAV I think this can help you guys

a simple css

<ResizablePanel

          minSize={2}

          defaultSize={2}

          style={{

            "min-width": "40px",

            "max-height":"400px"

          }}

        >

I ended up using "allotment", which works pretty well for me. For example, https://sql-academy.org/trainer/tasks/1

@johnstack94
Copy link

It doesn't have the collapsible panel feature tho. Otherwise seems nice.

@RomneyDa
Copy link

I don't think this has been brought up, if you have the container or screen dimensions, you can also just reverse calculate the percentage.

e.g.,

export function MyPanels() {
    const { width } = useContainerDimensions(); // or useScreenSize(), etc.
    const minSizePixels = 200;
    const minSizePercentage = minSizePixels / width * 100;
    return (
      <ResizablePanelGroup direction="horizontal">
        <ResizablePanel minSize={minSizePercentage}>Stuff 1</ResizablePanel>
        <ResizableHandle />
        <ResizablePanel>Stuff 2 </ResizablePanel>
      </ResizablePanelGroup>
    );
}

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

6 participants