Maska
repository·master·Indexed 24 days ago
https://github.com/beholdr/maskaA lightweight, zero-dependency input masking library for Vanilla JS, Vue, Alpine.js, and Svelte. Maska provides real-time input formatting via the Mask and MaskInput classes and framework-specific directives like vMaska and x-maska. It supports custom tokens, dynamic, reversed, and eager masks, as well as a specialized number mask mode for currency formatting.
What's inside maska
- Maska is a simple, zero-dependency input masking library. It is designed to be lightweight (~3 Kb gzipped) and works with both native and custom HTML inputs. It supports various masking modes including dynamic, reversed, and eager masks, as well as a dedicated number mask mode for easy money formatting.
Overview of Maska components
masterMaska is a zero-dependency input mask library that provides three main ways to apply masks:
Mask: A class used for core mask processing. It can be used to mask any arbitrary string, not just input elements.MaskInput: A class specifically designed to handle user interaction with<input>elements.- Directives: Framework-specific integrations for easy use:
- Vue: Use the
vMaskadirective. - Alpine.js: Use the
xMaskadirective. - Svelte: Use the
maskaaction.
- Vue: Use the
Maska Features and Integrations
masterMaska provides a vanilla JavaScript core along with official integrations for several frameworks. Key features include:
- Framework Integrations: Vue 2, Vue 3, Alpine.js, and Svelte.
- Customization: Support for custom tokens using modifiers, transform functions, and hooks.
- Specialized Modes: Number mask mode for currency/money formatting, and support for dynamic, reversed, and eager masks.
- Compatibility: Works with native HTML inputs and custom input components.
Understand default Maska tokens
masterMaska provides three default tokens for common character types. You can use these tokens directly in your mask string. To use the literal character instead of the token, escape it with an exclamation mark (
!).Default Tokens:
#: Digits (/[0-9]/)@: Letters (/[a-zA-Z]/)*: Letters & digits (/[a-zA-Z0-9]/)
Example: Using
!#will render the literal#character instead of matching a digit.{ '#': { pattern: /[0-9]/ }, // digits '@': { pattern: /[a-zA-Z]/ }, // letters '*': { pattern: /[a-zA-Z0-9]/ }, // letters & digits }Use token modifiers (optional, multiple, repeated)
masterModifiers allow you to change how a token behaves during input. They can be applied in the Simple form of
data-maska-tokensusing the syntaxT:P:M.Modifier Description optionalThe token is not required. multipleThe token can match multiple characters until the next token starts. repeatedThe token matches only one character, but the token itself (or a group of them) can be repeated any number of times. Examples:
- Optional (IP address): Mask
#00.#00.#00.#00using0:[0-9]:optional. - Multiple (Cardholder Name): Mask
A AusingA:[A-Z]:multiple. - Repeated (Money): Mask
9 99#,##using9:[0-9]:repeated.
{ 0: { pattern: /[0-9]/, optional: true }, 9: { pattern: /[0-9]/, repeated: true }, }- Optional (IP address): Mask
How Maska components work together
masterMaska consists of three main components designed for different use cases:
Maskclass: Used to mask string values programmatically.MaskInputclass: Used to applyMaskprocessing to<input>elements in Vanilla JS environments.vMaskadirective: A Vue directive to simplify using the library within Vue components.
Understand changes to Eager mode behavior
masterIn Maska v3,
eager: truemode has changed how entered characters interact with static mask symbols.In v2, entered characters appeared after the static mask characters. In v3, entered characters are treated as if they occupy the positions of the static symbols themselves.
// v2 behavior const mask = new Mask({ mask: '1##', eager: true }) mask.masked('1') // -> 11 mask.masked('12') // -> 112 mask.masked('2') // -> 12 // v3 behavior const mask = new Mask({ mask: '1##', eager: true }) mask.masked('1') // -> 1 mask.masked('12') // -> 12 mask.masked('2') // -> 12Understand changes to dynamic mask completion
masterWhen using an array of masks (dynamic masks), the definition of a 'completed' mask has changed between versions.
- v2: A dynamic mask is considered completed only when it matches the longest mask in the array.
- v3: A dynamic mask is considered completed as soon as it matches the first mask in the array that fits the input.
Use Maska via CDN (Vue)
masterInclude the Maska Vue build from CDN and register the
vMaskadirective in your Vue application.<script src="https://cdn.jsdelivr.net/npm/maska@3/dist/cdn/vue.js"></script> <script> const { vMaska } = Maska Vue.createApp({ directives: { maska: vMaska }}).mount('#app') </script>Setup Maska in Nuxt 3
masterTo use Maska in Nuxt 3, create a plugin file in your
pluginsfolder (e.g.,plugins/maska.ts) to register the directive globally.import { vMaska } from "maska/vue" export default defineNuxtPlugin((nuxtApp) => { nuxtApp.vueApp.directive("maska", vMaska) })Handle mask events in Vue
masterIn Vue, use the
v-maskadirective on your input element. You can specify the mask pattern using thedata-maskaattribute and listen for the@maskaevent.<input v-maska data-maska="#-#" @maska="onMaska" />Define custom tokens via data-attributes
masterYou can add custom tokens to an input using the
data-maska-tokensattribute. There are two supported formats:- JSON form: A valid JSON string (supporting single or double quotes) where the pattern is a string without delimiters.
- Simple form: A string using the format
T:P:M|...where:Tis the token character.Pis the pattern in string form.Mis an optional modifier.|is the separator for multiple tokens.
Note: You cannot define a
transformfunction using thedata-maska-tokensattribute; use thetokensoption for that.