5.4.3 • Published 6 months ago

@19h47/spinbutton v5.4.3

Weekly downloads
-
License
ISC
Repository
github
Last release
6 months ago

npm.io npm.io

@19h47/spinbutton

A spinbutton is an input widget that restricts its value to a set or range of discrete values. It provides an accessible, keyboard-navigable interface for numerical input that maintains value constraints, supports internationalization through custom text labels, and emits events when values change.

Installation

Using Package Manager

The recommended way to install the Spinbutton component is through a package manager like npm or yarn:

# Using npm
npm install @19h47/spinbutton

# Using yarn
yarn add @19h47/spinbutton

Manual Download

You can also download the component directly from the GitHub repository at https://github.com/19h47/19h47-spinbutton and include the distributed files in your project.

DOM structure

The Spinbutton requires a specific HTML structure to function properly:

The spinbutton is a div element with the role of spinbutton. It contains two buttons for increasing and decreasing the value, and an input field for displaying the current value.

<div role="spinbutton" aria-valuemin="0" aria-valuemax="100" aria-valuenow="10">
	<button class="js-decrease" tabindex="-1" aria-label="decrease" type="button">-</button>

	<input type="text" />

	<button class="js-increase" tabindex="-1" aria-label="increase" type="button">+</button>

</div>

JavaScript

Here is a simple example of how to use the spinbutton.

import Spinbutton from '@19h47/spinbutton';

const $element = document.querySelector('[role="spinbutton"]');
const spinbutton = new Spinbutton($element);

spinbutton.init();

Event Handling

The Spinbutton component emits a Spinbutton.change event when the value changes:

EventArgsDescription
Spinbutton.changevalueReturn the current activate value

Here is an example of how to use the Spinbutton.change event.

import Spinbutton from '@19h47/spinbutton';

const $element = document.querySelector('[role="spinbutton"]');
const spinbutton = new Spinbutton($element);

spinbutton.init();

spinbutton.addEventListener('Spinbutton.change', event => {
	console.log(event.detail.value); // Return the current activate value
});

Configuration Options

The spinbutton accepts the following options. The options can be set as data attributes on the div element or passed as an object when creating the spinbutton instance.

OptionTypeDefaultAttributes
textobjectObject containing single and plural text.-data-spinbutton-text
stepnumberThe step value to increase or decrease the value.1data-spinbutton-step
delaynumberThe delay in milliseconds before the event is triggered.20data-spinbutton-delay

Example with options:

import Spinbutton from '@19h47/spinbutton';
const $element = document.querySelector('[role="spinbutton"]');
const options = {
	text: {
		single: 'item',
		plural: 'items'
	},
	step: 5,
	delay: 100
};
const spinbutton = new Spinbutton($element, options);
spinbutton.init();

Keyboard Support

The spin buttons provide the following keyboard support described in the spinbutton design pattern.

KeyFunction
Down ArrowDecreases value 1 (or step option if given) step.
Up ArrowIncreases the value 1 (or step option if given) step.
Page DownDecreases the value 5 (or step option multiply by 5 if given) steps.
Page UpIncreases the value 5 (or step option multiply by 5 if given) steps.
HomeDecreases to mimimum value (if given).
EndIncreases to maximum value (if given).

Role, Property, State, and Tabindex Attributes

The spinbutton uses the following ARIA roles, properties, and states. The aria-valuenow attribute is updated when the value changes. The aria-valuemin and aria-valuemax attributes are used to define the minimum and maximum values of the spinbutton.

RoleAttributeElementUsage
spinbuttondivIdentifies the div element as a spinbutton.
spinbuttonaria-valuemindivThe minimum value of the spinbutton.
spinbuttonaria-valuemaxdivThe maximum value of the spinbutton.
spinbuttonaria-valuenowdivThe current value of the spinbutton.
spinbuttonaria-valuetextdivThe text representation of the current value.

Customization Examples

Step Size

You can customize the step size:

<div role="spinbutton" data-spinbutton-step="5" aria-valuemin="5" aria-valuemax="50" aria-valuenow="5">
    <!-- buttons and input -->
</div>

Custom Text

You can provide custom text for singular and plural forms:

<div role="spinbutton" data-spinbutton-text='{"single":"barrel","plural":"barrels"}' aria-valuemin="1" aria-valuemax="100" aria-valuenow="1">
    <!-- buttons and input -->
</div>

Dynamic Control

You can dynamically change the minimum, maximum, and current values:

// Set new minimum value
spinbutton.setMin(10);

// Set new maximum value
spinbutton.setMax(200);

// Set new value
spinbutton.setValue(50);

References

Spinbutton Pattern

5.3.2

6 months ago

5.2.2

6 months ago

5.2.1

6 months ago

5.1.1

6 months ago

5.1.0

6 months ago

5.0.0

6 months ago

2.0.2

12 months ago

3.0.0

7 months ago

4.0.5

6 months ago

4.0.4

6 months ago

4.0.1

6 months ago

4.0.0

6 months ago

4.0.3

6 months ago

4.0.2

6 months ago

5.4.3

6 months ago

2.0.1

2 years ago

1.0.0

2 years ago

0.3.8

2 years ago

0.4.0

2 years ago

2.0.0

2 years ago

0.3.7

4 years ago

0.3.6

4 years ago

0.3.5

4 years ago

0.3.2

5 years ago

0.3.4

5 years ago

0.3.1

5 years ago

0.3.0

5 years ago

0.2.3

5 years ago

0.1.3

5 years ago

0.1.2

5 years ago

0.1.0

6 years ago

0.0.4

6 years ago

0.0.3

6 years ago

0.0.2

6 years ago

0.0.1

6 years ago

0.0.0

6 years ago