@popovandrii/ui-elements 0.0.1 → 0.0.3

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/README.md CHANGED
@@ -2,8 +2,22 @@
2
2
 
3
3
  TypeScript-based NPM plugin.
4
4
  The plugin includes interface elements with specific properties.
5
- Currently implemented:
6
- - Spin-Box This is a field with two buttons. Provides increment and decrement of numbers
5
+ UI elements that contain a large share of JavaScript.
6
+
7
+ Currently implemented:<br>
8
+ <b>Spin-Box</b> This is a field with two buttons.<br>
9
+ - the ability to set the minimum and maximum value
10
+ - the number of digits after the decimal point
11
+ - accelerated increment when holding down the `Shift` key
12
+ - setting the value directly in the field and increment using `arrows`
13
+ - value validation
14
+ - accessibility in accordance with the `A11Y` standard
15
+ - colors (danger primary...) and markup (sm lg radius-none)
16
+ - setting a custom CSS selector
17
+
18
+ <p align="center">
19
+ <img src="https://gitlab.com/AndreyPopov/ui-elements/-/raw/main/docs/images/spinBox.png" alt="SpinBox Preview" width="672">
20
+ </p>
7
21
 
8
22
  # Information for Developers
9
23
  ```sh
@@ -24,6 +38,8 @@ $ npm run build # Compile the project into the ./dist folder.
24
38
  $ npm run dev # Work on the project
25
39
  $ npm run lint
26
40
  $ npm run format
41
+ $ npm publish --access public
42
+ $ npm info @popovandrii/ui-elements@0.0.1
27
43
  ```
28
44
 
29
45
  The './playground' folder for viewing in a browser
@@ -0,0 +1 @@
1
+ "use strict";var T=Object.defineProperty;var S=o=>{throw TypeError(o)};var C=(o,t,a)=>t in o?T(o,t,{enumerable:!0,configurable:!0,writable:!0,value:a}):o[t]=a;var y=(o,t,a)=>C(o,typeof t!="symbol"?t+"":t,a),K=(o,t,a)=>t.has(o)||S("Cannot "+a);var v=(o,t,a)=>(K(o,t,"read from private field"),a?a.call(o):t.get(o)),E=(o,t,a)=>t.has(o)?S("Cannot add the same private member more than once"):t instanceof WeakSet?t.add(o):t.set(o,a);var D=(o,t,a)=>(K(o,t,"access private method"),a);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});var d,f,g;class I{constructor(t={}){E(this,f);y(this,"selectors");y(this,"spinBoxes");E(this,d,(t,a,e,u=0,l=0)=>{t==u||t<u?(a.classList.add(this.selectors.disabledBtn),a.disabled=!0):(a.classList.remove(this.selectors.disabledBtn),a.disabled=!1),t==l||t>l?(e.classList.add(this.selectors.disabledBtn),e.disabled=!0):(e.classList.remove(this.selectors.disabledBtn),e.disabled=!1)});const a={main:"spin-box",disabledBtn:"disabled"};this.selectors={...a,...t},this.spinBoxes=document.querySelectorAll(`.${this.selectors.main}`),D(this,f,g).call(this)}}d=new WeakMap,f=new WeakSet,g=function(){this.spinBoxes.forEach(t=>{const a=t.firstElementChild,e=t.querySelector("input"),u=t.lastElementChild,l=Number(t.dataset.step),n=Number(t.dataset.min),r=Number(t.dataset.max),b=i=>{e.setAttribute("aria-valuenow",String(i)),e.setAttribute("aria-valuetext",`${i} items`)};Number(e.value)<=n&&(e.value=n.toFixed(l)),Number(e.value)>=r&&(e.value=r.toFixed(l)),n&&e.setAttribute("aria-valuemin",n.toFixed(l)),r&&e.setAttribute("aria-valuemax",r.toFixed(l)),v(this,d).call(this,Number(e.value),a,u,n,r),b(n);let c=null;const h=(i=1)=>{e.value=String(Math.abs(Number(e.value)));let s=parseFloat(e.value)||0;s=s+1*i/Math.pow(10,l),s>r&&(s=r),e.value=s.toFixed(l),v(this,d).call(this,Number(e.value),a,u,n,r),b(s)},p=(i=1)=>{e.value=String(Math.abs(Number(e.value)));let s=parseFloat(e.value)||0;s=s-1*i/Math.pow(10,l),s<n&&(s=n),e.value=s.toFixed(l),v(this,d).call(this,Number(e.value),a,u,n,r),b(s)},m=(i,s=150)=>{c===null&&(c=window.setInterval(i,s))},w=()=>{c!==null&&(clearInterval(c),c=null)};u.addEventListener("mousedown",i=>{const s=i.shiftKey?3:1;m(()=>h(s))}),u.addEventListener("touchstart",()=>m(h)),["mouseup","mouseleave","mouseout","touchend","touchcancel"].forEach(i=>{u.addEventListener(i,w)}),u.addEventListener("click",i=>{const s=i.shiftKey?3:1;c===null&&h(s)}),a.addEventListener("click",i=>{const s=i.shiftKey?3:1;c===null&&p(s)}),a.addEventListener("mousedown",i=>{const s=i.shiftKey?3:1;m(()=>p(s),100)}),a.addEventListener("touchstart",()=>m(p,100)),["mouseup","mouseleave","mouseout","touchend","touchcancel"].forEach(i=>{a.addEventListener(i,w)}),e.addEventListener("keydown",i=>{const s=i.key,k=i.shiftKey?3:1;if(["Backspace","Delete","ArrowLeft","ArrowRight","Tab","Enter","Home","End"].includes(s)||(i.ctrlKey||i.metaKey)&&["a","c","v","x"].includes(s.toLowerCase()))return;if(["e","+","-"].includes(s)){i.preventDefault();return}if(s==="ArrowUp"||s==="ArrowDown"){i.preventDefault();const A=parseFloat(e.value)||0,B=1*k/Math.pow(10,l);let x=s==="ArrowUp"?A+B:A-B;x<n&&(x=n),e.value=x.toFixed(l),Number(e.value)<n&&(e.value=n.toFixed(l)),Number(e.value)>r&&(e.value=r.toFixed(l)),v(this,d).call(this,Number(e.value),a,u,n,r);return}const L=s===","?".":s,N=/^[0-9]$/.test(L),F=L===".",M=e.value.includes(".");(l===0&&!N||l>0&&!(N||F)||F&&M)&&i.preventDefault()}),e.addEventListener("keyup",()=>{Number(e.value)<n&&(e.value=n.toFixed(l)),Number(e.value)>r&&(e.value=r.toFixed(l)),v(this,d).call(this,Number(e.value),a,u,n,r)}),e.addEventListener("change",()=>{Number(e.value)<n&&(e.value=n.toFixed(l)),Number(e.value)>r&&(e.value=r.toFixed(l)),v(this,d).call(this,Number(e.value),a,u,n,r)})})};exports.SpinBox=I;
package/dist/index.d.ts CHANGED
@@ -1,2 +1 @@
1
- export { SpinBox } from "./SpinBox";
2
- import "./scss/style.scss";
1
+ export { SpinBox } from './SpinBox';
package/dist/index.umd.js CHANGED
@@ -1 +1 @@
1
- (function(i,a){typeof exports=="object"&&typeof module<"u"?a(exports):typeof define=="function"&&define.amd?define(["exports"],a):(i=typeof globalThis<"u"?globalThis:i||self,a(i["Ui Elements"]={}))})(this,function(i){"use strict";var U=Object.defineProperty;var K=i=>{throw TypeError(i)};var C=(i,a,l)=>a in i?U(i,a,{enumerable:!0,configurable:!0,writable:!0,value:l}):i[a]=l;var E=(i,a,l)=>C(i,typeof a!="symbol"?a+"":a,l),D=(i,a,l)=>a.has(i)||K("Cannot "+l);var f=(i,a,l)=>(D(i,a,"read from private field"),l?l.call(i):a.get(i)),w=(i,a,l)=>a.has(i)?K("Cannot add the same private member more than once"):a instanceof WeakSet?a.add(i):a.set(i,l);var g=(i,a,l)=>(D(i,a,"access private method"),l);var l,m,k;class a{constructor(d={}){w(this,m);E(this,"selectors");E(this,"spinBoxes");w(this,l,(d,r,e,c=0,n=0)=>{d==c||d<c?(r.classList.add(this.selectors.disabledBtn),r.disabled=!0):(r.classList.remove(this.selectors.disabledBtn),r.disabled=!1),d==n||d>n?(e.classList.add(this.selectors.disabledBtn),e.disabled=!0):(e.classList.remove(this.selectors.disabledBtn),e.disabled=!1)});const r={main:"spin-box",disabledBtn:"disabled"};this.selectors={...r,...d},this.spinBoxes=document.querySelectorAll(`.${this.selectors.main}`),g(this,m,k).call(this)}}l=new WeakMap,m=new WeakSet,k=function(){this.spinBoxes.forEach(d=>{const r=d.firstElementChild,e=d.querySelector("input"),c=d.lastElementChild,n=Number(d.dataset.step),o=Number(d.dataset.min),u=Number(d.dataset.max),b=s=>{e.setAttribute("aria-valuenow",String(s)),e.setAttribute("aria-valuetext",`${s} items`)};Number(e.value)<=o&&(e.value=o.toFixed(n)),Number(e.value)>=u&&(e.value=u.toFixed(n)),o&&e.setAttribute("aria-valuemin",o.toFixed(n)),u&&e.setAttribute("aria-valuemax",u.toFixed(n)),f(this,l).call(this,Number(e.value),r,c,o,u),b(o);let v=null;const p=(s=1)=>{e.value=String(Math.abs(Number(e.value)));let t=parseFloat(e.value)||0;t=t+1*s/Math.pow(10,n),t>u&&(t=u),e.value=t.toFixed(n),f(this,l).call(this,Number(e.value),r,c,o,u),b(t)},x=(s=1)=>{e.value=String(Math.abs(Number(e.value)));let t=parseFloat(e.value)||0;t=t-1*s/Math.pow(10,n),t<o&&(t=o),e.value=t.toFixed(n),f(this,l).call(this,Number(e.value),r,c,o,u),b(t)},h=(s,t=150)=>{v===null&&(v=window.setInterval(s,t))},L=()=>{v!==null&&(clearInterval(v),v=null)};c.addEventListener("mousedown",s=>{const t=s.shiftKey?3:1;h(()=>p(t))}),c.addEventListener("touchstart",()=>h(p)),["mouseup","mouseleave","mouseout","touchend","touchcancel"].forEach(s=>{c.addEventListener(s,L)}),c.addEventListener("click",s=>{const t=s.shiftKey?3:1;v===null&&p(t)}),r.addEventListener("click",s=>{const t=s.shiftKey?3:1;v===null&&x(t)}),r.addEventListener("mousedown",s=>{const t=s.shiftKey?3:1;h(()=>x(t),100)}),r.addEventListener("touchstart",()=>h(x,100)),["mouseup","mouseleave","mouseout","touchend","touchcancel"].forEach(s=>{r.addEventListener(s,L)}),e.addEventListener("keydown",s=>{const t=s.key,M=s.shiftKey?3:1;if(["Backspace","Delete","ArrowLeft","ArrowRight","Tab","Enter","Home","End"].includes(t)||(s.ctrlKey||s.metaKey)&&["a","c","v","x"].includes(t.toLowerCase()))return;if(["e","+","-"].includes(t)){s.preventDefault();return}if(t==="ArrowUp"||t==="ArrowDown"){s.preventDefault();const B=parseFloat(e.value)||0,S=1*M/Math.pow(10,n);let y=t==="ArrowUp"?B+S:B-S;y<o&&(y=o),e.value=y.toFixed(n),Number(e.value)<o&&(e.value=o.toFixed(n)),Number(e.value)>u&&(e.value=u.toFixed(n)),f(this,l).call(this,Number(e.value),r,c,o,u);return}const N=t===","?".":t,F=/^[0-9]$/.test(N),A=N===".",T=e.value.includes(".");(n===0&&!F||n>0&&!(F||A)||A&&T)&&s.preventDefault()}),e.addEventListener("keyup",()=>{Number(e.value)<o&&(e.value=o.toFixed(n)),Number(e.value)>u&&(e.value=u.toFixed(n)),f(this,l).call(this,Number(e.value),r,c,o,u)}),e.addEventListener("change",()=>{Number(e.value)<o&&(e.value=o.toFixed(n)),Number(e.value)>u&&(e.value=u.toFixed(n)),f(this,l).call(this,Number(e.value),r,c,o,u)})})},i.SpinBox=a,Object.defineProperty(i,Symbol.toStringTag,{value:"Module"})});
1
+ (function(i,a){typeof exports=="object"&&typeof module<"u"?a(exports):typeof define=="function"&&define.amd?define(["exports"],a):(i=typeof globalThis<"u"?globalThis:i||self,a(i.UiElements={}))})(this,function(i){"use strict";var U=Object.defineProperty;var K=i=>{throw TypeError(i)};var C=(i,a,l)=>a in i?U(i,a,{enumerable:!0,configurable:!0,writable:!0,value:l}):i[a]=l;var E=(i,a,l)=>C(i,typeof a!="symbol"?a+"":a,l),D=(i,a,l)=>a.has(i)||K("Cannot "+l);var f=(i,a,l)=>(D(i,a,"read from private field"),l?l.call(i):a.get(i)),w=(i,a,l)=>a.has(i)?K("Cannot add the same private member more than once"):a instanceof WeakSet?a.add(i):a.set(i,l);var g=(i,a,l)=>(D(i,a,"access private method"),l);var l,m,k;class a{constructor(d={}){w(this,m);E(this,"selectors");E(this,"spinBoxes");w(this,l,(d,r,e,c=0,n=0)=>{d==c||d<c?(r.classList.add(this.selectors.disabledBtn),r.disabled=!0):(r.classList.remove(this.selectors.disabledBtn),r.disabled=!1),d==n||d>n?(e.classList.add(this.selectors.disabledBtn),e.disabled=!0):(e.classList.remove(this.selectors.disabledBtn),e.disabled=!1)});const r={main:"spin-box",disabledBtn:"disabled"};this.selectors={...r,...d},this.spinBoxes=document.querySelectorAll(`.${this.selectors.main}`),g(this,m,k).call(this)}}l=new WeakMap,m=new WeakSet,k=function(){this.spinBoxes.forEach(d=>{const r=d.firstElementChild,e=d.querySelector("input"),c=d.lastElementChild,n=Number(d.dataset.step),o=Number(d.dataset.min),u=Number(d.dataset.max),b=s=>{e.setAttribute("aria-valuenow",String(s)),e.setAttribute("aria-valuetext",`${s} items`)};Number(e.value)<=o&&(e.value=o.toFixed(n)),Number(e.value)>=u&&(e.value=u.toFixed(n)),o&&e.setAttribute("aria-valuemin",o.toFixed(n)),u&&e.setAttribute("aria-valuemax",u.toFixed(n)),f(this,l).call(this,Number(e.value),r,c,o,u),b(o);let v=null;const p=(s=1)=>{e.value=String(Math.abs(Number(e.value)));let t=parseFloat(e.value)||0;t=t+1*s/Math.pow(10,n),t>u&&(t=u),e.value=t.toFixed(n),f(this,l).call(this,Number(e.value),r,c,o,u),b(t)},x=(s=1)=>{e.value=String(Math.abs(Number(e.value)));let t=parseFloat(e.value)||0;t=t-1*s/Math.pow(10,n),t<o&&(t=o),e.value=t.toFixed(n),f(this,l).call(this,Number(e.value),r,c,o,u),b(t)},h=(s,t=150)=>{v===null&&(v=window.setInterval(s,t))},L=()=>{v!==null&&(clearInterval(v),v=null)};c.addEventListener("mousedown",s=>{const t=s.shiftKey?3:1;h(()=>p(t))}),c.addEventListener("touchstart",()=>h(p)),["mouseup","mouseleave","mouseout","touchend","touchcancel"].forEach(s=>{c.addEventListener(s,L)}),c.addEventListener("click",s=>{const t=s.shiftKey?3:1;v===null&&p(t)}),r.addEventListener("click",s=>{const t=s.shiftKey?3:1;v===null&&x(t)}),r.addEventListener("mousedown",s=>{const t=s.shiftKey?3:1;h(()=>x(t),100)}),r.addEventListener("touchstart",()=>h(x,100)),["mouseup","mouseleave","mouseout","touchend","touchcancel"].forEach(s=>{r.addEventListener(s,L)}),e.addEventListener("keydown",s=>{const t=s.key,M=s.shiftKey?3:1;if(["Backspace","Delete","ArrowLeft","ArrowRight","Tab","Enter","Home","End"].includes(t)||(s.ctrlKey||s.metaKey)&&["a","c","v","x"].includes(t.toLowerCase()))return;if(["e","+","-"].includes(t)){s.preventDefault();return}if(t==="ArrowUp"||t==="ArrowDown"){s.preventDefault();const B=parseFloat(e.value)||0,S=1*M/Math.pow(10,n);let y=t==="ArrowUp"?B+S:B-S;y<o&&(y=o),e.value=y.toFixed(n),Number(e.value)<o&&(e.value=o.toFixed(n)),Number(e.value)>u&&(e.value=u.toFixed(n)),f(this,l).call(this,Number(e.value),r,c,o,u);return}const N=t===","?".":t,F=/^[0-9]$/.test(N),A=N===".",T=e.value.includes(".");(n===0&&!F||n>0&&!(F||A)||A&&T)&&s.preventDefault()}),e.addEventListener("keyup",()=>{Number(e.value)<o&&(e.value=o.toFixed(n)),Number(e.value)>u&&(e.value=u.toFixed(n)),f(this,l).call(this,Number(e.value),r,c,o,u)}),e.addEventListener("change",()=>{Number(e.value)<o&&(e.value=o.toFixed(n)),Number(e.value)>u&&(e.value=u.toFixed(n)),f(this,l).call(this,Number(e.value),r,c,o,u)})})},i.SpinBox=a,Object.defineProperty(i,Symbol.toStringTag,{value:"Module"})});
package/package.json CHANGED
@@ -1,7 +1,8 @@
1
1
  {
2
2
  "author": "Andrii Popov",
3
3
  "name": "@popovandrii/ui-elements",
4
- "version": "0.0.1",
4
+ "description": "Custom UI elements like SpinBox with theme support",
5
+ "version": "0.0.3",
5
6
  "license": "MIT",
6
7
  "keywords": [
7
8
  "spinbox",
@@ -11,13 +12,20 @@
11
12
  ],
12
13
  "repository": {
13
14
  "type": "git",
14
- "url": "https://gitlab.com/AndreyPopov/ui-elements"
15
+ "url": "git+https://gitlab.com/AndreyPopov/ui-elements"
15
16
  },
17
+ "bugs": {
18
+ "url": "https://gitlab.com/AndreyPopov/ui-elements/-/issues"
19
+ },
20
+ "main": "./dist/index.cjs.js",
21
+ "module": "./dist/index.es.js",
22
+ "types": "./dist/index.d.ts",
16
23
  "exports": {
17
24
  ".": {
18
- "import": "./dist/index.js",
19
- "types": "./dist/index.d.ts"
20
- }
25
+ "import": "./dist/index.es.js",
26
+ "require": "./dist/index.cjs.js"
27
+ },
28
+ "./style.css": "./dist/style.css"
21
29
  },
22
30
  "files": [
23
31
  "dist",
@@ -27,20 +35,21 @@
27
35
  "type": "module",
28
36
  "scripts": {
29
37
  "dev": "vite --config playground/vite.config.js",
30
- "build": "vite build && tsc",
38
+ "build": "node build.js",
31
39
  "preview": "vite preview",
32
40
  "lint": "eslint src --ext .ts,.tsx",
33
41
  "format": "prettier --write \"src/**/*.{ts,tsx,js,jsx,json,css,scss,md}\""
34
42
  },
35
43
  "devDependencies": {
36
- "sass": "^1.89.2",
37
- "typescript": "~5.8.3",
38
- "vite": "^6.3.5",
44
+ "@typescript-eslint/eslint-plugin": "^8.8.0",
45
+ "@typescript-eslint/parser": "^8.8.0",
39
46
  "eslint": "^9.27.0",
40
47
  "eslint-config-prettier": "^10.1.5",
41
48
  "eslint-plugin-prettier": "^5.4.0",
42
49
  "prettier": "^3.5.3",
43
- "@typescript-eslint/eslint-plugin": "^8.8.0",
44
- "@typescript-eslint/parser": "^8.8.0"
50
+ "sass": "^1.89.2",
51
+ "typescript": "~5.8.3",
52
+ "vite": "^6.3.5",
53
+ "vite-plugin-dts": "^4.5.4"
45
54
  }
46
55
  }
package/dist/SpinBox.js DELETED
@@ -1,195 +0,0 @@
1
- var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
2
- if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
3
- if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
4
- return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
5
- };
6
- var _SpinBox_instances, _SpinBox_state, _SpinBox_event;
7
- export class SpinBox {
8
- constructor(selectors = {}) {
9
- _SpinBox_instances.add(this);
10
- Object.defineProperty(this, "selectors", {
11
- enumerable: true,
12
- configurable: true,
13
- writable: true,
14
- value: void 0
15
- });
16
- Object.defineProperty(this, "spinBoxes", {
17
- enumerable: true,
18
- configurable: true,
19
- writable: true,
20
- value: void 0
21
- });
22
- _SpinBox_state.set(this, (inputNumber, down, up, min = 0, max = 0) => {
23
- if (inputNumber == min || inputNumber < min) {
24
- down.classList.add(this.selectors.disabledBtn);
25
- down.disabled = true;
26
- }
27
- else {
28
- down.classList.remove(this.selectors.disabledBtn);
29
- down.disabled = false;
30
- }
31
- if (inputNumber == max || inputNumber > max) {
32
- up.classList.add(this.selectors.disabledBtn);
33
- up.disabled = true;
34
- }
35
- else {
36
- up.classList.remove(this.selectors.disabledBtn);
37
- up.disabled = false;
38
- }
39
- });
40
- const defaultSelectors = {
41
- main: "spin-box",
42
- disabledBtn: "disabled",
43
- };
44
- this.selectors = { ...defaultSelectors, ...selectors };
45
- this.spinBoxes = document.querySelectorAll(`.${this.selectors.main}`);
46
- __classPrivateFieldGet(this, _SpinBox_instances, "m", _SpinBox_event).call(this);
47
- }
48
- }
49
- _SpinBox_state = new WeakMap(), _SpinBox_instances = new WeakSet(), _SpinBox_event = function _SpinBox_event() {
50
- this.spinBoxes.forEach((e) => {
51
- const btnDown = e.firstElementChild;
52
- const input = e.querySelector("input");
53
- const btnUp = e.lastElementChild;
54
- const step = Number(e.dataset.step);
55
- const min = Number(e.dataset.min);
56
- const max = Number(e.dataset.max);
57
- const updateAria = (val) => {
58
- input.setAttribute("aria-valuenow", String(val));
59
- input.setAttribute("aria-valuetext", `${val} items`);
60
- };
61
- if (Number(input.value) <= min)
62
- input.value = min.toFixed(step);
63
- if (Number(input.value) >= max)
64
- input.value = max.toFixed(step);
65
- if (min)
66
- input.setAttribute("aria-valuemin", min.toFixed(step));
67
- if (max)
68
- input.setAttribute("aria-valuemax", max.toFixed(step));
69
- __classPrivateFieldGet(this, _SpinBox_state, "f").call(this, Number(input.value), btnDown, btnUp, min, max);
70
- updateAria(min);
71
- let timerId = null;
72
- const increment = (speed = 1) => {
73
- input.value = String(Math.abs(Number(input.value)));
74
- let val = parseFloat(input.value) || 0;
75
- val = val + (1 * speed) / Math.pow(10, step);
76
- if (val > max)
77
- val = max;
78
- input.value = val.toFixed(step);
79
- __classPrivateFieldGet(this, _SpinBox_state, "f").call(this, Number(input.value), btnDown, btnUp, min, max);
80
- updateAria(val);
81
- };
82
- const decrement = (speed = 1) => {
83
- input.value = String(Math.abs(Number(input.value)));
84
- let val = parseFloat(input.value) || 0;
85
- val = val - (1 * speed) / Math.pow(10, step);
86
- if (val < min)
87
- val = min;
88
- input.value = val.toFixed(step);
89
- __classPrivateFieldGet(this, _SpinBox_state, "f").call(this, Number(input.value), btnDown, btnUp, min, max);
90
- updateAria(val);
91
- };
92
- const startTimer = (action, ms = 150) => {
93
- if (timerId !== null)
94
- return;
95
- timerId = window.setInterval(action, ms);
96
- };
97
- const stopTimer = () => {
98
- if (timerId !== null) {
99
- clearInterval(timerId);
100
- timerId = null;
101
- }
102
- };
103
- // hold
104
- btnUp.addEventListener("mousedown", (e) => {
105
- const speed = e.shiftKey ? 3 : 1;
106
- startTimer(() => increment(speed));
107
- });
108
- btnUp.addEventListener("touchstart", () => startTimer(increment));
109
- // lost
110
- ["mouseup", "mouseleave", "mouseout", "touchend", "touchcancel"].forEach((event) => {
111
- btnUp.addEventListener(event, stopTimer);
112
- });
113
- // once click
114
- btnUp.addEventListener("click", (e) => {
115
- const speed = e.shiftKey ? 3 : 1;
116
- // block when holding
117
- if (timerId === null)
118
- increment(speed);
119
- });
120
- btnDown.addEventListener("click", (e) => {
121
- const speed = e.shiftKey ? 3 : 1;
122
- if (timerId === null)
123
- decrement(speed);
124
- });
125
- btnDown.addEventListener("mousedown", (e) => {
126
- const speed = e.shiftKey ? 3 : 1;
127
- startTimer(() => decrement(speed), 100);
128
- });
129
- btnDown.addEventListener("touchstart", () => startTimer(decrement, 100));
130
- ["mouseup", "mouseleave", "mouseout", "touchend", "touchcancel"].forEach((event) => {
131
- btnDown.addEventListener(event, stopTimer);
132
- });
133
- input.addEventListener("keydown", (e) => {
134
- const key = e.key;
135
- const speed = e.shiftKey ? 3 : 1;
136
- const allowedKeys = [
137
- "Backspace",
138
- "Delete",
139
- "ArrowLeft",
140
- "ArrowRight",
141
- "Tab",
142
- "Enter",
143
- "Home",
144
- "End",
145
- ];
146
- if (allowedKeys.includes(key) ||
147
- ((e.ctrlKey || e.metaKey) &&
148
- ["a", "c", "v", "x"].includes(key.toLowerCase()))) {
149
- return;
150
- }
151
- if (["e", "+", "-"].includes(key)) {
152
- e.preventDefault();
153
- return;
154
- }
155
- if (key === "ArrowUp" || key === "ArrowDown") {
156
- e.preventDefault();
157
- const val = parseFloat(input.value) || 0;
158
- const stepSize = (1 * speed) / Math.pow(10, step);
159
- let newVal = key === "ArrowUp" ? val + stepSize : val - stepSize;
160
- if (newVal < min)
161
- newVal = min;
162
- input.value = newVal.toFixed(step);
163
- if (Number(input.value) < min)
164
- input.value = min.toFixed(step);
165
- if (Number(input.value) > max)
166
- input.value = max.toFixed(step);
167
- __classPrivateFieldGet(this, _SpinBox_state, "f").call(this, Number(input.value), btnDown, btnUp, min, max);
168
- return;
169
- }
170
- const normalizedKey = key === "," ? "." : key;
171
- const isNumeric = /^[0-9]$/.test(normalizedKey);
172
- const isDot = normalizedKey === ".";
173
- const alreadyHasDot = input.value.includes(".");
174
- if ((step === 0 && !isNumeric) ||
175
- (step > 0 && !(isNumeric || isDot)) ||
176
- (isDot && alreadyHasDot)) {
177
- e.preventDefault();
178
- }
179
- });
180
- input.addEventListener("keyup", () => {
181
- if (Number(input.value) < min)
182
- input.value = min.toFixed(step);
183
- if (Number(input.value) > max)
184
- input.value = max.toFixed(step);
185
- __classPrivateFieldGet(this, _SpinBox_state, "f").call(this, Number(input.value), btnDown, btnUp, min, max);
186
- });
187
- input.addEventListener("change", () => {
188
- if (Number(input.value) < min)
189
- input.value = min.toFixed(step);
190
- if (Number(input.value) > max)
191
- input.value = max.toFixed(step);
192
- __classPrivateFieldGet(this, _SpinBox_state, "f").call(this, Number(input.value), btnDown, btnUp, min, max);
193
- });
194
- });
195
- };
package/dist/index.js DELETED
@@ -1,2 +0,0 @@
1
- export { SpinBox } from "./SpinBox";
2
- import "./scss/style.scss";