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

dual-range-bar

v1.1.0

Published

A dual range slider/scrolling bar without dependency

Downloads

851

Readme

Dual range bar

A minimal scrolling bar with two sliders that can be used for range scrolling/selection.

Demo: https://welai.github.io/dual-range-bar

Features

Dual range bar is something alike the timeline control in Adobe Audition/Premiere. It allows you to control a range variable via dragging operations on sliders and mouse wheel scrolling. The primary design purpose is an alternative scrollbar rather than a value input, though it can be used in both scenarios.

Dual range bar does not need any dependencies, and can work well by simply referring the script with a <script/> tag in your webpage, while at the same time it fits for modern web development paradigm, providing modules (UMD) and developed with TypeScript.

Installing

The old <script/> tag manner

You may have a quick look at the source of the demo page: index.html

Import dual range bar with a script reference declaration:

<script src="dist/dual-range-bar.min.js"></script>

The src path varies according to the location you make copy of the script.

Using modules

$ npm install dual-range-bar

And in you JavaScript or TypeScript code:

import { DualHRangeBar, DualVRangeBar } from 'dual-range-bar'

Usage

Declarative

You can create new dual range bars in a fully-declarative manner, but you will have no access to the DualRangeBar instance in JavaScript, which is usually unwanted. Anyway, initializing dual range bar with class names just works, and data properties are still accessible via data attributes of the container element.

<div class="drbar-container"></div>

This by default creates a horizontal dual range bar. Note that, initializations based on class names are executed only once, at window.onload, and only <div/>'s with no child nodes will be initialized.

To create a vertical dual range bar, adding a drbar-vertical class name to the container's class list.

<div class="drbar-container drbar-vertical"></div>

Using script

<div id="my-drbar-container"></div>

And in your JavaScript/TypeScript:

const drbar = new DualHRangeBar('my-drbar-container')

Or, this will create a vertical dual range bar:

const drbar = new DualVRangeBar('my-drbar-container')

Accessing data

Data interface of dual range bar is simple.

console.log(drbar.lower, drbar.upper, drbar.lowerBound, drbar.upperBound)

lower property is the value represented by the left/top slider. upper property is the value represented by the right/bottom slider.

lowerBound is the minimum possible value of the ranges, i.e. the leftmost/topmost value on the bar. upperBound is the maximum possible value of the ranges, i.e. the rightmost/bottommost value on the bar. A lowerBound that is larger than the upperBound is allowed.

console.log(drbar.minSpan, drbar.maxSpan)

minSpan is the minimum range span, and maxSpan is the maximum range span. Dual range bar will try to prevent the sliders from representing a range smaller than the minSpan or larger than the maxSpan. Here, the "span" is always upper - lower.

All of the properties mentioned above are mutable. But you have to ensure that you have made them correct, or unexpected behaviors may occur. Manual changes to the data properties does not emit any events.

When you have no access to the DualRangeBar instance, values are also accessible via data attributes of the container element.

console.log(parseFloat(document.getElementById('my-drbar-container').dataset.lowerBound))

Configuring

Dual range bar provides multiple configuration options for specifying initial values and styling.

const drbar = new DualHRangeBar('my-drbar-container', {
  minimizes: false, // Minimises the container when inactive
  size: 'default',  // Size of the dual range bar
  lowerBound: 0,    // Initial value for "lowerBound"
  upperBound: 1,    // Initial value for "upperBound"
  minSpan: 0.2,     // Initial value for "minSpan"
  maxSpan: 1,       // Initial value for "maxSpan"
  lower: 0,         // Initial value for "lower"
  upper: 1,         // Initial value for "upper"
  sliderColor: '#1E88A8', // Color of the slider
  sliderActive: '#08789b',// Slider color when active
  rangeColor: '#7DB9DE',  // Color of the range slider
  rangeActive: '#5da8d6', // Range slider color when active
  bgColor: '#aaaaaa44',   // Color of the background
})

When minimizes is true, the dual range bar will narrow to a thin line when the mouse pointer is out of the container.

There are four available size's for dual range bar, small, default, large and huge.

Dual range bar does not validate the color specification strings, the strings will be written directly into the CSS. When the sliderColor and rangeColor are specified, sliderActive and rangeActive will NOT change accordingly, you may have to specify the two colors manually. If you need to generate the darkened colors programmatically, chroma.js is a good choice.

Events

The event interface is quite straight-forward. When data is updated at user input, an update event is emitted from the container element. The DualRangeBar instance inherits the container's EventTarget interface, so listening to the DualRangeBar instance works the same. The emitted event is a CustomEvent object, with its detail property referring the DualRangeBar instance.

drbar.addEventListener('update', (e) => {
  console.log(e.detail.lower, e.detail.upper)
})

// This works the same
document.getElementById('my-drbar-container').addEventListener('update', 
  (e) => { console.log(e.detail.lower, e.detail.upper) })

Styling

Though styling in the configuration options of the constructor is recommended, it is also possible to add custom style sheets to achieve detailed control to the style.

To override the default styles, you can select the elements via the container's id. Eg., the container's id is my-drbar-container, to style the sliders, you need to query the #my-drbar-container .drbar-slider CSS selector. This will override the default style specification implemented using the selector .drbar-container .drbar-slider.

Most dimensions & colors are specified with CSS variables, overriding these variables will make changes to the dimensions & colors.

.drbar-container {
  /* Colors */
  --slider-color: #1E88A8;
  --range-color: #7DB9DE;
  --bg-color: #aaaaaa44;
  --slider-active: #08789b;
  --range-active: #5da8d6;
  /* Sizes */
  --slider-wh: 20px;
  --range-thick: 15px;
  --bg-thick: 10px;
  --mini-thick: 4px;
  --mini-ratio: calc(4/15);
}

Please refer to the src/style.css when advanced styling is needed.

Contributing

Pull requests are welcomed.