# sc-variant

> tiny helper to create variants of styled components

Latest version **0.1.1** (published 2022-08-22) · MIT license · 0 weekly downloads

## Install

```sh
npm install sc-variant
pnpm add sc-variant
yarn add sc-variant
bun add sc-variant
```

## Health

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

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.1 |
| Published | 2022-08-22 |
| First published | 2022-08-22 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 6.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Hannes Diercks |
| Maintainers | xiphe |
| Keywords | styled-components, variant, theme, props, type-save |

## Links

- npm: https://www.npmjs.com/package/sc-variant
- Repository: https://github.com/Xiphe/sc-variant
- Homepage: https://github.com/Xiphe/sc-variant#readme
- Issues: https://github.com/Xiphe/sc-variant/issues
- npm.io page: https://npm.io/package/sc-variant

## Recent versions

- 0.1.1 (latest) — 2022-08-22
- 0.1.0 — 2022-08-22

## README

# sc-variant

tiny _~100B_ helper to create variants of styled components

inspired by [styled-theming](https://jamie.build/styled-theming.html) but without the theming.

## install

```bash
npm i sc-variant
# yarn add sc-variant
```

## use

<!--
title: normal usage
before: import getCss from './getCss';
-->

```tsx
import styled from 'styled-components';
import variant from 'sc-variant';

interface CustomButtonProps {
  size?: 'small' | 'big' | 'custom';
  kind?: 'primary' | 'secondary';
}
const Button = styled.button<CustomButtonProps>`
  padding: ${variant('size', {
    // DEFAULT is optional and will be used when size="big"
    DEFAULT: '0.5rem',
    small: '0.125rem',
    // Setting null explicitly overwrites DEFAULT and no style is set
    custom: null,
  })};
  background-color: ${variant('kind', {
    primary: 'fuchsia',
    // kind="primary" is not covered here, so no background-color style is set for that variant
  })};
`;

expect(getCss(<Button />)).toBe('padding: 0.5rem;');
expect(getCss(<Button size="small" />)).toBe('padding: 0.125rem;');
expect(getCss(<Button kind="secondary" />)).toBe('padding: 0.5rem;');
expect(getCss(<Button kind="primary" />)).toBe(
  'padding: 0.5rem; background-color: fuchsia;',
);
expect(getCss(<Button kind="primary" size="custom" />)).toBe(
  'background-color: fuchsia;',
);
```

### Require extensive variants

We can require each variant to be set explicitly by passing `true` as third parameter.

Note that there are no runtime-checks. This exists only for dev convenience
with typescript.

<!--
title: extensive variants
-->

```tsx
import styled from 'styled-components';
import variant from 'sc-variant';

interface CustomButtonProps {
  size: 'small' | 'big' | 'normal';
}
const Button = styled.button<CustomButtonProps>`
  padding: ${variant(
    'size',
    // @ts-expect-error Property 'small' is missing
    {
      normal: '0.25rem',
      big: '0.5rem',
      // Note DEFAULT is not permitted here unless `size` prop is optional
    },
    true,
  )};
`;

expect(getCss(<Button size="big" />)).toBe('padding: 0.5rem;');
```

## What about...

Yes, there are a bunch of other tools doing mostly the same.
But I missed one that great TS support + absolute minimal footprint.

Shoutout to peers 😘

- [styled-variant](https://www.npmjs.com/package/styled-variant)
- [component-variants](https://www.npmjs.com/package/component-variants)
- [styled-component-variant](https://www.npmjs.com/package/styled-component-variant)
- [@styled-system/variant](https://www.npmjs.com/package/@styled-system/variant)
- and probably more...

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