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

@trousers/collector

v3.0.2

Published

`@trousers/collector` is a style collector inspired by the principles of the BEM methodology.

Downloads

10

Readme

@trousers/collector

@trousers/collector is a style collector inspired by the principles of the BEM methodology.

It's recommended to use this style collector when want to express styles across multiple states and appearances in a semantic way. If that's not the case, consier using css or a plain-ol' object.

Note: If the API of this style collector isn't to your liking, you can easily implement your own! Please see this guide.

API 🤖

styleCollector()

The styleCollector() function is designed to collect style definitions and provide some portability. If you deside to define CSS in another file, you can do and re-import it into your component.

NOTE: styleCollector return methods will always return this, which means the calls can be chained repeatedly.

Arugments:

  • componentName: String

Returns:

  • styleCollector().element
  • styleCollector().modifier(predicate)
  • styleCollector().get()

styleCollector().element

A function which accepts a Tagged Template.

You should treat element blocks like you would with Elements in BEM.

  • The element name describes its purpose ("What is this?" — item, text, etc.), not its state ("What type, or what does it look like?" — red, big, etc.).
  • The structure of an element's full name is block-nameelement-name. The element name is separated from the block name with a double underscore ().
  • The block name defines the namespace, which guarantees that the elements are dependent on the block (block__elem)
  • A block can have a nested structure of elements in the DOM tree

Arugments:

  • taggedTemplate: TaggedTemplate

Example:

import { styleCollector } from 'trousers';

const styles = styleCollector('button').element`
        background-color: red;
    `;

styleCollector().modifier(modifierName, predicate)

A function that accepts a predicate function or boolean and returns a new function which accepts a tagged template. The tagged template will only be rendered if the predicate returns a truthy value.

Note: Modifiers are dependant on order. Be sure to organise the order of your modifiers with the understanding that the bottom most modifier will potentially be overriding the style rules defined in the modifiers and elements declared before it.

Modifiers follow the same methodology as Modifiers in BEM.

  • Defines the appearance, state, or behavior of a block or element
  • A modifier can't be used alone, a modifier can't be used in isolation from the modified block or element. A modifier should change the appearance, behavior, or state of the entity, not replace it
  • You can have one or multiple modifiers active at any time
  • The modifier name describes its appearance ("What size?" or "Which theme?" and so on — size_s or theme_islands), its state ("How is it different from the others?" — disabled, focused, etc.) and its behavior ("How does it behave?" or "How does it respond to the user?" — such as directions_left-top)

Arguments:

  • modifierName: (optional) string
  • predicate: boolean | Function(props, state) => boolean

Returns:

  • Function(TaggedTemplate)

Example:

import { styleCollector } from 'trousers';

const styles = (props, state) => styleCollector('button').element``.modifier(
    props.primary,
)`
        background-color: yellow;
    `.modifier('active', state.isActive)`
        background-color: purple;
    `.modifier('disabled', props.isDisabled)`
        background-color: grey;
    `;

styleCollector().get()

Outputs the collected styleDefinitions. StyleDefintions is an array of objects that trousers passes around internally.

StyleDefinition:

{
    styles: TemplateStringsArray;
    expressions: number | string | Function(props) => number | string;
    predicate?: Predicate<Props>;
}

Returns:

  • styleDefinitions: StyleDefinition[];

Example:

import { styleCollector } from 'trousers';

const styles = styleCollector('button')
    .element``
    .modifier(...)``;

styles.get();