zero-hour 1.1.0 → 1.2.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/README.md +2 -2
- package/dist/index.cjs.js +2 -2
- package/dist/index.es.js +137 -115
- package/dist/index.umd.js +2 -2
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -43,7 +43,7 @@ import { initCountdownTimers } from 'zero-hour';
|
|
|
43
43
|
separator-url="/sprites/sep.webp"
|
|
44
44
|
date="2025-12-31"
|
|
45
45
|
time="23:59:59"
|
|
46
|
-
utc="
|
|
46
|
+
utc="+03:00"
|
|
47
47
|
></countdown-timer>
|
|
48
48
|
```
|
|
49
49
|
|
|
@@ -106,7 +106,7 @@ el?.start();
|
|
|
106
106
|
separator-url="/sprites/sep.webp"
|
|
107
107
|
date="2025-12-31"
|
|
108
108
|
time="23:59:59"
|
|
109
|
-
utc="
|
|
109
|
+
utc="+03:00"
|
|
110
110
|
units="h:m:s"
|
|
111
111
|
></countdown-timer>
|
|
112
112
|
```
|
package/dist/index.cjs.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const p=`:host {
|
|
2
2
|
display: block;
|
|
3
3
|
width: 100%;
|
|
4
4
|
container-type: inline-size;
|
|
@@ -51,4 +51,4 @@
|
|
|
51
51
|
white-space: nowrap;
|
|
52
52
|
clip-path: inset(50%);
|
|
53
53
|
}
|
|
54
|
-
`,
|
|
54
|
+
`,T=p;function z(r){const t=r.charCodeAt(0)-48;return t>=0&&t<=9?t:0}function u(r){return r<0?0:r}const d={showDays:!0,showHours:!0,showMinutes:!0,showSeconds:!0},f={hours:0,minutes:0,seconds:0};function m(r){return"adoptedStyleSheets"in r}function b(r){try{const t=new CSSStyleSheet;return t.replaceSync(r),t}catch{return null}}const x=b(p);function y(r){const t=Math.floor(r/1e3),s=t%60,e=Math.floor(t/60)%60,n=Math.floor(t/3600),i=Math.floor(n/24),o=n%24;return{d:i,h:o,m:e,s,totalSec:t}}function E(r){return String(r).padStart(2,"0")}function g(r,t){return[...String(r).padStart(t,"0")]}function F(r,t,s){if(!r.hasAttribute(t))return s;const e=r.getAttribute(t);return e==null||e===""?!0:e!=="false"}function w(r){if(r==null)return null;const t=r.trim();if(!t)return null;const s=t.match(/^(\d{4})-(\d{2})-(\d{2})$/);if(!s)return null;const e=Number(s[1]),n=Number(s[2]),i=Number(s[3]);return!Number.isFinite(e)||!Number.isFinite(n)||!Number.isFinite(i)?null:{year:e,month:n,day:i}}function _(r){if(r==null)return{...f};const t=r.trim();if(!t)return{...f};const s=t.split(":"),e=Number(s[0]??"0"),n=Number(s[1]??"0"),i=Number(s[2]??"0");return{hours:Number.isFinite(e)?e:0,minutes:Number.isFinite(n)?n:0,seconds:Number.isFinite(i)?i:0}}function v(r){if(r==null)return null;const t=r.trim();if(!t)return null;const s=t.match(/^(\d{1,2}):(\d{2})(?::(\d{2}))?$/);if(!s)return null;const e=Number(s[1]),n=Number(s[2]),i=Number(s[3]??"0");return!Number.isFinite(e)||!Number.isFinite(n)||!Number.isFinite(i)||e<0||e>23||n<0||n>59||i<0||i>59?null:{hours:e,minutes:n,seconds:i}}function S(r){if(r==null)return null;let t=r.trim();if(!t)return null;/^utc/i.test(t)&&(t=t.slice(3));let s=1;if(t[0]==="+"?t=t.slice(1):t[0]==="-"&&(s=-1,t=t.slice(1)),!t)return null;const[e,n="0"]=t.split(":"),i=Number(e),o=Number(n);if(!Number.isFinite(i)||!Number.isFinite(o))return null;const l=i*60+o;return s*l}function C(){if(typeof window>"u"||!("location"in window))return null;const r=window.location?.search??"";if(!r)return null;const t=new URLSearchParams(r),s={},e=t.get("date")?.trim();e&&(s.date=e);const n=t.get("time")?.trim();n&&(s.time=n);const i=t.get("utc")?.trim();i&&(s.utc=i);const o=t.get("units")?.trim();return o&&(s.units=o),Object.keys(s).length?s:null}function k(r){const t=(r??"").trim().toLowerCase();if(!t)return d;const s=t.split(":").map(h=>h.trim()).filter(Boolean);if(!s.length)return d;const e=new Set(s),n=e.has("d"),i=e.has("h"),o=e.has("m"),l=e.has("s");return!n&&!i&&!o&&!l?d:{showDays:n,showHours:i,showMinutes:o,showSeconds:l}}class c extends HTMLElement{static defaultStylesheet=x;static observedAttributes=["digits-url","separator-url","autostart","date","time","utc","units"];shadow=this.attachShadow({mode:"open"});digitsUrl=null;separatorUrl=null;autostart=!0;durationMs=0;targetEpochMs=null;startEpochMs=null;nextTickTimeout=null;doneFired=!1;rootEl;daysEl;hoursEl;minutesEl;secondsEl;a11yEl;sep0El;sep1El;sep2El;styleEl=null;showDays=!0;showHours=!0;showMinutes=!0;showSeconds=!0;connectedCallback(){this.render(),this.readAttributes(),this.autostart?this.start():this.renderStaticInitial()}disconnectedCallback(){this.stop()}attributeChangedCallback(t,s,e){if(!this.isConnected)return;const n=this.isRunning();this.readAttributes(),this.doneFired=!1,n&&this.autostart?this.start():this.renderStaticInitial()}start(){this.stop(),this.digitsUrl&&(this.durationMs=this.targetEpochMs!=null?this.targetEpochMs-Date.now():0,this.startEpochMs=Date.now(),this.tick(),this.scheduleNextSecondBoundary())}stop(){this.nextTickTimeout!=null&&(window.clearTimeout(this.nextTickTimeout),this.nextTickTimeout=null),this.startEpochMs=null}reset(){this.doneFired=!1,this.autostart?this.start():this.renderStaticInitial()}isRunning(){return this.startEpochMs!=null&&this.nextTickTimeout!=null}readAttributes(){const t=C();this.digitsUrl=this.getAttribute("digits-url"),this.separatorUrl=this.getAttribute("separator-url"),this.autostart=F(this,"autostart",!0);const s=k(t?.units??this.getAttribute("units"));this.showDays=s.showDays,this.showHours=s.showHours,this.showMinutes=s.showMinutes,this.showSeconds=s.showSeconds;const e=w(this.getAttribute("date")),i=w(t?.date??null)??e,o=_(this.getAttribute("time")),h=v(t?.time??null)??o,a=S(this.getAttribute("utc")),D=S(t?.utc??null)??a??0;if(!i)this.targetEpochMs=null;else{const N=Date.UTC(i.year,i.month-1,i.day,h.hours,h.minutes,h.seconds);this.targetEpochMs=N-D*60*1e3}if(this.durationMs=0,!this.digitsUrl){this.setTextFallback("—:—:—:—");return}this.rootEl.style.setProperty("--zh-digits-url",`url("${this.digitsUrl}")`),this.separatorUrl?this.rootEl.style.setProperty("--zh-sep-url",`url("${this.separatorUrl}")`):this.rootEl.style.removeProperty("--zh-sep-url"),this.applyUnitsVisibility()}renderStaticInitial(){if(this.targetEpochMs!=null){const t=Date.now(),s=u(this.targetEpochMs-t),{d:e,h:n,m:i,s:o}=y(s);this.setDigits({d:e,h:n,m:i,s:o})}else this.setDigits({d:0,h:0,m:0,s:0})}render(){this.applyStyles(null),this.rootEl=document.createElement("div"),this.rootEl.className="zh",this.daysEl=document.createElement("div"),this.daysEl.className="zh__group",this.hoursEl=document.createElement("div"),this.hoursEl.className="zh__group",this.minutesEl=document.createElement("div"),this.minutesEl.className="zh__group",this.secondsEl=document.createElement("div"),this.secondsEl.className="zh__group",this.sep0El=document.createElement("span"),this.sep0El.className="zh__sep",this.sep1El=document.createElement("span"),this.sep1El.className="zh__sep",this.sep2El=document.createElement("span"),this.sep2El.className="zh__sep",this.a11yEl=document.createElement("span"),this.a11yEl.className="zh__a11y",this.a11yEl.setAttribute("aria-live","polite"),this.rootEl.append(this.daysEl,this.sep0El,this.hoursEl,this.sep1El,this.minutesEl,this.sep2El,this.secondsEl,this.a11yEl),this.shadow.innerHTML="",this.styleEl&&this.shadow.append(this.styleEl),this.shadow.append(this.rootEl),this.setDigits({d:0,h:0,m:0,s:0})}setTextFallback(t){this.a11yEl.textContent=t}applyUnitsVisibility(){if(!this.rootEl)return;if(this.daysEl.style.display=this.showDays?"":"none",this.hoursEl.style.display=this.showHours?"":"none",this.minutesEl.style.display=this.showMinutes?"":"none",this.secondsEl.style.display=this.showSeconds?"":"none",!!!this.separatorUrl){this.sep0El.style.display="none",this.sep1El.style.display="none",this.sep2El.style.display="none";return}const s=[this.showDays,this.showHours,this.showMinutes,this.showSeconds],e=[];for(let i=0;i<s.length;i++)s[i]&&e.push(i);this.rootEl.style.setProperty("--zh-groups",String(e.length));const n=[!1,!1,!1];if(e.length>=2)for(let i=0;i<e.length-1;i++){const o=e[i+1],l=Math.min(2,Math.max(0,o-1));n[l]=!0}this.sep0El.style.display=n[0]?"":"none",this.sep1El.style.display=n[1]?"":"none",this.sep2El.style.display=n[2]?"":"none"}setDigits({d:t,h:s,m:e,s:n}){const i=g(Math.min(t,99),2),o=g(s,2),l=[...E(e)],h=[...E(n)];this.syncDigitGroup(this.daysEl,i),this.syncDigitGroup(this.hoursEl,o),this.syncDigitGroup(this.minutesEl,l),this.syncDigitGroup(this.secondsEl,h);const a=[];this.showDays&&a.push(i.join("")),this.showHours&&a.push(o.join("")),this.showMinutes&&a.push(l.join("")),this.showSeconds&&a.push(h.join("")),this.a11yEl.textContent=a.length?a.join(":"):"—"}syncDigitGroup(t,s){for(;t.children.length<s.length;){const e=document.createElement("span");e.className="zh__digit",t.appendChild(e)}for(;t.children.length>s.length;){const e=t.lastElementChild;if(!e)break;t.removeChild(e)}for(let e=0;e<s.length;e++){const n=t.children[e],i=z(s[e]);n.style.setProperty("--zh-sheet-index",String(i))}}tick(){if(!this.digitsUrl)return;const t=u(this.durationMs);if(t===0){this.setDigits({d:0,h:0,m:0,s:0}),this.fireDoneOnce(),this.stop();return}const s=this.startEpochMs??Date.now(),e=u(Date.now()-s),n=u(t-e),{d:i,h:o,m:l,s:h,totalSec:a}=y(n);this.setDigits({d:i,h:o,m:l,s:h}),a===0&&(this.fireDoneOnce(),this.stop())}fireDoneOnce(){this.doneFired||(this.doneFired=!0,this.dispatchEvent(new CustomEvent("done")))}scheduleNextSecondBoundary(){const s=1e3-Date.now()%1e3;this.nextTickTimeout=window.setTimeout(()=>{this.tick(),this.isRunning()&&this.scheduleNextSecondBoundary()},s)}adoptStylesheet(t){this.applyStyles(t)}adoptStyles(t){this.applyStyles(t)}applyStyles(t){if(typeof t=="string"){if(m(this.shadow)){const s=b(t);if(s){this.shadow.adoptedStyleSheets=[s],this.styleEl=null;return}}this.styleEl||(this.styleEl=document.createElement("style")),this.styleEl.textContent=t;return}if(t&&m(this.shadow)){this.shadow.adoptedStyleSheets=[t],this.styleEl=null;return}if(c.defaultStylesheet&&m(this.shadow)){this.shadow.adoptedStyleSheets=[c.defaultStylesheet],this.styleEl=null;return}this.styleEl||(this.styleEl=document.createElement("style")),this.styleEl.textContent=p}}customElements.get("countdown-timer")||customElements.define("countdown-timer",c);function A(r={}){const{selector:t="countdown-timer",onDone:s,stylesheet:e}=r,n=Array.from(document.querySelectorAll(t));return s&&n.forEach(i=>{i.addEventListener("done",()=>s(i))}),e&&n.forEach(i=>{const o=i;typeof e=="string"?o.adoptStyles(e):o.adoptStylesheet(e)}),n}exports.initCountdownTimers=A;exports.zeroHourCssText=T;
|
package/dist/index.es.js
CHANGED
|
@@ -52,13 +52,12 @@ const m = `:host {
|
|
|
52
52
|
clip-path: inset(50%);
|
|
53
53
|
}
|
|
54
54
|
`, C = m;
|
|
55
|
-
function
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
return t >= 1 && t <= 9 ? t - 1 : 9;
|
|
55
|
+
function T(r) {
|
|
56
|
+
const t = r.charCodeAt(0) - 48;
|
|
57
|
+
return t >= 0 && t <= 9 ? t : 0;
|
|
59
58
|
}
|
|
60
|
-
function u(
|
|
61
|
-
return
|
|
59
|
+
function u(r) {
|
|
60
|
+
return r < 0 ? 0 : r;
|
|
62
61
|
}
|
|
63
62
|
const d = {
|
|
64
63
|
showDays: !0,
|
|
@@ -66,90 +65,99 @@ const d = {
|
|
|
66
65
|
showMinutes: !0,
|
|
67
66
|
showSeconds: !0
|
|
68
67
|
}, f = { hours: 0, minutes: 0, seconds: 0 };
|
|
69
|
-
function p(
|
|
70
|
-
return "adoptedStyleSheets" in
|
|
68
|
+
function p(r) {
|
|
69
|
+
return "adoptedStyleSheets" in r;
|
|
71
70
|
}
|
|
72
|
-
function
|
|
71
|
+
function b(r) {
|
|
73
72
|
try {
|
|
74
73
|
const t = new CSSStyleSheet();
|
|
75
|
-
return t.replaceSync(
|
|
74
|
+
return t.replaceSync(r), t;
|
|
76
75
|
} catch {
|
|
77
76
|
return null;
|
|
78
77
|
}
|
|
79
78
|
}
|
|
80
|
-
const
|
|
81
|
-
function y(
|
|
82
|
-
const t = Math.floor(
|
|
83
|
-
return { d: i, h:
|
|
79
|
+
const z = b(m);
|
|
80
|
+
function y(r) {
|
|
81
|
+
const t = Math.floor(r / 1e3), s = t % 60, e = Math.floor(t / 60) % 60, n = Math.floor(t / 3600), i = Math.floor(n / 24), o = n % 24;
|
|
82
|
+
return { d: i, h: o, m: e, s, totalSec: t };
|
|
84
83
|
}
|
|
85
|
-
function E(
|
|
86
|
-
return String(
|
|
84
|
+
function E(r) {
|
|
85
|
+
return String(r).padStart(2, "0");
|
|
87
86
|
}
|
|
88
|
-
function g(
|
|
89
|
-
return [...String(
|
|
87
|
+
function g(r, t) {
|
|
88
|
+
return [...String(r).padStart(t, "0")];
|
|
90
89
|
}
|
|
91
|
-
function
|
|
92
|
-
if (!
|
|
93
|
-
const
|
|
94
|
-
return
|
|
90
|
+
function x(r, t, s) {
|
|
91
|
+
if (!r.hasAttribute(t)) return s;
|
|
92
|
+
const e = r.getAttribute(t);
|
|
93
|
+
return e == null || e === "" ? !0 : e !== "false";
|
|
95
94
|
}
|
|
96
|
-
function
|
|
97
|
-
if (
|
|
98
|
-
const t =
|
|
95
|
+
function w(r) {
|
|
96
|
+
if (r == null) return null;
|
|
97
|
+
const t = r.trim();
|
|
99
98
|
if (!t) return null;
|
|
100
|
-
const
|
|
101
|
-
if (!
|
|
102
|
-
const
|
|
103
|
-
return !Number.isFinite(
|
|
99
|
+
const s = t.match(/^(\d{4})-(\d{2})-(\d{2})$/);
|
|
100
|
+
if (!s) return null;
|
|
101
|
+
const e = Number(s[1]), n = Number(s[2]), i = Number(s[3]);
|
|
102
|
+
return !Number.isFinite(e) || !Number.isFinite(n) || !Number.isFinite(i) ? null : { year: e, month: n, day: i };
|
|
104
103
|
}
|
|
105
|
-
function
|
|
106
|
-
if (
|
|
107
|
-
const t =
|
|
104
|
+
function F(r) {
|
|
105
|
+
if (r == null) return { ...f };
|
|
106
|
+
const t = r.trim();
|
|
108
107
|
if (!t) return { ...f };
|
|
109
|
-
const
|
|
108
|
+
const s = t.split(":"), e = Number(s[0] ?? "0"), n = Number(s[1] ?? "0"), i = Number(s[2] ?? "0");
|
|
110
109
|
return {
|
|
111
|
-
hours: Number.isFinite(
|
|
112
|
-
minutes: Number.isFinite(
|
|
110
|
+
hours: Number.isFinite(e) ? e : 0,
|
|
111
|
+
minutes: Number.isFinite(n) ? n : 0,
|
|
113
112
|
seconds: Number.isFinite(i) ? i : 0
|
|
114
113
|
};
|
|
115
114
|
}
|
|
116
|
-
function
|
|
117
|
-
if (
|
|
118
|
-
|
|
115
|
+
function _(r) {
|
|
116
|
+
if (r == null) return null;
|
|
117
|
+
const t = r.trim();
|
|
118
|
+
if (!t) return null;
|
|
119
|
+
const s = t.match(/^(\d{1,2}):(\d{2})(?::(\d{2}))?$/);
|
|
120
|
+
if (!s) return null;
|
|
121
|
+
const e = Number(s[1]), n = Number(s[2]), i = Number(s[3] ?? "0");
|
|
122
|
+
return !Number.isFinite(e) || !Number.isFinite(n) || !Number.isFinite(i) || e < 0 || e > 23 || n < 0 || n > 59 || i < 0 || i > 59 ? null : { hours: e, minutes: n, seconds: i };
|
|
123
|
+
}
|
|
124
|
+
function S(r) {
|
|
125
|
+
if (r == null) return null;
|
|
126
|
+
let t = r.trim();
|
|
119
127
|
if (!t) return null;
|
|
120
128
|
/^utc/i.test(t) && (t = t.slice(3));
|
|
121
|
-
let
|
|
122
|
-
if (t[0] === "+" ? t = t.slice(1) : t[0] === "-" && (
|
|
123
|
-
const [
|
|
124
|
-
if (!Number.isFinite(i) || !Number.isFinite(
|
|
125
|
-
const
|
|
126
|
-
return
|
|
129
|
+
let s = 1;
|
|
130
|
+
if (t[0] === "+" ? t = t.slice(1) : t[0] === "-" && (s = -1, t = t.slice(1)), !t) return null;
|
|
131
|
+
const [e, n = "0"] = t.split(":"), i = Number(e), o = Number(n);
|
|
132
|
+
if (!Number.isFinite(i) || !Number.isFinite(o)) return null;
|
|
133
|
+
const l = i * 60 + o;
|
|
134
|
+
return s * l;
|
|
127
135
|
}
|
|
128
|
-
function
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
136
|
+
function v() {
|
|
137
|
+
if (typeof window > "u" || !("location" in window)) return null;
|
|
138
|
+
const r = window.location?.search ?? "";
|
|
139
|
+
if (!r) return null;
|
|
140
|
+
const t = new URLSearchParams(r), s = {}, e = t.get("date")?.trim();
|
|
141
|
+
e && (s.date = e);
|
|
142
|
+
const n = t.get("time")?.trim();
|
|
143
|
+
n && (s.time = n);
|
|
144
|
+
const i = t.get("utc")?.trim();
|
|
145
|
+
i && (s.utc = i);
|
|
146
|
+
const o = t.get("units")?.trim();
|
|
147
|
+
return o && (s.units = o), Object.keys(s).length ? s : null;
|
|
140
148
|
}
|
|
141
|
-
function
|
|
142
|
-
const t = (
|
|
149
|
+
function k(r) {
|
|
150
|
+
const t = (r ?? "").trim().toLowerCase();
|
|
143
151
|
if (!t)
|
|
144
152
|
return d;
|
|
145
|
-
const
|
|
146
|
-
if (!
|
|
153
|
+
const s = t.split(":").map((h) => h.trim()).filter(Boolean);
|
|
154
|
+
if (!s.length)
|
|
147
155
|
return d;
|
|
148
|
-
const
|
|
149
|
-
return !
|
|
156
|
+
const e = new Set(s), n = e.has("d"), i = e.has("h"), o = e.has("m"), l = e.has("s");
|
|
157
|
+
return !n && !i && !o && !l ? d : { showDays: n, showHours: i, showMinutes: o, showSeconds: l };
|
|
150
158
|
}
|
|
151
159
|
class c extends HTMLElement {
|
|
152
|
-
static defaultStylesheet =
|
|
160
|
+
static defaultStylesheet = z;
|
|
153
161
|
static observedAttributes = [
|
|
154
162
|
"digits-url",
|
|
155
163
|
"separator-url",
|
|
@@ -188,10 +196,10 @@ class c extends HTMLElement {
|
|
|
188
196
|
disconnectedCallback() {
|
|
189
197
|
this.stop();
|
|
190
198
|
}
|
|
191
|
-
attributeChangedCallback(t,
|
|
199
|
+
attributeChangedCallback(t, s, e) {
|
|
192
200
|
if (!this.isConnected) return;
|
|
193
|
-
const
|
|
194
|
-
this.readAttributes(), this.doneFired = !1,
|
|
201
|
+
const n = this.isRunning();
|
|
202
|
+
this.readAttributes(), this.doneFired = !1, n && this.autostart ? this.start() : this.renderStaticInitial();
|
|
195
203
|
}
|
|
196
204
|
start() {
|
|
197
205
|
this.stop(), this.digitsUrl && (this.durationMs = this.targetEpochMs != null ? this.targetEpochMs - Date.now() : 0, this.startEpochMs = Date.now(), this.tick(), this.scheduleNextSecondBoundary());
|
|
@@ -206,11 +214,25 @@ class c extends HTMLElement {
|
|
|
206
214
|
return this.startEpochMs != null && this.nextTickTimeout != null;
|
|
207
215
|
}
|
|
208
216
|
readAttributes() {
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
217
|
+
const t = v();
|
|
218
|
+
this.digitsUrl = this.getAttribute("digits-url"), this.separatorUrl = this.getAttribute("separator-url"), this.autostart = x(this, "autostart", !0);
|
|
219
|
+
const s = k(t?.units ?? this.getAttribute("units"));
|
|
220
|
+
this.showDays = s.showDays, this.showHours = s.showHours, this.showMinutes = s.showMinutes, this.showSeconds = s.showSeconds;
|
|
221
|
+
const e = w(this.getAttribute("date")), i = w(t?.date ?? null) ?? e, o = F(this.getAttribute("time")), h = _(t?.time ?? null) ?? o, a = S(this.getAttribute("utc")), D = S(t?.utc ?? null) ?? a ?? 0;
|
|
222
|
+
if (!i)
|
|
223
|
+
this.targetEpochMs = null;
|
|
224
|
+
else {
|
|
225
|
+
const N = Date.UTC(
|
|
226
|
+
i.year,
|
|
227
|
+
i.month - 1,
|
|
228
|
+
i.day,
|
|
229
|
+
h.hours,
|
|
230
|
+
h.minutes,
|
|
231
|
+
h.seconds
|
|
232
|
+
);
|
|
233
|
+
this.targetEpochMs = N - D * 60 * 1e3;
|
|
234
|
+
}
|
|
235
|
+
if (this.durationMs = 0, !this.digitsUrl) {
|
|
214
236
|
this.setTextFallback("—:—:—:—");
|
|
215
237
|
return;
|
|
216
238
|
}
|
|
@@ -218,8 +240,8 @@ class c extends HTMLElement {
|
|
|
218
240
|
}
|
|
219
241
|
renderStaticInitial() {
|
|
220
242
|
if (this.targetEpochMs != null) {
|
|
221
|
-
const t = Date.now(),
|
|
222
|
-
this.setDigits({ d:
|
|
243
|
+
const t = Date.now(), s = u(this.targetEpochMs - t), { d: e, h: n, m: i, s: o } = y(s);
|
|
244
|
+
this.setDigits({ d: e, h: n, m: i, s: o });
|
|
223
245
|
} else
|
|
224
246
|
this.setDigits({ d: 0, h: 0, m: 0, s: 0 });
|
|
225
247
|
}
|
|
@@ -244,37 +266,37 @@ class c extends HTMLElement {
|
|
|
244
266
|
this.sep0El.style.display = "none", this.sep1El.style.display = "none", this.sep2El.style.display = "none";
|
|
245
267
|
return;
|
|
246
268
|
}
|
|
247
|
-
const
|
|
248
|
-
for (let i = 0; i <
|
|
249
|
-
|
|
250
|
-
this.rootEl.style.setProperty("--zh-groups", String(
|
|
251
|
-
const
|
|
252
|
-
if (
|
|
253
|
-
for (let i = 0; i <
|
|
254
|
-
const
|
|
255
|
-
|
|
269
|
+
const s = [this.showDays, this.showHours, this.showMinutes, this.showSeconds], e = [];
|
|
270
|
+
for (let i = 0; i < s.length; i++)
|
|
271
|
+
s[i] && e.push(i);
|
|
272
|
+
this.rootEl.style.setProperty("--zh-groups", String(e.length));
|
|
273
|
+
const n = [!1, !1, !1];
|
|
274
|
+
if (e.length >= 2)
|
|
275
|
+
for (let i = 0; i < e.length - 1; i++) {
|
|
276
|
+
const o = e[i + 1], l = Math.min(2, Math.max(0, o - 1));
|
|
277
|
+
n[l] = !0;
|
|
256
278
|
}
|
|
257
|
-
this.sep0El.style.display =
|
|
279
|
+
this.sep0El.style.display = n[0] ? "" : "none", this.sep1El.style.display = n[1] ? "" : "none", this.sep2El.style.display = n[2] ? "" : "none";
|
|
258
280
|
}
|
|
259
|
-
setDigits({ d: t, h:
|
|
260
|
-
const i = g(Math.min(t, 99), 2),
|
|
261
|
-
this.syncDigitGroup(this.daysEl, i), this.syncDigitGroup(this.hoursEl,
|
|
262
|
-
const
|
|
263
|
-
this.showDays &&
|
|
281
|
+
setDigits({ d: t, h: s, m: e, s: n }) {
|
|
282
|
+
const i = g(Math.min(t, 99), 2), o = g(s, 2), l = [...E(e)], h = [...E(n)];
|
|
283
|
+
this.syncDigitGroup(this.daysEl, i), this.syncDigitGroup(this.hoursEl, o), this.syncDigitGroup(this.minutesEl, l), this.syncDigitGroup(this.secondsEl, h);
|
|
284
|
+
const a = [];
|
|
285
|
+
this.showDays && a.push(i.join("")), this.showHours && a.push(o.join("")), this.showMinutes && a.push(l.join("")), this.showSeconds && a.push(h.join("")), this.a11yEl.textContent = a.length ? a.join(":") : "—";
|
|
264
286
|
}
|
|
265
|
-
syncDigitGroup(t,
|
|
266
|
-
for (; t.children.length <
|
|
267
|
-
const
|
|
268
|
-
|
|
287
|
+
syncDigitGroup(t, s) {
|
|
288
|
+
for (; t.children.length < s.length; ) {
|
|
289
|
+
const e = document.createElement("span");
|
|
290
|
+
e.className = "zh__digit", t.appendChild(e);
|
|
269
291
|
}
|
|
270
|
-
for (; t.children.length >
|
|
271
|
-
const
|
|
272
|
-
if (!
|
|
273
|
-
t.removeChild(
|
|
292
|
+
for (; t.children.length > s.length; ) {
|
|
293
|
+
const e = t.lastElementChild;
|
|
294
|
+
if (!e) break;
|
|
295
|
+
t.removeChild(e);
|
|
274
296
|
}
|
|
275
|
-
for (let
|
|
276
|
-
const
|
|
277
|
-
|
|
297
|
+
for (let e = 0; e < s.length; e++) {
|
|
298
|
+
const n = t.children[e], i = T(s[e]);
|
|
299
|
+
n.style.setProperty("--zh-sheet-index", String(i));
|
|
278
300
|
}
|
|
279
301
|
}
|
|
280
302
|
tick() {
|
|
@@ -284,17 +306,17 @@ class c extends HTMLElement {
|
|
|
284
306
|
this.setDigits({ d: 0, h: 0, m: 0, s: 0 }), this.fireDoneOnce(), this.stop();
|
|
285
307
|
return;
|
|
286
308
|
}
|
|
287
|
-
const
|
|
288
|
-
this.setDigits({ d: i, h:
|
|
309
|
+
const s = this.startEpochMs ?? Date.now(), e = u(Date.now() - s), n = u(t - e), { d: i, h: o, m: l, s: h, totalSec: a } = y(n);
|
|
310
|
+
this.setDigits({ d: i, h: o, m: l, s: h }), a === 0 && (this.fireDoneOnce(), this.stop());
|
|
289
311
|
}
|
|
290
312
|
fireDoneOnce() {
|
|
291
313
|
this.doneFired || (this.doneFired = !0, this.dispatchEvent(new CustomEvent("done")));
|
|
292
314
|
}
|
|
293
315
|
scheduleNextSecondBoundary() {
|
|
294
|
-
const
|
|
316
|
+
const s = 1e3 - Date.now() % 1e3;
|
|
295
317
|
this.nextTickTimeout = window.setTimeout(() => {
|
|
296
318
|
this.tick(), this.isRunning() && this.scheduleNextSecondBoundary();
|
|
297
|
-
},
|
|
319
|
+
}, s);
|
|
298
320
|
}
|
|
299
321
|
adoptStylesheet(t) {
|
|
300
322
|
this.applyStyles(t);
|
|
@@ -305,9 +327,9 @@ class c extends HTMLElement {
|
|
|
305
327
|
applyStyles(t) {
|
|
306
328
|
if (typeof t == "string") {
|
|
307
329
|
if (p(this.shadow)) {
|
|
308
|
-
const
|
|
309
|
-
if (
|
|
310
|
-
this.shadow.adoptedStyleSheets = [
|
|
330
|
+
const s = b(t);
|
|
331
|
+
if (s) {
|
|
332
|
+
this.shadow.adoptedStyleSheets = [s], this.styleEl = null;
|
|
311
333
|
return;
|
|
312
334
|
}
|
|
313
335
|
}
|
|
@@ -326,16 +348,16 @@ class c extends HTMLElement {
|
|
|
326
348
|
}
|
|
327
349
|
}
|
|
328
350
|
customElements.get("countdown-timer") || customElements.define("countdown-timer", c);
|
|
329
|
-
function
|
|
330
|
-
const { selector: t = "countdown-timer", onDone:
|
|
331
|
-
return
|
|
332
|
-
i.addEventListener("done", () =>
|
|
333
|
-
}),
|
|
334
|
-
const
|
|
335
|
-
typeof
|
|
336
|
-
}),
|
|
351
|
+
function A(r = {}) {
|
|
352
|
+
const { selector: t = "countdown-timer", onDone: s, stylesheet: e } = r, n = Array.from(document.querySelectorAll(t));
|
|
353
|
+
return s && n.forEach((i) => {
|
|
354
|
+
i.addEventListener("done", () => s(i));
|
|
355
|
+
}), e && n.forEach((i) => {
|
|
356
|
+
const o = i;
|
|
357
|
+
typeof e == "string" ? o.adoptStyles(e) : o.adoptStylesheet(e);
|
|
358
|
+
}), n;
|
|
337
359
|
}
|
|
338
360
|
export {
|
|
339
|
-
|
|
361
|
+
A as initCountdownTimers,
|
|
340
362
|
C as zeroHourCssText
|
|
341
363
|
};
|
package/dist/index.umd.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
(function(a,
|
|
1
|
+
(function(a,c){typeof exports=="object"&&typeof module<"u"?c(exports):typeof define=="function"&&define.amd?define(["exports"],c):(a=typeof globalThis<"u"?globalThis:a||self,c(a.ZeroHour={}))})(this,(function(a){"use strict";const c=`:host {
|
|
2
2
|
display: block;
|
|
3
3
|
width: 100%;
|
|
4
4
|
container-type: inline-size;
|
|
@@ -51,4 +51,4 @@
|
|
|
51
51
|
white-space: nowrap;
|
|
52
52
|
clip-path: inset(50%);
|
|
53
53
|
}
|
|
54
|
-
`,
|
|
54
|
+
`,T=c;function D(o){const t=o.charCodeAt(0)-48;return t>=0&&t<=9?t:0}function d(o){return o<0?0:o}const m={showDays:!0,showHours:!0,showMinutes:!0,showSeconds:!0},y={hours:0,minutes:0,seconds:0};function f(o){return"adoptedStyleSheets"in o}function E(o){try{const t=new CSSStyleSheet;return t.replaceSync(o),t}catch{return null}}const N=E(c);function g(o){const t=Math.floor(o/1e3),e=t%60,s=Math.floor(t/60)%60,n=Math.floor(t/3600),i=Math.floor(n/24),r=n%24;return{d:i,h:r,m:s,s:e,totalSec:t}}function w(o){return String(o).padStart(2,"0")}function S(o,t){return[...String(o).padStart(t,"0")]}function z(o,t,e){if(!o.hasAttribute(t))return e;const s=o.getAttribute(t);return s==null||s===""?!0:s!=="false"}function b(o){if(o==null)return null;const t=o.trim();if(!t)return null;const e=t.match(/^(\d{4})-(\d{2})-(\d{2})$/);if(!e)return null;const s=Number(e[1]),n=Number(e[2]),i=Number(e[3]);return!Number.isFinite(s)||!Number.isFinite(n)||!Number.isFinite(i)?null:{year:s,month:n,day:i}}function x(o){if(o==null)return{...y};const t=o.trim();if(!t)return{...y};const e=t.split(":"),s=Number(e[0]??"0"),n=Number(e[1]??"0"),i=Number(e[2]??"0");return{hours:Number.isFinite(s)?s:0,minutes:Number.isFinite(n)?n:0,seconds:Number.isFinite(i)?i:0}}function F(o){if(o==null)return null;const t=o.trim();if(!t)return null;const e=t.match(/^(\d{1,2}):(\d{2})(?::(\d{2}))?$/);if(!e)return null;const s=Number(e[1]),n=Number(e[2]),i=Number(e[3]??"0");return!Number.isFinite(s)||!Number.isFinite(n)||!Number.isFinite(i)||s<0||s>23||n<0||n>59||i<0||i>59?null:{hours:s,minutes:n,seconds:i}}function M(o){if(o==null)return null;let t=o.trim();if(!t)return null;/^utc/i.test(t)&&(t=t.slice(3));let e=1;if(t[0]==="+"?t=t.slice(1):t[0]==="-"&&(e=-1,t=t.slice(1)),!t)return null;const[s,n="0"]=t.split(":"),i=Number(s),r=Number(n);if(!Number.isFinite(i)||!Number.isFinite(r))return null;const l=i*60+r;return e*l}function _(){if(typeof window>"u"||!("location"in window))return null;const o=window.location?.search??"";if(!o)return null;const t=new URLSearchParams(o),e={},s=t.get("date")?.trim();s&&(e.date=s);const n=t.get("time")?.trim();n&&(e.time=n);const i=t.get("utc")?.trim();i&&(e.utc=i);const r=t.get("units")?.trim();return r&&(e.units=r),Object.keys(e).length?e:null}function v(o){const t=(o??"").trim().toLowerCase();if(!t)return m;const e=t.split(":").map(h=>h.trim()).filter(Boolean);if(!e.length)return m;const s=new Set(e),n=s.has("d"),i=s.has("h"),r=s.has("m"),l=s.has("s");return!n&&!i&&!r&&!l?m:{showDays:n,showHours:i,showMinutes:r,showSeconds:l}}class p extends HTMLElement{static defaultStylesheet=N;static observedAttributes=["digits-url","separator-url","autostart","date","time","utc","units"];shadow=this.attachShadow({mode:"open"});digitsUrl=null;separatorUrl=null;autostart=!0;durationMs=0;targetEpochMs=null;startEpochMs=null;nextTickTimeout=null;doneFired=!1;rootEl;daysEl;hoursEl;minutesEl;secondsEl;a11yEl;sep0El;sep1El;sep2El;styleEl=null;showDays=!0;showHours=!0;showMinutes=!0;showSeconds=!0;connectedCallback(){this.render(),this.readAttributes(),this.autostart?this.start():this.renderStaticInitial()}disconnectedCallback(){this.stop()}attributeChangedCallback(t,e,s){if(!this.isConnected)return;const n=this.isRunning();this.readAttributes(),this.doneFired=!1,n&&this.autostart?this.start():this.renderStaticInitial()}start(){this.stop(),this.digitsUrl&&(this.durationMs=this.targetEpochMs!=null?this.targetEpochMs-Date.now():0,this.startEpochMs=Date.now(),this.tick(),this.scheduleNextSecondBoundary())}stop(){this.nextTickTimeout!=null&&(window.clearTimeout(this.nextTickTimeout),this.nextTickTimeout=null),this.startEpochMs=null}reset(){this.doneFired=!1,this.autostart?this.start():this.renderStaticInitial()}isRunning(){return this.startEpochMs!=null&&this.nextTickTimeout!=null}readAttributes(){const t=_();this.digitsUrl=this.getAttribute("digits-url"),this.separatorUrl=this.getAttribute("separator-url"),this.autostart=z(this,"autostart",!0);const e=v(t?.units??this.getAttribute("units"));this.showDays=e.showDays,this.showHours=e.showHours,this.showMinutes=e.showMinutes,this.showSeconds=e.showSeconds;const s=b(this.getAttribute("date")),i=b(t?.date??null)??s,r=x(this.getAttribute("time")),h=F(t?.time??null)??r,u=M(this.getAttribute("utc")),A=M(t?.utc??null)??u??0;if(!i)this.targetEpochMs=null;else{const U=Date.UTC(i.year,i.month-1,i.day,h.hours,h.minutes,h.seconds);this.targetEpochMs=U-A*60*1e3}if(this.durationMs=0,!this.digitsUrl){this.setTextFallback("—:—:—:—");return}this.rootEl.style.setProperty("--zh-digits-url",`url("${this.digitsUrl}")`),this.separatorUrl?this.rootEl.style.setProperty("--zh-sep-url",`url("${this.separatorUrl}")`):this.rootEl.style.removeProperty("--zh-sep-url"),this.applyUnitsVisibility()}renderStaticInitial(){if(this.targetEpochMs!=null){const t=Date.now(),e=d(this.targetEpochMs-t),{d:s,h:n,m:i,s:r}=g(e);this.setDigits({d:s,h:n,m:i,s:r})}else this.setDigits({d:0,h:0,m:0,s:0})}render(){this.applyStyles(null),this.rootEl=document.createElement("div"),this.rootEl.className="zh",this.daysEl=document.createElement("div"),this.daysEl.className="zh__group",this.hoursEl=document.createElement("div"),this.hoursEl.className="zh__group",this.minutesEl=document.createElement("div"),this.minutesEl.className="zh__group",this.secondsEl=document.createElement("div"),this.secondsEl.className="zh__group",this.sep0El=document.createElement("span"),this.sep0El.className="zh__sep",this.sep1El=document.createElement("span"),this.sep1El.className="zh__sep",this.sep2El=document.createElement("span"),this.sep2El.className="zh__sep",this.a11yEl=document.createElement("span"),this.a11yEl.className="zh__a11y",this.a11yEl.setAttribute("aria-live","polite"),this.rootEl.append(this.daysEl,this.sep0El,this.hoursEl,this.sep1El,this.minutesEl,this.sep2El,this.secondsEl,this.a11yEl),this.shadow.innerHTML="",this.styleEl&&this.shadow.append(this.styleEl),this.shadow.append(this.rootEl),this.setDigits({d:0,h:0,m:0,s:0})}setTextFallback(t){this.a11yEl.textContent=t}applyUnitsVisibility(){if(!this.rootEl)return;if(this.daysEl.style.display=this.showDays?"":"none",this.hoursEl.style.display=this.showHours?"":"none",this.minutesEl.style.display=this.showMinutes?"":"none",this.secondsEl.style.display=this.showSeconds?"":"none",!!!this.separatorUrl){this.sep0El.style.display="none",this.sep1El.style.display="none",this.sep2El.style.display="none";return}const e=[this.showDays,this.showHours,this.showMinutes,this.showSeconds],s=[];for(let i=0;i<e.length;i++)e[i]&&s.push(i);this.rootEl.style.setProperty("--zh-groups",String(s.length));const n=[!1,!1,!1];if(s.length>=2)for(let i=0;i<s.length-1;i++){const r=s[i+1],l=Math.min(2,Math.max(0,r-1));n[l]=!0}this.sep0El.style.display=n[0]?"":"none",this.sep1El.style.display=n[1]?"":"none",this.sep2El.style.display=n[2]?"":"none"}setDigits({d:t,h:e,m:s,s:n}){const i=S(Math.min(t,99),2),r=S(e,2),l=[...w(s)],h=[...w(n)];this.syncDigitGroup(this.daysEl,i),this.syncDigitGroup(this.hoursEl,r),this.syncDigitGroup(this.minutesEl,l),this.syncDigitGroup(this.secondsEl,h);const u=[];this.showDays&&u.push(i.join("")),this.showHours&&u.push(r.join("")),this.showMinutes&&u.push(l.join("")),this.showSeconds&&u.push(h.join("")),this.a11yEl.textContent=u.length?u.join(":"):"—"}syncDigitGroup(t,e){for(;t.children.length<e.length;){const s=document.createElement("span");s.className="zh__digit",t.appendChild(s)}for(;t.children.length>e.length;){const s=t.lastElementChild;if(!s)break;t.removeChild(s)}for(let s=0;s<e.length;s++){const n=t.children[s],i=D(e[s]);n.style.setProperty("--zh-sheet-index",String(i))}}tick(){if(!this.digitsUrl)return;const t=d(this.durationMs);if(t===0){this.setDigits({d:0,h:0,m:0,s:0}),this.fireDoneOnce(),this.stop();return}const e=this.startEpochMs??Date.now(),s=d(Date.now()-e),n=d(t-s),{d:i,h:r,m:l,s:h,totalSec:u}=g(n);this.setDigits({d:i,h:r,m:l,s:h}),u===0&&(this.fireDoneOnce(),this.stop())}fireDoneOnce(){this.doneFired||(this.doneFired=!0,this.dispatchEvent(new CustomEvent("done")))}scheduleNextSecondBoundary(){const e=1e3-Date.now()%1e3;this.nextTickTimeout=window.setTimeout(()=>{this.tick(),this.isRunning()&&this.scheduleNextSecondBoundary()},e)}adoptStylesheet(t){this.applyStyles(t)}adoptStyles(t){this.applyStyles(t)}applyStyles(t){if(typeof t=="string"){if(f(this.shadow)){const e=E(t);if(e){this.shadow.adoptedStyleSheets=[e],this.styleEl=null;return}}this.styleEl||(this.styleEl=document.createElement("style")),this.styleEl.textContent=t;return}if(t&&f(this.shadow)){this.shadow.adoptedStyleSheets=[t],this.styleEl=null;return}if(p.defaultStylesheet&&f(this.shadow)){this.shadow.adoptedStyleSheets=[p.defaultStylesheet],this.styleEl=null;return}this.styleEl||(this.styleEl=document.createElement("style")),this.styleEl.textContent=c}}customElements.get("countdown-timer")||customElements.define("countdown-timer",p);function C(o={}){const{selector:t="countdown-timer",onDone:e,stylesheet:s}=o,n=Array.from(document.querySelectorAll(t));return e&&n.forEach(i=>{i.addEventListener("done",()=>e(i))}),s&&n.forEach(i=>{const r=i;typeof s=="string"?r.adoptStyles(s):r.adoptStylesheet(s)}),n}a.initCountdownTimers=C,a.zeroHourCssText=T,Object.defineProperty(a,Symbol.toStringTag,{value:"Module"})}));
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "zero-hour",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"description": "Tiny countdown Web Component. Registers <countdown-timer> that renders a configurable DD:HH:MM:SS countdown to a target date/time (optional UTC offset) and fires a done event at zero.",
|
|
5
5
|
"author": "ux-ui.pro",
|
|
6
6
|
"license": "MIT",
|
|
@@ -39,8 +39,8 @@
|
|
|
39
39
|
"dist/"
|
|
40
40
|
],
|
|
41
41
|
"devDependencies": {
|
|
42
|
-
"@biomejs/biome": "2.3.
|
|
43
|
-
"@types/node": "25.0.
|
|
42
|
+
"@biomejs/biome": "2.3.10",
|
|
43
|
+
"@types/node": "25.0.3",
|
|
44
44
|
"lightningcss": "1.30.2",
|
|
45
45
|
"rimraf": "6.1.2",
|
|
46
46
|
"typescript": "5.9.3",
|