• Stars
    star
    213
  • Rank 185,410 (Top 4 %)
  • Language
    JavaScript
  • Created over 7 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Responsive margin and padding components for styled-components πŸ’….

styled-components-spacing

npm npm bundle size (minified + gzip) npm Build Status

Responsive margin and padding components for styled-components πŸ’….

Change log

Have a look πŸ‘€ at styled-components-breakpoint and styled-components-grid which both work well with this package.

Installation

npm install --save styled-components styled-components-spacing

Usage

Examples

Using the default spacings at the default breakpoints

import React from 'react';
import { Margin, Padding } from 'styled-components-spacing';

<HeroPanel>
  <Padding all={{ mobile: 2, tablet: 4, desktop: 6 }}>
    <Title>Hello World</Title>
    <SubTitle>You are on earth!</SubTitle>
    <Margin top={1}>
      <Button>Blast off!</Button>
    </Margin>
  </Padding>
</HeroPanel>;

Using custom spacings at custom breakpoints

Spacings and breakpoints can be customised using ThemeProvider. For example, to use the same breakpoints and spacings as Bootstrap, you can do so like this:

import React from 'react';
import { ThemeProvider } from 'styled-components';
import { Margin, Padding } from 'styled-components-spacing';

const theme = {
  breakpoints: {
    xs: 0,
    sm: 576,
    md: 768,
    lg: 992,
    xl: 1200
  },
  spacing: {
    0: '0',
    1: '0.25rem',
    2: '0.5rem',
    3: '1rem',
    4: '1.5rem',
    5: '3rem'
  }
};

<ThemeProvider theme={theme}>
  <HeroPanel>
    <Padding all={{ sm: 1, lg: 2 }}>
      <Title>Hello World</Title>
      <SubTitle>You are on earth!</SubTitle>
      <Margin top={1}>
        <Button>Blast off!</Button>
      </Margin>
    </Padding>
  </HeroPanel>
</ThemeProvider>;

Using the mixins

import React from 'react';
import styled from 'styled-components';
import { my, px } from 'styled-components-spacing';

const Panel = styled.div`
  ${my({ mobile: 2, tablet: 4 })} ${px(6)};
`;

Components

<Margin/>

all

Margin on all sides.

Optional. A string or number specifying the spacing key. May be a keyed object specifying spacing keys for multiple breakpoints.

horizontal

Margin on the left and right.

Optional. A string or number specifying the spacing key. May be a keyed object specifying spacing keys for multiple breakpoints.

vertical

Margin on the top and bottom.

Optional. A string or number specifying the spacing key. May be a keyed object specifying spacing keys for multiple breakpoints.

top

Margin on the top.

Optional. A string or number specifying the spacing key. May be a keyed object specifying spacing keys for multiple breakpoints.

bottom

Margin on the bottom.

Optional. A string or number specifying the spacing key. May be a keyed object specifying spacing keys for multiple breakpoints.

left

Margin on the left.

Optional. A string or number specifying the spacing key. May be a keyed object specifying spacing keys for multiple breakpoints.

right

Margin on the right.

Optional. A string or number specifying the spacing key. May be a keyed object specifying spacing keys for multiple breakpoints.

inline

Size the element to the width of its children.

Optional. A boolean. Defaults to false.

<Padding/>

all

Padding on all sides.

Optional. A string or number specifying the spacing key. May be a keyed object specifying spacing keys for multiple breakpoints.

horizontal

Padding on the left and right.

Optional. A string or number specifying the spacing key. May be a keyed object specifying spacing keys for multiple breakpoints.

vertical

Padding on the top and bottom.

Optional. A string or number specifying the spacing key. May be a keyed object specifying spacing keys for multiple breakpoints.

top

Padding on the top.

Optional. A string or number specifying the spacing key. May be a keyed object specifying spacing keys for multiple breakpoints.

bottom

Padding on the bottom.

Optional. A string or number specifying the spacing key. May be a keyed object specifying spacing keys for multiple breakpoints.

left

Padding on the left.

Optional. A string or number specifying the spacing key. May be a keyed object specifying spacing keys for multiple breakpoints.

right

Padding on the right.

Optional. A string or number specifying the spacing key. May be a keyed object specifying spacing keys for multiple breakpoints.

inline

Size the element to the width of its children.

Optional. A boolean. Defaults to false.

Mixins

m(values)

Margin on all sides.

Parameters:

  • values - Required. A string or number specifying the spacing key. May be a keyed object specifying spacing keys for multiple breakpoints.

mx(values)

Margin on the left and right.

Parameters:

  • values - Required. A string or number specifying the spacing key. May be a keyed object specifying spacing keys for multiple breakpoints.

my(values)

Margin on the top and bottom.

Parameters:

  • values - Required. A string or number specifying the spacing key. May be a keyed object specifying spacing keys for multiple breakpoints.

mt(values)

Margin on the top.

Parameters:

  • values - Required. A string or number specifying the spacing key. May be a keyed object specifying spacing keys for multiple breakpoints.

mr(values)

Margin on the right.

Parameters:

  • values - Required. A string or number specifying the spacing key. May be a keyed object specifying spacing keys for multiple breakpoints.

mb(values)

Margin on the bottom.

Parameters:

  • values - Required. A string or number specifying the spacing key. May be a keyed object specifying spacing keys for multiple breakpoints.

ml(values)

Margin on the left.

Parameters:

  • values - Required. A string or number specifying the spacing key. May be a keyed object specifying spacing keys for multiple breakpoints.

p(values)

Padding on all sides.

Parameters:

  • values - Required. A string or number specifying the spacing key. May be a keyed object specifying spacing keys for multiple breakpoints.

px(values)

Padding on the left and right.

Parameters:

  • values - Required. A string or number specifying the spacing key. May be a keyed object specifying spacing keys for multiple breakpoints.

py(values)

Padding on the top and bottom.

Parameters:

  • values - Required. A string or number specifying the spacing key. May be a keyed object specifying spacing keys for multiple breakpoints.

pt(values)

Padding on the top.

Parameters:

  • values - Required. A string or number specifying the spacing key. May be a keyed object specifying spacing keys for multiple breakpoints.

pr(values)

Padding on the right.

Parameters:

  • values - Required. A string or number specifying the spacing key. May be a keyed object specifying spacing keys for multiple breakpoints.

pb(values)

Padding on the bottom.

Parameters:

  • values - Required. A string or number specifying the spacing key. May be a keyed object specifying spacing keys for multiple breakpoints.

pl(values)

Padding on the left.

Parameters:

  • values - Required. A string or number specifying the spacing key. May be a keyed object specifying spacing keys for multiple breakpoints.

Defaults

{
  0: '0',
  1: '0.25rem',
  2: '0.5rem',
  3: '1rem',
  4: '2rem',
  5: '4rem',
  6: '8rem'
}

Rendering on a custom component

This library no longer supports the component prop - if you wish to use a custom component with this library use .withComponent()

More Repositories

1

styled-components-breakpoint

Utility function for using breakpoints with styled-components πŸ’….
JavaScript
240
star
2

styled-components-grid

A responsive grid components for styled-components.
JavaScript
220
star
3

xhr-mock

Utility for mocking XMLHttpRequest.
TypeScript
197
star
4

react-lazily-render

Delay mounting expensive components until a placeholder component has been scrolled into view.
JavaScript
22
star
5

react-create-component-from-tag-prop

Create a `react` component from a `tag` prop.
JavaScript
18
star
6

redux-when

Redux middleware for delaying dispatch of an action until a condition evaluates to true.
JavaScript
18
star
7

redux-formo

An alternate forms framework for Redux+React.
JavaScript
9
star
8

satay

A utility for uploading static sites to AWS S3.
TypeScript
9
star
9

react-render-image

Render an image in React.
TypeScript
8
star
10

wait-for-event

Wait for events to be triggered.
TypeScript
6
star
11

react-testutils-render

A lite wrapper for shallow rendering React components.
JavaScript
6
star
12

substance

βš—οΈPrimitives for building components.
TypeScript
5
star
13

publish-if-not-published

Publish a package if the current version isn't already published.
TypeScript
4
star
14

sass-triangle

Mixin for creating CSS triangles.
CSS
4
star
15

tradie-v4

A framework for building tools like `create-react-app` tailored to your use case and tech stack.
TypeScript
4
star
16

webpack-resolve-short-path-plugin

A plugin for `enhanced-resolve` (Webpack v2) that resolves paths like `~module` to `%rootPath%/module` so you don't have to write relative paths like `../../../module`.
JavaScript
4
star
17

react-firebase

React hooks for working with firebase.
TypeScript
3
star
18

koa

A number of packages made to work with koa.
TypeScript
3
star
19

tradie

A semi-opinionated build tool for frontend projects. Use it to lint, bundle and test your script and style files.
JavaScript
3
star
20

run-if-diff

Run a command if files have changed.
JavaScript
3
star
21

react-app-example

JavaScript
1
star
22

react

πŸ“¦ βš›οΈ A collection of packages for building React applications.
TypeScript
1
star
23

react-testutils-query

Utility functions for retrieving elements from within a React virtual-dom tree.
JavaScript
1
star
24

buildkite-pipelines

Generate Buildkite pipelines from code.
TypeScript
1
star
25

now-deploy-action

Deploy a project using ZEIT Now.
JavaScript
1
star
26

rev-manifest-path

Get the cache busted path of an asset from a `gulp-rev` or `gulp-rev-all` manifest.
JavaScript
1
star
27

cancelable-promise

A cancelable promise implementation.
TypeScript
1
star
28

tradie-template-react-static-site

Tradie template for building static sites with React.
JavaScript
1
star
29

generator-fed

Yeoman generator for Frontend Development with Browserify and SASS Composer.
JavaScript
1
star
30

react-static-site-example

react-static-site-example
JavaScript
1
star
31

design-system-example

A demonstration of the tooling setup for a React based Design System.
TypeScript
1
star
32

koa-reload-middleware

Reload middleware if it changes during development.
TypeScript
1
star
33

workspace-metadata-webpack-plugin

Display workspace metadata on your website.
TypeScript
1
star