@aaronuu/react-layout 中文文档教程

发布于 6年前 浏览 25 项目主页 更新于 3年前

react-layout

用于 React 的 CSS 网格和 flexbox 辅助组件。 使用情感进行 css 注入。

Features

  • Declarative layout - having layout properties on the component makes page layout easier to reason about while not sacrificing performance by inlining all styles
  • Class construction is memoized based on props
  • Aliased css properties - less typing required
  • All properties (except position which is shortened to pos) are shortened to the first letter of each word of the property
  • Some of the more common properties also have easier to remember aliases (e.g. rows for gridTemplateRows)
  • You can either use the full prop name or any of it's aliases.
  • Flexible breakpoints
  • Given an integer as the key (or a string that can be parsed to an integer) the breakpoint will default as @media (max-width: <key>px)
  • Anything else will simply use the key as the media query
  • Inspired by react-native styling, flex-direction defaults to column

Getting Started

Installing

使用 yarn

yarn add @aaronuu/react-layout emotion

安装 使用 npm 安装 subgrid 属性

npm i @aaronuu/react-layout emotion

Usage

import { Box, Grid, GridItem } from '@aaronuu/react-layout';

const BREAKPOINT_MOBILE = '768';
const BREAKPOINT_TABLET = '@media(min-width: 768px and max-width: 1024px)';

class YourComponent extends Component {
  render() {
    return (
      <Grid
        rowGap="20px"
        columnGap="10px"
        rows="auto 1fr auto"
        columns="repeat(10, 100px)"
        breakpoints={{
          [BREAKPOINT_TABLET]: {
            rows: 'auto minmax(150px, 500px) auto'
          },
          [BREAKPOINT_MOBILE]: {
            rows: '1fr'
          }
        }}
      >
        <GridItem
          subgrid
          rows="100px 100px 100px"
          columns="auto"
          tag="span"
          area="1 / 1 / 4 / 3"
          breakpoints={{
            [BREAKPOINT_MOBILE]: {
              area: '1 / 1 / 2 / 3'
            }
          }}
        >
          <Box padding="10px">
            <h1>Heading</h1>
          </Box>
        </GridItem>
        <GridItem
          flex
          flexDirection="row"
          area="1 / 3 / 4 / 11"
          breakpoints={{
            [BREAKPOINT_MOBILE]: {
              area: '1 / 3 / 2 / 11'
            }
          }}
        >
          <Box ai="center" jc="center">
            <p>Content</p>
          </Box>
        </GridItem>
      </Grid>
    );
  }
}

Props

Shared

PropAlias
tag-
positionpos
topt
rightr
bottomb
leftl
heighth
widthw
maxHeightmaxh
maxWidthmaxw
minHeightminh
minWidthminw
paddingp
paddingToppt
paddingRightpr
paddingBottompb
paddingLeftpl
marginm
marginTopmt
marginRightmr
marginBottommb
marginLeftml
overflowo

Box

PropAlias
flexDirectionfd, direction
flexWrapfw, wrap
alignItemsai
alignContentac
justifyContentjc
flexGrowfg
flexShrinkfs
flexf
flexAutofa
flexNonefn

Grid

PropAlias
gridGapgg, gap
gridRowGapgrg, rowGap
gridColumnGapgcg, columnGap
gridTemplateRowsgtr, rows
gridTemplateColumnsgtc, columns
justifyItemsji
alignItemsai
placeItemspc
justifyContentjc
alignContentac
placeContentpc
gridAutoColumnsgac
gridAutoRowsgar
gridAutoFlowgaf

GridItem

PropAlias
subgrid
flex
gridRowStartgrs, rowStart
gridRowEndgre, rowEnd
gridColumnStartgcs, columnStart
gridColumnEndgce, columnEnd
gridRowgr, row
gridColumngc, column
gridAreaga, area
justifySelfjs
alignSelfas
placeSelfps

将使 GridItem 也成为 Grid 并且 flex 属性将使它也成为 Box

Caveats

仅 React 16目前。

只公开了 flexbox 容器属性,所有 flexbox 子属性仍然必须直接添加到子元素中。

由于 CSS Grid 的工作方式,Grid 组件的所有子项在技术上都是“网格项”,因此您不仅限于使用 GridItem 组件来设置 Grid 子项的样式。

Refs 将使用 forwardRef API 转发到内部元素,因此 ref 将返回对底层 DOM 节点的引用,而不是组件的实例。

Authors

License

该项目根据 MIT 许可证获得许可 - 有关详细信息,请参阅 LICENSE.md 文件

react-layout

CSS grid and flexbox helper components for react. Uses emotion for css injection.

Features

  • Declarative layout - having layout properties on the component makes page layout easier to reason about while not sacrificing performance by inlining all styles
  • Class construction is memoized based on props
  • Aliased css properties - less typing required
  • All properties (except position which is shortened to pos) are shortened to the first letter of each word of the property
  • Some of the more common properties also have easier to remember aliases (e.g. rows for gridTemplateRows)
  • You can either use the full prop name or any of it's aliases.
  • Flexible breakpoints
  • Given an integer as the key (or a string that can be parsed to an integer) the breakpoint will default as @media (max-width: <key>px)
  • Anything else will simply use the key as the media query
  • Inspired by react-native styling, flex-direction defaults to column

Getting Started

Installing

Install using yarn

yarn add @aaronuu/react-layout emotion

Install using npm

npm i @aaronuu/react-layout emotion

Usage

import { Box, Grid, GridItem } from '@aaronuu/react-layout';

const BREAKPOINT_MOBILE = '768';
const BREAKPOINT_TABLET = '@media(min-width: 768px and max-width: 1024px)';

class YourComponent extends Component {
  render() {
    return (
      <Grid
        rowGap="20px"
        columnGap="10px"
        rows="auto 1fr auto"
        columns="repeat(10, 100px)"
        breakpoints={{
          [BREAKPOINT_TABLET]: {
            rows: 'auto minmax(150px, 500px) auto'
          },
          [BREAKPOINT_MOBILE]: {
            rows: '1fr'
          }
        }}
      >
        <GridItem
          subgrid
          rows="100px 100px 100px"
          columns="auto"
          tag="span"
          area="1 / 1 / 4 / 3"
          breakpoints={{
            [BREAKPOINT_MOBILE]: {
              area: '1 / 1 / 2 / 3'
            }
          }}
        >
          <Box padding="10px">
            <h1>Heading</h1>
          </Box>
        </GridItem>
        <GridItem
          flex
          flexDirection="row"
          area="1 / 3 / 4 / 11"
          breakpoints={{
            [BREAKPOINT_MOBILE]: {
              area: '1 / 3 / 2 / 11'
            }
          }}
        >
          <Box ai="center" jc="center">
            <p>Content</p>
          </Box>
        </GridItem>
      </Grid>
    );
  }
}

Props

Shared

PropAlias
tag-
positionpos
topt
rightr
bottomb
leftl
heighth
widthw
maxHeightmaxh
maxWidthmaxw
minHeightminh
minWidthminw
paddingp
paddingToppt
paddingRightpr
paddingBottompb
paddingLeftpl
marginm
marginTopmt
marginRightmr
marginBottommb
marginLeftml
overflowo

Box

PropAlias
flexDirectionfd, direction
flexWrapfw, wrap
alignItemsai
alignContentac
justifyContentjc
flexGrowfg
flexShrinkfs
flexf
flexAutofa
flexNonefn

Grid

PropAlias
gridGapgg, gap
gridRowGapgrg, rowGap
gridColumnGapgcg, columnGap
gridTemplateRowsgtr, rows
gridTemplateColumnsgtc, columns
justifyItemsji
alignItemsai
placeItemspc
justifyContentjc
alignContentac
placeContentpc
gridAutoColumnsgac
gridAutoRowsgar
gridAutoFlowgaf

GridItem

PropAlias
subgrid
flex
gridRowStartgrs, rowStart
gridRowEndgre, rowEnd
gridColumnStartgcs, columnStart
gridColumnEndgce, columnEnd
gridRowgr, row
gridColumngc, column
gridAreaga, area
justifySelfjs
alignSelfas
placeSelfps

The subgrid prop will make the GridItem also a Grid and the flex prop will make it also a Box

Caveats

React 16 only for now.

Only flexbox container properties have been exposed, all flexbox child properties will still have to be added to the child elements directly.

Due to how CSS Grid works all children of the Grid component are technically 'grid items' so you aren't limited to using the GridItem component to style Grid children.

Refs will be forwarded to the inner element using the forwardRef API so the ref will return a reference to the underlying DOM node rather than the instance of the component.

Authors

License

This project is licensed under the MIT License - see the LICENSE.md file for details

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文