# solid-styled-components

> Styled Components for Solid

Latest version **0.28.5** (published 2022-10-10) · MIT license · 0 weekly downloads

## Install

```sh
npm install solid-styled-components
pnpm add solid-styled-components
yarn add solid-styled-components
bun add solid-styled-components
```

## 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.28.5 |
| Published | 2022-10-10 |
| First published | 2020-04-08 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 2 |
| Unpacked size | 13.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 298 |
| Author | Ryan Carniato |
| Maintainers | ryansolid |

## Links

- npm: https://www.npmjs.com/package/solid-styled-components
- Repository: https://github.com/solidjs/solid-styled-components
- Homepage: https://github.com/solidjs/solid-styled-components#readme
- Issues: https://github.com/solidjs/solid-styled-components/issues
- npm.io page: https://npm.io/package/solid-styled-components

## Dependencies (2)

- [goober](https://npm.io/package/goober.md) ^2.1.10
- [csstype](https://npm.io/package/csstype.md) ^3.1.0

## Recent versions

- 0.28.5 (latest) — 2022-10-10
- 0.24.6-beta.0 (beta) — 2021-02-13
- 0.28.4 — 2022-06-29
- 0.28.3 — 2022-06-26
- 0.28.2 — 2022-06-26
- 0.28.1 — 2022-05-14
- 0.28.0 — 2022-05-14
- 0.27.6 — 2022-02-21
- 0.27.5 — 2022-02-20
- 0.27.4 — 2022-01-13
- 0.27.3 — 2021-09-04
- 0.27.2 — 2021-08-18
- 0.27.1 — 2021-06-28
- 0.27.0 — 2021-06-28
- 0.26.4 — 2021-05-27
- … 107 more at https://npm.io/package/solid-styled-components/versions

## README

# Solid Styled Components

[![NPM Version](https://img.shields.io/npm/v/solid-styled-components.svg?style=flat)](https://www.npmjs.com/package/solid-styled-components)
![](https://img.shields.io/librariesio/release/npm/solid-styled-components)
![](https://img.shields.io/npm/dm/solid-styled-components.svg?style=flat)

This library provides Styled Components and css helper found in popular JS in CSS libraries. This library uses [goober](https://github.com/cristianbote/goober) a 1kb style library with a wrapper to work with Solid's API. The wrapper also adds a Theming solution.

## Features

### `styled(tagName)`

- `@param {String} tagName` The name of the dom element you'd like the styled to be applied to
- `@returns {Function}` Returns the tag template function.

```js
import { styled } from "solid-styled-components";

const Btn = styled("button")`
  border-radius: 4px;
`;
```

#### Tagged Templates

```jsx
import { styled } from "solid-styled-components";

const Btn = styled("button")`
  border-radius: ${props => props.size}px;
`;

<Btn size={20} />;
```

#### Function returns a string

```jsx
import { styled } from "solid-styled-components";

const Btn = styled("button")(
  props => `
  border-radius: ${props.size}px;
`
);

<Btn size={20} />;
```

#### Nesting styled components

```jsx
import { styled } from "solid-styled-components";

const Icon = styled("span")`
  display: flex;
  flex: 1;
  color: red;
`;

const Button = styled("button")`
  background: dodgerblue;
  color: white;
  border: ${Math.random()}px solid white;

  &:focus,
  &:hover {
    padding: 1em;
  }

  .otherClass {
    margin: 0;
  }

  ${Icon.class} {
    color: black;
  }
`;
```

#### Style Object

```jsx
import { styled } from "solid-styled-components";

const Btn = styled("button")(props => ({
  borderRadius: props.size + "px"
}));

<Btn size={20} />;
```

### `css`

- `@returns {String}` Returns the class.

To create a class, you need to call `css` with your style rules in a tagged template:

```jsx
import { css } from "solid-styled-components";

const BtnClass = css`
  border-radius: 4px;
`;

const App => <button class={BtnClass}>click</button>
```

Or an object:

```js
import { css } from "solid-styled-components";

const BtnClass = css({ borderRadius: "4px" })

const App => <button class={BtnClass}>click</button>
```

#### Passing props to `css` tagged templates

```js
import { css } from "solid-styled-components";

// JSX
const CustomButton = props => (
  <button
    class={css`
      border-radius: ${props.size}px;
    `}
  >
    click
  </button>
);
```

### `extractCss(target?)`

- `@returns {String}`

Returns the `<style>` tag that is rendered in a target and clears the style sheet. Defaults to `<head>`. Used to grab the styles for SSR.

```js
const { extractCss } = require("goober");

// After your app has rendered, just call it:
const styleTag = `<style id="_goober">${extractCss()}</style>`;

// Note: To be able to `hydrate` the styles you should use the proper `id` so `goober` can pick it up and use it as the target from now on
```

### `keyframes`

- `@returns {String}`

Add keyframe animations to a style component.

```js
const rotate = keyframes`
  100% { 
    transform:rotate(360deg); 
  }
`

const LoadingIcon = styled.img`
  animation: ${ rotate } 1s linear infinite;
`
```

### `createGlobalStyles`

For a global style component, you call `createGlobalStyles` with your global tagged template.

```js
import { createGlobalStyles } from "solid-styled-components";

const GlobalStyles = () => {
  const Styles = createGlobalStyles`
    html,
    body {
      background: light;
    }

    * {
      box-sizing: border-box;
    }
  `;
  return <Styles />;
};
```

### `Theme`

You can set a Theme Provider (remember to use state or signals if you want it to be reactive).

```jsx
import { styled, ThemeProvider } from "solid-styled-components";

const theme = {
  colors: {
    primary: "hotpink"
  }
};

const SomeText = styled("div")`
  color: ${props => props.theme.colors.primary};
`;

render(
  () => (
    <ThemeProvider theme={theme}>
      <SomeText>some text</SomeText>
    </ThemeProvider>
  ),
  document.getElementById("app")
);
```

The library provides a `useTheme` hook if you wish to use it elsewhere like in you `css` functions.

### Custom prefixer

Use `setup` to set up a custom prefixer.

```ts
setup(
  prefixer: null | ((key: string, value: any) => string)
  shouldForwardProp?: null | ((props: string[]) => string[])
)
```

### `shouldForwardProp`

To prevent unwanted props attaching to the generated HTML, you can use the `shouldForwardProp` helper:

```ts
import { shouldForwardProp } from "solid-styled-components";

setup(null, shouldForwardProp(prop => {
  return prop !== "foo";
}));
```

This will prevent the `foo` prop from appearing as an HTML attribute.

Note: Be careful not to filter out props such as `children` or `onClick` this way; these are already handled internally.

## Using ThemeProvider in TypeScript

Before you can effectively start to use the ThemeProvider in TypeScript you will have to do a little bit of configuration.

### Create a declarations file

TypeScript definitions for solid-styled-components can be extended by using [declaration merging](https://www.typescriptlang.org/docs/handbook/declaration-merging.html).

The first step is to create a declarations file. For example, let's name it `styled.d.ts`:

```typescript
// import original module declarations
import "solid-styled-components";

// and extend them!
declare module "solid-styled-components" {
  export interface DefaultTheme {
    colors: {
      primary: string;
    };
  }
}
```

`DefaultTheme` is being used as an interface of `props.theme` out of the box. By default the interface `DefaultTheme` is empty so that's why we need to extend it.

Now we can create a theme just by using the DefaultTheme declared at the step above.

```tsx
import { styled, ThemeProvider, DefaultTheme } from "solid-styled-components";

const theme: DefaultTheme = {
  colors: {
    primary: "hotpink"
  }
};

const SomeText = styled("div")`
  color: ${props => props.theme.colors.primary};
`;

render(
  () => (
    <ThemeProvider theme={theme}>
      <SomeText>some text</SomeText>
    </ThemeProvider>
  ),
  document.getElementById("app")
);
```

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