Giter Club home page Giter Club logo

prop-styles's Introduction

prop-styles

Utility to create flexible React components which accepts props to enable/disable certain styles.


Travis

Why

I like implementing and using React components using a code style where during component usage, a prop can be added as a 'flag', rather than relying on ternaries to handle the logic. In my opinion, this leads to improved legibility and has the potential to reduce typos.

Credit for the original source goes to Kent C. Dodds who was kind enough to post a snippet.

This Solution

Exposes a function, propStyles that accepts an object where the key is a prop and the value are the styles that should be applied if that prop is passed. Returns a function which you pass to a glamorousComponentFactory.

Users of styled-components should reference the example below.

Installation

This package is distributed via npm which is bundled with node and should be installed as one of your project's dependencies:

yarn add prop-styles or npm install -s prop-styles

Usage

Example using glamorous, without ThemeProvider

Live demo

This is a minimal example of prop-styles usage with glamorous:

import React from 'react'
import { render } from 'react-dom'
import glamorous, { Div } from 'glamorous'
import propStyles from 'prop-styles'

const button = {
  fontSize: 16,
  margin: 10,
  border: 'none',
  cursor: 'pointer',
  display: 'inline-block',
  padding: '10px 20px',
  textAlign: 'center',
  transition: '0.25s cubic-bezier(0.17, 0.67, 0.52, 0.97)',
  borderRadius: 0,
  color: '#fff',
  boxShadow: '0 4px 6px rgba(50,50,93,.11), 0 1px 3px rgba(0,0,0,.08)',
  ':hover': {
    opacity: 0.7,
    transform: 'translateY(-1px)',
    boxShadow: '0 7px 14px rgba(50,50,93,.1), 0 3px 6px rgba(0,0,0,.08)'
  },
  ':focus': { outline: 0 },
  ':active': {
    transform: 'translateY(1px)'
  }
}

const small = {
  padding: '8px 16px'
}

const large = {
  padding: '12px 24px'
}

const colors = {
  success: '#29A88E',
  danger: '#C65F4A',
  primary: '#6DCFD3',
  info: '#FFD035',
  gray: '#5A6E73',
  accent: '#8E83A3'
}

const Button = glamorous.button(
  button,
  propStyles({
    success: success => ({ backgroundColor: colors.success }),
    danger: danger => ({ backgroundColor: colors.danger }),
    primary: primary => ({ backgroundColor: colors.primary }),
    info: info => ({ backgroundColor: colors.info }),
    gray: gray => ({ backgroundColor: colors.gray }),
    accent: accent => ({ backgroundColor: colors.accent }),
    small: [button, small, { fontSize: 12 }],
    large: [button, large, { fontSize: 18 }]
  })
)

function App() {
  return (
    <Div>
      <Div>
        <Button small success>
          Success
        </Button>
        <Button small danger>
          Danger
        </Button>
        <Button small primary>
          Primary
        </Button>
        <Button small info>
          Info
        </Button>
        <Button small gray>
          Gray
        </Button>
        <Button small accent>
          Accent
        </Button>
      </Div>
      <Div>
        <Button success>Success</Button>
        <Button danger>Danger</Button>
        <Button primary>Primary</Button>
        <Button info>Info</Button>
        <Button gray>Gray</Button>
        <Button accent>Accent</Button>
      </Div>
      <Div>
        <Button large success>
          Success
        </Button>
        <Button large danger>
          Danger
        </Button>
        <Button large primary>
          Primary
        </Button>
        <Button large info>
          Info
        </Button>
        <Button large gray>
          Gray
        </Button>
        <Button large accent>
          Accent
        </Button>
      </Div>
    </Div>
  )
}

render(<App />, document.getElementById('root'))
Example with glamorous

Live demo

Similar to the example above, this sample shows how prop-styles and glamorous ThemeProvider work together:

import React from "react"
import { render } from "react-dom"
import glamorous, { ThemeProvider } from "glamorous"
import propStyles from "prop-styles"

const heading = {
  display: "block",
  fontFamily: "inherit",
  fontWeight: "500",
  lineHeight: "1.1"
}

const largerHeading = {
  marginTop: "20px",
  marginBottom: "10px"
}

const smallerHeading = {
  marginTop: "10px",
  marginBottom: "10px"
}

// shoutout to https://seek-oss.github.io/seek-style-guide/typography/
const Text = glamorous.span(
  propStyles({
    faded: ({ theme }) => ({ color: theme.colors.faded }),
    fadedExtra: ({ theme }) => ({ color: theme.colors.fadedExtra }),
    superheading: [heading, largerHeading, { fontSize: 36 }],
    heading: [heading, largerHeading, { fontSize: 30 }],
    subheading: [heading, largerHeading, { fontSize: 24 }],
    superstandard: [heading, smallerHeading, { fontSize: 18 }],
    standard: [heading, smallerHeading, { fontSize: 14 }],
    substandard: [heading, smallerHeading, { fontSize: 12 }]
  })
)

function App() {
  return (
    <ThemeProvider
      theme={{
        colors: {
          faded: "#666",
          fadedExtra: "#888"
        }
      }}
    >
      <glamorous.Div maxWidth={600} margin="auto">
        <Text>Normal</Text>
        <Text subheading>subheading</Text>
        <Text faded superheading>
          faded superheading
        </Text>
        <Text fadedExtra substandard>
          fadedExtra substandard
        </Text>
      </glamorous.Div>
    </ThemeProvider>
  )
}

render(<App />, document.getElementById("root"));
Example with styled-components

Live demo

import React from 'react'
import {render} from 'react-dom'
import styled, {ThemeProvider} from 'styled-components'
import propStyles from 'prop-styles'

const heading = {
  display: 'block',
  fontFamily: 'inherit',
  fontWeight: '500',
  lineHeight: '1.1',
}
const largerHeading = {
  marginTop: '20px',
  marginBottom: '10px',
}

const smallerHeading = {
  marginTop: '10px',
  marginBottom: '10px',
}

// shoutout to https://seek-oss.github.io/seek-style-guide/typography/
const textPropStyles = propStyles({
  faded: ({theme}) => ({color: theme.colors.faded}),
  fadedExtra: ({theme}) => ({color: theme.colors.fadedExtra}),
  superheading: [heading, largerHeading, {fontSize: '36px'}],
  heading: [heading, largerHeading, {fontSize: '30px'}],
  subheading: [heading, largerHeading, {fontSize: '24px'}],
  superstandard: [heading, smallerHeading, {fontSize: '18px'}],
  standard: [heading, smallerHeading, {fontSize: '14px'}],
  substandard: [heading, smallerHeading, {fontSize: '12px'}],
})
const Text = styled.span`${textPropStyles};`

function App() {
  return (
    <ThemeProvider
      theme={{
        colors: {
          faded: '#666',
          fadedExtra: '#888',
        },
      }}
    >
      <div style={{maxWidth: 600, margin: 'auto'}}>
        <Text>Normal</Text>
        <Text subheading>subheading</Text>
        <Text faded superheading>
          faded superheading
        </Text>
        <Text fadedExtra substandard>
          fadedExtra substandard
        </Text>
      </div>
    </ThemeProvider>
  )
}

render(<App />, document.getElementById('root'))

Inspiration

License

AGPL-3.0

prop-styles's People

Contributors

peterschussheim avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

prop-styles's Issues

Action required: Greenkeeper could not be activated 🚨

🚨 You need to enable Continuous Integration on all branches of this repository. 🚨

To enable Greenkeeper, you need to make sure that a commit status is reported on all branches. This is required by Greenkeeper because it uses your CI build statuses to figure out when to notify you about breaking changes.

Since we didn’t receive a CI status on the greenkeeper/initial branch, it’s possible that you don’t have CI set up yet. We recommend using Travis CI, but Greenkeeper will work with every other CI service as well.

If you have already set up a CI for this repository, you might need to check how it’s configured. Make sure it is set to run on all new branches. If you don’t want it to run on absolutely every branch, you can whitelist branches starting with greenkeeper/.

Once you have installed and configured CI on this repository correctly, you’ll need to re-trigger Greenkeeper’s initial pull request. To do this, please delete the greenkeeper/initial branch in this repository, and then remove and re-add this repository to the Greenkeeper App’s white list on Github. You'll find this list on your repo or organization’s settings page, under Installed GitHub Apps.

suggestion: simplify usage example

Really cool project but I've had a hard time trying to grokk what this package does by looking at the usage example because there's a lot going out there. Maybe it could be simplified to give just the essence what this project is about, like:

Instead of writing functions for every conditional style:

const Text = glamorous.span(
    ({big}) => big && { fontSize: "24px" },
    ({small}) => small && { fontSize: "20px" }
)

You can use this package to simplify your code:

import flags from "prop-styles"

const Text = glamorous.span(flags({
   big: { fontSize: "24px" },
   small: { fontSize: "20px" }
}))

Does this require ThemeProvider?

Does prop-styles require ThemeProvider as a parent component?

This isn't clear in the documentation. Having a little trouble getting this to work. Trying to reduce the amount of parent components i have. Would be great if it just worked without ThemeProvider.

Action required: Greenkeeper could not be activated 🚨

🚨 You need to enable Continuous Integration on all branches of this repository. 🚨

To enable Greenkeeper, you need to make sure that a commit status is reported on all branches. This is required by Greenkeeper because it uses your CI build statuses to figure out when to notify you about breaking changes.

Since we didn’t receive a CI status on the greenkeeper/initial branch, it’s possible that you don’t have CI set up yet. We recommend using Travis CI, but Greenkeeper will work with every other CI service as well.

If you have already set up a CI for this repository, you might need to check how it’s configured. Make sure it is set to run on all new branches. If you don’t want it to run on absolutely every branch, you can whitelist branches starting with greenkeeper/.

Once you have installed and configured CI on this repository correctly, you’ll need to re-trigger Greenkeeper’s initial pull request. To do this, please delete the greenkeeper/initial branch in this repository, and then remove and re-add this repository to the Greenkeeper App’s white list on Github. You'll find this list on your repo or organization’s settings page, under Installed GitHub Apps.

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.