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

@vitoni/selector

v0.3.0

Published

A tiny DIV based selector supporting mouse and touch written in TypeScript

Downloads

7

Readme

Selector

A tiny DIV based selector supporting mouse and touch written in TypeScript.

Features

  • Modes for selecting elements
    • Elements needs to be only partially covered
    • Elements must be fully covered
  • Callback at end of selection with list of selected elements
  • Can switch between selecting and deselecting (already selected) elements

Using selector

    import { SelectionMode, Selector } from "@vitoni/selector";

    // identifies all elements which can be selected
    const selectableElementsSelector = "div.selectable";

    function handleSelected(selectedElements: HTMLElement[], selectionMode: SelectionMode) {
        // Other uses cases might change the data model which the elements visualize instead of the elements themselves.
        // The changed data model would trigger an update of the respective elements indirectly.
        selectedElements.forEach((selectedElement) => {
            if (selectionMode == SelectionMode.ADD) {
                selectedElement.classList.add(selectectedClass);
            } else {
                selectedElement.classList.remove(selectectedClass);
            }
        });
    }

    const selector = new Selector(
        selectableElementsSelector,
        handleSelected
    );

    ...

    selector.mount();
    ...
    // change mode of selection to deselect elements
    selector.setSelectionMode(SelectionMode.REMOVE);
    ...
    // change mode of selection to select elements
    selector.setSelectionMode(SelectionMode.ADD);
    ...
    // cleanup only needed for SPA
    selector.unmount();
  • mount() registers event listeners and adds a <div> element to DOM to visualize the selection area.
  • unmount() deregisters event listeners and removes the selection area <div> element from DOM.
  • The callback handleSelected is called at the end of the selection with all elements which have been marked by the selection area.
  • The SelectionMode indicates whether the elements matching the selection should be regarded as selected or deselected.

Multiple selectors

    ...

    const areaSelector = "#" + areaId;

    // Identifies an area, the selection is restricted to.
    // Useful:
    //  - when multiple selection areas are used
    //  - to show visual constrains of the selection area
    const selectablesRootSelector = areaSelector + " div.selectables-root";

    // identifies all elements which can be selected
    const selectableElementsSelector = "div.selectable";

    ...

    const selector = new Selector(
        selectableElementsSelector,
        handleSelected,
        {
            selectablesRootSelector
        }
    );

    ...
  • The selectablesRootSelector allows to restrict the selection to the area identified by the selector.

Allow selection of single elements by clicking

    ...

    // allows changes on single elements without selection / changing the selection mode
    function onClick(element: HTMLElement) {
        if (element.classList.contains(selectectedClass)) {
            element.classList.remove(selectectedClass);
        } else {
            element.classList.add(selectectedClass);
        }
    }

    const selector = new Selector(
        selectableElementsSelector,
        handleSelected,
        {
            onClick
        }
    );

    ...

Example code

The example code in setup.ts shows how all these work together and also shows usage of multiple selection areas.

Getting started with development

Install dependencies

npm install

Start local vite server

npm run dev

Linting and fixing linting errors automatically

The workspace is configured to apply ESLint rules on save. To allow linting without IDE support there are also CLI commands available:

Show linting errors:

npm run lint

Fix linting errors automatically:

npm run lint-fix

Local usage without publishing for development

Make selector package locally available:

cd selector
npm link

Use local version directly in consuming project

cd consuming-project
npm link "@vitoni/selector"