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

sass-styled-theme

v0.1.0

Published

Create styled-components theme from SASS variables

Downloads

4

Readme

sass-styled-theme

Extract global variables from Sass stylesheets into a JS object.

Huh? Why?

I work on a team that uses Sass. We've got a shared variables file that gets referenced throughout our styleguide and in our components. I wanted to start using styled-components in some projects and wanted to keep things consistent but I didn't want to have to copy our Sass variables over. With this package I can simply reference our shared variables file and it will be converted into a JS object that gets passed down to my components via styled-components' <ThemeProvider>.

Cool! How do I get it?

$ yarn add sass-styled-theme

(npm install works too)

Nice! How do I use it?

Wrap your App in a ThemeProvider component like this:

// Import the lib
import sassToStyledTheme from 'sass-styled-theme';

// Call the function with absolute path to your Sass file
const theme = sassToStyledTheme('./path/to/vars.scss');

// Pass the theme into your ThemeProvider component
render(
  <ThemeProvider theme={theme}>
    <App />
  </ThemeProvider>
);

Then use themes in your styled components:

import styled from 'styled-components';

const Button = styled.button`
  background-color: ${props => props.theme.primary}
`;

Sweet! What does the output look like?

Given a Sass file with some global variable declarations:

$primary: rgb(255, 202, 77);
$seondary: #1A93C8;
$primary-light: lighten($primary, 20%);
$base-padding: 10px;
$base-margin: 0 1em;
$base-border: 1px solid #ccc;
$font-family-sans: 'Helvetica', 'Arial', sans-serif;
$base-font-size: 16px;
$line-height: $base-font-size * 1.8;

It will yield the following object:

{ 
  primary: 'rgb(255, 202, 77)',
  seondary: 'rgb(26, 147, 200)',
  'primary-light': 'rgb(255, 232, 179)',
  'base-padding': '10px',
  'base-margin': '0 1em',
  'base-border': '1px solid rgb(204, 204, 204)',
  'font-family-sans': '\'Helvetica\', \'Arial\', sans-serif',
  'base-font-size': '16px',
  'line-height': '28.8px'
}

Rad! Can I customize it?

You want options? Sure. The sassToStyledTheme function takes an options object as its second parameter.

const theme = sassToStyledTheme(file, { /* options! */ });

Options object

Key | Type | Default | Description :-- | :---- | :----- | :---------- camel | boolean | false | convert output variable names to camelCase sassOptions | object | | options to pass thru to node-sass

Help! It's not working for me.

This project is open source. I've tried to make sure it works for a lot of use cases (read: mine) but if I missed yours, feel free to open an issue. Better yet, submit a PR! Seriously, any feedback and help is welcome.

Known Issues

This library uses the awesome sass-extract package under the hood to render and parse the Sass. There is currently an issue where mixins or functions that use default values may cause the variable extraction to silently fail (sass-extract #12). If you run into this, a workaround is to make sure that you follow the mixin or function with a normal variable declaration.