Quantity

A beautiful quantity control with increment and decrement buttons

Import
the javascript logo
import { Quantity } from "@schukai/monster/source/components/form/quantity.mjs";
Source
the git logo
Package
the npm logo
Since
4.41.0

Introduction

The <monster-quantity> component is a modern and user-friendly numeric input control featuring increment and decrement buttons. It provides fine-grained control over numerical input, with support for minimum and maximum bounds, precision rounding, and manual input. Ideal for forms where users need to adjust quantities with ease, it combines visual clarity with accessibility and configurability.

Key Features

  • Increment and decrement buttons: Allows users to change values with a click, supporting both mouse and keyboard navigation.
  • Press-and-hold acceleration: Holding down a button speeds up value changes for efficient adjustments.
  • Manual input with validation: Supports direct typing when enabled, with real-time clamping and precision control.
  • Custom icons and classes: Use your own SVG paths and CSS classes to visually tailor the component.
  • Minimum, maximum, and step options: Set precise boundaries and increments for full control over valid values.
  • Precision rounding: Automatically rounds to a fixed number of decimal places if needed.
  • Accessible keyboard behavior: Use ArrowUp and ArrowDown keys to adjust the value via keyboard.
  • Fully reactive: Updates internal state, form value, and emits change events when the value changes.

Improving the user experience

The <monster-quantity> component enhances traditional number inputs by combining visual buttons with intelligent behavior such as clamping, step incrementing, and acceleration on press-and-hold. It ensures a consistent and predictable experience across desktop and mobile environments while enabling better control over input ranges and formats.

This component is especially useful in contexts like product quantity selectors, numeric rating inputs, or any scenario where precise numeric control improves usability and reduces input errors.

Efficiency in the development process

Built with Monster.js conventions, the <monster-quantity> component is easy to integrate and configure via HTML attributes or JavaScript. Developers can define options declaratively using the data-monster-options attribute or manipulate the value dynamically using the value property. Support for dynamic features like enabling/disabling manual input, customizing the template, and enforcing bounds makes it versatile in a wide range of applications.

The component adheres to best practices in accessibility and component lifecycle, ensuring high maintainability, responsiveness, and reusability across projects.

Quantity Simple

Javascript

/** this example does not use an extra script **/

HTML

/** this example does not use an extra html file **/

Stylesheet

/** no additional stylesheet is defined **/

HTML Structure

<monster-quantity></monster-quantity>

JavaScript Initialization

const element = document.createElement('monster-quantity');
document.body.appendChild(element);

Exported

Quantity

Derived from

CustomControl

Options

The Options listed in this section are defined directly within the class. This class is derived from several parent classes, including the CustomElement class. Therefore, it inherits Options from these parent classes. If you cannot find a specific Options in this list, we recommend consulting the documentation of the CustomControl.

Option
Type
Default
Description
object
templates
templates.main
string
undefined
Main template
object
templateMapping
templateMapping.plus
string
undefined
Icon (SVG-Path) Plus
templateMapping.minus
string
undefined
Icon (SVG-Path) Minus
classes
object
CSS classes
classes.button
string
monster-button-outline-primary
Button class (e.g. monster-button-outline-primary)
classes.input
string
Additional class for input
features
object
Feature toggles
features.editable
boolean
true
Allow manual input
features.hold
boolean
true
Press-and-hold accelerates
features.enforceBounds
boolean
true
Clamp value when manual input is out of bounds
value
number
0
Current value
min
number
0
Use Number.NEGATIVE_INFINITY and Number.POSITIVE_INFINITY for no bounds
max
number
undefined
Use Number.NEGATIVE_INFINITY and Number.POSITIVE_INFINITY for no bounds
step
number
1
Increment/decrement step
precision
number
Round to N decimal places (null = no explicit rounding)
disabled
boolean
false
Disable the input field (also disables manual input)
placeholder
string
Placeholder text
inputmode
string
decimal
For mobile keyboards

  • since
  • deprecated

Properties and Attributes

The Properties and Attributes listed in this section are defined directly within the class. This class is derived from several parent classes, including the CustomElement class and ultimately from HTMLElement. Therefore, it inherits Properties and Attributes from these parent classes. If you cannot find a specific Properties and Attributes in this list, we recommend consulting the documentation of the CustomControl.

  • data-monster-options: Sets the configuration options for the collapse component when used as an HTML attribute.
  • data-monster-option-[name]: Sets the value of the configuration option [name] for the collapse component when used as an HTML attribute.

Methods

The methods listed in this section are defined directly within the class. This class is derived from several parent classes, including the CustomElement class and ultimately from HTMLElement. Therefore, it inherits methods from these parent classes. If you cannot find a specific method in this list, we recommend consulting the documentation of the CustomControl.

State query methods

value()
Returns
  • {number|null}
Current numeric value
value(v)
Parameters
  • v {number|string|null}: v
Sets the value programmatically (including clamping & FormValue)

Static methods

[instanceSymbol]()4.41.0
Events
  • monster-quantity-change
This Control shows an input field with increment and decrement buttons.
getCSSStyleSheet()
If you want a stylesheet, return it here.
getTag()
Options

Lifecycle methods

Lifecycle methods are called by the environment and are usually not intended to be called directly.

[assembleMethodSymbol]()4.41.0
Events
  • monster-quantity-change
This Control shows an input field with increment and decrement buttons.

Events

The component emits the following events:

  • monster-quantity-change
  • monster-quantity-change
  • monster-quantity-change

For more information on how to handle events, see the mdn documentation.

The current width of the area is too small to display the content correctly.