GithubHelp home page GithubHelp logo

alfredosalzillo / masquerades Goto Github PK

View Code? Open in Web Editor NEW
23.0 4.0 3.0 70 KB

A library for styled web components.

JavaScript 100.00%
web-components styled-components styled-system css javascript html5 html-css-javascript

masquerades's Introduction

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;
  `}
`

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 {
      /*...*/
    }
  `}
`

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

Compatibility

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

masquerades's People

Contributors

alfredosalzillo avatar kcmr avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

masquerades's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.