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

@sightread/flake-determined

v0.6.5

Published

This is a different implementation of [@sightread/flake](https://www.npmjs.com/package/@sightread/flake). In this library, classNames are determinstic (hashes the class definition), and you will insert the css to be added yourself. This often has better r

Downloads

2

Readme

flake-determined

This is a different implementation of @sightread/flake. In this library, classNames are determinstic (hashes the class definition), and you will insert the css to be added yourself. This often has better results with SSG and SSR, and gives you more control.

A minimalist css in javascript renderer. Our mission is to be as small as possible while supporting all of the important use cases.

Features

  • media queries
  • pseudo-classes/pseudo-selectors
  • your favorite css properties
  • SSR
  • no dependencies
  • written in typescript

Installation

$ npm i @sightread/flake-determined
or
$ yarn add @sightread/flake-determined

Usage

import { css } from "@sightread/flake-determined";

const flake = css({
  ...
},
// optional second arg (default true) for caching classes
true | false | undefined
)

calling css function returns an object:

  {
    // key, value mapping with [your key]: class name string
    classes :  { ... },
    // the css string compiled from the css definitions
    append: "",
    // a hash key that corresponds to the append css string.
    id: ""
  }

Example:

import { css } from "@sightread/flake-determined";

const flake = css({
  headerItem: {
    color: "blue",
    fontSize: "24px",
  },
});

function MyComponent() {
  // ... react stuff

  return (
    <div>
      <h1 className={flake.classes.headerItem}>It works!</h1>
      {flake.append && <style>{flake.append}</style>}
    </div>
  );
}

css function returns the classes as well as the css string to insert yourself. Since the classes are deterministic, multiple instances of a component will only return the append string once. After the first addition of a component, append will be an empty string. Also individual class definitions are cached so multiple calls to css with the same definitions will not return css.

This is done by creating a hash for each class definition. If this hash key has been seen before, the css string does not have to be added again.

Example:

import { css } from "@sightread/flake-determined";

const flake = css({
  button: {
    backgroundColor: "white"
    border: "1px solid lightgrey",
    borderRadius: 5,
    fontSize: 15,
  },
  header: {
    color: "blue",
    fontSize: "24px",
  },
});

function MyComponent1() {
  // ... react stuff

  return (
    <div>
      <style>{flake.append}</style>
      <h1 className={flake.classes.header}>It works!</h1>
      <button className={flake.classes.button}>Click me</button>
      {/* or:
        {flake.append && <style>{flake.append}</style>}
      */}
    </div>
  );
}

/* since the definition of
header: {
  color: "blue",
  fontSize: "24px"
}
has been seen before, this call to css will
return {
  ...
  classes: {
    header: "header-[hash]"
  }
  append: "",
  id: 0,
}
*/
const flake2 = css({
  header: {
    color: "blue",
    fontSize: "24px",
  },
})

function OtherHeader() {
  return (
    <>
      {flake.append && <style>{flake.append}</style>}
      <h2 className={flake.classes.header}>It works too!</h2>
    </>
  )
}

function App() {
  return (
    <div>
      <MyComponent1/>
      <OtherHeader/>
  </div>)
}

Turn off caching

If you would like to tell flake to always return a full append string, pass false as a second argument to css.

With nextjs

A convenient way to return style tags in the dom is to use Head with the flake id as the key.

import Head from "next/head"

// same as previous example
function MyComponent() {

  return (
    <div>
      {/* same as previous example */}
      <Head>
        <style key={flake.id}>{flake.append}</style>
      </Head>
    </div>
  );

Peudoclasses

const flake = css({
    navLink: {
      color: "blue",
      "&:hover": {
        color: "black",
        textDecoration: "underline"
      },
      "&:focus": {
        color: "black"
      }
    }

  },
})

Selectors


const flake = css({
  container: {
    '& p:nth-child(odd)': {
      fontSize: 18,
      color: 'lightgrey',
      transition: '300ms',
    },
  }
})
...
...
<div className={flake.classes.container}>
  <p>paragraph one</p>
  <p>paragraph two</p>
  <p>paragraph three</p>
  <p>paragraph four</p>
</div>

Media Queries

We provide a small api for media queries for classes. The following example is equivalent to:

@media only screen and (min-width: 900px) {
  .container {
    width: 50%;
  }
}
@media only screen and (max-width: 900px) {
  .container {
    width: 100%;
  }
}
import { mediaQuery } from "@sightread/flake-determined";

const flake = css({
  container: {
    [mediaQuery.up(900)]: {
      width: "50%",
    },
    [mediaQuery.down(900)]: {
      width: "100%",
    },
  },
});

Combining multiple classes

Often you may want to use multiple defined classes for one element, or conditionally use classes. For either of these, use our classNames function.

classNames takes a variable number of arguments of either type string or object. If the argument is a string, it will be concatonated the the return string. If the parameter is an object, the associated keys of truthy values will be added.

In the example bellow, the button will have the classes associated with button and btnPrimary while error is false, else button and btnDisabled.

import { classNames } from "@sightread/flake-determined";

const classes = css({
  button: {
    padding: "5px 10px",
    fontSize: "16px",
    color: "white",
  },
  btnPrimary: {
    backgroundColor: "blue",
  },
  btnDisabled: {
    backgroundColor: "red",
  },
});

function MyComponent() {
  const [error, setError] = useState(false);
  // .. react stuff..

  return (
    <div>
      ...
      <button
        className={classNames(classes.button, {
          [classes.btnPrimary]: !error,
          [classes.btnDisabled]: error,
        })}
      >
        Submit
      </button>
    </div>
  );
}

License

MIT