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

if-sf

v0.2.15

Published

speculative fabulation

Downloads

9

Readme

sf (speculative fabulation)

generate themes, theme objects, and theme tokens

  • merge/extend by default. explicit overrides
  • opt-in/opt-out of tokens and components
  • roll out your own tokens and components


quick start

yarn add if-sf

Wherever you're defining your theme:

import { ThemeProvider } from 'theme-ui';
import { genTheme } from 'if-sf';

const theme = genTheme();

function MyApp() {
  return (
    <ThemeProvider theme={theme}>
      <App />
    </ThemeProvider>
  )
}

If you need access to more granular primitives, you can import individual generators and components:

import { Button } from 'if-sf/components';
import { defaultButtonStyles, genButton } from 'if-sf/tokens';

motivations

sf is built on top of emotion and styled-system's variants. In the past, I've enjoyed working with both, utilizing variants in my emotion or styled-components mark up. However, I found it difficult to think about how component variants should live on the theme object. Additionally, it became difficult to think about how to extend components that had variations -- what if I needed an additional variant typing? What if I wanted to wholesale override the variants? I had similar difficulties on a smaller scale with theme tokens -- what's the best way to override breakpoints? What if I want to extend certain color properties, while overriding others?

sf answers these questions by thinking in terms of merging, extending, and overriding. Using generator functions that receive and return objects, sf generates theme objects to pass into emotion's theme provider while also allowing granular control over how that theme is generated. Here's an example of generating color tokens:

import { genColors } from '@theme/tokens';

const colors = genColors();
/**
 * returns generic color object
 * 
 * colors = { 
 *  text: '#111827',
 *  background: '#F9FAFB',
 *  primary: '#2563EB',
 *  secondary: '#de7283',
 *  accent: '#7C3AED',
 *  muted: '#88d1ff'
 * }
 * 
 */
const mergeColors = genColors({ primary: 'red' }); // replaces 'primary' key in colors object
const extendColors = genColors({ red: 'red' }); // adds 'red' key to colors object
const overrideColors = genColors({ override: { red: 'red' }}); // replaces color object with override object

We can do similar things with more complicated tokens. Checkout the tests for the button component and breakpoints to see how sf handles overrides and merges.

tl;dr

  • Put object in, get object out
  • Merge your object or
  • Override tokens wholesale

patterns

merge styles // map variants

const componentStyle = {
  ...objectStyles,
  variants: [
    {
      key: props => variant({})
    }
  ]
}

const stylesForEmotion = mergeStyles(defaultStyles, props); // returns objectStyles

Using this shape, we can merge the object styles and reduce the variants into something emotion can consume. Additionally, this allows us to use the props available at the component level to generate particular variants.

const Button = styled.button`
  ${props => mergeStyles(defaultStyles, props)}
`;

Because mergeStyles() just returns a styles object, we can opt-in to variants at the component level. We can also add additional styles beyond what the variant provides.

current tokens

  • tokens
    • colors
    • borders
    • breakpoints
    • radii
    • space
    • typography
  • markdown
    • text (p, li, ol, blockquote, small, link)
    • headings
  • components
    • button
    • tag
    • text input