A clean integration between Cleave.js and Alpine. ✨

Overview

Help support the maintenance of this package by sponsoring me.

Alpine Mask

This packages provide a custom x-mask directive and $mask magic variable, powered by Cleave.js.

GitHub tag (latest by date) Build size Brotli Monthly downloads via CDN

This package only supports Alpine v3.x.

Installation

CDN

Include the following <script> tag in the <head> of your document, just before Alpine.

<script
    src="https://cdn.jsdelivr.net/npm/@ryangjchandler/[email protected]/dist/cdn.min.js"
    defer
></script>

NPM

npm install @ryangjchandler/alpine-mask

Add the x-mask directive and $mask magic property to your project by registering the plugin with Alpine.

import Alpine from "alpinejs";
import Mask from "@ryangjchandler/alpine-mask";

Alpine.plugin(Mask);

window.Alpine = Alpine;
window.Alpine.start();

Usage

To mask an input, add the x-mask directive to your element.

<input x-data x-mask="{ ...allMyCleaveOptionsHere }">

By default, the x-mask directive will expect a configuration object. This gives you full control over the Cleave.js instance.

There's also a list of convenience modifiers below that configure Cleave.js for you with sensible defaults.

Credit Cards

<input x-mask.card>

This will format your input as a credit card input. By default, it will separate each section of the card number into blocks separated by a space.

If you would like to support 19-digit PANs, you can add the .strict modifier to the directive.

<input x-mask.card.strict>

Date

To format your input as a date, use the .date modifier.

<input x-mask.date>

By default, Cleave will format your data in a d/m/Y pattern. If you wish to change this, provide a custom pattern inside of the directive expression.

<input x-mask.date="['Y', 'm', 'd']">

The input will now be formatted as Y/m/d.

Time

To format your input as a time, use the .time modifier.

<input x-mask.time>

By default, Cleave will format your time with the h:m:s pattern. If you wish to change this, provide a custom pattern inside of the directive expression.

<input x-mask.date="['h', 'm']">

The input will now be formatted as h:m.

Numeral / Numeric

If you would like to format a number inside of your input, you can use the .numeral modifier.

To format your input as a date, use the .date modifier.

<input x-mask.date>

By default, Cleave will format your data in a d/m/Y pattern. If you wish to change this, provide a custom pattern inside of the directive expression.

<input x-mask.numeral>

By default, Cleave formats your number using a comma (,) as the thousands-separator and a full-stop (.) as the decimal-separator.

If you would to change the thousands delimiter, you can provide either .delimiter.dot or .delimiter.comma.

<input x-mask.numeral.delimiter.dot>

The number 100,000 will now be formatted as 100.000.

If you would to provide a custom prefix (money fields, etc), you can use the .prefix modifier followed by your prefix of choice.

<input x-mask.numeral.prefix.£>

This will prefix your input with £.

NOTE: HTML attributes are case-insensitive. If you would like to use a more complex prefix or configuration, I recommend using x-mask with a custom configuration object.

Blocks

If you would like to segment your input and format the data in blocks, you can use the .blocks modifier and provide a list of block lengths as the directive expression.

<input x-mask.blocks="[3, 4, 5]">

This would input the following input 333444455555 as 333 4444 55555.

Two-way data binding

<div x-data="{ raw: 2000 }">
    <input x-mask.numeral x-model="raw" >
</div>

When this input is initialised, raw will be formatted and the value will be set on the input. Anytime the input is updated, the raw value will be synced back to the raw property.

All changes made to the raw property outside of Cleave.js (other functions, etc) will also sync back and be formatted.

Versioning

This projects follow the Semantic Versioning guidelines.

License

Copyright (c) 2021 Ryan Chandler and contributors

Licensed under the MIT license, see LICENSE.md for details.

You might also like...

Multi-step wizard helpers for Alpine.js

Multi-step wizard helpers for Alpine.js

Alpine Wizard This package provides an Alpine directive (x-wizard) and a magic helper ($wizard) that allow you to quickly build multi-step wizards usi

Dec 23, 2022

📦 Alpine JS plugin to extend the functionality of the official $persist plugin

Alpine JS Persist Extended Alpine JS magic method $storage extends the official $persist plugin to help you work with local storage 📦 Example 👀 div

Dec 28, 2022

Alpine.js Language Features (Volar) extension for coc.nvim

[Experimental] coc-volar-alpinejs fork from vscode-alpine-language-features Alpine Language Features extension for coc.nvim Note @volar/alpine-languag

Oct 12, 2022

Little Alpine.js plugin to add a typewriter effect to any HTML element.

Little Alpine.js plugin to add a typewriter effect to any HTML element.

⌨️ Alpine Typewriter ⌨️ An Alpine.js plugin to add a typewriter effect to any HTML element. 🚀 Installation CDN Include the following script tag in

Dec 28, 2022

Straightforward interactive HTTP requests from within your Alpine.JS markup

Alpine Fetch Straightforward interactive HTTP requests from within your Alpine.JS markup. View the live demo here What does this do? Alpine.JS is a ru

Dec 21, 2022

Alpine.js wrapper for @formkit/auto-animate.

💫 Alpine AutoAnimate 💫 An Alpine.js wrapper for @formkit/auto-animate. 🚀 Installation CDN Include the following script tag in the head of your

Dec 28, 2022

Babel plugin and helper functions for interoperation between Node.js native ESM and Babel ESM

babel-plugin-node-cjs-interop and node-cjs-interop: fix the default import interoperability issue in Node.js The problem to solve Consider the followi

Nov 6, 2022

DoMe is a ToDo App. you can add, delete and reorder elements of the todo list using drag and drop. You can also toggle between dark&light mode

DO ME Todo App Live Preview : DO ME Built With : - ReactJS - TailwindCSS Make sure you have: - Git - Nodejs version 14 or higher (we recommend using

Nov 18, 2022
Comments
  • Cursor is always moved to end when input has x-model (only on Firefox)

    Cursor is always moved to end when input has x-model (only on Firefox)

    Hello Ryan, First, thank you for this package!

    I use it for an input that masks a version (x.x.x.x). This input has an x-model. But when you place your cursor at a position other than the last one and press a key, it shifts the numbers as expected but the cursor is directly moved at the end of the field.

    I created a JSFiddle to show you correct behaviour and buggy behaviour: https://jsfiddle.net/hz651evm/3/ After test it on different browsers (Chrome, Opera, Edge & Firefox), it seems that this problem happens only on Firefox (I'm on Firefox 97.0.2)

    Is this a bug that you can fix or is it due to the behavior of x-model on Firefox?

    Thank you in advance for your help!

    bug 
    opened by monsieurbab 4
  • Is this still working?

    Is this still working?

    Probably because alpine-mask and the native mask plugin both use the x-mask parameter, I am getting:

    Alpine Warning: You can't use [x-mask] without first installing the "Mask" plugin (…)

    Initially I thought this repository would de deprecated, but this commit shows otherwise.

    Example: https://jsfiddle.net/hmaesta/zx4bpko8/9/

    opened by hmaesta 2
  • There is a typo in README

    There is a typo in README

    This seems to be wrong (it does not work):

    <input x-mask.date="['h', 'm']">

    it shuold be:

    <input x-mask.time="['h', 'm']">

    opened by giacomok 0
  • Two way data binding does not work

    Two way data binding does not work

    I copy paste this <div x-data="{ raw: 2000 }"> <input x-mask.numeral x-mask:model="raw"> </div> from this link https://codetea.com/a-clean-integration-between-cleave-js-and-alpine/.

    data property does not update, same as <div x-data="{ raw: 2000 }"> <input x-mask.numeral model="raw"> </div>

    using Alpinejs v3.10.2

    opened by fredrickreuben 2
Releases(v0.2.0)
  • v0.2.0(Feb 28, 2022)

    What's Changed

    • Feature: x model support by @SimoTod in https://github.com/ryangjchandler/alpine-mask/pull/1

    New Contributors

    • @SimoTod made their first contribution in https://github.com/ryangjchandler/alpine-mask/pull/1

    Full Changelog: https://github.com/ryangjchandler/alpine-mask/compare/v0.1.0...v0.2.0

    Source code(tar.gz)
    Source code(zip)
  • v0.1.0(Feb 15, 2022)

Owner
Ryan Chandler
Ryan Chandler
Personal project to a student schedule classes according his course level. Using GraphQL, Clean Code e Clean Architecture.

classes-scheduler-graphql This is a personal project for student scheduling, with classes according his course level. I intend to make just the backen

Giovanny Lucas 6 Jul 9, 2022
Connect Web Integration illustrates the integration of Connect-Web in various JS frameworks and tooling

Connect Web Integration Connect Web Integration is a repository of example projects using Connect-Web with various JS frameworks and tooling. It provi

Buf 43 Dec 29, 2022
E-commerce website using Laravel, Tailwindcss and Alpine.js

About Laravel Laravel is a web application framework with expressive, elegant syntax. We believe development must be an enjoyable and creative experie

TheCodeholic 15 Dec 12, 2022
Easy to use and flexible router for Alpine.js

alpinejs-router Easy to use and flexible router for Alpine.js Installation npm npm install @shaun/alpinejs-router yarn yarn add @shaun/alpinejs-router

Shaun 33 Dec 30, 2022
Add focal point alignment of images to your Alpine 3.x components with a custom directive.

Alpine Focal Add focal point alignment of images to your Alpine 3.x components with a custom directive. This package only supports Alpine v3.x. About

Michael Lefrancois 2 Oct 12, 2021
Animate your Alpine components.

Animate your Alpine components ?? This package provides you with a simple helper to animate your ALpine.js components. Installation The recommended wa

Ralph J. Smit 18 Nov 16, 2022
Adds a handy $parent magic property to your Alpine components.

✨ Help support the maintenance of this package by sponsoring me. Alpine $parent Access parent components using a handy $parent magic variable. This pa

Ryan Chandler 10 Aug 29, 2022
↕️ A little Alpine.js plugin to automatically resize a textarea to fit its content.

↕️ Alpine Autosize ↕️ A little Alpine.js plugin to automatically resize a textarea to fit its content. ?? Installation CDN Include the following <scri

Marc Reichel 42 Nov 5, 2022
A simple library for handling keyboard shortcuts with Alpine.js.

✨ Help support the maintenance of this package by sponsoring me. Alpine.js Mousetrap A simple library for handling keyboard shortcuts with Alpine.js.

Dan Harrin 102 Nov 14, 2022
IntelliSense for Alpine.js

IntelliSense for Alpine.js Features Provides syntax highlighting for Alpine.js directives along with autocomplete for all base directives and modifier

P Christopher Bowers 8 Nov 17, 2022