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

@daign/style-sheets

v1.1.1

Published

Simple style sheet processor.

Downloads

1,221

Readme

daign-style-sheets

CI Coverage NPM package

Simple style sheet processor.

Installation

npm install @daign/style-sheets --save

Usage

What is it for?

Given you have a tree like data structure that you want to render in the browser. You might want to use CSS if you are using SVG to display your objects. But if you also want to support rendering on the canvas element you can't use CSS, because canvas does not support it for its graphic elements. This is where this library comes into play. Create a CSS-like style sheet, assign class names to your objects and let the library calculate which style to apply to each element.

Creating the style class

The library does not define which attributes you can use in a style. You have to define this yourself by implementing the IStyleDeclaration interface.

import { IStyleDeclaration } from '@daign/style-sheets';

/**
 * Implementation of IStyleDeclaration.
 */
class MyStyle implements IStyleDeclaration {
  public fill: string | null = null;
  public stroke: string | null = null;

  /**
   * Returns whether the declaration is empty (all attributes are equal null).
   */
  public get isEmpty(): boolean {
    return ( this.fill === null && this.stroke === null );
  }

  /**
   * Constructor.
   */
  public constructor( fill?: string, stroke?: string ) {
    if ( fill ) {
      this.fill = fill;
    }
    if ( stroke ) {
      this.stroke = stroke;
    }
  }

  /**
   * Parse the value of an attribute from string.
   * @param name The name of the attribute.
   * @param value The value as a string.
   */
  public parseAttribute( name: string, value: string ): void {
    // If your attributes have a different type than string, then define how they should be parsed.
    if ( name === 'fill' ) {
      this.fill = value;
    } else if ( name === 'stroke' ) {
      this.stroke = value;
    }
  }

  /**
   * Copy style attributes from given style declaration but don't override already existing values.
   * @param declaration The style declaration whose values to use.
   */
  public complementWith( declaration: MyStyle ): void {
    if ( this.fill === null ) {
      this.fill = declaration.fill;
    }
    if ( this.stroke === null ) {
      this.stroke = declaration.stroke;
    }
  }
}

Defining a style sheet

The style sheet only supports a very minimal set of instructions. For selectors only class names are allowed. Nesting has to be done in a SCSS like syntax. Create the style sheet as a string or read it from file.

import { StyleSheet } from '@daign/style-sheet';

const styleSheet = new StyleSheet<MyStyle>();
styleSheet.parseFromString(
  `.button.active {
    .rectangle {
      fill: light-blue;
      stroke: blue;
    }
    .text {
      fill: black;
    }
  }
  .button.inactive {
    .rectangle {
      fill: light-gray;
      stroke: gray;
    }
    .text {
      fill: gray;
    }
  }
  .button.hidden {
    fill: none;
    stroke: none;
  }`, MyStyle
);

Constructing the selector chain

Construct a selector chain from your document tree for each element to render.

If your document would look like this when expressed in XML:

<elem class="document">
  <elem class="button active">
    <elem class="rectangle">
      <elem class="text">
        Click me!
      </elem>
    </elem>
  </elem>
</elem>

Then the selector chain for the text element should be created like this:

import { StyleSelector, StyleSelectorChain } from '@daign/style-sheet';

const selectorChain = new StyleSelectorChain();
selectorChain.addSelector( new StyleSelector( '.document' ) );
selectorChain.addSelector( new StyleSelector( '.button.active' ) );
selectorChain.addSelector( new StyleSelector( '.rectangle' ) );
selectorChain.addSelector( new StyleSelector( '.text' ) );
// Or use .addSelectorToFront() when constructing the other way around.

Calculating the style

The priority logic of which rule applies first should be the same as in CSS. If you find any error in the calculation please look at our unit tests to see if we have wrongly defined test cases or are missing some.

import { StyleProcessor } from '@daign/style-sheet';

const styleProcessor = new StyleProcessor<MyStyle>();

const result = styleProcessor.calculateStyle( styleSheet, selectorChain, MyStyle );
console.log( result.fill, result.stroke );

Scripts

# Build
npm run build

# Run lint analysis
npm run lint

# Run unit tests with code coverage
npm run test

# Get a full lcov report
npm run coverage