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

context-selector

v0.1.1

Published

Find elements relative other by CSS selector

Downloads

10

Readme

context-selector

NPM version Build Status Downloads License

Find elements relative other by CSS selector.

Russian readme

Install

npm install context-selector

Usage

<div class="accord">
    <div class="accord__item">
        <button class="accord__btn" type="button">Expand</button>
        <div class="accord__content">item 1</div>
    </div>
    <div class="accord__item">
        <button class="accord__btn" type="button">Expand</button>
        <div class="accord__content">item 2</div>
    </div>
</div>
import ContextSelector from 'context-selector';

document.addEventListener('click', function(e) {
    let btn = e.target.closest('.accord__btn');
    if (btn) {
        ContextSelector.one(btn, '.accord__item << .accord__content').classList.add('accord__content_active');
    }
});

Documentation

Selectors

The library provides extended CSS syntax for searching items relative to context element. The syntax consists of two parts separated by operator <<. First part of the selector selects top context for the search element by second part of selector. First part can take the following values:

  • $document or empty first part - selector becomes the normal selector on the document;
  • $context - search by selector inside context element;
  • any css-selector - search closest element of context element and search for it.

Second part is simple CSS-selector. If second part is missing, top context returned by first part of the selector is returned.

Import types

There are several entry points for importing a library:

  • import ContextSelector from 'context-selector' - similarly with-shims;
  • import ContextSelector from 'context-selector/standard' - easy import without polyfills for ie11;
  • import ContextSelector from 'context-selector/with-shims' - import with shims for ie11;
  • import ContextSelector from 'context-selector/with-polyfills' - import with polyfill for ie11;

Differences shims from polyfills you can read in polyshim package.

When directly include the script from the dist folder to the browser, you can get an ContextSelector instance via window.ContextSelector.default.

Methods

  • one(context, selector) - search first element by extended selector
    • context {Element} - context element
    • selector {String} - extended CSS-selector
    • @return {Element|null} - returns target element or null
  • all(context, selector) - search all elements by extended selector
    • context {Element} - context element
    • selector {String} - extended CSS-selector
    • @return {Element[]} - returns array of target elements
  • setShim([setClosest]) - sets shims for non-cross-browser methods
    • setClosest {Function|null} - shim for Element.prototype.closest

Testing

To run tests, use:

npm test

If necessary, you can install launchers for other browsers and activate them in karma.conf.js:

npm i --save-dev karma-ie-launcher

Browsers support

  • Internet Explorer 11+
  • Other modern browsers