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

@radically-straightforward/css

v1.0.7

Published

💄 CSS in Tagged Templates

Downloads

948

Readme

Radically Straightforward · CSS

💄 CSS in Tagged Templates

Installation

$ npm install --save-dev @radically-straightforward/css

Note: We recommend installing @radically-straightforward/css as a development dependency because @radically-straightforward/build can build the css`___` tagged templates away on the server and bundle the CSS framework on the browser.

Note: We recommend the following tools:

Prettier: A code formatter that supports CSS in tagged templates.

Prettier - Code formatter: A Visual Studio Code extension to use Prettier more ergonomically.

Usage

import css, { CSS } from "@radically-straightforward/css";

CSS

export type CSS = string;

A type alias to make your type annotations more specific.

css()

export default function css(
  templateStrings: TemplateStringsArray,
  ...substitutions: (CSS | CSS[])[]
): CSS;

A tagged template for CSS:

css`
  body {
    background-color: ${"red"};
  }
`;

Note: CSS is represented as strings and this tagged template works by performing string interpolation. This is conceptually simple and fast. To extract and process the CSS refer to @radically-straightforward/build.

Interpolated arrays are joined:

css`
  ${["red", "green", "blue"].map(
    (color) => css`
      .text--${color} {
        color: ${color};
      }
    `,
  )}
`;

CSS Framework

Beyond the tagged template to define CSS in JavaScript, @radically-straightforward/css also includes a CSS framework featuring a CSS reset, a design system, and helper classes.

To use this framework:

  1. Use HTML 5 and include a <meta name="viewport" /> meta tag:

    <!doctype html>
    <html lang="en">
      <head>
        <meta
          name="viewport"
          content="width=device-width, initial-scale=1, maximum-scale=1"
        />
        <!-- ... -->
      </head>
      <body>
        <!-- ... -->
      </body>
    </html>

    Note: Unfortunately there isn’t a way to set properties about the viewport from CSS, which is why the <meta name="viewport" /> meta tag is necessary.

    Note: maximum-scale=1 prevents iOS from zooming into the viewport when a form field is active. People on the internet have written that this could be an accessibility issue because it would prevent users from zooming in with a pinch gesture, but our tests reveal that this is not the case.

  2. Include @radically-straightforward/css/static/index.css at the top of your CSS:

    @import "@radically-straightforward/css/static/index.css";
    /* ... */

    Note: @radically-straightforward/javascript already includes this file. If you’re importing @radically-straightforward/javascript/static/index.css you don’t need to import @radically-straightforward/css/static/index.css as well.

  3. Learn about the framework, particularly the design system, by reading the the source.

  4. (Optional but recommended) Transpile for broader browser support and bundle your CSS. This framework uses modern CSS features, for example, CSS Nesting and properties that need to be prefixed. We recommend @radically-straightforward/build.

Related Work

Tailwind

@radically-straightforward/css follows some of the ideas popularized by Tailwind:

  • Colocate HTML and its corresponding CSS.

  • Include few opinions on style, so that every design looks unique.

  • Include a design system with a selection of spaces, colors, fonts, and so forth.

    Note: To learn more about how to use a design system, we recommend Refactoring UI and Practical Typography.

But @radically-straightforward/css is different from Tailwind in how it solves some issues:

  • Designing with constraints. The design systems in Tailwind and @radically-straightforward/css are largely identical, because we ported Tailwind’s design system. But Tailwind presents its design system as classes, and @radically-straightforward/css presents its design system as CSS variables.

    The class names in Tailwind are an extra layer of indirection that you need to learn about: at some point you will have to translate the CSS you find in MDN, blog posts, and so forth, into Tailwind class names. Copying-and-pasting doesn’t work anymore, and using third-party libraries may be awkward.

    In the best of cases the layer of indirection is small, for example, the class .float-right corresponds to the CSS float: right;. In these cases the class names simply add noise over the CSS that you would write normally.

    In the worst of cases the layer of indirection is more confusing, for example, the class .m-2 corresponds to the CSS margin: 0.5rem;. In Tailwind’s defense, abbreviations like these are used only on the most common properties, and they’re for the most part tasteful. But they’re still one extra layer of indirection, and they can be disorienting, especially to beginners, and especially when they pile up.

    In addition, customizing the design system in Tailwind involves learning about a whole customization system.

    The CSS variables in @radically-straightforward/css, on the other hand, integrate seamlessly with the CSS you already know and write, including CSS imported from third-party libraries. Copying-and-pasting snippets from the internet just works and goes well with the rest of the style in the codebase. And customizing is a matter of setting new CSS variables.

  • Responsive design and Hover, focus, and other states. Media queries, pseudo-elements, children, and so forth, are supported in @radically-straightforward/css paired with @radically-straightforward/build, all while still colocating HTML and its corresponding CSS—you can think of it as inline styles infused with the power of nesting.

Note: Another small quality-of-life gain of @radically-straightforward/build over Tailwind: Prettier formats CSS in tagged templates over multiple lines, making the code more readable than the long lines of classes in Tailwind.

Note: Tailwind’s documentation is fantastic and worth the read. It’s a great supplement to MDN, full of examples, and concentrates the most useful CSS properties all in one place. And it includes the CSS version of everything, so you can skip over the Tailwind classes and still benefit from the documentation.

Open Props

It’s a design system similar in spirit to the one included in @radically-straightforward/css, in the sense that it’s also presented a CSS variables. Open Props takes the approach of normalizing CSS across browsers, instead of resetting browser styles; it comes with more transitions and animations, and fewer sizes. It also comes with an approach of fixed breakpoints, while in @radically-straightforward/css’s style we prefer to use breakpoints case-by-case.