compostrap 0.0.0-stage → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/css/auth.css +166 -0
- package/dist/css/blog.css +512 -0
- package/dist/css/chart.css +99 -0
- package/dist/css/compostrap.css +5047 -0
- package/dist/css/dashboard.css +360 -0
- package/dist/css/datagrid.css +64 -0
- package/dist/css/datepicker.css +514 -0
- package/dist/css/dialogs.css +185 -0
- package/dist/css/dropzone.css +86 -0
- package/dist/css/elements.css +181 -0
- package/dist/css/empty.css +94 -0
- package/dist/css/gallery.css +122 -0
- package/dist/css/lightbox.css +140 -0
- package/dist/css/number-input.css +100 -0
- package/dist/css/otp-input.css +177 -0
- package/dist/css/password-toggle.css +52 -0
- package/dist/css/rating.css +95 -0
- package/dist/css/selection.css +59 -0
- package/dist/css/settings.css +85 -0
- package/dist/css/shop.css +1026 -0
- package/dist/css/sidebar-menu.css +438 -0
- package/dist/css/sidebar.css +95 -0
- package/dist/css/spinner.css +102 -0
- package/dist/css/strength.css +118 -0
- package/dist/css/theme-switcher.css +5 -0
- package/dist/css/theme.css +13006 -0
- package/dist/css/tooltip.css +158 -0
- package/dist/js/chart.js +257 -0
- package/dist/js/datepicker.js +82 -0
- package/dist/js/dialogs.js +7 -0
- package/dist/js/dropzone.js +15 -0
- package/dist/js/index.js +24 -0
- package/dist/js/lightbox.js +77 -0
- package/dist/js/number-input.js +44 -0
- package/dist/js/otp-input.js +182 -0
- package/dist/js/password-toggle.js +16 -0
- package/dist/js/selection.js +32 -0
- package/dist/js/sidebar-menu.js +76 -0
- package/dist/js/sidebar.js +120 -0
- package/dist/js/spinner.js +27 -0
- package/dist/js/strength.js +87 -0
- package/dist/js/theme-switcher.js +98 -0
- package/dist/js/tooltip.js +17 -0
- package/docs/auth.md +25 -0
- package/docs/blog.md +85 -0
- package/docs/chart.md +39 -0
- package/docs/datagrid.md +64 -0
- package/docs/datepicker.md +35 -0
- package/docs/dialogs.md +81 -0
- package/docs/dropzone.md +23 -0
- package/docs/elements.md +38 -0
- package/docs/empty.md +25 -0
- package/docs/gallery.md +23 -0
- package/docs/layout.md +143 -0
- package/docs/lightbox.md +18 -0
- package/docs/number-input.md +19 -0
- package/docs/otp-input.md +52 -0
- package/docs/password-toggle.md +21 -0
- package/docs/rating.md +15 -0
- package/docs/selection.md +52 -0
- package/docs/settings.md +25 -0
- package/docs/shop.md +140 -0
- package/docs/spinner.md +51 -0
- package/docs/strength.md +26 -0
- package/docs/theme-switcher.md +60 -0
- package/docs/theme.md +100 -0
- package/docs/tooltip.md +30 -0
- package/license +21 -0
- package/package.json +106 -6
- package/readme.md +198 -0
- package/src/illustrations/empty.svg +15 -0
- package/src/illustrations/forbidden.svg +16 -0
- package/src/illustrations/maintenance.svg +14 -0
- package/src/illustrations/not-found.svg +22 -0
- package/src/illustrations/server-error.svg +21 -0
- package/src/js/chart.js +359 -0
- package/src/js/datepicker.js +154 -0
- package/src/js/dialogs.js +25 -0
- package/src/js/dropzone.js +35 -0
- package/src/js/index.js +29 -0
- package/src/js/lightbox.js +165 -0
- package/src/js/number-input.js +87 -0
- package/src/js/otp-input.js +401 -0
- package/src/js/password-toggle.js +45 -0
- package/src/js/selection.js +114 -0
- package/src/js/sidebar-menu.js +178 -0
- package/src/js/sidebar.js +326 -0
- package/src/js/spinner.js +76 -0
- package/src/js/strength.js +115 -0
- package/src/js/theme-switcher.js +197 -0
- package/src/js/tooltip.js +46 -0
- package/src/scss/_bootstrap.scss +9 -0
- package/src/scss/_variables.scss +58 -0
- package/src/scss/auth/_index.scss +210 -0
- package/src/scss/auth.scss +5 -0
- package/src/scss/blog/_article.scss +181 -0
- package/src/scss/blog/_comments.scss +108 -0
- package/src/scss/blog/_index.scss +18 -0
- package/src/scss/blog/_list.scss +246 -0
- package/src/scss/blog/_page.scss +138 -0
- package/src/scss/blog.scss +5 -0
- package/src/scss/chart/_index.scss +114 -0
- package/src/scss/chart.scss +5 -0
- package/src/scss/compostrap.scss +29 -0
- package/src/scss/dashboard/_content.scss +25 -0
- package/src/scss/dashboard/_dropdown.scss +71 -0
- package/src/scss/dashboard/_index.scss +9 -0
- package/src/scss/dashboard/_nanobar.scss +34 -0
- package/src/scss/dashboard/_navbar.scss +314 -0
- package/src/scss/dashboard/_theme.scss +27 -0
- package/src/scss/dashboard.scss +5 -0
- package/src/scss/datagrid/_index.scss +111 -0
- package/src/scss/datagrid.scss +5 -0
- package/src/scss/datepicker/_index.scss +557 -0
- package/src/scss/datepicker.scss +5 -0
- package/src/scss/dialogs/_index.scss +8 -0
- package/src/scss/dialogs/_modal.scss +82 -0
- package/src/scss/dialogs/_offcanvas.scss +13 -0
- package/src/scss/dialogs/_search.scss +79 -0
- package/src/scss/dialogs/_toast.scss +61 -0
- package/src/scss/dialogs.scss +5 -0
- package/src/scss/dropzone/_index.scss +111 -0
- package/src/scss/dropzone.scss +5 -0
- package/src/scss/elements/_datagrid-filter.scss +21 -0
- package/src/scss/elements/_index.scss +9 -0
- package/src/scss/elements/_list-row.scss +40 -0
- package/src/scss/elements/_loading.scss +47 -0
- package/src/scss/elements/_stat.scss +73 -0
- package/src/scss/elements/_status-dot.scss +38 -0
- package/src/scss/elements.scss +5 -0
- package/src/scss/empty/_index.scss +117 -0
- package/src/scss/empty.scss +5 -0
- package/src/scss/gallery/_index.scss +162 -0
- package/src/scss/gallery.scss +5 -0
- package/src/scss/lightbox/_index.scss +164 -0
- package/src/scss/lightbox.scss +5 -0
- package/src/scss/number-input/_index.scss +143 -0
- package/src/scss/number-input.scss +5 -0
- package/src/scss/otp-input/_index.scss +214 -0
- package/src/scss/otp-input.scss +5 -0
- package/src/scss/password-toggle/_index.scss +82 -0
- package/src/scss/password-toggle.scss +5 -0
- package/src/scss/rating/_index.scss +121 -0
- package/src/scss/rating.scss +5 -0
- package/src/scss/selection/_index.scss +84 -0
- package/src/scss/selection.scss +5 -0
- package/src/scss/settings/_index.scss +6 -0
- package/src/scss/settings/_nav-list.scss +52 -0
- package/src/scss/settings/_setting-row.scss +50 -0
- package/src/scss/settings.scss +5 -0
- package/src/scss/shop/_bar.scss +64 -0
- package/src/scss/shop/_catalog.scss +308 -0
- package/src/scss/shop/_checkout.scss +204 -0
- package/src/scss/shop/_filters.scss +116 -0
- package/src/scss/shop/_hero.scss +130 -0
- package/src/scss/shop/_index.scss +22 -0
- package/src/scss/shop/_page.scss +154 -0
- package/src/scss/shop/_reviews.scss +165 -0
- package/src/scss/shop/_stepper.scss +159 -0
- package/src/scss/shop.scss +5 -0
- package/src/scss/sidebar/_body.scss +8 -0
- package/src/scss/sidebar/_index.scss +8 -0
- package/src/scss/sidebar/_sidebar.scss +89 -0
- package/src/scss/sidebar/_theme.scss +7 -0
- package/src/scss/sidebar/_wrapper.scss +17 -0
- package/src/scss/sidebar-menu/_compact.scss +173 -0
- package/src/scss/sidebar-menu/_index.scss +9 -0
- package/src/scss/sidebar-menu/_list.scss +131 -0
- package/src/scss/sidebar-menu/_pills.scss +82 -0
- package/src/scss/sidebar-menu/_scrollbar.scss +53 -0
- package/src/scss/sidebar-menu/_theme.scss +109 -0
- package/src/scss/sidebar-menu.scss +5 -0
- package/src/scss/sidebar.scss +5 -0
- package/src/scss/spinner/_index.scss +130 -0
- package/src/scss/spinner.scss +5 -0
- package/src/scss/strength/_index.scss +118 -0
- package/src/scss/strength.scss +5 -0
- package/src/scss/theme/_accordion.scss +20 -0
- package/src/scss/theme/_alerts.scss +24 -0
- package/src/scss/theme/_avatar.scss +37 -0
- package/src/scss/theme/_badges.scss +53 -0
- package/src/scss/theme/_base.scss +9 -0
- package/src/scss/theme/_breadcrumb.scss +64 -0
- package/src/scss/theme/_buttons.scss +200 -0
- package/src/scss/theme/_cards.scss +17 -0
- package/src/scss/theme/_colors.scss +139 -0
- package/src/scss/theme/_forms.scss +175 -0
- package/src/scss/theme/_index.scss +18 -0
- package/src/scss/theme/_navs.scss +15 -0
- package/src/scss/theme/_overlays.scss +114 -0
- package/src/scss/theme/_tables.scss +59 -0
- package/src/scss/theme/_variables.scss +189 -0
- package/src/scss/theme-switcher/_index.scss +9 -0
- package/src/scss/theme-switcher.scss +5 -0
- package/src/scss/theme.scss +9 -0
- package/src/scss/tooltip/_index.scss +177 -0
- package/src/scss/tooltip.scss +5 -0
- package/README.md +0 -3
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Lightbox: a link with data-lightbox opens its image (href) in a full-screen preview; links with the same value of
|
|
3
|
+
* data-lightbox form a group to browse by the arrows, the keys ← → and a swipe. data-caption gives the text under the
|
|
4
|
+
* image. Built on the native <dialog>, so Esc closes it and the focus returns to the link. The links are found on
|
|
5
|
+
* every click, so content loaded later (AJAX snippets) needs no new init(). An image linked twice in a group (a big
|
|
6
|
+
* photo over its thumbnails) is shown once, at the place of its last link.
|
|
7
|
+
*
|
|
8
|
+
* <a href="photo-large.jpg" data-lightbox="gallery" data-caption="photo.jpg">…</a>
|
|
9
|
+
*/
|
|
10
|
+
const icons = {
|
|
11
|
+
close: '<path d="M6 6l12 12M18 6L6 18"/>',
|
|
12
|
+
previous: '<path d="M15 5l-7 7 7 7"/>',
|
|
13
|
+
next: '<path d="M9 5l7 7-7 7"/>',
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
const icon = (name) => `<svg viewBox="0 0 24 24" width="22" height="22" fill="none" stroke="currentColor"
|
|
17
|
+
stroke-width="1.75" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">${icons[name]}</svg>`;
|
|
18
|
+
|
|
19
|
+
export class Lightbox {
|
|
20
|
+
static labels = {
|
|
21
|
+
dialog: 'Image preview',
|
|
22
|
+
close: 'Close',
|
|
23
|
+
previous: 'Previous',
|
|
24
|
+
next: 'Next',
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
static _dialog = null;
|
|
28
|
+
static _links = [];
|
|
29
|
+
static _index = 0;
|
|
30
|
+
static _initialized = false;
|
|
31
|
+
|
|
32
|
+
static init(labels = {}) {
|
|
33
|
+
this.labels = { ...this.labels, ...labels };
|
|
34
|
+
if (this._initialized) {
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
this._initialized = true;
|
|
39
|
+
document.addEventListener('click', (event) => {
|
|
40
|
+
const link = event.target.closest('[data-lightbox]');
|
|
41
|
+
if (!link || event.defaultPrevented || event.button !== 0 || event.ctrlKey || event.metaKey || event.shiftKey) {
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
event.preventDefault();
|
|
46
|
+
const group = link.dataset.lightbox;
|
|
47
|
+
const address = (item) => item.getAttribute('href');
|
|
48
|
+
const links = group
|
|
49
|
+
? [...document.querySelectorAll('[data-lightbox]')].filter((item) => item.dataset.lightbox === group)
|
|
50
|
+
: [link];
|
|
51
|
+
// an image linked twice (a big photo over its thumbnails) is shown once, at the place of its last link
|
|
52
|
+
this._links = links.filter((item, index) => !links.slice(index + 1).some((later) => address(later) === address(item)));
|
|
53
|
+
this.open(Math.max(0, this._links.findIndex((item) => address(item) === address(link))));
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
static open(index) {
|
|
58
|
+
const dialog = this._getDialog();
|
|
59
|
+
this._setLabels(dialog);
|
|
60
|
+
dialog.classList.toggle('lightbox-single', this._links.length < 2);
|
|
61
|
+
this._show(index);
|
|
62
|
+
if (!dialog.open) {
|
|
63
|
+
document.documentElement.classList.add('lightbox-open');
|
|
64
|
+
dialog.showModal();
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
static close() {
|
|
69
|
+
this._dialog?.close();
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
static _show(index) {
|
|
73
|
+
const count = this._links.length;
|
|
74
|
+
this._index = (index + count) % count;
|
|
75
|
+
const link = this._links[this._index];
|
|
76
|
+
const dialog = this._dialog;
|
|
77
|
+
const image = dialog.querySelector('.lightbox-image');
|
|
78
|
+
const caption = dialog.querySelector('.lightbox-caption');
|
|
79
|
+
|
|
80
|
+
image.src = link.getAttribute('href');
|
|
81
|
+
image.alt = link.dataset.caption || link.getAttribute('aria-label') || '';
|
|
82
|
+
caption.textContent = link.dataset.caption || '';
|
|
83
|
+
dialog.querySelector('.lightbox-counter').textContent = count > 1 ? `${this._index + 1} / ${count}` : '';
|
|
84
|
+
|
|
85
|
+
// the neighbours are loaded ahead, so browsing does not wait
|
|
86
|
+
[this._index - 1, this._index + 1].forEach((i) => {
|
|
87
|
+
const next = this._links[(i + count) % count];
|
|
88
|
+
if (next && next !== link) {
|
|
89
|
+
new Image().src = next.getAttribute('href');
|
|
90
|
+
}
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// on every opening, so labels given by a later init() reach a dialog that already exists
|
|
95
|
+
static _setLabels(dialog) {
|
|
96
|
+
dialog.setAttribute('aria-label', this.labels.dialog);
|
|
97
|
+
dialog.querySelector('.lightbox-close').setAttribute('aria-label', this.labels.close);
|
|
98
|
+
dialog.querySelector('.lightbox-prev').setAttribute('aria-label', this.labels.previous);
|
|
99
|
+
dialog.querySelector('.lightbox-next').setAttribute('aria-label', this.labels.next);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
static _getDialog() {
|
|
103
|
+
if (this._dialog) {
|
|
104
|
+
return this._dialog;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const dialog = document.createElement('dialog');
|
|
108
|
+
dialog.className = 'lightbox';
|
|
109
|
+
// the markup is fixed; the labels are texts of a project (translations), so they are set as attributes
|
|
110
|
+
// in _setLabels(), never written into the HTML
|
|
111
|
+
dialog.innerHTML = `
|
|
112
|
+
<figure class="lightbox-figure">
|
|
113
|
+
<img class="lightbox-image" alt="">
|
|
114
|
+
<figcaption class="lightbox-caption"></figcaption>
|
|
115
|
+
</figure>
|
|
116
|
+
<div class="lightbox-counter" aria-live="polite"></div>
|
|
117
|
+
<button type="button" class="lightbox-button lightbox-close">${icon('close')}</button>
|
|
118
|
+
<button type="button" class="lightbox-button lightbox-prev">${icon('previous')}</button>
|
|
119
|
+
<button type="button" class="lightbox-button lightbox-next">${icon('next')}</button>`;
|
|
120
|
+
|
|
121
|
+
dialog.querySelector('.lightbox-close').addEventListener('click', () => this.close());
|
|
122
|
+
dialog.querySelector('.lightbox-prev').addEventListener('click', () => this._show(this._index - 1));
|
|
123
|
+
dialog.querySelector('.lightbox-next').addEventListener('click', () => this._show(this._index + 1));
|
|
124
|
+
|
|
125
|
+
// a click beside the image closes the preview
|
|
126
|
+
dialog.addEventListener('click', (event) => {
|
|
127
|
+
if (event.target === dialog || event.target.classList.contains('lightbox-figure')) {
|
|
128
|
+
this.close();
|
|
129
|
+
}
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
dialog.addEventListener('keydown', (event) => {
|
|
133
|
+
if (event.key === 'ArrowLeft') {
|
|
134
|
+
this._show(this._index - 1);
|
|
135
|
+
} else if (event.key === 'ArrowRight') {
|
|
136
|
+
this._show(this._index + 1);
|
|
137
|
+
}
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
let touchX = null;
|
|
141
|
+
dialog.addEventListener('touchstart', (event) => {
|
|
142
|
+
touchX = event.touches[0].clientX;
|
|
143
|
+
}, { passive: true });
|
|
144
|
+
dialog.addEventListener('touchend', (event) => {
|
|
145
|
+
if (touchX === null || this._links.length < 2) {
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
const distance = event.changedTouches[0].clientX - touchX;
|
|
150
|
+
touchX = null;
|
|
151
|
+
if (Math.abs(distance) > 50) {
|
|
152
|
+
this._show(this._index + (distance < 0 ? 1 : -1));
|
|
153
|
+
}
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
dialog.addEventListener('close', () => {
|
|
157
|
+
document.documentElement.classList.remove('lightbox-open');
|
|
158
|
+
dialog.querySelector('.lightbox-image').removeAttribute('src');
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
document.body.append(dialog);
|
|
162
|
+
this._dialog = dialog;
|
|
163
|
+
return dialog;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Number input: the minus and plus buttons (data-number-input="down" / "up") of a .number-input step its input by its
|
|
3
|
+
* step, keep the decimals of a fractional step (step="0.25" → 1.50) and stop at min and max, whose buttons are
|
|
4
|
+
* disabled. A step fires input and change on the input, so a form can react (e.g. recount a cart by AJAX). The keys
|
|
5
|
+
* ↑ ↓ step a text input too. The buttons are found on every click, so inputs loaded later need no new init().
|
|
6
|
+
*
|
|
7
|
+
* <div class="number-input">
|
|
8
|
+
* <button type="button" class="number-input-btn" data-number-input="down" aria-label="Decrease"></button>
|
|
9
|
+
* <input type="number" class="form-control" value="1" min="1" max="99">
|
|
10
|
+
* <button type="button" class="number-input-btn" data-number-input="up" aria-label="Increase"></button>
|
|
11
|
+
* </div>
|
|
12
|
+
*/
|
|
13
|
+
export class NumberInput {
|
|
14
|
+
static _initialized = false;
|
|
15
|
+
|
|
16
|
+
static init() {
|
|
17
|
+
document.querySelectorAll('.number-input > input').forEach((input) => this.update(input));
|
|
18
|
+
if (this._initialized) {
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
this._initialized = true;
|
|
23
|
+
document.addEventListener('click', (event) => {
|
|
24
|
+
const button = event.target.closest('[data-number-input]');
|
|
25
|
+
const input = button?.closest('.number-input')?.querySelector('input');
|
|
26
|
+
if (input && !input.disabled && !input.readOnly) {
|
|
27
|
+
this.step(input, button.dataset.numberInput === 'down' ? -1 : 1);
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
document.addEventListener('keydown', (event) => {
|
|
31
|
+
const input = event.target;
|
|
32
|
+
if (input instanceof HTMLInputElement && input.type !== 'number' && input.closest('.number-input')
|
|
33
|
+
&& (event.key === 'ArrowUp' || event.key === 'ArrowDown')) {
|
|
34
|
+
event.preventDefault();
|
|
35
|
+
this.step(input, event.key === 'ArrowDown' ? -1 : 1);
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
document.addEventListener('input', (event) => {
|
|
39
|
+
if (event.target instanceof HTMLInputElement && event.target.closest('.number-input')) {
|
|
40
|
+
this.update(event.target);
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** Steps the input up (1) or down (-1) and fires input and change. */
|
|
46
|
+
static step(input, direction) {
|
|
47
|
+
const { step, decimals, min, max } = this._limits(input);
|
|
48
|
+
const current = this._value(input);
|
|
49
|
+
let next = Number.isNaN(current) ? (min ?? 0) : current + direction * step;
|
|
50
|
+
if (min !== null) next = Math.max(min, next);
|
|
51
|
+
if (max !== null) next = Math.min(max, next);
|
|
52
|
+
|
|
53
|
+
input.value = next.toFixed(decimals);
|
|
54
|
+
this.update(input);
|
|
55
|
+
input.dispatchEvent(new Event('input', { bubbles: true }));
|
|
56
|
+
input.dispatchEvent(new Event('change', { bubbles: true }));
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** Disables the button that would step past min or max. */
|
|
60
|
+
static update(input) {
|
|
61
|
+
const box = input.closest('.number-input');
|
|
62
|
+
if (!box) return;
|
|
63
|
+
const { min, max } = this._limits(input);
|
|
64
|
+
const value = this._value(input);
|
|
65
|
+
box.querySelectorAll('[data-number-input]').forEach((button) => {
|
|
66
|
+
const down = button.dataset.numberInput === 'down';
|
|
67
|
+
const limit = down ? min : max;
|
|
68
|
+
button.disabled = input.disabled || input.readOnly
|
|
69
|
+
|| (limit !== null && !Number.isNaN(value) && (down ? value <= limit : value >= limit));
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
static _value(input) {
|
|
74
|
+
return parseFloat(String(input.value).replace(',', '.'));
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
static _limits(input) {
|
|
78
|
+
const attr = (name) => {
|
|
79
|
+
const value = parseFloat(input.getAttribute(name));
|
|
80
|
+
return Number.isNaN(value) ? null : value;
|
|
81
|
+
};
|
|
82
|
+
const stepAttr = input.getAttribute('step');
|
|
83
|
+
const step = attr('step') || 1;
|
|
84
|
+
const decimals = stepAttr && stepAttr.includes('.') ? stepAttr.split('.')[1].length : 0;
|
|
85
|
+
return { step, decimals, min: attr('min'), max: attr('max') };
|
|
86
|
+
}
|
|
87
|
+
}
|
|
@@ -0,0 +1,401 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* OtpInput: a one-time code typed into separate slots, with the markup, options, methods and events of the OTP input
|
|
3
|
+
* of Bootstrap 6. One real input holds the value (so a form, a screen reader, a password manager and the autofill of
|
|
4
|
+
* a code from an SMS see a plain field); the script draws a slot for each character under it and keeps the caret on
|
|
5
|
+
* one slot. The number of slots is the maxlength of the input (6 without it).
|
|
6
|
+
*
|
|
7
|
+
* <div class="otp" data-bs-otp>
|
|
8
|
+
* <input type="text" class="otp-input" maxlength="6" aria-label="Verification code">
|
|
9
|
+
* </div>
|
|
10
|
+
*
|
|
11
|
+
* Options as data attributes of the wrapper: data-bs-length, data-bs-type (numeric, alphanumeric, alpha),
|
|
12
|
+
* data-bs-mask="true", data-bs-groups="[3,3]", data-bs-separator. The wrapper fires input.bs.otpInput after every
|
|
13
|
+
* change and complete.bs.otpInput when all slots are filled (the value is in event.value and event.detail.value).
|
|
14
|
+
* The events of the inputs are caught on the document, so a code loaded later (an AJAX snippet) works without a new
|
|
15
|
+
* init(); update() draws its slots at once.
|
|
16
|
+
*/
|
|
17
|
+
const MASK_CHARACTER = '•';
|
|
18
|
+
|
|
19
|
+
// the keyboard of a phone, the pattern of the validation and the characters that are thrown away
|
|
20
|
+
const TYPES = {
|
|
21
|
+
numeric: { inputmode: 'numeric', pattern: '[0-9]*', filter: /[^0-9]/g },
|
|
22
|
+
alphanumeric: { inputmode: 'text', pattern: '[A-Za-z0-9]*', filter: /[^A-Za-z0-9]/g },
|
|
23
|
+
alpha: { inputmode: 'text', pattern: '[A-Za-z]*', filter: /[^A-Za-z]/g },
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export class OtpInput {
|
|
27
|
+
static Default = {
|
|
28
|
+
groups: null,
|
|
29
|
+
length: null,
|
|
30
|
+
mask: false,
|
|
31
|
+
separator: '·',
|
|
32
|
+
type: 'numeric',
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
static _instances = new WeakMap();
|
|
36
|
+
static _initialized = false;
|
|
37
|
+
|
|
38
|
+
/** Starts the handling of all codes of the page and draws the slots of those in the root. */
|
|
39
|
+
static init(root = document) {
|
|
40
|
+
if (!this._initialized) {
|
|
41
|
+
this._initialized = true;
|
|
42
|
+
const on = (type, method) => document.addEventListener(type, (event) => this._of(event.target)?.[method](event), true);
|
|
43
|
+
on('beforeinput', '_handleBeforeInput');
|
|
44
|
+
on('input', '_handleInput');
|
|
45
|
+
on('paste', '_handlePaste');
|
|
46
|
+
on('pointerdown', '_handlePointerDown');
|
|
47
|
+
on('focus', '_handleFocus');
|
|
48
|
+
on('blur', '_render');
|
|
49
|
+
on('keyup', '_render');
|
|
50
|
+
on('select', '_render');
|
|
51
|
+
document.addEventListener('selectionchange', () => this._of(document.activeElement)?._render());
|
|
52
|
+
// a reset form returns the first value after the event
|
|
53
|
+
document.addEventListener('reset', (event) => setTimeout(() => this.update(event.target)));
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
this.update(root);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** Draws the slots of the codes in the root (after an AJAX redraw) and redraws the ones that have them. */
|
|
60
|
+
static update(root = document) {
|
|
61
|
+
root.querySelectorAll('[data-bs-otp]').forEach((element) => {
|
|
62
|
+
const instance = this.getOrCreateInstance(element);
|
|
63
|
+
if (instance._input) {
|
|
64
|
+
instance._render();
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
static getInstance(element) {
|
|
70
|
+
return this._instances.get(element) ?? null;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
static getOrCreateInstance(element, config = {}) {
|
|
74
|
+
return this.getInstance(element) ?? new this(element, config);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// the instance of the code an input belongs to, created when the input is touched for the first time
|
|
78
|
+
static _of(target) {
|
|
79
|
+
if (!(target instanceof HTMLInputElement)) {
|
|
80
|
+
return null;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const element = target.closest('[data-bs-otp]');
|
|
84
|
+
const instance = element ? this.getOrCreateInstance(element) : null;
|
|
85
|
+
return instance?._input === target ? instance : null;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
constructor(element, config = {}) {
|
|
89
|
+
this._element = element;
|
|
90
|
+
this._input = element.querySelector('input');
|
|
91
|
+
if (!this._input) {
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
this._config = { ...OtpInput.Default, ...this._dataConfig(), ...config };
|
|
96
|
+
this._type = TYPES[this._config.type] || TYPES.numeric;
|
|
97
|
+
this._length = this._resolveLength();
|
|
98
|
+
this._slots = [];
|
|
99
|
+
// a focus that came from a click keeps the clicked slot instead of going to the first empty one
|
|
100
|
+
this._pointerActive = false;
|
|
101
|
+
this._pointerIndex = 0;
|
|
102
|
+
|
|
103
|
+
OtpInput._instances.set(element, this);
|
|
104
|
+
this._setupInput();
|
|
105
|
+
this._renderSlots();
|
|
106
|
+
this._render();
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
getValue() {
|
|
110
|
+
return this._input.value;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
setValue(value) {
|
|
114
|
+
this._input.value = this._sanitize(String(value));
|
|
115
|
+
this._render();
|
|
116
|
+
this._checkComplete();
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
clear() {
|
|
120
|
+
this._input.value = '';
|
|
121
|
+
this._render();
|
|
122
|
+
this._input.focus();
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/** Focuses the code at its first empty slot (the last one when it is full). */
|
|
126
|
+
focus() {
|
|
127
|
+
this._input.focus();
|
|
128
|
+
this._selectSlot(this._firstEmptyIndex());
|
|
129
|
+
this._render();
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
dispose() {
|
|
133
|
+
this._slotsContainer?.remove();
|
|
134
|
+
this._element.classList.remove('otp-rendered');
|
|
135
|
+
OtpInput._instances.delete(this._element);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
_dataConfig() {
|
|
139
|
+
const { bsLength, bsType, bsMask, bsGroups, bsSeparator } = this._element.dataset;
|
|
140
|
+
const config = {};
|
|
141
|
+
if (bsLength) {
|
|
142
|
+
config.length = Number.parseInt(bsLength, 10) || null;
|
|
143
|
+
}
|
|
144
|
+
if (bsType) {
|
|
145
|
+
config.type = bsType;
|
|
146
|
+
}
|
|
147
|
+
if (bsMask !== undefined) {
|
|
148
|
+
config.mask = bsMask === 'true' || bsMask === '';
|
|
149
|
+
}
|
|
150
|
+
if (bsSeparator !== undefined) {
|
|
151
|
+
config.separator = bsSeparator;
|
|
152
|
+
}
|
|
153
|
+
if (bsGroups) {
|
|
154
|
+
try {
|
|
155
|
+
config.groups = JSON.parse(bsGroups);
|
|
156
|
+
} catch {
|
|
157
|
+
config.groups = null;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
return config;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
_resolveLength() {
|
|
165
|
+
if (this._config.length) {
|
|
166
|
+
return this._config.length;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
const maxLength = Number.parseInt(this._input.getAttribute('maxlength'), 10);
|
|
170
|
+
return Number.isNaN(maxLength) || maxLength < 1 ? 6 : maxLength;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
_setupInput() {
|
|
174
|
+
const input = this._input;
|
|
175
|
+
if (input.type === 'number' || input.type === 'password') {
|
|
176
|
+
input.type = 'text';
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
input.classList.add('otp-input');
|
|
180
|
+
input.setAttribute('maxlength', String(this._length));
|
|
181
|
+
input.setAttribute('inputmode', this._type.inputmode);
|
|
182
|
+
input.setAttribute('pattern', this._type.pattern);
|
|
183
|
+
if (!input.getAttribute('autocomplete')) {
|
|
184
|
+
input.setAttribute('autocomplete', 'one-time-code');
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
if (input.value) {
|
|
188
|
+
input.value = this._sanitize(input.value);
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
_renderSlots() {
|
|
193
|
+
const container = document.createElement('div');
|
|
194
|
+
container.className = 'otp-slots';
|
|
195
|
+
container.setAttribute('aria-hidden', 'true');
|
|
196
|
+
|
|
197
|
+
const { groups } = this._config;
|
|
198
|
+
let groupIndex = 0;
|
|
199
|
+
let inGroup = 0;
|
|
200
|
+
|
|
201
|
+
for (let i = 0; i < this._length; i++) {
|
|
202
|
+
const slot = document.createElement('div');
|
|
203
|
+
slot.className = 'otp-slot';
|
|
204
|
+
container.append(slot);
|
|
205
|
+
this._slots.push(slot);
|
|
206
|
+
|
|
207
|
+
// a separator between the groups; the last size repeats for the rest of the slots
|
|
208
|
+
if (Array.isArray(groups) && groups.length > 0) {
|
|
209
|
+
inGroup++;
|
|
210
|
+
if (inGroup === groups[groupIndex] && i < this._length - 1) {
|
|
211
|
+
const separator = document.createElement('div');
|
|
212
|
+
separator.className = 'otp-separator';
|
|
213
|
+
separator.textContent = this._config.separator;
|
|
214
|
+
container.append(separator);
|
|
215
|
+
groupIndex = Math.min(groupIndex + 1, groups.length - 1);
|
|
216
|
+
inGroup = 0;
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
this._slotsContainer = container;
|
|
222
|
+
this._element.append(container);
|
|
223
|
+
this._element.classList.add('otp-rendered');
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
_handleFocus() {
|
|
227
|
+
if (this._pointerActive) {
|
|
228
|
+
// the click focused the input itself (so a phone shows its keyboard); now the caret goes to the clicked slot
|
|
229
|
+
this._pointerActive = false;
|
|
230
|
+
this._selectSlot(this._pointerIndex);
|
|
231
|
+
} else {
|
|
232
|
+
this._selectSlot(this._firstEmptyIndex());
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
this._render();
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
// more characters at once (the autofill of a code from an SMS, a composition); single keys go by _handleBeforeInput
|
|
239
|
+
_handleInput() {
|
|
240
|
+
const sanitized = this._sanitize(this._input.value);
|
|
241
|
+
if (sanitized !== this._input.value) {
|
|
242
|
+
this._input.value = sanitized;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
if (document.activeElement === this._input) {
|
|
246
|
+
this._selectSlot(this._firstEmptyIndex());
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
this._afterValueChange();
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
_handlePaste(event) {
|
|
253
|
+
const pasted = event.clipboardData?.getData('text');
|
|
254
|
+
if (!pasted || !this._isEditable()) {
|
|
255
|
+
return;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
// cleaned before the length is cut: the browser would cut "123 456" to its maxlength with the space inside
|
|
259
|
+
const sanitized = this._sanitize(pasted);
|
|
260
|
+
let start = this._input.selectionStart ?? this._input.value.length;
|
|
261
|
+
let end = this._input.selectionEnd ?? start;
|
|
262
|
+
if (sanitized.length === this._length) {
|
|
263
|
+
start = 0;
|
|
264
|
+
end = this._input.value.length;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
const inserted = sanitized.slice(0, this._length - this._input.value.length + end - start);
|
|
268
|
+
event.preventDefault();
|
|
269
|
+
if (!inserted) {
|
|
270
|
+
return;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
this._input.setRangeText(inserted, start, end, 'end');
|
|
274
|
+
this._selectSlot(this._input.selectionStart);
|
|
275
|
+
this._afterValueChange();
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
// a typed character replaces the one of its slot (it does not push the others), backspace and delete remove one
|
|
279
|
+
_handleBeforeInput(event) {
|
|
280
|
+
if (!this._isEditable()) {
|
|
281
|
+
return;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
const { inputType, data } = event;
|
|
285
|
+
if (inputType === 'insertText' && data && data.length === 1) {
|
|
286
|
+
event.preventDefault();
|
|
287
|
+
const char = this._sanitize(data);
|
|
288
|
+
if (!char) {
|
|
289
|
+
return;
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
const index = Math.min(this._input.selectionStart ?? 0, this._length - 1);
|
|
293
|
+
const chars = [...this._input.value];
|
|
294
|
+
chars[index] = char;
|
|
295
|
+
this._input.value = chars.join('').slice(0, this._length);
|
|
296
|
+
this._selectSlot(index + 1);
|
|
297
|
+
this._afterValueChange();
|
|
298
|
+
return;
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
if (inputType === 'deleteContentBackward' || inputType === 'deleteContentForward') {
|
|
302
|
+
event.preventDefault();
|
|
303
|
+
|
|
304
|
+
// a chosen slot is cleared; with a plain caret backspace takes the character before it, delete the one after
|
|
305
|
+
let start = this._input.selectionStart ?? 0;
|
|
306
|
+
let end = this._input.selectionEnd ?? start;
|
|
307
|
+
if (end === start) {
|
|
308
|
+
if (inputType === 'deleteContentBackward') {
|
|
309
|
+
start -= 1;
|
|
310
|
+
} else {
|
|
311
|
+
end += 1;
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
const chars = [...this._input.value];
|
|
316
|
+
if (start < 0 || end > chars.length) {
|
|
317
|
+
return;
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
chars.splice(start, end - start);
|
|
321
|
+
this._input.value = chars.join('');
|
|
322
|
+
this._selectSlot(start);
|
|
323
|
+
this._afterValueChange();
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
_handlePointerDown(event) {
|
|
328
|
+
if (this._input.matches(':disabled')) {
|
|
329
|
+
return;
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
// never past the first empty slot
|
|
333
|
+
const target = Math.min(this._slotIndexFromPoint(event.clientX), this._firstEmptyIndex());
|
|
334
|
+
if (document.activeElement === this._input) {
|
|
335
|
+
event.preventDefault();
|
|
336
|
+
this._selectSlot(target);
|
|
337
|
+
this._render();
|
|
338
|
+
return;
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
// not focused yet: the browser focuses the input by the click itself, the caret is set in _handleFocus
|
|
342
|
+
this._pointerActive = true;
|
|
343
|
+
this._pointerIndex = target;
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
_slotIndexFromPoint(x) {
|
|
347
|
+
const index = this._slots.findIndex((slot) => x <= slot.getBoundingClientRect().right);
|
|
348
|
+
return index === -1 ? this._slots.length - 1 : index;
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
_afterValueChange() {
|
|
352
|
+
this._render();
|
|
353
|
+
this._trigger('input.bs.otpInput');
|
|
354
|
+
this._checkComplete();
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
_checkComplete() {
|
|
358
|
+
if (this._input.value.length === this._length) {
|
|
359
|
+
this._trigger('complete.bs.otpInput');
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
_trigger(type) {
|
|
364
|
+
const value = this._input.value;
|
|
365
|
+
const event = new CustomEvent(type, { bubbles: true, detail: { value } });
|
|
366
|
+
event.value = value;
|
|
367
|
+
this._element.dispatchEvent(event);
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
_firstEmptyIndex() {
|
|
371
|
+
return Math.min(this._input.value.length, this._length - 1);
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
_isEditable() {
|
|
375
|
+
return !this._input.readOnly && !this._input.matches(':disabled');
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
// the current slot is a selection: a filled one is selected (the next key replaces it), an empty one has the caret
|
|
379
|
+
_selectSlot(index) {
|
|
380
|
+
const clamped = Math.max(0, Math.min(index, this._length - 1));
|
|
381
|
+
const end = clamped < this._input.value.length ? clamped + 1 : clamped;
|
|
382
|
+
this._input.setSelectionRange(clamped, end);
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
_sanitize(value) {
|
|
386
|
+
return value.replace(this._type.filter, '').slice(0, this._length);
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
_render() {
|
|
390
|
+
const { value } = this._input;
|
|
391
|
+
const isFocused = document.activeElement === this._input;
|
|
392
|
+
const caret = Math.min(this._input.selectionStart ?? value.length, this._length - 1);
|
|
393
|
+
|
|
394
|
+
this._slots.forEach((slot, index) => {
|
|
395
|
+
const char = value[index] ?? '';
|
|
396
|
+
slot.textContent = char && this._config.mask ? MASK_CHARACTER : char;
|
|
397
|
+
slot.classList.toggle('otp-slot-filled', Boolean(char));
|
|
398
|
+
slot.classList.toggle('otp-slot-active', isFocused && index === caret);
|
|
399
|
+
});
|
|
400
|
+
}
|
|
401
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* PasswordToggle: a button in a password input shows the typed password and hides it again (the password toggle of
|
|
3
|
+
* drago-ex/form, without Naja). The button switches the type of the input of its .password-toggle wrapper between
|
|
4
|
+
* password and text and keeps aria-pressed in line; the icon is drawn by CSS from the type. The buttons are found
|
|
5
|
+
* on every click, so forms loaded later (AJAX snippets) need no new init().
|
|
6
|
+
*
|
|
7
|
+
* <div class="password-toggle">
|
|
8
|
+
* <input type="password" class="form-control">
|
|
9
|
+
* <button type="button" class="password-toggle-btn" data-password-toggle aria-label="Show password" aria-pressed="false"></button>
|
|
10
|
+
* </div>
|
|
11
|
+
*/
|
|
12
|
+
export class PasswordToggle {
|
|
13
|
+
static _initialized = false;
|
|
14
|
+
|
|
15
|
+
static init() {
|
|
16
|
+
if (this._initialized) {
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
this._initialized = true;
|
|
21
|
+
|
|
22
|
+
// the buttons are hidden until the script runs (a page without JavaScript has no dead button)
|
|
23
|
+
document.documentElement.setAttribute('data-password-toggle', '');
|
|
24
|
+
|
|
25
|
+
document.addEventListener('click', (event) => {
|
|
26
|
+
const button = event.target.closest('[data-password-toggle]');
|
|
27
|
+
const input = button?.closest('.password-toggle')?.querySelector('input');
|
|
28
|
+
if (input) {
|
|
29
|
+
this.toggle(input);
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
// a shown password is sent as a password again: a text input could be kept by the browser among its
|
|
34
|
+
// suggestions, and a form drawn again (a validation error) starts hidden
|
|
35
|
+
document.addEventListener('submit', (event) => {
|
|
36
|
+
event.target.querySelectorAll('.password-toggle > input[type="text"]').forEach((input) => this.toggle(input, false));
|
|
37
|
+
}, true);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** Shows or hides the password of the input; without `show` it switches. */
|
|
41
|
+
static toggle(input, show = input.type === 'password') {
|
|
42
|
+
input.type = show ? 'text' : 'password';
|
|
43
|
+
input.closest('.password-toggle')?.querySelector('[data-password-toggle]')?.setAttribute('aria-pressed', String(show));
|
|
44
|
+
}
|
|
45
|
+
}
|