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

masquerades

v0.0.1-alpha.5

Published

A library for styled web components

Downloads

3

Readme

masquerades 🎭

Actions Status

A library for styled web components.

This library is a work in progress, feel free to participate to the project.

Why

Styled components are easy to write and manage. They focus only on style and anything else.

Usage

Install via npm or yarn.

npm i masquerades@next
yarn add masquerades@next

import using es modules.

import styled from 'masquerades';

// define styled components
const StyledDiv = styled.div`
  /*some css...*/
`
customElements.define('styled-div', StyledDiv, { extends: "div" });

Examples

Style Custom Elements

Style a custom component using styled(CustomComponent).

import styled from 'masquerades';

const StyledCustomComponent = styled(CustomComponent)`
  background: ${({ disabled }) => (disabled ? 'grey' : '#f1c40f')};
  color: #fff;
  border: 3px solid #fff;
  border-radius: 5px;
  padding: 0.8rem 2rem;
  font: 24px "Margarine", sans-serif;
  outline: none;
  display: block;
  letter-spacing: 2px;
  ${({ disabled }) => disabled && styled.css`
    border-radius: 15px;
  `}
`

// Define the styled button as extension of the native button
customElements.define('custom-component', StyledCustomComponent);

Style Native Elements

Shortcut for style native components are available, for example to style a button use styled.button.

import styled from 'masquerades';

// Create the button
const StyledButton = styled.button`
  background: ${({ disabled }) => (disabled ? 'grey' : '#f1c40f')};
  color: #fff;
  border: 3px solid #fff;
  border-radius: 50px;
  padding: 0.8rem 2rem;
  font: 24px "Margarine", sans-serif;
  outline: none;
  cursor: pointer;
  position: relative;
  transition: 0.2s ease-in-out;
  letter-spacing: 2px;
  ${({ disabled }) => disabled && styled.css`
    border-radius: 15px;
  `}
`
// set up observedAttributes
  .observeAttributes(['disabled']);

// Define the styled button as extension of the native button
customElements.define('styled-button', StyledButton, {
  extends: 'button',
});

Use the styled button

<button is="styled-button">Styled Button</button>
<button is="styled-button" disabled>Styled Button</button>

Style Shadow DOM

The css is injected in a stylesheet adopted by the root of the element and inside the shadow root, so it's possible to style both the light and shadow dom.

import styled from 'masquerades';

const StyledCustomComponent = styled(CustomComponent)`
  /* some Light DOM style ... */
  :global(:host(&)) {
    /* Shadow DOM style */
    selector {
      /*...*/
    }
  }
  /* or use the styled.shadow shortcut */
  ${(props) => styled.shadow`
    /* Shadow DOM style */
    selector {
      /*...*/
    }
  `}
`

// Define the styled button as extension of the native button
customElements.define('custom-component', StyledCustomComponent);

Using Theme

Use theme configure global value for styled components.

Create a Theme.

import styled, { createTheme } from 'masquerades';

const ThemeProvider = createTheme({
  primary: '#12a57a',
  accent: '#f1c40f',
});
customElements.define('theme-provider', ThemeProvider);

Use a Theme

The value of the nearest theme can be used as attribute (and props) in the styled element.

import styled from 'masquerades';

const StyledButton = styled.button`
  /* the props theme is the value of the nearest theme provider */
  background: ${({ type = 'primary', theme }) => (theme[type])};
  color: #fff;
  border: 3px solid #fff;
  border-radius: 50px;
  padding: 0.8rem 2rem;
  font: 24px "Margarine", sans-serif;
  outline: none;
  cursor: pointer;
  position: relative;
  transition: 0.2s ease-in-out;
  letter-spacing: 2px;
`.observeAttributes(['disabled']);

// Define the styled button as extension of the native button
customElements.define('styled-button', StyledButton, {
  extends: 'button',
});

Or using the static method valueOf of the theme provider class.

class CustomElement extends HTMLElement {
  constructor() {
    super();
    this.onThemeValueChange = this.onThemeValueChange.bind(this);
  }

  connectedCallback() {
    const themeProvider = ThemeProvider.of(this);
    if (themeProvider) {
      themeProvider.addEventListener('provider-value-change', this.onThemeValueChange);
    }
  }

  onThemeValueChange() {
    const value = ThemeProvider.valueOf(this);
    // do something with the theme value
  }

  disconnectCallback() {
    const themeProvider = ThemeProvider.of(this);
    if (themeProvider) {
      themeProvider.removeEventListener('provider-value-change', this.onThemeValueChange);
    }
  }
}

Add the theme in the element three.

<theme-provider .value=${theme1}>
    <div>
        <button is="styled-button">Styled Button With Theme1</button>
        <theme-provider .value=${theme2}>
            <button is="styled-button">Styled Button With Theme2</button>
        </theme-provider>
    </div>
</theme-provider>

Pros

  • works in both Light and Shadow DOM
  • works with native web component (HTMLButtonElement, HTMLDivElement, ...)
  • works with custom web component
  • write css in js
  • no need to manage class names
  • support stylis css

Hints

Webstorm support

For add support for stylus in styled tagged template, in settings > language injections > + > new generic js add:

  • Name: Styled Web Components

  • Id: Stylus

  • Prefix: .some-class {

  • Suffix: }

  • Places Patterns:

    + jsLiteralExpression().and(jsArgument("styled\\.", 0))
    + jsLiteralExpression().withParent(psiElement().withText(string().contains(".extend")))
    + jsLiteralExpression().withParent(psiElement().withText(string().startsWith("injectGlobal")))
    + jsLiteralExpression().withParent(psiElement().withText(string().startsWith("styled")))
    + taggedString("css")

Inspired by styled-components