Rebass
Styled components v3
npm i rebass-next
TIP
If you're using Babel, use module-resolver
plugin and add an alias for rebass-next
.
"plugins": "module-resolver" "root": "./" "alias": "rebass": "rebass-next" "cwd": "babelrc"
and leave all imports as they were
import Flex Text from 'rebass'
Original repo
Rebass is a library of highly-composable, primitive UI components for React, built with styled-components to keep styles isolated and reduce the need to write custom CSS in your application. Based upon a configurable design system, Rebass‘s props API makes building consistent, responsive web apps simpler and faster.
Getting Started
import React from 'react'import Provider Heading Button from 'rebass' const App = <Provider> <Heading>Hello</Heading> <Button>Rebass</Button> </Provider>
Features
- Functional stateless UI components
- Style encapsulation with styled-components
- No external CSS dependencies
- Configurable theming
- Extensible base components
- Design-system based consistency
- Built for responsive web design
- Reduces the need to write custom CSS
Architectural Approach
Rebass is built around a component architectural approach inspired by Dan Abramov’s Presentational and Container Components, where presentational components are the only ones that encapsulate styles and contain no application logic, and container components do not contain any styles or DOM markup and handle all the application logic.
Rebass only contains presentational components, which means controlling things like progressive disclosure mechanisms or dropdown menus should be handled at a higher level in container components. Therefore, Rebass itself does not require any client-side JavaScript, is well suited to server-side rendering, and can fit into virtually any higher level application architecture.
See Patterns for Style Composition in React for more on some of the thought behind Rebass.
Base Props
All Rebass components are wrapped in a higher order component that handles design-system-based responsive style props using styled-system.
Width
// Numbers from 0–1 are converted to percentage widths// e.g. width 50%<Text = /> // Numbers greater than 1 are converted to pixels<Text = /> // Strings can be used for other values<Text ='32em' /> // Arrays can be used for mobile-first responsive styles<Text =/> // The shorthand `w` prop can be used instead of `width`<Text = />
Font Size
The fontSize
prop makes referencing steps on the typographic scale
simple and helps promote consistent design.
// Numbers are used to reference steps on the typographic scale// i.e. the `theme.fontSizes` array<Text = /> // Numbers greater than the length of the typographic scale// are converted to pixels<Text = /> // Strings can be used for other values<Text ='3em' /> // Arrays can be used for mobile-first responsive styles<Text = /> // The shorthand `f` prop can be used instead of `fontSize`<Text = />
Margin and Padding
The margin and padding props make referencing steps on the spacing scale
(i.e. the theme.space
array) simple and help promote consistency in
layout design without the need to add custom margin and padding declarations throughout an application.
The margin and padding props use a shorthand syntax.
Prop | Meaning |
---|---|
m |
margin |
mt |
margin-top |
mr |
margin-right |
mb |
margin-bottom |
ml |
margin-left |
mx |
margin-left and margin-right |
my |
margin-top and margin-bottom |
p |
padding |
pt |
padding-top |
pr |
padding-right |
pb |
padding-bottom |
pl |
padding-left |
px |
padding-left and padding-right |
py |
padding-top and padding-bottom |
// Numbers reference steps on the spacing scale// e.g. 8px<Text = /> // Numbers greater than the length of `theme.space.length` are converted to pixels<Text = /> // Negative values can be used to add negative margins<Text = /> // Strings can be used for other values<Text ='auto' /> // Arrays can be used for mobile-first responsive styles<Text = />
Colors
The color
and bg
props make using colors from the color palette simple to help promote design consistency.
// Keys reference values in the color palette object<Text ='blue' /> // Background color can be set with the `bg` prop<Button ='red' /> // Values that do not map to a key in `theme.colors` can be used<Button ='tomato' /> // Arrays can be used to change colors responsively<Text = />
Responsive Styles
All of the core props above accept arrays as values to set mobile-first responsive styles.
The first value is not scoped to a media query and applies to all breakpoints.
Each value after the first corresponds to a media query derived from theme.breakpoints
.
<Text =/>
is
Prop
Each component accepts an is
prop to change the underlying HTML element on a per-instance basis.
This is useful for ensuring semantic markup, while keeping styles decoupled.
<Heading ='h1' ='Top-level heading'/> <Button ='a' ='#!' ='Link Button'/>
Components
<Provider />
The <Provider />
component is a wrapper around styled-components' ThemeProvider.
It also sets a default font-family value based on theme.font
.
The Provider should be wrapped around a top-level component to ensure Rebass works as expected.
import React from 'react'import Provider from 'rebass'import Page from './Page' const App = <Provider> <Page /> </Provider>
You'll likely want to add additional global styles that set box-sizing: border-box
in your application.
This can be done with styled-components injectGlobal
:
import injectGlobal from 'styled-components' injectGlobal` * { box-sizing: border-box; } body { margin: 0; }`
UI Components
For an interactive demo of all Rebass components, see http://jxnblk.com/rebass
Component-Specific Props
Some components accept other props for styling.
<Text />
The <Text />
component, which is also the base for <Heading />
, <Subhead />
, <Lead />
, and <Small />
,
accepts several typographic style props.
left
(boolean) text-align: leftcenter
(boolean) text-align: centerright
(boolean) text-align: rightjustify
(boolean) text-align: justifybold
(boolean) font-weight: theme.weights[1]caps
(boolean) text-transform: uppercase; letter-spacing: .2em
<Border />
borderWidth
(number) pixel value for border widthtop
(boolean) border-topright
(boolean) border-rightbottom
(boolean) border-bottomleft
(boolean) border-leftcolor
(string) sets only the border color
<Container />
maxWidth
(number) the maximum width of the container element
<NavLink />
, <TabItem />
, <DotButton />
active
(boolean) adjusts style for an active state
<Banner />
backgroundImage
(string) URL for a background image
<BackgroundImage />
ratio
(number) converted into a percentage to maintain aspect ratio
<Avatar />
size
(number) pixel width and height
<Fixed />
and <Absolute />
Both components accept props to control positioning. The margin and padding props can be used to control distance from the edge of a container.
top
(boolean) top: 0right
(boolean) right: 0bottom
(boolean) bottom: 0left
(boolean) left: 0z
(number) z-index
Grid Styled
For convenience, the Grid Styled <Flex />
and <Box />
components are included in Rebass
to handle most page layout needs.
import Flex Box from 'rebass'
Configuration
Rebass’s core design system includes breakpoints, a spacing scale,
a typographic scale, fonts, font weights, border radius, and a color palette –
all of which can be configured with the <Provider />
component.
import React from 'react'import Provider from 'rebass' const theme = breakpoints: // min-width breakpoints in ems 40 52 64 space: 0 6 12 18 24 30 36 fontSizes: 12 16 18 24 36 48 72 weights: 400 600 colors: black: '#111' white: '#fff' blue: '#07c' font: 'Georgia, serif' monospace: '"Roboto Mono", Menlo, monospace' radius: 2 const App = <Provider => <Heading>Hello</Heading> </Provider>
Customizing Components
Rebass components can be completely customized using styled-components.
import styled from 'styled-components'import Button from 'rebass' const CustomButton = ` border: 1px solid rgba(0, 0, 0, 0.25); background-image: linear-gradient(transparent, rgba(0, 0, 0, 0.125)); box-shadow: 0 0 4px rgba(0, 0, 0, 0.25);`
Server-Side Rendering
Rebass uses styled-components for styling. The styled-components documentation explains how to handle Server-Side Rendering.