npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

solid-styled-components

v0.28.5

Published

Styled Components for Solid

Downloads

10,894

Readme

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