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

@onenexus/squery

v1.0.0-beta.7

Published

Interact with Synergy modules/BEM DOM elements

Downloads

4

Readme

GitHub license Inline docs Build Status npm version npm downloads codecov

Interact with Synergy modules/BEM DOM elements

Overview

sQuery is used for interacting with DOM elements that follow the Synergy naming convention.

Learn how to integrate with React components

Example
<div class="foo">
    <div class="foo_bar"><div>
    <div class="foo_bar-alpha"><div>
</div>

<div class="foo-fizz">
    <div class="foo_bar"><div>
    <div class="foo_bar-beta"><div>
</div>
sQuery('foo').getComponents('bar').addModifier('buzz');
Result
<div class="foo">
    <div class="foo_bar-buzz"><div>
    <div class="foo_bar-alpha-buzz"><div>
</div>

<div class="foo-fizz">
    <div class="foo_bar-buzz"><div>
    <div class="foo_bar-beta-buzz"><div>
</div>

Why?

In a world where DOM elements are becoming more structured and modular, querying DOM elements via classes quickly makes your code become WET, affecting things like readability, scalability and maintainability. The Synergy naming convention (as used above) keeps the DOM as clean and DRY as possible by only requiring one class per element, even if that element has modifiers (learn more).

Consider attempting something like the above using vanilla JS:

// get all `foo` modules
const foos = document.querySelectorAll('.foo, [class*="foo-"]');

foos.forEach(foo => {
    // get all `bar` components
    const bars = foo.querySelectorAll('.foo_bar, [class*="foo_bar-"]');

    bars.forEach(bar => {
        // replace old class with new class
        bar.classList.forEach(className => {
            if (className.indexOf('foo_bar') === 0) {
                bar.classList.replace(className, `${className}-buzz`);
            }
        });
    }
});

Some issues here include:

  • Repeating the module name foo
  • Repeating the component name bar
  • Awkward querySelectorAll query (required to allow the 'one class per element' paradigm)
  • Hard coded component/modifier glue (typically a non-issue, but still...)
  • Order of magnitude more code required than initial 1 line example

Something like this would be a common occurance in projects that use the Synergy naming convention (and indeed any convention that requires you to query DOM elements and manipulate their classList property, such as BEM), so by identifying and abstracting these commonly-occurring behaviours into their own API, sQuery allows you to interact with structured DOM elements effortlessly by targeting modules and components and adding/removing modifiers to them, allowing you to achieve the above with that one liner from earlier:

sQuery('foo').getComponents('bar').addModifier('buzz');

Checkout these other One-Nexus tools for working with Synergy modules:

  • Cell - Style DOM elements that follow the Synergy naming convention (including BEM) using Sass
  • Polymorph - Style DOM elements that follow the Synergy naming convention (including BEM) using JavaScript
  • Lucid - A set of Higher-Order React Components for rendering UI elements that follow the Synergy naming convention
  • Synergy - A front-end framework for creating modular, configurable and scalable UI components (all of the above in one packaged framework)

Installation & Setup

Want to render your DOM elements with React, style them with JavaScript, and interact with them using sQuery? Checkout the Synergy framework instead

npm install --save @onenexus/squery
import 'sQuery' from '@onenexus/squery';

sQuery.init(); 

See the sQuery.init() method for advanced setup

Using BEM? Checkout the Working With BEM page

API

Usage

The above APIs can be used in the following ways:

Return value of sQuery() function (Recommended)

  • Element(s) retreived via a Synergy query
  • Config automatically deteced from global Synergy object

See the sQuery() page for more information

sQuery(query)[method](...args);
Example
sQuery('.accordion').getComponents('panel');

Property of sQuery

  • Element(s) must be explicitly passed as the first argument
  • Config must be explicitly passed via the config argument
sQuery[method](node, ...args, config);
Example
sQuery.getComponents(document.querySelectorAll('.accordion'), 'panel', {
    componentGlue: '_',
    modifierGlue: '-'
});

Via Element/NodeList Prototype

This may be useful and convenient, but use with caution as it is recommended to avoid modifying JavaScript prototypes

Element[method](...args);
NodeList[method](...args);
Example
document.querySelectorAll('.accordion').getComponents('panel');