-
Notifications
You must be signed in to change notification settings - Fork 1
/
example.html
98 lines (91 loc) · 5.67 KB
/
example.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Code Puncher Example</title>
<script src="https://cdn.tailwindcss.com"></script>
<script defer src="https://cdn.jsdelivr.net/npm/@alpinejs/[email protected]/dist/cdn.min.js"></script>
<script defer src="https://cdn.jsdelivr.net/npm/[email protected]/dist/cdn.min.js"></script>
</head>
<body class="min-h-screen pt-10 flex items-center justify-center bg-slate-100">
<main x-data="{ code: null }" class="w-full md:w-auto">
<div class="mb-10 px-8">
<p class="font-black text-slate-800 text-2xl">Alpine.js CodePuncher</p>
<p class="mb-8 mt-2 text-sm text-slate-700">
<a class="underline" href="https://github.com/ganyicz/alpinejs-codepuncher">Visit github</a> or <a class="underline" href="https://github.com/ganyicz/alpinejs-codepuncher/blob/main/example.html">explore the source code</a>
</p>
<div class="h-px w-full bg-slate-300"></div>
</div>
<div class="px-8 md:px-14 pt-12 pb-14 bg-white shadow md:rounded-lg">
<p class="mb-2 text-sm font-semibold text-slate-700">Example use</p>
<h5 class="mb-4 text-2xl font-semibold text-slate-800">Two-step authentication<h5>
<p class="mb-4 max-w-xs text-sm text-slate-700">To continue, please enter the 6 digit verification code sent to your phone ending in 9876.</p>
<p class="text-sm text-slate-700">Didn't receive a code? <span class="text-slate-800 font-semibold">Resend</span></p>
<div x-model="code" x-bind="codePuncher" class="mt-8 flex items-center gap-x-2">
<input x-bind="digit" class="w-8 py-1 border border-slate-300 rounded-md text-2xl md:text-3xl text-center text-slate-700 md:w-12 md:py-2.5" autofocus>
<input x-bind="digit" class="w-8 py-1 border border-slate-300 rounded-md text-2xl md:text-3xl text-center text-slate-700 md:w-12 md:py-2.5">
<input x-bind="digit" class="w-8 py-1 border border-slate-300 rounded-md text-2xl md:text-3xl text-center text-slate-700 md:w-12 md:py-2.5">
<div class="text-3xl text-slate-300 px-2">-</div>
<input x-bind="digit" class="w-8 py-1 border border-slate-300 rounded-md text-2xl md:text-3xl text-center text-slate-700 md:w-12 md:py-2.5">
<input x-bind="digit" class="w-8 py-1 border border-slate-300 rounded-md text-2xl md:text-3xl text-center text-slate-700 md:w-12 md:py-2.5">
<input x-bind="digit" class="w-8 py-1 border border-slate-300 rounded-md text-2xl md:text-3xl text-center text-slate-700 md:w-12 md:py-2.5">
</div>
</div>
<div x-text="code" class="mt-4 h-6 text-center text-slate-400"></div>
</main>
<script>
document.addEventListener('alpine:init', () => {
Alpine.bind('codePuncher', () => ({
'x-modelable': 'glued',
'x-data': () => ({
digits: [],
inputs: [],
glued: '',
init() {
this.inputs = Array.from(this.$root.querySelectorAll('[x-bind="digit"]'));
this.digits = Array(this.inputs.length).fill(null)
this.$watch('digits', (value) => this.glued = value.join(""))
},
clearCodeFrom(index) {
this.digits = this.digits.map((value, i) => i >= index ? null : value)
},
focusPreviousInput(index) {
this.$nextTick(() => this.inputs[index - 1].focus())
},
focusNextInput(index) {
this.$nextTick(() => this.$el.value && this.inputs[index + 1].focus())
},
handlePaste(index, event) {
const pastedDigits = String(event.clipboardData.getData('text/plain')).match(/\d/g) ?? []
const queuedDigits = pastedDigits.slice(0, this.digits.length - index)
const focusedInputIndex = Math.min(index + queuedDigits.length, this.digits.length - 1)
queuedDigits.forEach((value, i) => this.digits[index + i] = value)
this.$nextTick(() => this.inputs[focusedInputIndex].focus())
},
digit (index) {
return {
'x-data': `{
index: inputs.indexOf($el),
get first() { return this.index === 0 },
get last() { return this.index === this.digits.length - 1 },
}`,
'x-mask': '9',
'x-model': 'digits[index]',
'x-bind:disabled': 'index > glued.length',
'x-bind:tabindex': 'index < glued.length ? -1 : 0',
'x-on:focus': '!last && clearCodeFrom(index)',
'x-on:input': '!last && focusNextInput(index)',
'x-on:keydown.left': '!first && focusPreviousInput(index)',
'x-on:keydown.backspace': '!first && (last && $el.value ? $el.value = null : focusPreviousInput(index))',
'x-on:paste.prevent' : 'handlePaste(index, event)',
'inputmode': 'numeric',
}
}
})
}))
})
</script>
</body>
</html>