Digits
A beautiful Digits that can make your life easier and also looks good.
import { Digits } from "@schukai/monster/source/components/form/digits.mjs";Introduction
The <monster-digits> component is a flexible and user-friendly form control designed for numeric input fields with fixed-length digit sequences. It offers both precision and improved user interaction by separating input into individual digit fields, allowing for better readability and input validation. Whether you're building forms, verification steps, or PIN inputs, this component is crafted to enhance user experience in both simple and complex web applications.
Key Features
- Separated digit inputs: Each digit is rendered as an individual input element, providing clear visual separation and reducing input errors.
- Customizable character set: Define allowed characters using the
characterSetoption, supporting use cases beyond numeric input. - Validation feedback: Automatically validates user input and provides real-time visual feedback for invalid characters.
- Clipboard paste support: Intelligent paste handling allows users to paste complete values, which are then distributed across the digit fields.
- Keyboard navigation: Built-in support for arrow key navigation and backspace behavior to mimic natural input flow.
- Programmatic control: Access and modify the value programmatically using the
valueproperty, and control focus behavior with methods likefocus()andblur().
Improving the user experience
The <monster-digits> component transforms traditional input interactions into a cleaner and more structured experience. By isolating digits into individual fields, users can focus more easily on their input, which is especially beneficial for PIN entry, security codes, or verification steps. Instant validation and navigation controls help prevent frustration and guide users toward successful interaction.
This improvement in usability supports increased form completion rates and reduces user errors, backed by usability best practices for input segmentation and feedback.
Efficiency in the development process
The component is easy to integrate into any modern web application that uses Monster.js. It follows standard web practices and is fully configurable using HTML attributes or JavaScript. The data-monster-options attribute allows developers to declaratively define behaviors, while lifecycle methods and observers ensure responsive updates when values change. Its modular nature promotes reusability and maintainability, making it a valuable addition to any frontend project.
Digits Simple
This example demonstrates the use of the <monster-digits> component with a custom digit count. The data-monster-option-digits attribute is used to define how many individual input fields should be displayed.
In this case, the component will render exactly 8 separate input fields, allowing the user to enter an 8-character string. This setup is ideal for use cases such as long access codes, extended PINs, or serial numbers where each character needs to be entered and validated individually.
Each input field accepts a single character based on the defined character set. By default, this is limited to digits (0123456789). The component will automatically validate the input and provide visual feedback if any invalid characters are entered.
You can further customize the component using other options such as data-monster-option-character-set or by applying CSS through exported parts like control and digits.
Javascript
import '@schukai/monster/source/components/form/digits.mjs';HTML
<monster-digits data-monster-option-digits="8">
</monster-digits>Stylesheet
/** no additional stylesheet is defined **/Component Design
The <monster-digits> component is built using the Shadow DOM to ensure proper encapsulation of its internal logic, layout, and styling. By using a shadow root, the internal structure and style definitions are isolated from the rest of the document. This prevents external styles or scripts from unintentionally altering the component’s behavior or layout.
Shadow DOM and Accessibility
The use of a shadow root means that internal elements such as individual digit inputs are not directly accessible or styleable from outside the component. This protects the structure of the component and maintains consistency in its appearance and interactivity. However, all input elements within the component remain accessible through keyboard navigation, and the component is designed to support assistive technologies.
Customizing Through Exported Parts
To support visual customization while preserving encapsulation, specific parts of the component are exported using the part attribute. These parts can be styled externally using the ::part() selector in CSS.
Available Part Attributes
control: Represents the outer container of the digit input field area.digits: Wraps the set of digit input elements, rendered dynamically based on the configured digit count.
Example of customizing the appearance of the control and digit areas using exported parts:
monster-digits::part(control) {
border: 1px solid #ccc;
padding: 8px;
border-radius: 4px;
}
monster-digits::part(digits) {
gap: 6px;
display: grid;
}
Explanation of the Example
monster-digits::part(control): Adds a border and padding to the entire control container.monster-digits::part(digits): Adjusts the spacing between individual digit input fields and ensures a consistent grid layout.
Accessibility
Accessibility is an important part of the <monster-digits> design. The component supports full keyboard navigation using arrow keys and backspace behavior that mimics native input fields. Input validation provides visual feedback for invalid characters, and users can paste full sequences via clipboard support. These features enhance the experience for users with assistive needs and align with modern accessibility best practices.
HTML Structure
<monster-digits></monster-digits>JavaScript Initialization
const element = document.createElement('monster-digits');
document.body.appendChild(element);Exported
DigitsDerived from
CustomControlOptions
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.
- 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.
Constructor
constructor()State query methods
value()- {string}
e = document.querySelector('monster-toggle-switch'); console.log(e.value) // ↦ on``value(value)value
e = document.querySelector('monster-toggle-switch'); e.value="on"``Static methods
[instanceSymbol]()instanceof operator.getCSSStyleSheet()- {CSSStyleSheet[]}
getTag()- {string}
Lifecycle methods
Lifecycle methods are called by the environment and are usually not intended to be called directly.
[assembleMethodSymbol]()- {Components.Form.Digits
Other methods
blur()- {void}
focus(options)options
- {void}
Events
This component does not fire any public events. It may fire events that are inherited from its parent classes.