7.0.0 β€’ Published 11 months ago

@nfq/react-grid v7.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
11 months ago

@nfq/react-grid


!WARNING

This project switched over to @emotion/styled. As Styled-Components is in "Maintainance Mode".
If you use styled-components you can use the version <= 6.0.0 of this package.


Table of Contents


About the project

A powerful, theme-free, responsive grid system for React powered by @emotion/react.
Built on modern CSS variables, mobile-first principles, and full TypeScript support.

Inspired by react-awesome-styled-grid, re-engineered for real-world production use.

πŸš€ Features

  • βš›οΈ No dependency on theme providers – works out of the box with CSS custom properties.
  • 🧱 Responsive grid layout: Container, Row, Col, Spacer
  • 🧩 Extra helpers: Skeleton, Hidden, Visible, ResponsiveText
  • 🧠 Hooks and utilities: useScreenSize, spacing, media()
  • πŸ” Built-in debug layer via Ctrl + D
  • 🎨 Fully configurable skeleton animations and design tokens
  • πŸ§ͺ Test-friendly via testId props

πŸ“¦ Installation

To install the package run

npm install @nfq/react-grid @emotion/react @emotion/styled
# or
yarn add @nfq/react-grid @emotion/react @emotion/styled
# or
pnpm add @nfq/react-grid @emotion/react @emotion/styled

πŸ“¦ PeerDependencies

The following PeerDependencies are needed so the component does work:

  • @emotion/react >= 10
  • @emotion/styled >= 10
  • react >= 18
  • react-dom >= 18

Configuration

Define your grid configuration using createConfig somewhere in your app. This is a required step to use the grid system. Full documentation can be found here.

// grid.config.ts
import { createConfig } from '@nfq/react-grid';

export const breakpoints = ['xs', 'sm', 'md', 'lg', 'xl', 'xxl'] as const; // Add your Breakpoints and its sorting smallest to biggest.
export const { configType, globalCss: globalGridCss } = createConfig(breakpoints, {
  baseSpacing: 0.4,
  breakpoints: {}, // ['xs', 'sm', 'md', 'lg', 'xl', 'xxl'] are defaulted and don't need to be defined. If your breakpoints are different you can define them here.
  container: {
    xs: 'fluid',
    sm: 'fluid',
    md: 'fluid',
    lg: 'fluid',
    xl: 1140,
    xxl: 1140
  } // this is the only mandatory property. It defines the container width for every breakpoint. You can also set it to 'fluid'.
});

globalGridCss (Required)

You must inject globalGridCss once at app level to enable the grid’s base variables and behavior:

/** globals.ts */
import { css, Global } from '@emotion/react';
import { globalCss, globalGridCss } from './grid.config';

export const globals = (
  <Global
    styles={css`
      ${globalCss}
      ${globalGridCss}
      html {
        font-size: 10px;
      }
    `}
  />
);

Extend Module Declarations (For typescript autocompletion for your custom breakpoints)

// grid.d.ts
import '@nfq/react-grid';
import {breakpoints, configType} from './grid.config';

declare module '@nfq/react-grid' {
  export interface UserConfig {
    Breakpoints: typeof breakpoints;
    Config: typeof configType;
  }
}

Usage

import {
    Col,
    Container,
    Hidden,
    ResponsiveText,
    Row,
    ScreenBadge,
    ScreenSizeContext,
    ScreenSizeProvider,
    Skeleton,
    Spacer,
    Visible,
    createConfig,
    getScreenSize,
    lighten,
    media,
    mediaBetween,
    spacing,
    useScreenSize
} from '@nfq/react-grid';

Responsive Columns

<Container>
    <Row>
        <Col xs={2} sm={6} md={2} lg={6}>Col 1</Col>
        <Col xs={2} sm={2} md={6} lg={6}>Col 2</Col>
    </Row>
    <Row>
        <Col xs={1} sm={4} md={3} lg={4}>Col 3</Col>
        <Col xs={3} sm={4} md={5} lg={8}>Col 4</Col>
    </Row>
</Container>

The screen params define the Col size in its respective screen class. If no size is defined the col will be auto width. The mobile first nature defines also the definitions used for undefined sizes. (in the above example xl and xxl will use the same value as lg).

Offset usage

<Container>
    <Row>
        <Col offset={2}>Col 1</Col>
    </Row>
    <Row>
        <Col offset={{xs:3, sm:7, lg:11}}>Col 2</Col>
    </Row>
</Container>

An offset with an single number defines the offset for all screens. Because all screen sizes can have an different number of columns the offset will get calculated on basis of the smallest defined breakpoint. So if you have 4 columns on this breakpoint and an offset of 2 all screensizes will render the column with 50% width.

If you define an object you can set the offset for every screen size. And so all offsets will calculated for the specific screen sizes. So if you want an offset of one column for all screens you can define an offset of your smallest breakpoint as {[breakpoint]: 1} because we render mobile first all other screens will calculate the offset with 1 column and use its own max columns.

Order usage

<Container>
    <Row order={{xs: 1, lg: 2}}>
        <Col order={2}>Col 1</Col>
        <Col order={1}>Col 2</Col>
    </Row>
    <Row order={{xs: 1, lg: 2}}>
        <Col order={{xs: 1, lg: 2}}>Col 1</Col>
        <Col order={{xs: 2, lg: 1}}>Col 2</Col>
    </Row>
</Container>

The order can be defined for Cols and Rows and orders the components in its parent container. Components without an order will be rendered as if they had an order of 0. If you define an object you can define an order for every screen in seperate

Alignment

<Container>
    <Row align="center" justify="center">
        <Col align="flex-end" justify="space-between">Col 1</Col>
        <Col align="flex-end" justify="space-between">Col 2</Col>
    </Row>
    <Row align={{xs: 'flex-start', lg: 'center'}} justify={{xs: 'flex-start', lg: 'center'}}>
        <Col align={{xs: 'flex-start', lg: 'center'}} justify={{xs: 'space-between', lg: 'space-around'}}>Col 1</Col>
        <Col align={{xs: 'flex-start', lg: 'center'}} justify={{xs: 'space-between', lg: 'space-around'}}>Col 2</Col>
    </Row>
</Container>

The alignment of rows and columns can be set with the align and justify props. Both use the flex css api so all values valid for thoose are also valid here. If you define an object you can set different alignments for different screens.

Changing directions

<Container>
    <Row direction={{xs: 'column', lg: 'row'}} isReverse>
        <Col isReverse>Col 1</Col>
        <Col isReverse={['xs', 'md']}>Col 2</Col>
    </Row>
    <Row isReverse={['xs', 'md']}>
        <Col direction="row" isReverse>Col 1</Col>
        <Col direction={{xs: 'column', lg: 'row'}} isReverse={['xs', 'md']}>Col 2</Col>
    </Row>
</Container>

You can change the directions of rows and columns. Rows can be reversed if needed. If you give an array you can define in which screen sizes the row should be reversed. Columns have also and direction prop. Its usefull if you want to render column children layed out as row. You can also define an object to change direction for different screen sizes.

Spacer usage

<Container>
    <Row isReverse>
        <Col>Col 1</Col>
        <Spacer x={2}>
        <Col>Col 2</Col>
    </Row>
    <Row isReverse={['xs', 'md']}>
        <Col direction="row" isReverse>Col 1</Col>
        <Spacer maxX={2} maxY={2} x={{xs: 0, lg: 2}} y={{xs: 2, lg: 0}}>
        <Col direction={{xs: 'column', lg: 'row'}} isReverse={['xs', 'md']}>Col 2</Col>
    </Row>
</Container>

The Spacer component helps to set spacings like designers do and has some logic for dynamic flex spacings and so on. Its the best way to define spacings in an grid conform matter.

Skeleton usage

<Container>
    <Row>
        <Col>
            <h1><Skeleton isLoading>This is text</Skeleton></h1>
        </Col>
        <Col>
            <p><Skeleton group="one" isLoading>This is text</Skeleton></p>
        </Col>
        <Col>
            <p><Skeleton group="one" isCircle isLoading>This is text</Skeleton></p>
        </Col>
    </Row>
</Container>

The Skeleton component is an easy way to render a skeleton of an component. It can be used to render an skeleton of an component while its loading. It renders its children only if isLoading is false. If it is true it renders an skeleton loader. The skeleton loader can be configured with the config object. (See Custom config) The Skeleton has some usefull properties to define its appearance and its behavior. You can group Skeletons to have an slightly delayed loading effect between different skeletons of the same group. You can also define if the skeleton should be a circle or not. The skeleton will then render an circle instead of a rectangle.

!NOTE

For the skeletons to work you need to wrap your app in the ScreenSizeProvider.

Usage of Hidden and Visible

<Container>
    <Visible xs sm md>
        <Row>
            <Col>Col 1</Col>
            <Col>Col 2</Col>
        </Row>
    </Visible>
    <Hidden xs sm md>
        <Row>
            <Col>Col 1</Col>
            <Col>Col 2</Col>
        </Row>
    </Hidden>
</Container>

Sometimes you want to hide something on specific screens. Or show something only on some screens. Thats the usecase for Visible and Hidden. Both can get screensizes as props. The screen sizes define when the components apply their logic for hiding and showing.

Usage of ResponsiveText

<Container>
    <Row>
        <Col>
            <ResponsiveText
                xs="Extra Small Screen Text"
                sm="Small Screen Text"
                md="Medium Screen Text"
                lg="Large Screen Text"
                xl="Extra Large Screen Text"
                xxl="Extra Extra Large Screen Text"
            />
            <ResponsiveText
                xs="Extra Small Screen Text"
                lg="Large Screen Text"
                xxl="Extra Extra Large Screen Text"
            />
        </Col>
    </Row>
</Container>

Sometimes you need to render different text snippets depending on your screensize. Thats an usecase for ResponsiveText.
This component can get an text for every screensize and renders it accordingly. As it works also mobile first you need to only set the smallest defined breakpoints and the other screensizes will use the same text as this breakpoint or the text thats used for the next smallest given size.


Props

Container

ProptyperequiredDescription
asstring (ElementType)Sets the html element type of the container. If you overwrite its styles with styled() it has to be forwardedAs.
classNamestringClassname property to overwrite styles with styled(Container)
hasNoPaddingBreakpoints[]|booleanDetermines if the container has an padding. (Should always be set if the container has an container as parent already). It takes an array ofBreakpoints or a boolean value.
isFluidBreakpoints[]|booleanMakes the container fluid. (Should always be set if the container has an container as parent already). It takes an array ofBreakpoints or a boolean value.
maxWidthSizesObject|numberSets the max width of the container. It takes aSizesObject or a number value. Its intended for usage with isFluid.
testIdstringTestId for cypress testing. (If applicable.) Can than be selected with data-cy="testId"
onClick(event: MouseEvent\<HTMLElement>) => voidA function that will be invoked when the user clicks the element with the mouse.
onContextMenu(event: MouseEvent\<HTMLElement>) => voidA function that will be invoked when the user right-clicks the element with the mouse.
onDoubleClick(event: MouseEvent\<HTMLElement>) => voidA function that will be invoked when the user double-clicks the element with the mouse.
onDrag(event: MouseEvent\<HTMLElement>) => voidA function that will be invoked when the user starts dragging the element with the mouse.
onDragEnd(event: MouseEvent\<HTMLElement>) => voidA function that will be invoked when the user stops dragging the element with the mouse.
onDragEnter(event: MouseEvent\<HTMLElement>) => voidA function that will be invoked when the user drags another element over this element.
onDragLeave(event: MouseEvent\<HTMLElement>) => voidA function that will be invoked when the user stops dragging another element over this element.
onDragOver(event: MouseEvent\<HTMLElement>) => voidA function that will be invoked when the user is dragging another element over this element.
onDrop(event: MouseEvent\<HTMLElement>) => voidA function that will be invoked when the user is dropping another element on this element.
onMouseDown(event: MouseEvent\<HTMLElement>) => voidA function that will be invoked when the user presses a mouse button over the element.
onMouseEnter(event: MouseEvent\<HTMLElement>) => voidA function that will be invoked when the mouse cursor enters the element.
onMouseLeave(event: MouseEvent\<HTMLElement>) => voidA function that will be invoked when the mouse cursor leaves the element.
onMouseMove(event: MouseEvent\<HTMLElement>) => voidA function that will be invoked when the mouse cursor moves over the element.
onMouseOut(event: MouseEvent\<HTMLElement>) => voidA function that will be invoked when the mouse cursor moves out of the element.
onMouseOver(event: MouseEvent\<HTMLElement>) => voidA function that will be invoked when the mouse cursor moves over the element.
onMouseUp(event: MouseEvent\<HTMLElement>) => voidA function that will be invoked when the user releases a mouse button over the element.
onScroll(event: UIEvent\<HTMLElement>) => voidA function that will be invoked when the user scrolls inside this element.

Row

ProptyperequiredDescription
alignAlignObject|FlexAlignDefines the content alignment of the row. It takes aAlignObject or a FlexAlign type value. Its direction is dependent on the direction prop.
asstring (ElementType)Sets the html element type of the row. If you overwrite its styles with styled() it has to be forwardedAs.
classNamestringClassname property to overwrite styles with styled(Row)
directionDirectionObject|FlexDirectionSets the direction the row children should render ('row' or 'column'). It takes aDirectionObject or a FlexDirection type value.
hasNoGapFlexGap|GapObjectRemoves the gap between columns in the grid. Can be also no-column and no-row which deactivates the gap for either direction. It takes aFlexGap or a GapObject type value.
hasNoWrapBreakpoints[]|booleanDefines if the row will wrap or not. It takes an array ofBreakpoints or a boolean value.
isReverseBreakpoints[]|booleanReverses the direction of the row. It takes an array ofBreakpoints or a boolean value.
justifyFlexJustify|JustifyObjectDefines the content justification of the row. It takes aFlexJustify or a JustifyObject type value. Its direction is dependent on the direction prop.
orderOrderObject|numberSets the order this row should be in. It takes anOrderObject or a number value.
testIdstringTestId for cypress testing. (If applicable.) Can than be selected with data-cy="testId"
onClick(event: MouseEvent\<HTMLElement>) => voidA function that will be invoked when the user clicks the element with the mouse.
onContextMenu(event: MouseEvent\<HTMLElement>) => voidA function that will be invoked when the user right-clicks the element with the mouse.
onDoubleClick(event: MouseEvent\<HTMLElement>) => voidA function that will be invoked when the user double-clicks the element with the mouse.
onDrag(event: MouseEvent\<HTMLElement>) => voidA function that will be invoked when the user starts dragging the element with the mouse.
onDragEnd(event: MouseEvent\<HTMLElement>) => voidA function that will be invoked when the user stops dragging the element with the mouse.
onDragEnter(event: MouseEvent\<HTMLElement>) => voidA function that will be invoked when the user drags another element over this element.
onDragLeave(event: MouseEvent\<HTMLElement>) => voidA function that will be invoked when the user stops dragging another element over this element.
onDragOver(event: MouseEvent\<HTMLElement>) => voidA function that will be invoked when the user is dragging another element over this element.
onDrop(event: MouseEvent\<HTMLElement>) => voidA function that will be invoked when the user is dropping another element on this element.
onMouseDown(event: MouseEvent\<HTMLElement>) => voidA function that will be invoked when the user presses a mouse button over the element.
onMouseEnter(event: MouseEvent\<HTMLElement>) => voidA function that will be invoked when the mouse cursor enters the element.
onMouseLeave(event: MouseEvent\<HTMLElement>) => voidA function that will be invoked when the mouse cursor leaves the element.
onMouseMove(event: MouseEvent\<HTMLElement>) => voidA function that will be invoked when the mouse cursor moves over the element.
onMouseOut(event: MouseEvent\<HTMLElement>) => voidA function that will be invoked when the mouse cursor moves out of the element.
onMouseOver(event: MouseEvent\<HTMLElement>) => voidA function that will be invoked when the mouse cursor moves over the element.
onMouseUp(event: MouseEvent\<HTMLElement>) => voidA function that will be invoked when the user releases a mouse button over the element.
onScroll(event: UIEvent\<HTMLElement>) => voidA function that will be invoked when the user scrolls inside this element.

Col

ProptyperequiredDescription
alignAlignObject|FlexAlignDefines the content alignment of the column. It takes aAlignObject or a FlexAlign type value. Its direction is dependent on the direction prop.
asString (ElementType)Sets the html element type of the column. If you overwrite its styles with styled() it has to be forwardedAs.
classNamestringClassname property to overwrite styles with styled(Col).
directionDirectionObject|FlexDirectionSets the direction the column children should render ('row' or 'column'). It takes aDirectionObject or a FlexDirection type value.
isReverseBreakpoints[]|booleanReverses the direction of the column. It takes an array ofBreakpoints or a boolean value.
justifyFlexJustify|JustifyObjectDefines the content justification of the column. It takes aFlexJustify or a JustifyObject type value. Its direction is dependent on the direction prop.
offsetOffsetObject|numberSets the number of columns this column should offset to the left. It takes anOffsetObject or a number value.
orderOrderObject|numberSets the order this column should be in. It takes anOrderObject or a number value.
paddingPadding|PaddingObjectSets the padding added to both sides of the column. It takes aPadding or a PaddingObject type value.
paddingLeftPadding|PaddingObjectSets the padding added to the left side of the column (Gets overwritten by padding). It takes aPadding or a PaddingObject type value.
paddingRightPadding|PaddingObjectSets the padding added to the right side of the column (Gets overwritten by padding). It takes aPadding or a PaddingObject type value.
Your defined Breakpointsnumber|StringSizesSets the number of columns the col takes in width on your defined screensizes. (Can also be auto, max-content, min-content).
testIdstringTestId for cypress testing. (If applicable.) Can than be selected with data-cy="testId"
onClick(event: MouseEvent\<HTMLElement>) => voidA function that will be invoked when the user clicks the element with the mouse.
onContextMenu(event: MouseEvent\<HTMLElement>) => voidA function that will be invoked when the user right-clicks the element with the mouse.
onDoubleClick(event: MouseEvent\<HTMLElement>) => voidA function that will be invoked when the user double-clicks the element with the mouse.
onDrag(event: MouseEvent\<HTMLElement>) => voidA function that will be invoked when the user starts dragging the element with the mouse.
onDragEnd(event: MouseEvent\<HTMLElement>) => voidA function that will be invoked when the user stops dragging the element with the mouse.
onDragEnter(event: MouseEvent\<HTMLElement>) => voidA function that will be invoked when the user drags another element over this element.
onDragLeave(event: MouseEvent\<HTMLElement>) => voidA function that will be invoked when the user stops dragging another element over this element.
onDragOver(event: MouseEvent\<HTMLElement>) => voidA function that will be invoked when the user is dragging another element over this element.
onDrop(event: MouseEvent\<HTMLElement>) => voidA function that will be invoked when the user is dropping another element on this element.
onMouseDown(event: MouseEvent\<HTMLElement>) => voidA function that will be invoked when the user presses a mouse button over the element.
onMouseEnter(event: MouseEvent\<HTMLElement>) => voidA function that will be invoked when the mouse cursor enters the element.
onMouseLeave(event: MouseEvent\<HTMLElement>) => voidA function that will be invoked when the mouse cursor leaves the element.
onMouseMove(event: MouseEvent\<HTMLElement>) => voidA function that will be invoked when the mouse cursor moves over the element.
onMouseOut(event: MouseEvent\<HTMLElement>) => voidA function that will be invoked when the mouse cursor moves out of the element.
onMouseOver(event: MouseEvent\<HTMLElement>) => voidA function that will be invoked when the mouse cursor moves over the element.
onMouseUp(event: MouseEvent\<HTMLElement>) => voidA function that will be invoked when the user releases a mouse button over the element.
onScroll(event: UIEvent\<HTMLElement>) => voidA function that will be invoked when the user scrolls inside this element.

Spacer

ProptyperequiredDescription
isInlineBreakpoints[]|booleanWhether the spacer should behave like an inline element. If true, the spacer will behave like a regular inline element and not create a new line. If false, the spacer will behave like a block element and create a new line. It takes an array ofBreakpoints or a boolean value.
isNotStretchingBreakpoints[]|booleanWhether the spacer should not stretch. If true, the spacer will not stretch to fill its container if its an flex container. If false, the spacer will stretch to fill its container. It takes an array ofBreakpoints or a boolean value.
maxXSpacerObject|numberThe maximum horizontal spacing size in a flex container. This is a multiplication factor that is used with the baseSpacing property of the theme to calculate the maximum horizontal spacing value. It takes an SpacerObject or a number value.
maxYSpacerObject|numberThe maximum vertical spacing size in a flex container. This is a multiplication factor that is used with the baseSpacing property of the theme to calculate the maximum vertical spacing value. It takes an SpacerObject or a number value.
testIdstringTestId for cypress testing. (If applicable.) Can than be selected with data-cy="testId"
xSpacerObject|numberThe horizontal spacing size. In a flex container, this defines the base horizontal spacing value and is a multiplication factor that is used with the baseSpacing property of the theme to calculate the horizontal spacing value. In a non-flex container, this defines the actual horizontal spacing value in pixels. It takes an SpacerObject or a number value.
ySpacerObject|numberThe vertical spacing size. In a flex container, this defines the base vertical spacing value and is a multiplication factor that is used with the baseSpacing property of the theme to calculate the vertical spacing value. In a non-flex container, this defines the actual vertical spacing value in pixels. It takes an SpacerObject or a number value.

Skeleton

ProptyperequiredDescription
isCircleBooleanSet to true to set the border-radius to 50% to get an circular shape.
classNameStringTo override the current styles the Skeleton does not have options for.
countNumberDefines the number of skeleton lines the component should render
groupStringTo apply the delay on your skeleton use this property to add it to an delay group
heightHeight|NumberSets the height of the skeleton.
isLoadingBooleanSet to true to hide the child only on screen size xxl.
testIdStringTestId for cypress testing. (If applicable.) Can than be selected with data-cy="testId"
widthWidth|NumberSets the width of the sceleton

Hidden

ProptyperequiredDescription
Your defined BreakpointsBooleanSet to true to hide the child only on the defined screen size
isLoadingHtmlBooleanSet to true to render the HTML even when the component is hidden.

Visible

ProptyperequiredDescription
Your defined BreakpointsBooleanSet to true to show the child only on the defined screen size.
isLoadingHtmlBooleanSet to true to render the HTML even when the component is hidden.

ResponsiveText

ProptyperequiredDescription
Your defined BreakpointsString:white_check_mark: (only the smallest breakpoint)Shows the given text on the appropreate screen size.

Types

Breakpoints

Your breakpoints are defined in the config under the key breakpoints. This is the default.

type Breakpoints = 'xs' | 'sm' | 'md' | 'lg' | 'xl' | 'xxl';

FlexAlign

type FlexAlign = 'baseline' | 'center' | 'flex-end' | 'flex-start' | 'inherit' | 'initial' | 'stretch' | 'unset';

AlignObject

interface AlignObject {
    xs?: FlexAlign;
    sm?: FlexAlign;
    md?: FlexAlign;
    lg?: FlexAlign;
    xl?: FlexAlign;
    xxl?: FlexAlign;
}

FlexDirection

type FlexDirection = 'column' | 'row';

DirectionObject

interface DirectionObject {
    xs?: FlexDirection;
    sm?: FlexDirection;
    md?: FlexDirection;
    lg?: FlexDirection;
    xl?: FlexDirection;
    xxl?: FlexDirection;
}

FlexGap

type FlexGap = boolean | 'no-column' | 'no-row';

GapObject

interface GapObject {
    xs?: FlexGap;
    sm?: FlexGap;
    md?: FlexGap;
    lg?: FlexGap;
    xl?: FlexGap;
    xxl?: FlexGap;
}

FlexJustify

type FlexJustify = 'center' | 'flex-end' | 'flex-start' | 'inherit' | 'initial' | 'space-around' | 'space-between' | 'space-evenly' | 'unset';

JustifyObject

interface JustifyObject {
    xs?: FlexJustify;
    sm?: FlexJustify;
    md?: FlexJustify;
    lg?: FlexJustify;
    xl?: FlexJustify;
    xxl?: FlexJustify;
}

OrderObject

interface OrderObject {
    xs?: number;
    sm?: number;
    md?: number;
    lg?: number;
    xl?: number;
    xxl?: number;
}

OffsetObject

interface OffsetObject {
    xs?: number;
    sm?: number;
    md?: number;
    lg?: number;
    xl?: number;
    xxl?: number;
}

Padding

type PaddingExt = 'em' | 'px' | 'rem' | 'vh' | 'vw';
type Padding = `${number}${PaddingExt}`;

PaddingObject

interface PaddingObject {
    xs?: Padding;
    sm?: Padding;
    md?: Padding;
    lg?: Padding;
    xl?: Padding;
    xxl?: Padding;
}

StringSizes

type StringSizes = 'auto' | 'max-content' | 'min-content';

SpacerObject

interface SpacerObject {
    xs?: number;
    sm?: number;
    md?: number;
    lg?: number;
    xl?: number;
    xxl?: number;
}

Height

type HeightExt = 'em' | 'px' | 'rem' | 'vh' | 'vw' | '%';
type Height = `${number}${HeightExt}`;

Width

type WidthExt = 'em' | 'px' | 'rem' | 'vh' | 'vw' | '%';
type Width = `${number}${WidthExt}`;

Utilities

media

const DemoComponent = styled.div`
    ${media('xs')} {
        padding-top: 2rem;
    }
`;

Creates a CSS media query using the given breakpoint and styled-components theme.

mediaBetween

const DemoComponent = styled.div`
    ${mediaBetween('xs', 'md')} {
        padding-top: 2rem;
    }
`;

The same as media but it creates an media query between two breakpoints.

spacing

const DemoComponent = styled.div`
    padding: ${spacing(2)}; /* Yields 2 * baseSpacing */
`;

Generates a CSS value for a given spacing value, based on the current grid configuration in the theme.

This function uses the base spacing value from the grid configuration to convert the input space value to rem. The base spacing value is defined in baseSpacing key of your configuration object, and represents the base spacing unit for the grid system.

ScreenSizeProvider

class App extends Component {
    render() {
        return (
            <ThemeProvider theme={theme}>
                <ScreenSizeProvider>
                    <AppPage />
                </ScreenSizeProvider>
            </ThemeProvider>
        )
    }
}

A component that provides the current screen size to its children via context. Its also needed to for the Skeleton component to work. It has to be used in the root of your app.


Hooks

useScreenSize

const DemoComponent = () => {
    const screenSize = useScreenSize();
}

The useScreenSize hook gives the actual breakpoint defined in your config as string back. (xs, sm, md, lg, xl or xxl) The ScreenSizeProvider has to be used in the root of your app for this hook to work.


Debugging

For debugging there are two helpfull things. First you can see the current breakpoint anytime if you use the ScreenBadge component.

class App extends Component {
    render() {
        return (
            <div>
                <ScreenSizeProvider>
                    <AppPage />
                    <ScreenBadge>
                </ScreenSizeProvider>
            </div>
        )
    }
}

This component renders always on top in the lower right corner and shows the screen class your in currently.

Also if you want to see your grid you can toggle debug mode with this keybord shortcut.

Windows: Strg + D

Mac Os: Ctrl + D

This Shortcut only works if your build is in development mode. On production builds this feature is deactivated.


Custom config

There are many options you can define for the Grid. To give you an overview and show the actual defaults here the complete object:

const breakpoints = ['xs', 'sm', 'md', 'lg', 'xl', 'xxl'] as const;
createConfig(breakpoints, {
    baseSpacing: 0.5,
    breakpoints: {
        xs: 0,
        sm: 576,
        md: 769,
        lg: 992,
        xl: 1200,
        xxl: 1600
    },
    columnGap: {
        xs: 20,
        sm: 20,
        md: 20,
        xl: 20,
        lg: 20,
        xxl: 20
    },
    columns: {
        xs: 12,
        sm: 12,
        md: 12,
        lg: 12,
        xl: 12,
        xxl: 12
    },
    container: {
        xs: 'fluid',
        sm: 'fluid',
        md: 'fluid',
        lg: 1440,
        xl: 1440,
        xxl: 1440
    },
    containerPadding: {
        xs: 10,
        sm: 10,
        md: 10,
        lg: 10,
        xl: 10,
        xxl: 10
    },
    debug: {
        col: {
            background: '#9a67cb',
            outline: '#ffffff',
            padding: '#c2cf8a'
        },
        container: {
            background: '#5901ad40',
            outline: '#ffffff',
            padding: '#c2cf8a'
        },
        row: {
            background: '#5901ad40',
            outline: '#ffffff',
            padding: '#c2cf8a'
        },
        spacer: {
            background: '#f9cc9d',
            outline: '#ffffff',
            padding: '#c2cf8a'
        }
    },
    skeleton: {
        dark: {
            animation: {
                delay: 0.02,
                direction: 'ltr',
                duration: 1.8
            },
            borderRadius: 0.4,
            colors: {
                base: 'rgba(0, 0, 102, 0.3)',
                baseHighlight: 'rgba(0, 0, 102, 0)',
                highlight: 'rgba(0, 0, 102, 0.3)'
            }
        },
        light: {
            animation: {
                delay: 0.02,
                direction: 'ltr',
                duration: 1.8
            },
            borderRadius: 0.4,
            colors: {
                base: 'rgba(255, 255, 255, 0.3)',
                baseHighlight: 'rgba(0, 0, 102, 0)',
                highlight: 'rgba(0, 0, 102, 0.3)'
            }
        }
    },
    skeletonDefault: 'dark'
};

Configuration Object

If you dont define one or more of the screen sizes the defaults will get used for these.

baseSpacing

Defines the spacing factor to use for the spacer component. Its defined in rem. The default is 0.5 rem.

breakpoints

Defines the breakpoint values. For the different screen sizes in px. (If you use default breakpoints its completely optional. Should you define breakpoints that are not in the defaults this becomes madatory.)

columnGap

This defines the width of the actual gap between each column.

columns

Defines the number of columns for each screen size.

container

The container sizes for the different screens. If you use fluid the container will be 100% width. (This option is required for all your defined breakpoints.)

containerPadding

Defines the outer padding of the container.

debug

Defines the colors used for debug mode. You can define them for containers, rows, columns and spacers. Columns have also an padding color to show you the gutter gap.

skeleton

This defines the behavior and styling of the skeleton component. Its possible to define custom variants. The default variants are an dark and light variant. You can define your own variants and use them in the skeleton component. If you change the default variants you have to define a default variant.

skeletonDefault

Defines the default variant for the skeleton component.


Contributions

Christoph Kruppe - c.kruppe@nfq.de
Max Heuschneider - m.heuschneider@nfq.de
Michael Fest - m.fest@nfq.de

This is inspired by santosfrancisco/react-awesome-styled-grid


License

The licence used is: GitHub license
Click on licence badge for licence details.


Questions

If you have any furter questions please contact me.

7.0.0

11 months ago

6.1.0-beta2

1 year ago

6.1.0-beta3

1 year ago

6.1.0-beta

1 year ago

6.0.1

1 year ago

6.0.0

1 year ago

5.2.3

2 years ago

5.2.2

2 years ago

5.2.1

2 years ago

5.2.0

2 years ago

5.1.1

2 years ago

5.1.0

2 years ago

5.0.0

2 years ago

4.8.8

2 years ago

4.8.7

2 years ago

4.8.6

2 years ago

4.8.4

2 years ago

4.8.3

2 years ago

4.8.1

2 years ago

4.8.0

2 years ago

4.8.2

2 years ago

4.7.1

2 years ago

3.2.0

2 years ago

4.4.0

2 years ago

4.0.0

2 years ago

3.1.3

2 years ago

3.1.2

3 years ago

3.1.5

2 years ago

3.1.4

2 years ago

4.3.2

2 years ago

4.7.0

2 years ago

4.6.0

2 years ago

4.2.1

2 years ago

4.2.0

2 years ago

4.5.0

2 years ago

4.5.1

2 years ago

4.1.0

2 years ago

3.1.1

3 years ago

3.0.2

3 years ago

3.0.1

3 years ago

3.0.0

3 years ago

3.1.0

3 years ago

2.13.0

3 years ago

2.13.1

3 years ago

2.12.1

3 years ago

2.11.0

3 years ago

2.2.7

3 years ago

2.2.6

3 years ago

2.2.9

3 years ago

2.2.8

3 years ago

2.2.3

4 years ago

2.2.2

4 years ago

2.2.5

4 years ago

2.2.4

4 years ago

2.2.1

4 years ago

2.0.3

4 years ago

2.2.0

4 years ago

2.0.2

4 years ago

2.0.1

4 years ago

2.1.0

4 years ago

1.8.0

4 years ago

2.0.0

4 years ago

1.7.2

4 years ago

1.7.1

4 years ago

1.7.0

4 years ago

1.5.0

4 years ago

1.4.1

4 years ago

1.4.0

4 years ago

1.2.0

4 years ago

1.2.8

4 years ago

1.2.7

4 years ago

1.2.6

4 years ago

1.2.5

4 years ago

1.2.4

4 years ago

1.2.3

4 years ago

1.2.2

4 years ago

1.2.1

4 years ago

1.2.10

4 years ago

1.2.11

4 years ago

1.3.2

4 years ago

1.3.1

4 years ago

1.3.0

4 years ago

1.1.1

4 years ago

1.1.0

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago

1.0.20-alpha

4 years ago

1.0.19-alpha

4 years ago

1.0.18-alpha

4 years ago

1.0.17-alpha

4 years ago

1.0.16-alpha

4 years ago

1.0.15-alpha

4 years ago

1.0.14-alpha

4 years ago

1.0.13-alpha

4 years ago

1.0.12-alpha

4 years ago

1.0.11-alpha

4 years ago

1.0.10-alpha

4 years ago

1.0.9-alpha

5 years ago

1.0.8-alpha

5 years ago

1.0.7-alpha

5 years ago

1.0.6-alpha

5 years ago

1.0.5-alpha

5 years ago

1.0.4-alpha

5 years ago

1.0.3-alpha

5 years ago

1.0.2-alpha

5 years ago

1.0.1-alpha

5 years ago

1.0.0-alpha

5 years ago

0.0.14

5 years ago

0.0.13

5 years ago

0.0.12

5 years ago

0.0.11

5 years ago

0.0.10

5 years ago

0.0.9

5 years ago

0.0.8

5 years ago

0.0.7

5 years ago

0.0.6

5 years ago

0.0.5

5 years ago

0.0.4

5 years ago

0.0.3

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago