3.0.0 • Published 4 years ago

use-styled-system v3.0.0

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

use-styled-system

A custom React Hook to help you implement "styled system props" in combination with styled-jsx for your application.

How it works

use-styled-system provides a custom hook to inject all common styled-system props into <style jsx> tags. Its especially made to work seamless within any NextJs environment. The hook is completely build with Typescript and provides excellent support for all CSS props, including proper scoping.

Currently use-styled-system uses Javascript and .matchMedia media queries and global context for responsive css. The syntax is the same as with styled system. i.e.

<Box as='div' p={['20px 10px', 60]}>...</Box>

translates into the following for matchMedia below the first breakpoint ie. 600px

<div class="jsx-123">
...
</div>
<style>
   .jsx-123 {
       padding: 20px 10px
   }
</style>

and automatically changes to the below once the next media query is triggered.

<div class="jsx-123">
...
</div>
<style>
   .jsx-123 {
       padding: 60px
   }
</style>

Roadmap

In next versions the focus will be on adding support for Pseudo Selectors, CSS based Media Queries and autoprefix injection.

Requirement

To use use-styled-system, you must use styled-jsx@3.0.0 and react@16.8.0 or greater which includes Hooks (both shipped with Nextjs).

Installation

$ npm i use-styled-system

Usage

The usage within a NextJs environment works out of the box. Any other environment needs react & styled-jsx as a peer dependency. Refer to their docs for more information.

/* import only the Types that you need */
import { FC } from 'react';
import { Decor, Layout, Space, useStyledSystem } from 'use-styled-system';

export const Box: FC<Space & Layout & Decor> = (props) => {
  
  const { styleJsx, nonCssProps } = useStyledSystem(props, { Space: true, Layout: true, Decor: true });
  
  return <>
      <div {...nonCssProps}>{props.children}</div>
      <style jsx>{`
        div {
          color: red;
          ${styleJsx}
        }
      `}</style>
  </>
}

use-styled-system returns an Abject with two items: 1. styleJSX which is a string containing all CSS properties and values 2. nonCssProps which is an Object with the previously added Props, filtered for any Css props.

Parameters

You pass useStyledSystem the props (or rest of props, using the ... spread operator) an optional Config object. The configuration object may contain the following keys.

KeyDescriptionTypeDefault
remBaseSets the base size to use rem properties wherever possible (defaults to 10px)number10
fontSizesSets the fontsizes shortcuts for common sizes. i.e.fontSize={3} equals to 20px(number)[]12, 14, 16, 20, 24, 32, 48, 64, 72
spaceSizesSets the space shortcuts for common sizes. i.e.marginBottom={3} equals to 16px(number)[]0, 4, 8, 16, 32, 64, 128, 256, 512
PaddingActivates Padding properties to be used as Propsbooleanfalse
MarginActivates Margin properties to be used as Propsbooleanfalse
SizeActivates Size properties to be used as Propsbooleanfalse
SpaceShortcut to activate Padding, Margin, and Size properties as Propsbooleanfalse
PositionActivates Position properties to be used as Propsbooleanfalse
FlexActivates Flex properties to be used as Propsbooleanfalse
GridActivates Grid properties to be used as Propsbooleanfalse
LayoutShortcut to activate Position, Flex, and Grid properties as Propsbooleanfalse
BorderActivates Border properties to be used as Propsbooleanfalse
ColorActivates Color properties to be used as Propsbooleanfalse
TypographyActivates Typography properties to be used as Propsbooleanfalse
DecorShortcut to activate Border, Color, and Typography properties as Propsbooleanfalse
OtherActivates Other properties to be used as Propsbooleanfalse
AllActivates all of the above properties to be used as Props. * Defaults to true if no other property is selectedbooleanfalse*

Return object

A useStyleSystem object is returned with the following properties.

KeyDescription
`styleJsx | string of all css properties
nonCssPropsProps filtered for any non CSS prop

Examples

Using style-jsx/css .resolve functionality to create custom elements.

import { createElement, FC } from 'react';
import { CSS, useStyledSystem } from 'use-styled-system';
import { css } from 'styled-jsx/css';

type BoxProps = {
  as?: 'h1' | 'h2' | 'h3' | 'h4' | 'h5' | 'h6' | 'p' | 'div' | 'code' | 'blockquote' | 'a'
  className?: string
  onClick?: Function
}

export const Text: FC<BoxProps & CSS> = ({ as = 'p', children, className = '', ...props }) => {
  const { styleJsx, nonCssProps } = useStyledSystem(props, { Decor: true, Space: true, Other: true });
  const { className: cssClass, styles } = css.resolve`${styleJsx}`;
  return <>
    {createElement(as, { className: `${cssClass} ${className}`, ...nonCssProps }, children)}
    {styles}
  </>;
};

export default Text;

use with styled-jsx-plugin-sass in a Link Component.

import { FC, MouseEvent } from 'react';
import NextLink from 'next/link';
import { Decor, Layout, Space, useStyledSystem } from 'use-styled-system';

type LinkProps = {
  onClick?: (event: MouseEvent) => void
  href: string
  title: string | JSX.Element
  target?: string
  className?: string
  secondary?: boolean
  small?: boolean
  large?: boolean
};

export const Link: FC<LinkProps & Space & Layout & Decor> = ({ onClick, className = '', href, target, title, secondary, small, large, children, ...props }) => {
  
  const { styleJsx, nonCssProps } = useStyledSystem(props, { Space: true, Layout: true, Decor: true });
  const classNames = `link ${secondary ? 'secondary' : ''} ${small ? 'small' : ''} ${large ? 'large' : ''} ${className}`.trim();
  return <>
    <NextLink href={href}>
      <a target={target} className={classNames} onClick={onClick} {...nonCssProps}>{title}</a>
    </NextLink>
    
    <style jsx>{`
      .link {
        cursor: pointer;
        color: var(--color-link);
        font-family: inherit;
        text-decoration: none;
        transition: color 0.25s, background-color 0.25s;

        &.small {
          font-size: 1.4rem;
        }

        &.large {
          font-size: 1.8rem;
        }
        
        &:hover {
          color: var(--color-link-hover)
        }

        ${styleJsx}
      }
    `}</style>
  </>;
};

export default Link;

License

MIT Licensed

3.0.0

4 years ago

2.3.0

4 years ago

2.2.17

4 years ago

2.2.18

4 years ago

2.2.16

4 years ago

2.2.19

4 years ago

2.2.28

4 years ago

2.2.29

4 years ago

2.2.26

4 years ago

2.2.27

4 years ago

2.2.24

4 years ago

2.2.25

4 years ago

2.2.22

4 years ago

2.2.23

4 years ago

2.2.20

4 years ago

2.2.21

4 years ago

2.2.31

4 years ago

2.2.32

4 years ago

2.2.30

4 years ago

2.2.15

4 years ago

2.2.14

4 years ago

2.2.13

4 years ago

2.2.11

4 years ago

2.2.12

4 years ago

2.2.10

4 years ago

2.2.9

4 years ago

2.2.8

4 years ago

2.2.7

4 years ago

2.2.4

4 years ago

2.2.3

4 years ago

2.2.2

4 years ago

2.2.1

4 years ago

2.2.0

4 years ago

2.1.20

4 years ago

2.1.19

4 years ago

2.1.18

4 years ago

2.1.17

4 years ago

2.1.16

4 years ago

2.1.15

4 years ago

2.1.13

4 years ago

2.1.12

4 years ago

2.1.9

4 years ago

2.1.10

4 years ago

2.1.11

4 years ago

2.1.8

4 years ago

2.1.7

4 years ago

2.1.4

4 years ago

2.1.6

4 years ago

2.1.5

4 years ago

2.1.3

4 years ago

2.1.2

4 years ago

2.1.1

4 years ago

2.1.0

4 years ago

2.0.12

4 years ago

2.0.11

4 years ago

2.0.10

4 years ago

2.0.9

4 years ago

2.0.7

4 years ago

2.0.8

4 years ago

2.0.5

4 years ago

2.0.6

4 years ago

2.0.3

4 years ago

2.0.4

4 years ago

2.0.2

4 years ago

2.0.1

4 years ago

2.0.0

4 years ago

1.4.0

4 years ago

1.3.13

4 years ago

1.3.10

4 years ago

1.3.11

4 years ago

1.3.12

4 years ago

1.3.7

4 years ago

1.3.9

4 years ago

1.3.8

4 years ago

1.3.6

4 years ago

1.3.4

4 years ago

1.3.3

4 years ago

1.3.2

4 years ago

1.3.1

4 years ago

1.3.0

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.0

4 years ago

1.1.9

4 years ago

1.1.8

4 years ago

1.1.7

4 years ago

1.1.6

4 years ago

1.1.5

4 years ago

1.1.4

4 years ago

1.1.3

4 years ago

1.1.2

4 years ago

1.1.1

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago

0.0.2

4 years ago

0.0.1

4 years ago