Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

flex-wrap-layout

tkrotoff97MIT0.13.0TypeScript support: included

Experiments with flexbox layout

flexbox, flex-wrap

readme

flex-wrap-layout

npm version Node.js CI Bundle size Prettier Airbnb Code Style

Detects flex-wrap via JavaScript (unfortunately not possible in CSS).

detectWrappedElements() and useDetectWrappedElements() are JavaScript functions that detect when elements are wrapped and let you define the CSS that goes with it.

This allows for responsive UIs without hardcoded CSS (width, min-width, media queries...) using the "natural" width of elements instead.

  • With detectWrappedElements():

Minimal example with detectWrappedElements()

  • Without:

Minimal example without detectWrappedElements()

Usage

npm install flex-wrap-layout

import { useRef } from 'react';

import { useDetectWrappedElements, wrapChildrenClassName } from 'flex-wrap-layout';
import 'flex-wrap-layout/src/detectWrappedElements.scss';

const boxStyle = {
  border: '1px solid',
  height: 60,
  margin: 5,
  padding: 5
};

function MyComponent() {
  const ref = useRef(null);
  useDetectWrappedElements(ref);

  return (
    <div ref={ref} className={wrapChildrenClassName}>
      <div style={boxStyle}>Paris</div>
      <div style={{ ...boxStyle, flex: 'auto' }}>Los Angeles</div>
      <div style={boxStyle}>Phnom Penh</div>
    </div>
  );
}

Examples

Limitations

This will cause reflows (layout thrashing): https://gist.github.com/paulirish/5d52fb081b3570c81e3a

changelog

0.13.0 (2023/03/11)

  • Drop old browsers support

0.12.0 (2022/07/08)

0.11.0 (2022/07/08)

  • Just ship CommonJS
  • Update npm packages
  • Remove all Playwright screenshots: too fragile

0.10.2 (2021/12/15)

  • Update npm packages
  • Switch from jest-playwright to @playwright/test
  • Rewrite end-to-end tests

0.10.1 (2021/05/08)

  • Update npm packages

0.10.0 (2021/04/29)

  • Breaking change: make useDevTools() params more user-friendly
  • Fix: .next-is-wrapped may be present if the next sibling has been removed since the last run
  • New function forceDetectWrappedElements() to support IE
  • Add a "Show details" button to the Bootstrap example
  • Switch Bootstrap example to Bootstrap 5
  • Simplify code
  • Update npm packages

0.9.2 (2020/03/18)

  • Update npm packages

0.9.1 (2020/02/24)

  • DevTools accepts div props

0.9.0 (2020/02/24)

  • Rework everything: exposes useDetectWrappedElements(), detectWrappedElements() and DevTools
  • Remove DetectRowWrapController, flex-wrap-layout.scss (layout='column', layout='row', vspace, hspace, grow)

0.2.1 (2020/01/31)

  • Fix remove .next-is-wrapped
  • Listen to load event does not work under React

0.2.0 (2020/01/30)

  • Replace .wrapped by .next-is-wrapped ~ *
  • Specify width: initial only with layout='column'

0.1.2 (2020/01/30)

  • Update npm packages
  • GitHub Actions
  • ESLint Airbnb & stylelint & Husky

0.1.1 (2019/03/25)

  • Fix crash when testing with Jest

0.1.0 (2019/03/25)

  • Modernize build system
  • $layout-item-margin-x and $layout-item-margin-y instead of just $layout-item-margin
  • Bootstrap 4 example and demo
  • React hook useDetectRowWrap()

0.0.2 (2017/09/12)

First release