2.0.0-rc.2 • Published 4 years ago

react-styled-flex v2.0.0-rc.2

Weekly downloads
63
License
MIT
Repository
github
Last release
4 years ago

react-styled-flex

module verion and npm link bundlephobia minified size bundlephobia minizipped size types available Codacy Badge CircleCI codecov

Simple, light, unopinionated, CSS standard compliant Flexbox component for react using styled-components

Changelog

This is react-styled-flex@2 documentation. For version 1, please follow this link. Following changes are introduced in version 2:

✂️ Breaking changes

  • is prop of FlexBox and FlexItem component is longer supported. Instead use native styled-components "as" polymorphic prop to render another react component or html element.
  • Supports styled-components version greater than or equal to >=5.1.0. If you want to use older versions of styled-components, please install react-styled-flex version 1 by using npm install react-styled-flex@latest-1 command.

🚀 Enhancements

  • Typescript rewrite.
  • ~20% lightweight than version 1.
  • Supports SSR and SSG rendering.
  • Introduces Box component.
  • Uses styled-components shouldForwardProp mechanism to avoid leaking props to DOM. As a result, is prop from version 1 is no longer supported.

Features

  • Lightweight and dependency free, ~2.4 KB minified or ~1.1 KB minified + gzipped.
  • Clean underlying HTML DOM. No prop leakage to DOM.
  • Supports flex-gap feature. For non supported browsers, it degrades gracefully by applying appropriate margin properties.
  • Supports rendering of any react component or html element.
  • Supports unitless values wherever required.
  • Supports SSR and SSG rendering.
  • TypeScript support.

Installation

Yarn

yarn add react-styled-flex

Npm

npm i react-styled-flex

react-styled-flex requires peer dependencies react and styled-components. You need to add them separately.

API

  • react-styled-flex exports three components: Box, FlexBox and FlexItem.
  • Box behaves as basic CSS box. FlexBox and FlexItem extends Box.
  • FlexBox behaves as a container with display: flex rule.
  • FlexItem as acts as a child for FlexBox. Though FlexBox can have other components as child as well.
  • Only use FlexItem if you need to provide additional styles to child components. See Props section for more details.
  • FlexItem can be treated as FlexBox for nested childs by setting box prop as true on FlexItem

Usage

react-styled-flex exports three components: Box, FlexBox and FlexItem. All renders simple div with styles derived from passed props.

import { Box, FlexBox, FlexItem } from "react-styled-flex";

const Layout = () => {
    return <FlexBox center>
        <Box padding={10}>Child 1</Box>
        <FlexItem>Child 2</FlexItem>
        <FlexItem flex={1}>Child 3</FlexItem>
    </FlexBox>
}

On rendering Layout component,

  • One parent div with style display: flex; justify-content: center; align-items: center and three nested divs will be rendered.
    • First child will have padding of 10px.
    • Second child will be simple div.
    • Third child will have style flex: 1;

For rendering elements other than divs, please refer Change underlying element section.

Props

Box

All props are optional.

PropsTypeDescription
heightstring | numberApplies height
widthstring | numberApplies width
marginstring | numberApplies margin using CSS margin shorthand specification
paddingstring | numberApplies padding using CSS padding shorthand specification
borderstring | numberApplies border using CSS border shorthand specification

FlexBox

All props are optional and all boolean props defaults to false.

PropsTypeDescription
heightstring | numberApplies height
widthstring | numberApplies width
marginstring | numberApplies margin using CSS margin shorthand specification
paddingstring | numberApplies padding using CSS padding shorthand specification
borderstring | numberApplies border using CSS border shorthand specification
inlinebooleanIf true, applies display: inline-flex rule otherwise applies display: flex
columnbooleanIf true, flex-direction rule is set as column otherwise set as row
reversebooleanIt works in tandem with column prop to generate flex-direction: {row\|column}-reverse. Following table summaries it, columnreverseflexdirectionfalsefalserowfalsetruerow-reversetruefalsecolumntruetruecolumn-reverse
wrapbooleanIf true, applies flex-wrap as wrap
wrapReversebooleanIf true, applies flex-wrap as wrap-reverse
centerbooleanIf true, then applies justify-content: center and align-items: center
gapstring | numberApplies gap using CSS gap shorthand specification if browser supports it, otherwise fallbacks to using margin property. Read flex gap feature to understand more
columnGapstring | numberApplies CSS column-gap property if browser supports it, otherwise fallbacks to using margin property. Read flex gap feature to understand more
rowGapstring | numberApplies CSS row-gap property if browser supports it, otherwise fallbacks to using margin property. Read flex gap feature to understand more
justifyItemsstringApplies justify-items rule. Depending on the browser, these justify-items values might be supported
justifyContentstringApplies justify-content rule. Depending on the browser, these justify-content values might be supported
alignItemsstringApplies align-items rule. Depending on the browser, these align-items values might be supported
alignContentstringApplies align-content rule. Depending on the browser, these align-content values might be supported

FlexItem

All props are optional and all boolean props defaults to false.

PropsTypeDescription
heightstring | numberApplies height
widthstring | numberApplies width
marginstring | numberApplies margin using CSS margin shorthand specification
paddingstring | numberApplies padding using CSS padding shorthand specification
borderstring | numberApplies border using CSS border shorthand specification
flexstring | numberApplies flex using CSS flex shorthand specification
growstring | numberApplies CSS flex-grow property
shrinkstring | numberApplies CSS flex-shrink property
basisstring | numberApplies CSS flex-basis property
orderstring | numberApplies CSS order property
justifySelfstringApplies justify-self rule. Depending on the browser, these justify-self values might be supported.
alignSelfstringApplies align-self rule. Depending on the browser, these align-self values might be supported
boxbooleanIf true, then FlexItem also behaves as a FlexBox. So in addition to FlexItem props, all the FlexBox props can also be applied

Supports unitless values

  • react-styled-flex supports unitless values where units are required. In that case value will be auto suffixed with with px unit.
  • Only values where unites are required(eg. height, width, margin) will be suffixed.
  • CSS rules which don't have units won't be suffixed (eg. order)

Supports flex gap feature

  • Browser supports flex gap feature

    • If flex gap feature is supported in browser than gap, columnGap and rowGap props will function as per specification.
  • Browser don't support flex gap feature

    • If browser does not supports it, then we intend to provide graceful degradation of flex gap feature by setting margin. This fallback is provided only if, either of gap props is set and wrap prop is not set.
    • If wrap is set then gap wont work in non-supported browser.
    • Rest all props are supported.

Change underlying element

  • By default FlexBox and FlexItem renders div in the DOM.

  • We can change it to any HTML element or react component using styled-components as prop.

  • Example:

    import { FlexBox, FlexItem } from "react-styled-flex";
    
    /* other logic */
    
    <FlexBox center>
        <FlexItem as={"button"}>Child 1</FlexItem>
        <FlexItem as={"button"}>Child 2</FlexItem>
    </FlexBox>

    Render Child 1 and Child 2 as button

  • Similarly any react component can be rendered

License

MIT © Piyush Lodaya

Resources

2.2.0

4 years ago

2.1.1

4 years ago

2.1.0

4 years ago

2.0.0

4 years ago

2.0.0-rc.4

4 years ago

2.0.0-rc.3

4 years ago

2.0.0-rc.2

4 years ago

2.0.0-rc.1

4 years ago

2.0.0-rc.0

4 years ago

1.0.19

5 years ago

1.0.18

6 years ago

1.0.17

6 years ago

1.0.16

6 years ago

1.0.15

6 years ago

1.0.14

6 years ago

1.0.13

6 years ago

1.0.12

6 years ago

1.0.11

6 years ago

1.0.10

6 years ago

1.0.9

6 years ago

1.0.8

6 years ago

1.0.7

6 years ago

1.0.6

6 years ago

1.0.5

6 years ago

1.0.4

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago