vanilla-wheel-number-picker 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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,71 @@
1
+ # Plain Wheel Number Picker
2
+
3
+ A small plain JavaScript wheel-style number picker. No framework required.
4
+
5
+ ## CDN usage after npm publish
6
+
7
+ ```html
8
+ <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/plain-wheel-number-picker@1/dist/wheel-number-picker.css">
9
+ <script src="https://cdn.jsdelivr.net/npm/plain-wheel-number-picker@1/dist/wheel-number-picker.min.js"></script>
10
+
11
+ <wheel-number-picker min="0" max="99" value="25"></wheel-number-picker>
12
+
13
+ <script>
14
+ const picker = document.querySelector('wheel-number-picker');
15
+
16
+ picker.addEventListener('change', event => {
17
+ console.log(event.detail.value);
18
+ });
19
+
20
+ console.log(picker.value);
21
+ picker.value = 42;
22
+ </script>
23
+ ```
24
+
25
+ ## Class usage
26
+
27
+ ```html
28
+ <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/plain-wheel-number-picker@1/dist/wheel-number-picker.css">
29
+ <script src="https://cdn.jsdelivr.net/npm/plain-wheel-number-picker@1/dist/wheel-number-picker.min.js"></script>
30
+
31
+ <div id="agePicker"></div>
32
+
33
+ <script>
34
+ const picker = new WheelNumberPicker('#agePicker', {
35
+ min: 18,
36
+ max: 100,
37
+ value: 30,
38
+ onChange(value) {
39
+ console.log(value);
40
+ }
41
+ });
42
+ </script>
43
+ ```
44
+
45
+ ## Options / attributes
46
+
47
+ | Option | Attribute | Default | Description |
48
+ |---|---|---:|---|
49
+ | `min` | `min` | `0` | Minimum value |
50
+ | `max` | `max` | `100` | Maximum value |
51
+ | `value` | `value` | `min` | Initial value |
52
+ | `itemHeight` | `item-height` | `44` | Item height in pixels |
53
+ | `visibleItems` | `visible-items` | `5` | Number of visible rows |
54
+
55
+ ## Styling
56
+
57
+ Override CSS variables:
58
+
59
+ ```css
60
+ wheel-number-picker {
61
+ --wnp-width: 180px;
62
+ --wnp-height: 240px;
63
+ --wnp-active-color: #111;
64
+ --wnp-muted-color: #aaa;
65
+ --wnp-item-height: 44px;
66
+ }
67
+ ```
68
+
69
+ ## Local demo
70
+
71
+ Open `demo/index.html` in a browser.
@@ -0,0 +1,53 @@
1
+ <!doctype html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>Plain Wheel Number Picker Demo</title>
7
+ <link rel="stylesheet" href="../dist/wheel-number-picker.css">
8
+ <style>
9
+ * { box-sizing: border-box; }
10
+ body {
11
+ min-height: 100vh;
12
+ margin: 0;
13
+ display: grid;
14
+ place-items: center;
15
+ background: #f5f5f5;
16
+ font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
17
+ color: #111;
18
+ }
19
+ main {
20
+ width: min(420px, calc(100vw - 32px));
21
+ padding: 32px;
22
+ border-radius: 24px;
23
+ background: white;
24
+ box-shadow: 0 20px 60px rgba(0, 0, 0, 0.12);
25
+ text-align: center;
26
+ }
27
+ h1 { margin: 0 0 8px; font-size: 24px; }
28
+ p { color: #666; }
29
+ .selected { margin-top: 24px; font-size: 18px; }
30
+ .selected strong { font-size: 28px; }
31
+ </style>
32
+ </head>
33
+ <body>
34
+ <main>
35
+ <h1>Wheel Number Picker</h1>
36
+ <p>Drag, scroll, or use keyboard arrows.</p>
37
+
38
+ <wheel-number-picker min="0" max="99" value="25"></wheel-number-picker>
39
+
40
+ <div class="selected">Selected: <strong id="selectedValue">25</strong></div>
41
+ </main>
42
+
43
+ <script src="../dist/wheel-number-picker.min.js"></script>
44
+ <script>
45
+ const picker = document.querySelector('wheel-number-picker');
46
+ const selectedValue = document.getElementById('selectedValue');
47
+
48
+ picker.addEventListener('change', event => {
49
+ selectedValue.textContent = event.detail.value;
50
+ });
51
+ </script>
52
+ </body>
53
+ </html>
@@ -0,0 +1,59 @@
1
+ wheel-number-picker,
2
+ .wheel-number-picker {
3
+ display: inline-block;
4
+ width: var(--wnp-width, 160px);
5
+ user-select: none;
6
+ touch-action: none;
7
+ outline: none;
8
+ font-family: var(--wnp-font-family, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif);
9
+ }
10
+
11
+ .wheel-number-picker__window {
12
+ position: relative;
13
+ height: var(--wnp-height, 220px);
14
+ overflow: hidden;
15
+ border-radius: var(--wnp-radius, 20px);
16
+ background: var(
17
+ --wnp-background,
18
+ linear-gradient(to bottom, rgba(255,255,255,0.95), rgba(255,255,255,0.4) 28%, rgba(255,255,255,0.1) 50%, rgba(255,255,255,0.4) 72%, rgba(255,255,255,0.95))
19
+ );
20
+ border: var(--wnp-border, 1px solid #ddd);
21
+ }
22
+
23
+ .wheel-number-picker__list {
24
+ will-change: transform;
25
+ }
26
+
27
+ .wheel-number-picker__item {
28
+ height: var(--wnp-item-height, 44px);
29
+ display: grid;
30
+ place-items: center;
31
+ font-size: var(--wnp-item-font-size, 22px);
32
+ color: var(--wnp-muted-color, #aaa);
33
+ transition: color 120ms ease, font-size 120ms ease, opacity 120ms ease;
34
+ }
35
+
36
+ .wheel-number-picker__item--active {
37
+ color: var(--wnp-active-color, #111);
38
+ font-size: var(--wnp-active-font-size, 34px);
39
+ font-weight: var(--wnp-active-font-weight, 750);
40
+ }
41
+
42
+ .wheel-number-picker__highlight {
43
+ position: absolute;
44
+ left: var(--wnp-highlight-inset, 14px);
45
+ right: var(--wnp-highlight-inset, 14px);
46
+ top: 50%;
47
+ height: var(--wnp-item-height, 44px);
48
+ transform: translateY(-50%);
49
+ border-top: var(--wnp-highlight-border, 1px solid #ddd);
50
+ border-bottom: var(--wnp-highlight-border, 1px solid #ddd);
51
+ pointer-events: none;
52
+ }
53
+
54
+ .wheel-number-picker__fade {
55
+ position: absolute;
56
+ inset: 0;
57
+ pointer-events: none;
58
+ background: var(--wnp-fade, linear-gradient(to bottom, white 0%, rgba(255,255,255,0) 30%, rgba(255,255,255,0) 70%, white 100%));
59
+ }
@@ -0,0 +1,283 @@
1
+ (function (global) {
2
+ 'use strict';
3
+
4
+ class WheelNumberPicker {
5
+ constructor(root, options = {}) {
6
+ this.root = typeof root === 'string' ? document.querySelector(root) : root;
7
+ if (!this.root) throw new Error('WheelNumberPicker root element was not found.');
8
+
9
+ this.min = Number(options.min ?? this.root.getAttribute('min') ?? 0);
10
+ this.max = Number(options.max ?? this.root.getAttribute('max') ?? 100);
11
+ this.value = this.clamp(options.value ?? this.root.getAttribute('value') ?? this.min);
12
+ this.onChange = options.onChange ?? function () {};
13
+ this.itemHeight = Number(options.itemHeight ?? this.root.getAttribute('item-height') ?? 44);
14
+ this.visibleItems = Number(options.visibleItems ?? this.root.getAttribute('visible-items') ?? 5);
15
+
16
+ this.isDragging = false;
17
+ this.startY = 0;
18
+ this.startOffset = 0;
19
+ this.offset = this.valueToOffset(this.value);
20
+ this.lastY = 0;
21
+ this.lastTime = 0;
22
+ this.velocity = 0;
23
+ this.animationFrame = null;
24
+
25
+ this.render();
26
+ this.bindEvents();
27
+ this.snapToValue(this.value, false);
28
+ }
29
+
30
+ render() {
31
+ this.root.classList.add('wheel-number-picker');
32
+ this.root.tabIndex = this.root.tabIndex >= 0 ? this.root.tabIndex : 0;
33
+ this.root.setAttribute('role', 'spinbutton');
34
+ if (!this.root.getAttribute('aria-label')) this.root.setAttribute('aria-label', 'Number picker');
35
+
36
+ this.root.innerHTML = `
37
+ <div class="wheel-number-picker__window">
38
+ <div class="wheel-number-picker__list"></div>
39
+ <div class="wheel-number-picker__highlight"></div>
40
+ <div class="wheel-number-picker__fade"></div>
41
+ </div>
42
+ `;
43
+
44
+ this.list = this.root.querySelector('.wheel-number-picker__list');
45
+ const spacerCount = Math.floor(this.visibleItems / 2);
46
+
47
+ for (let i = 0; i < spacerCount; i++) this.list.appendChild(this.createItem(''));
48
+ for (let value = this.min; value <= this.max; value++) this.list.appendChild(this.createItem(value));
49
+ for (let i = 0; i < spacerCount; i++) this.list.appendChild(this.createItem(''));
50
+ }
51
+
52
+ createItem(value) {
53
+ const item = document.createElement('div');
54
+ item.className = 'wheel-number-picker__item';
55
+ item.style.height = `${this.itemHeight}px`;
56
+ item.textContent = value;
57
+ if (value !== '') item.dataset.value = value;
58
+ return item;
59
+ }
60
+
61
+ bindEvents() {
62
+ this.onPointerDownBound = this.onPointerDown.bind(this);
63
+ this.onPointerMoveBound = this.onPointerMove.bind(this);
64
+ this.onPointerUpBound = this.onPointerUp.bind(this);
65
+ this.onWheelBound = this.onWheel.bind(this);
66
+ this.onKeyDownBound = this.onKeyDown.bind(this);
67
+
68
+ this.root.addEventListener('pointerdown', this.onPointerDownBound);
69
+ window.addEventListener('pointermove', this.onPointerMoveBound);
70
+ window.addEventListener('pointerup', this.onPointerUpBound);
71
+ this.root.addEventListener('wheel', this.onWheelBound, { passive: false });
72
+ this.root.addEventListener('keydown', this.onKeyDownBound);
73
+ }
74
+
75
+ destroy() {
76
+ this.cancelAnimation();
77
+ this.root.removeEventListener('pointerdown', this.onPointerDownBound);
78
+ window.removeEventListener('pointermove', this.onPointerMoveBound);
79
+ window.removeEventListener('pointerup', this.onPointerUpBound);
80
+ this.root.removeEventListener('wheel', this.onWheelBound);
81
+ this.root.removeEventListener('keydown', this.onKeyDownBound);
82
+ this.root.innerHTML = '';
83
+ }
84
+
85
+ onWheel(event) {
86
+ event.preventDefault();
87
+ this.setValue(this.value + Math.sign(event.deltaY));
88
+ }
89
+
90
+ onKeyDown(event) {
91
+ if (event.key === 'ArrowUp') {
92
+ event.preventDefault();
93
+ this.setValue(this.value - 1);
94
+ } else if (event.key === 'ArrowDown') {
95
+ event.preventDefault();
96
+ this.setValue(this.value + 1);
97
+ } else if (event.key === 'PageUp') {
98
+ event.preventDefault();
99
+ this.setValue(this.value - 10);
100
+ } else if (event.key === 'PageDown') {
101
+ event.preventDefault();
102
+ this.setValue(this.value + 10);
103
+ } else if (event.key === 'Home') {
104
+ event.preventDefault();
105
+ this.setValue(this.min);
106
+ } else if (event.key === 'End') {
107
+ event.preventDefault();
108
+ this.setValue(this.max);
109
+ }
110
+ }
111
+
112
+ onPointerDown(event) {
113
+ this.cancelAnimation();
114
+ this.isDragging = true;
115
+ this.root.setPointerCapture?.(event.pointerId);
116
+ this.startY = event.clientY;
117
+ this.startOffset = this.offset;
118
+ this.lastY = event.clientY;
119
+ this.lastTime = performance.now();
120
+ this.velocity = 0;
121
+ }
122
+
123
+ onPointerMove(event) {
124
+ if (!this.isDragging) return;
125
+
126
+ const currentTime = performance.now();
127
+ const deltaY = event.clientY - this.startY;
128
+ const timeDelta = currentTime - this.lastTime;
129
+
130
+ this.offset = this.clampOffset(this.startOffset + deltaY);
131
+ this.applyOffset();
132
+ this.updateActiveFromOffset();
133
+
134
+ if (timeDelta > 0) this.velocity = (event.clientY - this.lastY) / timeDelta;
135
+ this.lastY = event.clientY;
136
+ this.lastTime = currentTime;
137
+ }
138
+
139
+ onPointerUp() {
140
+ if (!this.isDragging) return;
141
+ this.isDragging = false;
142
+ this.startMomentum();
143
+ }
144
+
145
+ startMomentum() {
146
+ const friction = 0.94;
147
+ const minVelocity = 0.02;
148
+ const frame = () => {
149
+ this.velocity *= friction;
150
+ this.offset = this.clampOffset(this.offset + this.velocity * 16);
151
+ this.applyOffset();
152
+ this.updateActiveFromOffset();
153
+
154
+ if (Math.abs(this.velocity) > minVelocity) {
155
+ this.animationFrame = requestAnimationFrame(frame);
156
+ } else {
157
+ this.snapToNearest();
158
+ }
159
+ };
160
+ this.animationFrame = requestAnimationFrame(frame);
161
+ }
162
+
163
+ snapToNearest() {
164
+ const rawIndex = -this.offset / this.itemHeight;
165
+ const value = this.clamp(this.min + Math.round(rawIndex));
166
+ this.setValue(value);
167
+ }
168
+
169
+ snapToValue(value, animated = true) {
170
+ this.offset = this.valueToOffset(value);
171
+ this.list.style.transition = animated ? 'transform 160ms ease-out' : 'none';
172
+ this.applyOffset();
173
+ window.setTimeout(() => {
174
+ if (this.list) this.list.style.transition = 'none';
175
+ }, 170);
176
+ this.updateActive(value);
177
+ this.updateAria();
178
+ }
179
+
180
+ setValue(value) {
181
+ const next = this.clamp(value);
182
+ if (next === this.value) {
183
+ this.snapToValue(next);
184
+ return;
185
+ }
186
+ this.value = next;
187
+ this.root.setAttribute('value', String(next));
188
+ this.snapToValue(next);
189
+ this.onChange(this.value);
190
+ this.root.dispatchEvent(new CustomEvent('change', { detail: { value: this.value } }));
191
+ }
192
+
193
+ getValue() {
194
+ return this.value;
195
+ }
196
+
197
+ valueToOffset(value) {
198
+ return -(value - this.min) * this.itemHeight;
199
+ }
200
+
201
+ clamp(value) {
202
+ return Math.min(this.max, Math.max(this.min, Number(value)));
203
+ }
204
+
205
+ clampOffset(offset) {
206
+ const minOffset = this.valueToOffset(this.max);
207
+ const maxOffset = this.valueToOffset(this.min);
208
+ return Math.min(maxOffset, Math.max(minOffset, offset));
209
+ }
210
+
211
+ applyOffset() {
212
+ this.list.style.transform = `translateY(${this.offset}px)`;
213
+ }
214
+
215
+ updateActiveFromOffset() {
216
+ const rawIndex = -this.offset / this.itemHeight;
217
+ const value = this.clamp(this.min + Math.round(rawIndex));
218
+ this.updateActive(value);
219
+ }
220
+
221
+ updateActive(value) {
222
+ this.root.querySelectorAll('.wheel-number-picker__item').forEach(item => {
223
+ item.classList.toggle('wheel-number-picker__item--active', Number(item.dataset.value) === value);
224
+ });
225
+ }
226
+
227
+ updateAria() {
228
+ this.root.setAttribute('aria-valuemin', this.min);
229
+ this.root.setAttribute('aria-valuemax', this.max);
230
+ this.root.setAttribute('aria-valuenow', this.value);
231
+ }
232
+
233
+ cancelAnimation() {
234
+ if (this.animationFrame) {
235
+ cancelAnimationFrame(this.animationFrame);
236
+ this.animationFrame = null;
237
+ }
238
+ }
239
+ }
240
+
241
+ class WheelNumberPickerElement extends HTMLElement {
242
+ static get observedAttributes() {
243
+ return ['value'];
244
+ }
245
+
246
+ connectedCallback() {
247
+ if (this.picker) return;
248
+ this.picker = new WheelNumberPicker(this, {
249
+ min: this.getAttribute('min') ?? 0,
250
+ max: this.getAttribute('max') ?? 100,
251
+ value: this.getAttribute('value') ?? this.getAttribute('min') ?? 0,
252
+ itemHeight: this.getAttribute('item-height') ?? 44,
253
+ visibleItems: this.getAttribute('visible-items') ?? 5
254
+ });
255
+ }
256
+
257
+ disconnectedCallback() {
258
+ this.picker?.destroy();
259
+ this.picker = null;
260
+ }
261
+
262
+ attributeChangedCallback(name, oldValue, newValue) {
263
+ if (name === 'value' && this.picker && oldValue !== newValue) {
264
+ this.picker.setValue(Number(newValue));
265
+ }
266
+ }
267
+
268
+ get value() {
269
+ return this.picker ? this.picker.getValue() : Number(this.getAttribute('value'));
270
+ }
271
+
272
+ set value(value) {
273
+ if (this.picker) this.picker.setValue(value);
274
+ else this.setAttribute('value', value);
275
+ }
276
+ }
277
+
278
+ if (!global.customElements.get('wheel-number-picker')) {
279
+ global.customElements.define('wheel-number-picker', WheelNumberPickerElement);
280
+ }
281
+
282
+ global.WheelNumberPicker = WheelNumberPicker;
283
+ })(window);
@@ -0,0 +1 @@
1
+ (function (global) {'use strict';class WheelNumberPicker {constructor(root, options = {}) {this.root = typeof root === 'string' ? document.querySelector(root) : root;if (!this.root) throw new Error('WheelNumberPicker root element was not found.');this.min = Number(options.min ?? this.root.getAttribute('min') ?? 0);this.max = Number(options.max ?? this.root.getAttribute('max') ?? 100);this.value = this.clamp(options.value ?? this.root.getAttribute('value') ?? this.min);this.onChange = options.onChange ?? function () {};this.itemHeight = Number(options.itemHeight ?? this.root.getAttribute('item-height') ?? 44);this.visibleItems = Number(options.visibleItems ?? this.root.getAttribute('visible-items') ?? 5);this.isDragging = false;this.startY = 0;this.startOffset = 0;this.offset = this.valueToOffset(this.value);this.lastY = 0;this.lastTime = 0;this.velocity = 0;this.animationFrame = null;this.render();this.bindEvents();this.snapToValue(this.value, false);}render() {this.root.classList.add('wheel-number-picker');this.root.tabIndex = this.root.tabIndex >= 0 ? this.root.tabIndex : 0;this.root.setAttribute('role', 'spinbutton');if (!this.root.getAttribute('aria-label')) this.root.setAttribute('aria-label', 'Number picker');this.root.innerHTML = `<div class="wheel-number-picker__window"><div class="wheel-number-picker__list"></div><div class="wheel-number-picker__highlight"></div><div class="wheel-number-picker__fade"></div></div>`;this.list = this.root.querySelector('.wheel-number-picker__list');const spacerCount = Math.floor(this.visibleItems / 2);for (let i = 0; i < spacerCount; i++) this.list.appendChild(this.createItem(''));for (let value = this.min; value <= this.max; value++) this.list.appendChild(this.createItem(value));for (let i = 0; i < spacerCount; i++) this.list.appendChild(this.createItem(''));}createItem(value) {const item = document.createElement('div');item.className = 'wheel-number-picker__item';item.style.height = `${this.itemHeight}px`;item.textContent = value;if (value !== '') item.dataset.value = value;return item;}bindEvents() {this.onPointerDownBound = this.onPointerDown.bind(this);this.onPointerMoveBound = this.onPointerMove.bind(this);this.onPointerUpBound = this.onPointerUp.bind(this);this.onWheelBound = this.onWheel.bind(this);this.onKeyDownBound = this.onKeyDown.bind(this);this.root.addEventListener('pointerdown', this.onPointerDownBound);window.addEventListener('pointermove', this.onPointerMoveBound);window.addEventListener('pointerup', this.onPointerUpBound);this.root.addEventListener('wheel', this.onWheelBound, { passive: false });this.root.addEventListener('keydown', this.onKeyDownBound);}destroy() {this.cancelAnimation();this.root.removeEventListener('pointerdown', this.onPointerDownBound);window.removeEventListener('pointermove', this.onPointerMoveBound);window.removeEventListener('pointerup', this.onPointerUpBound);this.root.removeEventListener('wheel', this.onWheelBound);this.root.removeEventListener('keydown', this.onKeyDownBound);this.root.innerHTML = '';}onWheel(event) {event.preventDefault();this.setValue(this.value + Math.sign(event.deltaY));}onKeyDown(event) {if (event.key === 'ArrowUp') {event.preventDefault();this.setValue(this.value - 1);} else if (event.key === 'ArrowDown') {event.preventDefault();this.setValue(this.value + 1);} else if (event.key === 'PageUp') {event.preventDefault();this.setValue(this.value - 10);} else if (event.key === 'PageDown') {event.preventDefault();this.setValue(this.value + 10);} else if (event.key === 'Home') {event.preventDefault();this.setValue(this.min);} else if (event.key === 'End') {event.preventDefault();this.setValue(this.max);}}onPointerDown(event) {this.cancelAnimation();this.isDragging = true;this.root.setPointerCapture?.(event.pointerId);this.startY = event.clientY;this.startOffset = this.offset;this.lastY = event.clientY;this.lastTime = performance.now();this.velocity = 0;}onPointerMove(event) {if (!this.isDragging) return;const currentTime = performance.now();const deltaY = event.clientY - this.startY;const timeDelta = currentTime - this.lastTime;this.offset = this.clampOffset(this.startOffset + deltaY);this.applyOffset();this.updateActiveFromOffset();if (timeDelta > 0) this.velocity = (event.clientY - this.lastY) / timeDelta;this.lastY = event.clientY;this.lastTime = currentTime;}onPointerUp() {if (!this.isDragging) return;this.isDragging = false;this.startMomentum();}startMomentum() {const friction = 0.94;const minVelocity = 0.02;const frame = () => {this.velocity *= friction;this.offset = this.clampOffset(this.offset + this.velocity * 16);this.applyOffset();this.updateActiveFromOffset();if (Math.abs(this.velocity) > minVelocity) {this.animationFrame = requestAnimationFrame(frame);} else {this.snapToNearest();}};this.animationFrame = requestAnimationFrame(frame);}snapToNearest() {const rawIndex = -this.offset / this.itemHeight;const value = this.clamp(this.min + Math.round(rawIndex));this.setValue(value);}snapToValue(value, animated = true) {this.offset = this.valueToOffset(value);this.list.style.transition = animated ? 'transform 160ms ease-out' : 'none';this.applyOffset();window.setTimeout(() => {if (this.list) this.list.style.transition = 'none';}, 170);this.updateActive(value);this.updateAria();}setValue(value) {const next = this.clamp(value);if (next === this.value) {this.snapToValue(next);return;}this.value = next;this.root.setAttribute('value', String(next));this.snapToValue(next);this.onChange(this.value);this.root.dispatchEvent(new CustomEvent('change', { detail: { value: this.value } }));}getValue() {return this.value;}valueToOffset(value) {return -(value - this.min) * this.itemHeight;}clamp(value) {return Math.min(this.max, Math.max(this.min, Number(value)));}clampOffset(offset) {const minOffset = this.valueToOffset(this.max);const maxOffset = this.valueToOffset(this.min);return Math.min(maxOffset, Math.max(minOffset, offset));}applyOffset() {this.list.style.transform = `translateY(${this.offset}px)`;}updateActiveFromOffset() {const rawIndex = -this.offset / this.itemHeight;const value = this.clamp(this.min + Math.round(rawIndex));this.updateActive(value);}updateActive(value) {this.root.querySelectorAll('.wheel-number-picker__item').forEach(item => {item.classList.toggle('wheel-number-picker__item--active', Number(item.dataset.value) === value);});}updateAria() {this.root.setAttribute('aria-valuemin', this.min);this.root.setAttribute('aria-valuemax', this.max);this.root.setAttribute('aria-valuenow', this.value);}cancelAnimation() {if (this.animationFrame) {cancelAnimationFrame(this.animationFrame);this.animationFrame = null;}}}class WheelNumberPickerElement extends HTMLElement {static get observedAttributes() {return ['value'];}connectedCallback() {if (this.picker) return;this.picker = new WheelNumberPicker(this, {min: this.getAttribute('min') ?? 0,max: this.getAttribute('max') ?? 100,value: this.getAttribute('value') ?? this.getAttribute('min') ?? 0,itemHeight: this.getAttribute('item-height') ?? 44,visibleItems: this.getAttribute('visible-items') ?? 5});}disconnectedCallback() {this.picker?.destroy();this.picker = null;}attributeChangedCallback(name, oldValue, newValue) {if (name === 'value' && this.picker && oldValue !== newValue) {this.picker.setValue(Number(newValue));}}get value() {return this.picker ? this.picker.getValue() : Number(this.getAttribute('value'));}set value(value) {if (this.picker) this.picker.setValue(value);else this.setAttribute('value', value);}}if (!global.customElements.get('wheel-number-picker')) {global.customElements.define('wheel-number-picker', WheelNumberPickerElement);}global.WheelNumberPicker = WheelNumberPicker;})(window);
package/package.json ADDED
@@ -0,0 +1,26 @@
1
+ {
2
+ "name": "vanilla-wheel-number-picker",
3
+ "version": "1.0.0",
4
+ "description": "A plain JavaScript wheel-style number picker with drag, inertia, snapping, keyboard support, and a custom element API.",
5
+ "main": "dist/wheel-number-picker.js",
6
+ "unpkg": "dist/wheel-number-picker.min.js",
7
+ "jsdelivr": "dist/wheel-number-picker.min.js",
8
+ "files": [
9
+ "dist",
10
+ "demo",
11
+ "README.md",
12
+ "LICENSE"
13
+ ],
14
+ "keywords": [
15
+ "wheel-picker",
16
+ "number-picker",
17
+ "javascript",
18
+ "web-component",
19
+ "custom-element"
20
+ ],
21
+ "author": "",
22
+ "license": "MIT",
23
+ "scripts": {
24
+ "prepare-package": "node -e \"console.log('No build step required. Files are ready in dist/.')\""
25
+ }
26
+ }