Code Monkey home page Code Monkey logo

components's Introduction

@rebass/components

Create consistent styled-system based React UI components (formerly system-components)

Built with styled-system, with support for styled-components & emotion

Build Status

import system from '@rebass/components'

// creates a Box with default props tied to your theme
const Box = system({
  p: 2,
  bg: 'blue'
}, 'space', 'color')

Or, to use with emotion:

import system from '@rebass/components/emotion'

Usage

To create a styled-component with default props that hook into styled-system props, pass a plain object as the first argument to the system function and pass the names of the styled-system functions as arguments after.

const Card = system({
  px: 2,
  py: 3,
  border: '1px solid',
  borderColor: 'lightGray',
  borderRadius: 2
}, 'space', 'borders', 'borderColor', 'borderRadius')

@rebass/components automatically adds prop type definitions and removes style props from the underlying HTML element.

See the styled-system docs for a complete list of the available style functions.

Add style props without defaultProps

@rebass/components can also be created with styled-system props without defining defaultProps.

const Box = system(
  'space',
  'width',
  'color'
)

This allows for style props to be passed to the component instance:

<Box
  width={1/2}
  px={3}
  py={4}
  bg='blue'
/>

Using custom functions

Custom style functions can be passed as an argument.

const Box = system(
  props => ({
    height: props.height
  })
)

Changing the underlying HTML element

@rebass/components default to using a <div> as the HTML element. To change the HTML element use the is prop.

const Heading = system({
  is: 'h2',
  m: 0,
  fontSize: 6
}, 'space', 'fontSize')

Since is is a prop, it can also be passed to the element when used. This is useful for one-off changes to ensure semantic markup.

<Heading is='h1'>
  Hello
</Heading>

Extending components

To extend another component, use the extend prop in your component definition.

const Text = system({
  fontSize: 2,
}, 'fontSize')

const Bold = system({
  extend: Text,
  fontWeight: 'bold'
}, 'fontWeight')

CSS prop

To add one-off custom CSS to any @rebass/component, use the css prop.

<Heading css='opacity:0.75;'>
  Hello
</Heading>

The css prop can also accept object literal syntax.

<Heading
  css={{
    opacity: 3/4
  }}>
  Hello
</Heading>

MIT License

components's People

Contributors

jxnblk avatar

Watchers

James Cloos avatar Kiko Beats avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.