Functional React UI component library, built with styled-components
npm i rebass@next
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.
import React from 'react'
import { Provider, Heading, Button } from 'rebass'
const App = props => (
<Provider>
<Heading>Hello</Heading>
<Button>Rebass</Button>
</Provider>
)
- Style encapsulation with styled-components
- No external CSS dependencies
- Configurable theming
- Design-system based consistency
- Built for responsive web design
- Reduces the need to write custom CSS
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.
All Rebass components are wrapped in a higher order component that handles design-system-based responsive style props using styled-system.
// Numbers from 0–1 are converted to percentage widths
// e.g. width 50%
<Text width={1/2} />
// Numbers greater than 1 are converted to pixels
<Text width={256} />
// Strings can be used for other values
<Text width='32em' />
// Arrays can be used for mobile-first responsive styles
<Text
width={[
1, // 100% width at the smallest breakpoint
1/2, // 50% width at the next breakpoint
1/4 // 25% width at the next
]}
/>
// The shorthand `w` prop can be used instead of `width`
<Text w={1/2} />
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 fontSize={3} />
// Numbers greater than the length of the typographic scale
// are converted to pixels
<Text fontSize={18} />
// Strings can be used for other values
<Text fontSize='3em' />
// Arrays can be used for mobile-first responsive styles
<Text fontSize={[ 3, 4, 5 ]} />
// The shorthand `f` prop can be used instead of `width`
<Text f={3} />
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 m={2} />
// Numbers greater than the length of `theme.space.length` are converted to pixels
<Text my={256} />
// Negative values can be used to add negative margins
<Text mx={-2} />
// Strings can be used for other values
<Text mx='auto' />
// Arrays can be used for mobile-first responsive styles
<Text m={[ 0, 1, 2 ]} />
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 color='blue' />
// Background color can be set with the `bg` prop
<Button bg='red' />
// Values that do not map to a key in `theme.colors` can be used
<Button bg='tomato' />
// Arrays can be used to change colors responsively
<Text color={[ 'blue', 'green' ]} />
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
width={[
1, // 100% width at the smallest breakpoint
1/2, // 50% width at the next breakpoint
null, // null skips a breakpoint
1/4 // 25% width at the next
]}
/>
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
is='h1'
children='Top-level heading'
/>
<Button
is='a'
href='#!'
children='Link Button'
/>
The <Provider />
component is a wrapper around styled-components' ThemeProvider.
It also provides global styles that remove the body tag's margin, sets all elements to box-sizing: border-box
,
and 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 = props => (
<Provider>
<Page />
</Provider>
)
To see an interactive demo of all Rebass components, see http://jxnblk.com/rebass
- Button
- ButtonOutline
- ButtonCircle
- Link
- NavLink
- BlockLink
- Heading
- Subhead
- Text
- Small
- Lead
- Pre
- Code
- Samp
- Blockquote
- Label
- Input
- Select
- Textarea
- Checkbox
- Radio
- Slider
- Image
- Avatar
- BackgroundImage
- Container
- Divider
- Border
- Media
- Card
- Banner
- Panel
- PanelHeader
- PanelFooter
- Progress
- Message
- Group
- Toolbar
- Badge
- Tabs
- TabItem
- DotButton
- Relative
- Absolute
- Fixed
Some components accept other props for styling.
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: uppecase; letter-spacing: .2em
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
active
(boolean) adjusts style for an active state
backgroundImage
(string) URL for a background image
ratio
(number) converted into a percentage to maintain aspect ratio
size
(number) pixel width and height
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
For convenience, the Grid Styled <Flex />
and <Box />
components are included in Rebass
to handle most page layout needs.
import { Flex, Box } from 'rebass'
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 = props => (
<Provider theme={theme}>
<Heading>Hello</Heading>
</Provider>
)
Rebass components can be completely customized using styled-components.
import styled from 'styled-components'
import { Button } from 'rebass'
const CustomButton = styled(Button)`
border: 1px solid rgba(0, 0, 0, .25);
background-image: linear-gradient(transparent, rgba(0, 0, 0, .125));
box-shadow: 0 0 4px rgba(0, 0, 0, .25)
`
Rebass uses styled-components for styling. The styled-components documentation explains how to handle Server-Side Rendering.