kibana/packages/kbn-resizable-layout
Marta Bondyra 3a784106df
[testing] replace testing-library/dom package with testing-library/react (#170594)
## Summary

Removes `testing-library/dom` from dependencies. As all the utilities
from`dom` are available already in `testing-library/react`, there's no
need to have both `dom` and `react` libraries available in our
package.json.

Following the [@testing-library/react
documentation:](https://testing-library.com/docs/react-testing-library/intro)

> [React Testing
Library](https://github.com/testing-library/react-testing-library)
builds on top of DOM Testing Library by adding APIs for working with
React components.

Let's just import everything from `testing-library/react`, this way we
won't need to worry about inconsistencies between `testing-library/dom`
we have in our `package.json` and the one that is
`testing-library/react` dependency.
2023-11-08 15:53:48 +01:00
..
src [testing] replace testing-library/dom package with testing-library/react (#170594) 2023-11-08 15:53:48 +01:00
index.ts [Discover] Add resize support to the Discover field list sidebar (#167066) 2023-09-27 21:52:25 -03:00
jest.config.js [Discover] Add resize support to the Discover field list sidebar (#167066) 2023-09-27 21:52:25 -03:00
kibana.jsonc [Discover] Add resize support to the Discover field list sidebar (#167066) 2023-09-27 21:52:25 -03:00
package.json [Discover] Add resize support to the Discover field list sidebar (#167066) 2023-09-27 21:52:25 -03:00
README.md [Discover] Add resize support to the Discover field list sidebar (#167066) 2023-09-27 21:52:25 -03:00
tsconfig.json [Discover] Add resize support to the Discover field list sidebar (#167066) 2023-09-27 21:52:25 -03:00
types.ts [Discover] Add resize support to the Discover field list sidebar (#167066) 2023-09-27 21:52:25 -03:00

@kbn/resizable-layout

A component for creating resizable layouts containing a fixed width panel and a flexible panel, with support for horizontal and vertical layouts.

Example

Note

For advanced usage see the example plugin.

import { useIsWithinBreakpoints } from '@elastic/eui';
import { css } from '@emotion/react';
import {
  ResizableLayout,
  ResizableLayoutDirection,
  ResizableLayoutMode,
} from '@kbn/resizable-layout';
import React, { useRef, useState } from 'react';
// Using react-reverse-portal is recommended for complex/heavy layouts to prevent
// re-mounting panel components when the layout switches from resizable to static
import { createHtmlPortalNode, InPortal, OutPortal } from 'react-reverse-portal';

export const ResizablePage = () => {
  const [fixedPanelSize, setFixedPanelSize] = useState(500);
  const [container, setContainer] = useState<HTMLDivElement | null>(null);
  const [fixedPanelNode] = useState(() =>
    createHtmlPortalNode({ attributes: { class: 'eui-fullHeight' } })
  );
  const [flexPanelNode] = useState(() =>
    createHtmlPortalNode({ attributes: { class: 'eui-fullHeight' } })
  );

  const isMobile = useIsWithinBreakpoints(['xs']);
  const layoutMode = isMobile ? ResizableLayoutMode.Static : ResizableLayoutMode.Resizable;
  const layoutDirection = isMobile
    ? ResizableLayoutDirection.Vertical
    : ResizableLayoutDirection.Horizontal;

  const fullWidthAndHeightCss = css`
    position: relative;
    width: 100%;
    height: 100%;
  `;
  const panelBaseCss = css`
    ${fullWidthAndHeightCss}
    padding: 20px;
  `;
  const fixedPanelCss = css`
    ${panelBaseCss}
    background-color: rgb(255 0 0 / 30%);
  `;
  const flexPanelCss = css`
    ${panelBaseCss}
    background-color: rgb(0 0 255 / 30%);
  `;

  return (
    <div ref={setContainer} css={fullWidthAndHeightCss}>
      <InPortal node={fixedPanelNode}>
        <div css={fixedPanelCss}>
          This is the fixed width panel. It will remain the same size when resizing the window until
          the flexible panel reaches its minimum size.
        </div>
      </InPortal>
      <InPortal node={flexPanelNode}>
        <div css={flexPanelCss}>
          This is the flexible width panel. It will resize as the window resizes until it reaches
          its minimum size.
        </div>
      </InPortal>
      <ResizableLayout
        mode={layoutMode}
        direction={layoutDirection}
        container={container}
        fixedPanelSize={fixedPanelSize}
        minFixedPanelSize={300}
        minFlexPanelSize={500}
        fixedPanel={<OutPortal node={fixedPanelNode} />}
        flexPanel={<OutPortal node={flexPanelNode} />}
        onFixedPanelSizeChange={setFixedPanelSize}
      />
    </div>
  );
};