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

style-wrap

v1.3.0

Published

A framework-agnostic web component to simplify styling HTML elements.

Downloads

2

Readme

style-wrap

Framework-agnostic element styling.

npm i style-wrap

import StyleWrap from "style-wrap";
StyleWrap.register(/* options */);

How it works:

Basically the element you wanna style, you wrap in a style-wrap element. Next, you can add attributes to the style-wrap which will target the element it wraps.

<style-wrap id="wrap" color="red" color.hover="blue" padding="8px">
    <p>red colored text with 8px padding</p>
</style-wrap>

These attributes can be changed later (possibly with setAttribute or reactive data binding using a framework like React or Vue):

document.getElementById("wrap").setAttribute("color", "green"); // color: green; by default
document.getElementById("wrap").setAttribute("color.hover", "pink"); // color: pink; on hover

Variable system:

style-wrap has first-class support for reactive CSS variables, allowing one to easily create and update structured, continuous design systems.

Global variables:

You can declare global variables in the register function:

StyleWrap.register({
    globals: {
        primary: "#003366",
        success: "forestgreen",
        link: "cornflowerblue",
    },
});

You can then make use of these globals in your style-wrap element using the _ prefix (kinda like $ in SASS):

<style-wrap color="_primary">
    <p>this has primary text color</p>
</style-wrap>

If you still have access to the StyleWrap class, you can change the values of global variables on the fly, and the changes will reflect everywhere they're used.

StyleWrap.globalStyles.set("primary", "black");

Not-so-global variables:

You can declare new variables within style-wrap elements and use these directly within your CSS, this is useful for reusable components where only a few CSS rules are customizable.

Create an attribute on the style-wrap element using the _ prefix (similar to above):

<style-wrap _active="cornflowerblue">
    <button>hello im dynamic to the _active variable</button>
</style-wrap>
button {
    background: white;
    color: var(--active); /* use the CSS variable syntax to access the variable. */
}

button:hover {
    color: white;
    background: var(--active);
}

Now I can create differently-styled buttons with minimal code in the markup itself. While I could make a custom class for each possible combination, over style-wrap, this approach allows for easier extensibility, while still keeping code clean and concise.

Lastly, you can use these variables exactly how you used global variables:

<style-wrap>
    <a _active-color="red" background="_active-color"> hey! </a>
</style-wrap>

States:

An intuitive way to style different CSS states (such as :hover, :focus, etc...):

<style-wrap
    color="gray"
    color:hover="black"
    transition="color 250ms ease-in-out"
>
    <a> hover me to change the color! </a>
</style-wrap>

Quirks:

Since this is a custom element, over a JavaScript component, style-wraps have an effect on the outputted HTML markup. In other words, you will see <style-wrap> tags in your devtools/inspector tab.

As a result, if you're not careful to remember this, you could ignore that they are there and break the flow of your markup.

If you directly need to style a style-wrap, you can add an id, class, or style attribute to it. These 3 attributes are ignored and won't add CSS rules to the child element.

This is similar to Svelte's --style-props, but the existence of a wrapper element is explicit in our case, rather than implicitly added at build time.

Disclaimer:

This concept of attribute-based CSS rules may seem like a budget version of TailwindCSS (lol). However, this library is in no way meant to be a Tailwind or CSS replacement. Rather, it's meant to extend regular CSS (or Tailwind) with that sprinkle of reactivity that allows for customization and extensibility.

In principle, my recommendation for this library is to continue writing CSS (or Tailwind) wherever you can, and use this wherever you need to wield the power of CSS variables or want to write dynamic components where the CSS is as customizable as the JavaScript data (which is why this library coexists beautifully with frameworks such as React and Vue).

Enjoy!

Leave star pls. <3