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

styled-theme-tokens

v0.0.8

Published

Easily configure and manage themes for styled components in React.

Downloads

1

Readme

Styled Theme Tokens

Styled Theme Tokens makes it easy to configure and manage themes for styled components within react projects. It provides an unopinionated yet powerful api for defining themes as javascript objects that works well with designers and developers.

Philosophy

The Philosophy behind styled-theme-tokens is that defining themes should be easy yet flexible enough to manage theme variations such as dark or light mode. Developers should be able to define all of the variations of a particular design token in 1 place.

API

ThemeProvider

The ThemeProvider provides all of your styled-components with the resolved theme. You'll need to wrap your entire app with the ThemeProvider.

import { ThemeProvider } from 'styled-theme-tokens';
import theme from './source/to/theme';

function App() {
    return (
        <ThemeProvider theme={theme} variants={{ mode: "light", size: "compact" }}>
            {...}
        </ThemeProvider>
    );
}

token

The token function creates a design token that will be resolved based on the current theme's variants i.e. light or dark or compact or dense, etc...

You can define your tokens in as many or as few files as you like.

Tokens are defined as such:

import { token } from 'styled-theme-tokens';

// ex: /src/theme/colors.ts
// will resolve to either `darkred` or `red`
const PrimaryColor = token("mode", {
    dark: "darkred",
    light: "red"
}),

// ex: /src/theme/spacing.ts
// will resolve to either `4px` or `8px`
const DefaultPadding = token("size", {
    compact: "4px",
    dense: "8px"
}),

complex tokens

Tokens don't have to resolve to just strings, they can resolve any any number of objects, functions, or primitives.

Example:

import { token } from 'styled-theme-tokens';

// ex: /src/theme/colors.ts
const PrimaryColor = token("mode", {
    dark: {
        color: "darkred",
        rgba: () => {}
    },
    light: {
        color: "red",
        rgba: () => {}
    }
}),

Depending on how you defined your theme, in a styled component you could access the props as such:

    color: ${theme.colors.primary.dark.color};

Example Theme Definition

import { token } from 'styled-theme-tokens';

const theme = {
    colors: {
        primary: token("mode", {
            dark: "darkred",
            light: "red"
        }),
        secondary: token("mode", {
            dark: "darkblue",
            light: "blue"
        })
    },
    sizes: {
        headerHeight: token("size", {
            compact: 24,
            dense: 48
        }),
        sidebarWidth: 400
    }
}

Example Theme Definition as JSON

{
    "colors": {
        "primary":  {
            "token": "mode",
            "values": {
                "dark": "darkred",
                "light": "red",
            }
        },
        "secondary":  {
            "token": "mode",
            "values": {
                "dark": "darkblue",
                "light": "blue",
            }
        }
    },
    "sizes": {
        "headerHeight":  {
            "token": "size",
            "values": {
                "compact": 24,
                "dense": 48,
            }
        },
        "sidebarWidth": 400
    }
}

Typescript

To use the properly with TypeScript and Styled-Components you will need to created a styled components declaration file to override the DefaultTheme from styled-components. This will ensure that your styled-components have the correct theme object values being used.

Example:

// /src/declarations/styled.d.ts
import 'styled-components'
import { Theme } from './source/to/theme/typescript/definition';
import { ConvertToThemeObject } from 'styled-theme-tokens';

declare module 'styled-components' {
  export interface DefaultTheme extends ConvertToThemeObject<Theme> {}
}