# prop-styles

> Utility to create a glamorous component which accepts props to enable/disable certain styles.

Latest version **2.0.0** (published 2017-10-17) · AGPL-3.0 license · 0 weekly downloads

## Install

```sh
npm install prop-styles
pnpm add prop-styles
yarn add prop-styles
bun add prop-styles
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.0 |
| Published | 2017-10-17 |
| First published | 2017-10-16 |
| Weekly downloads | 0 |
| License | AGPL-3.0 |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 29 |
| Author | Peter Schussheim |
| Maintainers | peterschussheim |
| Keywords | styles |

## Links

- npm: https://www.npmjs.com/package/prop-styles
- Repository: https://github.com/peterschussheim/prop-styles
- Homepage: https://github.com/peterschussheim/prop-styles#readme
- Issues: https://github.com/peterschussheim/prop-styles/issues
- npm.io page: https://npm.io/package/prop-styles

## Recent versions

- 2.0.0 (latest) — 2017-10-17
- 1.0.1-beta.0 (beta) — 2017-10-16
- 0.0.0 — 2017-10-17
- 0.0.0-semantically-released — 2017-10-17
- 1.0.2 — 2017-10-17
- 1.0.1 — 2017-10-16

## README

# prop-styles

[![Travis](https://img.shields.io/travis/peterschussheim/prop-styles.svg?style=flat-square)](https://travis-ci.org/peterschussheim/prop-styles)

Utility to make it easier to create a glamorous component which accepts props to enable/disable certain styles.

---

- [prop-styles](#prop-styles)
  - [Why](#why)
  - [This Solution](#this-solution)
  - [Installation](#installation)
  - [Usage](#usage)
    - [Example](#example)
  - [Inspiration](#inspiration)
  - [License](#license)

## 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](https://twitter.com/kentcdodds) who [was kind enough to post a snippet](https://codesandbox.io/s/AGRRMl63) to easily enable developers to use this style with [💄`glamorous`💄](https://glamorous.rocks).

## 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`.

## Installation

This package is distributed via [npm](https://www.npmjs.com/) which is bundled with [node](https://nodejs.org/en/) and should be installed as one of your project's `dependencies`:

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

## Usage

### Example

[Live demo](https://codesandbox.io/s/92m6q0krqr)

```javascript
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"));
```

## Inspiration

- [Kent C. Dodds](https://twitter.com/kentcdodds) who [was kind enough to post this snippet](https://codesandbox.io/s/AGRRMl63)
- [Ryan Florence](https://twitter.com/ryanflorence)
- [reactpatterns.com](http://reactpatterns.com/#style-component)

## License

AGPL-3.0

---
_Source: https://npm.io/package/prop-styles · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
