Skip to content

Spinner

The Spinner component provides a numeric input field with increment and decrement buttons, allowing users to easily adjust numeric values.

The Spinner component depends on:

  • Input Common component
  • Button component
<input data-role="spinner">
<input data-role="spinner" data-default-value="10" data-step="5" data-min-value="0" data-max-value="100">
<input data-role="spinner" data-label="Quantity">
ParameterTypeDefaultDescription
spinnerDeferrednumber0Deferred initialization time in milliseconds
labelstring""Label for the spinner
stepnumber1Step value for increment/decrement
plusIconstring”+“Icon for the increment button
minusIconstring”-“Icon for the decrement button
buttonsPositionstring”default”Position of the buttons: “default”, “left”, or “right”
defaultValuenumber0Default value for the spinner
minValuenumbernullMinimum allowed value
maxValuenumbernullMaximum allowed value
fixednumber0Number of decimal places
repeatThresholdnumber2000Threshold for repeating increment/decrement when button is held
hideCursorbooleanfalseWhether to hide the cursor in the input field
clsSpinnerstring""Additional CSS class for the spinner container
clsSpinnerInputstring""Additional CSS class for the input element
clsSpinnerButtonstring""Additional CSS class for both buttons
clsSpinnerButtonPlusstring""Additional CSS class for the increment button
clsSpinnerButtonMinusstring""Additional CSS class for the decrement button
clsLabelstring""Additional CSS class for the label
  • val(value) - Gets or sets the current value of the spinner.
  • toDefault() - Resets the spinner to its default value.
  • disable() - Disables the spinner.
  • enable() - Enables the spinner.
  • toggleState() - Toggles the enabled/disabled state of the spinner.
// Get the spinner instance
const spinner = Metro.getPlugin('#my-spinner', 'spinner');
// Get current value
const value = spinner.val();
// Set value
spinner.val(10);
// Reset to default value
spinner.toDefault();
// Disable spinner
spinner.disable();
// Enable spinner
spinner.enable();
// Toggle state
spinner.toggleState();
EventDescription
onBeforeChangeTriggered before the value changes (return false to cancel)
onChangeTriggered when the value changes
onPlusClickTriggered when the plus button is clicked
onMinusClickTriggered when the minus button is clicked
onArrowUpTriggered when the up arrow key is pressed
onArrowDownTriggered when the down arrow key is pressed
onButtonClickTriggered when any button is clicked
onArrowClickTriggered when any arrow key is pressed
onSpinnerCreateTriggered when the spinner is created
// Using jQuery-style event binding
$("@spinner").on("arrow-click", function(e){
console.log("Arrow " + (e.detail.button === "plus" ? "up" : "down") + " pressed");
});
// Using data attributes
<input data-role="spinner" data-on-change="console.log('Value changed to: ' + arguments[0].detail.val)">
VariableDefault (Light)Dark ModeDescription
--spinner-border-radius4px4pxBorder radius for the spinner
/* Custom styling example */
#my-spinner {
--spinner-border-radius: 8px;
}
  • .spinner - Main container class (automatically added)
  • .spinner-button - Base class for buttons
  • .spinner-button-plus - Plus button
  • .spinner-button-minus - Minus button
  • .buttons-left - Positions both buttons on the left
  • .buttons-right - Positions both buttons on the right
  • .pill-input - Makes the spinner and buttons rounded (pill-shaped)
  • .input-small - Small size variant
  • .input-large - Large size variant

The Spinner component supports different sizes:

<input data-role="spinner" class="input-large">
<input data-role="spinner">
<input data-role="spinner" class="input-small">

You can change the position of the buttons:

<input data-role="spinner" data-buttons-position="default">
<input data-role="spinner" data-buttons-position="left">
<input data-role="spinner" data-buttons-position="right">