A powerful React component to abstract over flexbox and create any layout on any browser

Overview

FlexView

A powerful React component to abstract over flexbox and create any layout on any browser.

Install

yarn add react-flexview

Why

The flexbox API is powerful but far from being perfect. The API is complex and there are still many inconsistencies between browsers that force developers to overuse vendor prefixes and literally do magic tricks to achieve the desired layout.

For these reasons, we asked ourselves: is there a way to simplify the API and handle any browser inconsistency in a single place? Our attempt to answer "yes!" to that question gave birth to FlexView.

// flex row
<FlexView />

// flex column
<FlexView column />

// grow, shrink and basis
<FlexView grow shrink basis={100} />
<FlexView grow={2} shrink={1} basis='auto' />
<FlexView basis={100} /> // shrink is set to `false` by default so you're certain to a have it `100px` wide/tall

// wrap
<FlexView wrap />

Remember how difficult it was to center a div inside another div? flexbox definitely improved it, but still having to switch from align-items to justify-content based on flex-direction of the parent is confusing and error prone.

FlexView lets you align and center children with two intuitive props: vAlignContent and hAlignContent.

<FlexView hAlignContent='center' vAlignContent='center'>
  <FlexView>the center of the Earth</FlexView>
</FlexView>

How to use

In your component:

import React from 'react';
import FlexView from 'react-flexview';

export default class Component extends React.Component {
  render() {
    return (
      <FlexView vAlignContent='center'>
        I'm vertically centered!
      </FlexView>
    );
  }
}

Props

Name Type Default Description
children ReactChildren required. FlexView content
column Boolean optional. Flex-direction: column
vAlignContent enum("top" | "center" | "bottom") optional. Align content vertically
hAlignContent enum("left" | "center" | "right") optional. Align content horizontally
marginLeft union(String | Number) optional. Margin-left property ("auto" to align self right)
marginTop union(String | Number) optional. Margin-top property ("auto" to align self bottom)
marginRight union(String | Number) optional. Margin-right property ("auto" to align self left)
marginBottom union(String | Number) optional. Margin-bottom property ("auto" to align self top)
grow union(Boolean | Number) optional. Property (for parent primary axis)
shrink union(Boolean | Number) optional. Flex-shrink property
basis union(String | Number) optional. Flex-basis property
wrap Boolean optional. Wrap content
height union(String | Number) optional. Height property (for parent secondary axis)
width union(String | Number) optional. Width property (for parent secondary axis)
className String optional. Additional className for wrapper element
style Object optional. Inline-style overrides for wrapper element

Demo

Here's a live playground

Documentation

Refer to the Book of FlexView

Supported browsers

We removed vendor prefixes in #66, since than we only supports browsers that don't require prefixes for flexbox CSS properties which are most browsers:

  • IE 11
  • Edge (any version)
  • Google Chrome 29+
  • Firefox 28+
  • Safari 9+

If you need to support older browsers, try with FlexView 3.x.

Used By

At buildo we've been using FlexView in production in every web application we built since July 2015 (it was in a different repo back then).

As of today, FlexView has replaced the div as the brick of our projects and, thanks to it, our developers can finally build without too much trouble complex layouts that work the same on Chrome, Firefox, Safari and Internet Explorer 11.

You can see it in action here:

Comments
  • Allow customizing the underlying intrinsic element

    Allow customizing the underlying intrinsic element

    FlexView uses div as a default for the underlying component.

    This PR allows changing the default to another intrinsic component. For example:

    <FlexView />
    <FlexView component="form" />
    

    will render

    <div ... />
    <form ... />
    
    breaking 
    opened by gabro 4
  • Remove type parameter from FlexView

    Remove type parameter from FlexView

    The changes introduced in #84 to make the component generic didn't really work.

    I've been struggling with TypeScript for a while and I couldn't come up with anything that works (either TS dies trying to compute the types or it computes the wrong types).

    This PR reverts to a middle ground, where the type of the element is simply HTMLElement which should be good enough for most use-cases

    opened by gabro 2
  • Allow to pass a ref for the underlying div

    Allow to pass a ref for the underlying div

    Description

    At the moment it's impossible to pass a ref to the underlying div element. We should add an innerRef prop to FlexView and forward it as ref for the root div.

    breaking FE maint 
    opened by veej 2
  • #72: Remove lodash deps (closes #72)

    #72: Remove lodash deps (closes #72)

    Closes #72

    Test Plan

    tests performed

    tested that all cases of pick, omit and some are still working:

    • omit: flexview props are not being passed to div (react is not complaining)
    • pick: marginTop/Left/Bottom/Right, width and height still works
    • some: warnings still work

    tests not performed (domain coverage)

    At times not everything can be tested, and writing what hasn't been tested is just as important as writing what has been tested.

    An example of partial test is a field displaying 4 possible values. If 3 values are tested, with screenshots, and 1 is not, then it should be mentioned here.

    opened by FrancescoCioria 2
  • Add prop for easy centering

    Add prop for easy centering

    There should be an option for fast, easy centring of children.

    // From
    <FlexView vAlignContent={'Center'} hAlignContent={'center'} />
    
    // To
    <FlexView center />
    

    Alternatively one could have the vAlignContent fields default to center

    //From
    <FlexView vAlignContent={'Center'} hAlignContent={'center'} />
    
    // To
    <FlexView vAlignContent hAlignContent />
    

    Any thought?

    discarded 
    opened by cupcakearmy 2
  • more justify/align props?

    more justify/align props?

    seems like you can't do justify-content: space-between or justify-content: space-around

    Failed prop type: Invalid prop `hAlignContent` of value `space-between` supplied to `FlexView`, expected one of ["left","center","right"]
    

    also seems to be a lot of other missing aspects of flexbox or limited property values. is this something you would take a PR to fix or is this intended?

    opened by yocontra 2
  • Typescript 2.9.2 failed when compiling typelevel-ts dependency

    Typescript 2.9.2 failed when compiling typelevel-ts dependency

    ts 2.8.4 compiles successfully, but 2.9.2 not:

    ERROR in [at-loader] ./node_modules/typelevel-ts/lib/index.d.ts:76:13 TS2344: Type '{ T: N2; F: Succ<any[N1["prev"]["prev"]["isZero"]]>; }[N1["prev"]["isZero"]]' does not satisfy the constraint 'Nat'. Type '{ T: N2; F: Succ<any[N1["prev"]["prev"]["isZero"]]>; }[N1["prev"]["isZero"]]' is not assignable to type 'Succ'.

    ERROR in [at-loader] ./node_modules/typelevel-ts/lib/index.d.ts:82:81 TS2344: Type '{ T: { T: Some; F: None; }[N2["isZero"]]; F: { T: Some; F: any[N1["prev"]["isZero"]]; }...' does not satisfy the constraint 'Option'. Type '{ T: Some; F: None; }[N2["isZero"]] | { T: Some; F: { T: any[N2["prev"]["isZero"]]; F: ...' is not assignable to type 'Option'. Type '{ T: Some; F: { T: any[N2["prev"]["isZero"]]; F: any[N2["prev"]["isZero"]]; }[N1["prev"]["isZ...' is not assignable to type 'Option'. Type 'Some | { T: { T: Some; F: None; }[N2["prev"]["isZero"]]; F: { T: Some<N1["prev"]>; F: a...' is not assignable to type 'Option'. Type '{ T: { T: Some; F: None; }[N2["prev"]["isZero"]]; F: { T: Some<N1["prev"]>; F: any[N1["prev...' is not assignable to type 'Option'. Type '{ T: { T: Some; F: None; }[N2["prev"]["isZero"]]; F: { T: Some<N1["prev"]>; F: any[N1["prev...' is not assignable to type 'Some'. Type '{ T: Some; F: { T: any[N2["prev"]["isZero"]]; F: any[N2["prev"]["isZero"]]; }[N1["prev"]["isZ...' is not assignable to type 'Some'. Type 'Some | { T: { T: Some; F: None; }[N2["prev"]["isZero"]]; F: { T: Some<N1["prev"]>; F: a...' is not assignable to type 'Some'. Type '{ T: { T: Some; F: None; }[N2["prev"]["isZero"]]; F: { T: Some<N1["prev"]>; F: any[N1["prev...' is not assignable to type 'Some'. Type '{ T: { T: Some; F: None; }[N2["isZero"]]; F: { T: Some; F: any[N1["prev"]["isZero"]]; }...' is not assignable to type 'Some'. Type '{ T: Some; F: None; }[N2["isZero"]] | { T: Some; F: { T: any[N2["prev"]["isZero"]]; F: ...' is not assignable to type 'Some'. Type '{ T: Some; F: None; }[N2["isZero"]]' is not assignable to type 'Some'. Type 'None | Some' is not assignable to type 'Some'. Type 'None' is not assignable to type 'Some'. ... ERROR in [at-loader] ./node_modules/typelevel-ts/lib/index.d.ts:104:50 TS2536: Type '{ T: "0"; F: any[N["prev"]["isZero"]]; }[N["isZero"]]' cannot be used to index type 'StringToNumber'. ...

    ... and many many errors like above

    opened by A77X7 2
  • Different Values at Different Breakpoints

    Different Values at Different Breakpoints

    Hello, how do I set values differently at different breakpoints? Say for instance that I want a sidebar to collapse entirely but only on mobile screens?

    discussion 
    opened by picosam 2
  • #15: add Flow/TypeScript support (closes #15)

    #15: add Flow/TypeScript support (closes #15)

    Proof of concept

    This PR adds:

    • for consumers
      • Flow and TypeScript typings, shipped with the npm package itself
    • for developers
      • source code ready for Flow (typings of dependencies that are needed are included)
    WIP 
    opened by gcanti 2
  • slightly stricter children prop typings

    slightly stricter children prop typings

    Small improvement wrt the "original" ReactNode (https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/react/index.d.ts#L186-L191) where we don't accept {}.

    e.g. with this change, the following code does not compile:

    <FlexView>{some('a')}</FlexView>
    

    Some additional background here: https://github.com/DefinitelyTyped/DefinitelyTyped/issues/35622

    tested on an internal project, doesn't seem to break anything

    FE maint 
    opened by giogonzo 1
  • Remove lodash deps

    Remove lodash deps

    It should be possible to do this without lodash. according to bundlephobia the package is 23.6kB minified and 7.5kb gzipped. which is insane.

    Other than that you are the only guys to include flex direction 🎉❤️

    opened by cupcakearmy 1
  • FlexView ignores basis prop when value is 0

    FlexView ignores basis prop when value is 0

    If I try to pass a value of 0 for the basis prop, the FlexView will ignore that and leave the basis at auto (its default). This can also be seen on the live playground (in the interactive section at the bottom, try entering 0 as the basis).

    I suspect this is happening because of an if-condition that checks the value of basis, and a value of 0 evaluates to be falsy, so the if-block acts as if no basis prop was provided. This is potentially happening in FlexView.tsx, on line 201, in the getBasis function.

    FE maint 
    opened by Rifdhan 1
  • Proposal: default `shrink` to `false`

    Proposal: default `shrink` to `false`

    Partly inspired by https://visly.app/blog/layout-isolated-components, the flex default of shrink = 1 (true in FlexView terms) could be switched here, in order to defaulting to more "isolated" components by default

    breaking FE maint 
    opened by giogonzo 0
  • IE11: Column inside a row may overflow its parent

    IE11: Column inside a row may overflow its parent

    description

    On IE11 if the content of a column rendered inside a row has a base width bigger than the row itself, the column will overflow its parent even if it shouldn't

    how to reproduce

    <FlexView width={100}>
      <FlexView column>
        asduhaudhausda asdoihaid dasidaishd alodhiashd alidhiasdh alisdhiashd aoidshaisdh oasdihasidh aidhasidh oaidhaisdh aoidhaisdh asdhaosidh
      </FlexView>
    </FlexView>
    

    On chrome the text column is 100px wide, on IE11 it's as wide as its content...

    FlexView should somehow prevent this bug or, if not possible, warn the user when in dev that they might face it.

    specs

    Apparently not only IE11 fails at forcing the column to not exceed its parent but it also ignores an explicit max-width if set to 100%....

    I found this possible fix: add style={{ maxWidth: 99.99 }} to the column (any value apart from 100% is correctly understood by IE11)

    We could:

    1. add that fix to any FlexView column -> very breaking, implicit and opinionated
    2. only in dev: if I'm a column + my parent is a flexview row + I don't have an explicit basis/width/max-width -> log a warning that link to a section in the readme that explain the issue and how to manually fix it

    misc

    {optional: other useful info}

    bug 
    opened by FrancescoCioria 1
Releases(v6.0.1)
React-layout - Layout component for React. Handling the overall layout of a page

Layout Handling the overall layout of a page. ⚠️ Note: Implemented with flex lay

uiw 2 Jul 10, 2022
React Responsive Grid Layout makes it easy to develop fluid layouts for mobile, tablet and desktop using ReactJs, Styled Components, CSS and Flexbox.

React Responsive Grid Layout makes it easy to develop fluid layouts for mobile, tablet and desktop using ReactJs, Styled Components, CSS and Flexbox.

Taras Pysarskyi 14 Sep 26, 2022
React-masonry - An easy to use and simple masonry layout for React Js based on flexbox column

React Masonry An easy to use and simple masonry layout for React Js based on fle

null 16 Dec 23, 2022
React-Grid-Layout is a grid layout system much like Packery or Gridster, for React.

A draggable and resizable grid layout with responsive breakpoints, for React.

RGL 16.9k Jan 2, 2023
Layout-reactJS - Layout with React JS using NASA API

Layout with React JS using NASA API Website link on the web: Click Here Preview:

Anastacio Menezes 4 Feb 2, 2022
A no-frills flexbox grid system for React, powered by styled-components.

Quick Jump Quick Jump Features Requirements Installation Using yarn Using npm Usage Basic Example Responsive Example Upgrading Documentation Grid.Prov

Garet McKinley 858 Dec 20, 2022
Responsive React Flexbox Grid System

Responsive React Flexbox Grid System

Brent Jackson 1.4k Dec 10, 2022
Physical representation of layout composition to create declarative responsive layouts in React.

Atomic Layout is a spatial distribution library for React. It uses CSS Grid to define layout areas and render them as React components. This pattern e

Artem Zakharchenko 1.1k Dec 26, 2022
🦸 Motion Layout - Create beautiful immersive React hero animations using shared components.

Motion Layout Create beautiful immersive React hero animations using shared components. Docs React Motion Layout Docs About Motivation There are amazi

Jefferson Licet 584 Dec 25, 2022
Animated grid layout component for React

react-stonecutter Animated grid layout component for React, inspired by Masonry. Choose between CSS Transitions or React-Motion for animation. Demo In

Dan Train 1.2k Dec 24, 2022
This react component resize the layout of HTML using a handle

react-resize-layout This react component resize the layout of HTML using a handle Demo View the demo page Example View the example demo page npm insta

null 36 Nov 3, 2022
A draggable and resizable grid layout with responsive breakpoints, for React.

React-Grid-Layout React-Grid-Layout is a grid layout system much like Packery or Gridster, for React. Unlike those systems, it is responsive and suppo

Samuel Reed 16.9k Jan 1, 2023
FlexLayout is a layout manager that arranges React components in multiple tab sets, tabs can be resized and moved.

FlexLayout is a layout manager that arranges React components in multiple tab sets, tabs can be resized and moved.

Caplin 649 Jan 7, 2023
🤸 Configurable grid and layout engine for React

gymnast is a configurable grid for React With gymnast, you can eliminate your layout css or reduce it to just a few lines. Learn how to use gymnast in

gymnast.js 38 May 7, 2022
A powerful Bootstrap-like responsive grid system for React.

React Grid System A powerful Bootstrap-like responsive grid system for React. ⚠️ Upgrading to v7 react-grid-system v7 adds a new screen class xxl for

Sealninja 761 Jan 9, 2023
Auto Responsive Layout Library For React

autoresponsive-react Auto responsive grid layout library for React. Who are using ⭐ ⭐ ⭐ alibaba/ice ⭐ ⭐ ⭐ ice-lab/react-materials ⭐ ⭐ ⭐ ant-design/ant

达峰的夏天 1.5k Dec 15, 2022
The layout engine for React

Responsive, sortable, filterable and draggable grid layouts with React Design Principles ????‍?? Muuri-React is the React implementation of the amazin

Paolo Longo 274 Dec 21, 2022
Resizable Flex layout container components for advanced React web applications

About Re-F|ex Re-F|ex is a React flex-based layout component library which I created because none of the components I found out there could satisfy my

Philippe Leefsma 532 Jan 8, 2023
Resizable Flex layout container components for advanced React web applications

About Re-F|ex Re-F|ex is a React flex-based layout component library which I created because none of the components I found out there could satisfy my

Philippe Leefsma 530 Dec 21, 2022