• Stars
    star
    283
  • Rank 146,066 (Top 3 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created over 3 years ago
  • Updated 5 months ago

Reviews

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

Repository Details

A 1kb Styled Components library for Solid

Solid Styled Components

NPM Version

This library provides Styled Components and css helper found in popular JS in CSS libraries. This library uses 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.
import { styled } from "solid-styled-components";

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

Tagged Templates

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

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

<Btn size={20} />;

Function returns a string

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

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

<Btn size={20} />;

Nesting styled components

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

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:

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

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

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

Or an object:

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

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

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

Passing props to css tagged templates

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.

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.

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.

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).

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.

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:

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.

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

// 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.

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

More Repositories

1

solid

A declarative, efficient, and flexible JavaScript library for building user interfaces.
TypeScript
31,893
star
2

solid-start

SolidStart, the Solid app framework
TypeScript
5,117
star
3

solid-router

A universal router for Solid inspired by Ember and React Router
TypeScript
1,146
star
4

templates

Vite + solid templates
TypeScript
434
star
5

vite-plugin-solid

A simple integration to run solid-js with vite
TypeScript
433
star
6

solid-realworld

A Solid Implementation of the Realworld Example App
JavaScript
228
star
7

solid-docs

SolidJS Docs.
MDX
214
star
8

solid-testing-library

Simple and complete Solid testing utilities that encourage good testing practices.
TypeScript
195
star
9

solid-playground

Quickly discover what the solid compiler will generate from your JSX template
TypeScript
195
star
10

react-solid-state

Auto tracking state management for modern React
TypeScript
188
star
11

solid-site

Code that powers the SolidJS.com platform.
TypeScript
159
star
12

solid-hackernews

Solid implementation of Hacker News
JavaScript
158
star
13

signals

TypeScript
134
star
14

solid-meta

Write meta tags to the document head
TypeScript
127
star
15

solid-refresh

TypeScript
84
star
16

solid-workgroup

Workgroup for future Solid Releases
54
star
17

solid-styled-jsx

A Styled JSX wrapper for Solid
JavaScript
47
star
18

solid-jest

Jest preset for SolidJS
JavaScript
34
star
19

create-solid

Set up a modern web app by running one command
JavaScript
32
star
20

solid-todomvc

Solid implementation of TodoMVC
TypeScript
29
star
21

solidhack-submissions

A repository for collecting SolidHack submissions.
JavaScript
20
star
22

solid-repl

A REPL for SolidJS
TypeScript
14
star
23

solid-service-api

Code that powers Solid Service API on Cloudflare Workers.
JavaScript
13
star
24

solidex

Solidex is a list of SolidJS ecosystem resources and packages.
TypeScript
11
star
25

solid-scripts

No configuration CLI tools to bootstrap Solid applications
JavaScript
10
star
26

solid-assets

Access official dynamic and static assets from SolidJS. Made for the community! 🌟
TypeScript
7
star
27

solid-hot-loader

Webpack Loader with Hot Module Reloading for SolidJS
JavaScript
5
star
28

.github

Community health files for the @solidjs organization
3
star
29

solidhack

Website and contest portal for SolidHack.
TypeScript
2
star