zero-hour 1.3.1 → 1.3.2
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 -0
- package/dist/index.cjs.js +23 -2
- package/dist/index.d.ts +11 -0
- package/dist/index.es.js +241 -152
- package/dist/index.umd.js +23 -2
- package/dist/zero-hour.css +1 -1
- package/package.json +2 -1
package/README.md
CHANGED
|
@@ -125,6 +125,7 @@ el?.start();
|
|
|
125
125
|
| `time` | `HH:MM[:SS]` | `00:00:00` | Target time. |
|
|
126
126
|
| `utc` | `UTC±H[:MM]` or `±H[:MM]` | `UTC+0` | UTC offset used to compute the target moment. Examples: `utc="UTC+03:00"`, `utc="UTC-5"`. |
|
|
127
127
|
| `units` | `string` | `"d:h:m:s"` | Visible groups pattern using `d`, `h`, `m`, `s` separated by `:` (e.g. `"h:m:s"`). Empty/invalid value falls back to showing all. |
|
|
128
|
+
| `mode` | `"static"` \| `"scroll"` | `"static"` | Digit transition mode. `scroll` animates digits (rolling effect), `static` swaps without scroll. |
|
|
128
129
|
|
|
129
130
|
<br>
|
|
130
131
|
|
|
@@ -147,6 +148,7 @@ el?.start();
|
|
|
147
148
|
- Updates tick **exactly on second boundaries** (schedules the next tick to the next full second) to keep the display stable.
|
|
148
149
|
- Days render as **two digits** and are capped at **99**.
|
|
149
150
|
- `units` controls which groups (d/h/m/s) are visible. Separators are auto-hidden when `separator-url` is not set, or when a separator is not needed between visible groups.
|
|
151
|
+
- `mode="scroll"` enables rolling digit transitions; default is `static` (no scroll).
|
|
150
152
|
- The `done` event fires once per run (after `reset()` it can fire again).
|
|
151
153
|
- Digit sprite must be horizontal, frames left-to-right: `0,1,2,3,4,5,6,7,8,9`. The frame index equals the digit.
|
|
152
154
|
|
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 g=`:host {
|
|
2
2
|
display: block;
|
|
3
3
|
width: 100%;
|
|
4
4
|
container-type: inline-size;
|
|
@@ -35,6 +35,27 @@
|
|
|
35
35
|
.zh__digit {
|
|
36
36
|
aspect-ratio: var(--zh-digit-aspect, 9 / 12);
|
|
37
37
|
width: 100%;
|
|
38
|
+
position: relative;
|
|
39
|
+
overflow: hidden;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
.zh__digit-track {
|
|
43
|
+
width: 100%;
|
|
44
|
+
height: 100%;
|
|
45
|
+
display: flex;
|
|
46
|
+
flex-direction: column;
|
|
47
|
+
transform: translateY(0);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
.zh--mode-scroll .zh__digit-track {
|
|
51
|
+
transition: transform var(--zh-scroll-duration, 375ms)
|
|
52
|
+
var(--zh-scroll-timing, cubic-bezier(0.445, 0.05, 0.55, 0.95));
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
.zh__digit-face {
|
|
56
|
+
flex: 0 0 100%;
|
|
57
|
+
width: 100%;
|
|
58
|
+
height: 100%;
|
|
38
59
|
background-image: var(--zh-digits-url);
|
|
39
60
|
background-repeat: no-repeat;
|
|
40
61
|
/* Digits sprite is horizontal (frames left-to-right). */
|
|
@@ -51,4 +72,4 @@
|
|
|
51
72
|
white-space: nowrap;
|
|
52
73
|
clip-path: inset(50%);
|
|
53
74
|
}
|
|
54
|
-
`,T=m;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 p(r){return"adoptedStyleSheets"in r}function b(r){try{const t=new CSSStyleSheet;return t.replaceSync(r),t}catch{return null}}const x=b(m);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}isDone(){return!this.digitsUrl||this.targetEpochMs==null?!1:Date.now()>=this.targetEpochMs}readAttributes(){this.rootEl||this.render();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(p(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&&p(this.shadow)){this.shadow.adoptedStyleSheets=[t],this.styleEl=null;return}if(c.defaultStylesheet&&p(this.shadow)){this.shadow.adoptedStyleSheets=[c.defaultStylesheet],this.styleEl=null;return}this.styleEl||(this.styleEl=document.createElement("style")),this.styleEl.textContent=m}}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));if(s){const i=new WeakSet,o=l=>{i.has(l)||(i.add(l),s(l))};n.forEach(l=>{l.addEventListener("done",()=>o(l));const h=l;typeof h.isDone=="function"&&h.isDone()&&o(l)})}return e&&n.forEach(i=>{const o=i;typeof e=="string"?o.adoptStyles(e):o.adoptStylesheet(e)}),n}exports.initCountdownTimers=A;exports.zeroHourCssText=T;
|
|
75
|
+
`,T=g;function F(o){const t=o.charCodeAt(0)-48;return t>=0&&t<=9?t:0}function d(o){return o<0?0:o}const p={showDays:!0,showHours:!0,showMinutes:!0,showSeconds:!0},y={hours:0,minutes:0,seconds:0};function f(o){return"adoptedStyleSheets"in o}function z(o){try{const t=new CSSStyleSheet;return t.replaceSync(o),t}catch{return null}}const N=z(g);function E(o){const t=Math.floor(o/1e3),e=t%60,s=Math.floor(t/60)%60,i=Math.floor(t/3600),n=Math.floor(i/24),r=i%24;return{d:n,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 x(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]),i=Number(e[2]),n=Number(e[3]);return!Number.isFinite(s)||!Number.isFinite(i)||!Number.isFinite(n)?null:{year:s,month:i,day:n}}function C(o){if(o==null)return{...y};const t=o.trim();if(!t)return{...y};const e=t.split(":"),s=Number(e[0]??"0"),i=Number(e[1]??"0"),n=Number(e[2]??"0");return{hours:Number.isFinite(s)?s:0,minutes:Number.isFinite(i)?i:0,seconds:Number.isFinite(n)?n:0}}function v(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]),i=Number(e[2]),n=Number(e[3]??"0");return!Number.isFinite(s)||!Number.isFinite(i)||!Number.isFinite(n)||s<0||s>23||i<0||i>59||n<0||n>59?null:{hours:s,minutes:i,seconds:n}}function D(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,i="0"]=t.split(":"),n=Number(s),r=Number(i);if(!Number.isFinite(n)||!Number.isFinite(r))return null;const l=n*60+r;return e*l}function A(){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 i=t.get("time")?.trim();i&&(e.time=i);const n=t.get("utc")?.trim();n&&(e.utc=n);const r=t.get("units")?.trim();return r&&(e.units=r),Object.keys(e).length?e:null}function U(o){const t=(o??"").trim().toLowerCase();if(!t)return p;const e=t.split(":").map(a=>a.trim()).filter(Boolean);if(!e.length)return p;const s=new Set(e),i=s.has("d"),n=s.has("h"),r=s.has("m"),l=s.has("s");return!i&&!n&&!r&&!l?p:{showDays:i,showHours:n,showMinutes:r,showSeconds:l}}class m extends HTMLElement{static defaultStylesheet=N;static observedAttributes=["digits-url","separator-url","autostart","date","time","utc","units","mode"];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;mode="static";hasDigitsRendered=!1;connectedCallback(){this.render(),this.readAttributes(),this.autostart?this.start():this.renderStaticInitial()}disconnectedCallback(){this.stop()}attributeChangedCallback(t,e,s){if(!this.isConnected)return;const i=this.isRunning();this.readAttributes(),this.doneFired=!1,i&&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}isDone(){return!this.digitsUrl||this.targetEpochMs==null?!1:Date.now()>=this.targetEpochMs}readAttributes(){this.rootEl||this.render();const t=A();this.digitsUrl=this.getAttribute("digits-url"),this.separatorUrl=this.getAttribute("separator-url"),this.autostart=x(this,"autostart",!0);const e=(this.getAttribute("mode")??"").trim().toLowerCase();this.mode=e==="scroll"?"scroll":"static";const s=U(t?.units??this.getAttribute("units"));this.showDays=s.showDays,this.showHours=s.showHours,this.showMinutes=s.showMinutes,this.showSeconds=s.showSeconds;const i=b(this.getAttribute("date")),r=b(t?.date??null)??i,l=C(this.getAttribute("time")),h=v(t?.time??null)??l,c=D(this.getAttribute("utc")),_=D(t?.utc??null)??c??0;if(!r)this.targetEpochMs=null;else{const M=Date.UTC(r.year,r.month-1,r.day,h.hours,h.minutes,h.seconds);this.targetEpochMs=M-_*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(),this.rootEl.classList.toggle("zh--mode-scroll",this.mode==="scroll")}renderStaticInitial(){if(this.targetEpochMs!=null){const t=Date.now(),e=d(this.targetEpochMs-t),{d:s,h:i,m:n,s:r}=E(e);this.setDigits({d:s,h:i,m:n,s:r},!1)}else this.setDigits({d:0,h:0,m:0,s:0},!1)}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},!1),this.hasDigitsRendered=!1}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 n=0;n<e.length;n++)e[n]&&s.push(n);this.rootEl.style.setProperty("--zh-groups",String(s.length));const i=[!1,!1,!1];if(s.length>=2)for(let n=0;n<s.length-1;n++){const r=s[n+1],l=Math.min(2,Math.max(0,r-1));i[l]=!0}this.sep0El.style.display=i[0]?"":"none",this.sep1El.style.display=i[1]?"":"none",this.sep2El.style.display=i[2]?"":"none"}setDigits({d:t,h:e,m:s,s:i},n=!0){let r=n;this.hasDigitsRendered||(r=!1);const l=S(Math.min(t,99),2),a=S(e,2),h=[...w(s)],c=[...w(i)];this.syncDigitGroup(this.daysEl,l,r),this.syncDigitGroup(this.hoursEl,a,r),this.syncDigitGroup(this.minutesEl,h,r),this.syncDigitGroup(this.secondsEl,c,r);const u=[];this.showDays&&u.push(l.join("")),this.showHours&&u.push(a.join("")),this.showMinutes&&u.push(h.join("")),this.showSeconds&&u.push(c.join("")),this.a11yEl.textContent=u.length?u.join(":"):"—",this.hasDigitsRendered=!0}syncDigitGroup(t,e,s){for(;t.children.length<e.length;){const i=t.children.length;t.appendChild(this.createDigitSlot(e[i]))}for(;t.children.length>e.length;){const i=t.lastElementChild;if(!i)break;t.removeChild(i)}for(let i=0;i<e.length;i++){const n=t.children[i];this.syncDigitSlot(n,e[i],s)}}createDigitSlot(t){const e=document.createElement("span");e.className="zh__digit";const s=document.createElement("span");s.className="zh__digit-track";const i=this.createDigitFace(t);return i.classList.add("zh__digit-face--current"),s.append(i),e.append(s),e}createDigitFace(t){const e=document.createElement("span");return e.className="zh__digit-face",this.setFaceDigit(e,t),e}setFaceDigit(t,e){t.dataset.zhDigit=e;const s=F(e);t.style.setProperty("--zh-sheet-index",String(s))}ensureDigitTrack(t){let e=t.querySelector(".zh__digit-track");return e||(e=document.createElement("span"),e.className="zh__digit-track",t.innerHTML="",t.append(e)),e}getOrCreateCurrentFace(t,e){let s=t.querySelector(".zh__digit-face--current");if(!s)s=t.querySelector(".zh__digit-face")??this.createDigitFace(e),s.classList.add("zh__digit-face--current"),this.setFaceDigit(s,s.dataset.zhDigit??e),t.innerHTML="",t.append(s);else{const i=Array.from(t.children);for(const n of i)n!==s&&t.removeChild(n)}return s}cleanupTrack(t,e){if(!t.contains(e))return;const s=Array.from(t.children);for(const i of s)i!==e&&t.removeChild(i);e.classList.remove("zh__digit-face--next"),e.classList.add("zh__digit-face--current"),t.style.transition=this.mode==="scroll"?"":"none",t.style.transform="translateY(0)"}parseTransitionMs(t){const e=window.getComputedStyle(t),s=e.transitionDuration.split(",").map(a=>a.trim()),i=e.transitionDelay.split(",").map(a=>a.trim()),n=a=>a?a.endsWith("ms")?Number.parseFloat(a):a.endsWith("s")?Number.parseFloat(a)*1e3:Number.parseFloat(a)||0:0,r=s[0]?n(s[0]):0,l=i[0]?n(i[0]):0;return r+l}animateDigitChange(t,e,s){const i=this.createDigitFace(s);i.classList.add("zh__digit-face--next"),t.innerHTML="",t.append(i,e),t.style.transition="none",t.style.transform="translateY(-100%)",t.offsetHeight,t.style.transition="",t.style.transform="translateY(0)";const n=()=>{t.removeEventListener("transitionend",n),t.contains(i)&&this.cleanupTrack(t,i)};t.addEventListener("transitionend",n,{once:!0});const r=this.parseTransitionMs(t)+150;window.setTimeout(n,r||800)}syncDigitSlot(t,e,s){const i=this.ensureDigitTrack(t),n=this.getOrCreateCurrentFace(i,e),r=n.dataset.zhDigit??e;if(this.setFaceDigit(n,r),r===e||!s||this.mode!=="scroll"){r!==e&&this.setFaceDigit(n,e),this.cleanupTrack(i,n);return}this.animateDigitChange(i,n,e)}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),i=d(t-s),{d:n,h:r,m:l,s:a,totalSec:h}=E(i);this.setDigits({d:n,h:r,m:l,s:a}),h===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=z(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(m.defaultStylesheet&&f(this.shadow)){this.shadow.adoptedStyleSheets=[m.defaultStylesheet],this.styleEl=null;return}this.styleEl||(this.styleEl=document.createElement("style")),this.styleEl.textContent=g}}customElements.get("countdown-timer")||customElements.define("countdown-timer",m);function L(o={}){const{selector:t="countdown-timer",onDone:e,stylesheet:s}=o,i=Array.from(document.querySelectorAll(t));if(e){const n=new WeakSet,r=l=>{n.has(l)||(n.add(l),e(l))};i.forEach(l=>{l.addEventListener("done",()=>r(l));const a=l;typeof a.isDone=="function"&&a.isDone()&&r(l)})}return s&&i.forEach(n=>{const r=n;typeof s=="string"?r.adoptStyles(s):r.adoptStylesheet(s)}),i}exports.initCountdownTimers=L;exports.zeroHourCssText=T;
|
package/dist/index.d.ts
CHANGED
|
@@ -26,6 +26,8 @@ declare class ZeroHour extends HTMLElement {
|
|
|
26
26
|
private showHours;
|
|
27
27
|
private showMinutes;
|
|
28
28
|
private showSeconds;
|
|
29
|
+
private mode;
|
|
30
|
+
private hasDigitsRendered;
|
|
29
31
|
connectedCallback(): void;
|
|
30
32
|
disconnectedCallback(): void;
|
|
31
33
|
attributeChangedCallback(name: string, oldValue: string | null, newValue: string | null): void;
|
|
@@ -41,6 +43,15 @@ declare class ZeroHour extends HTMLElement {
|
|
|
41
43
|
private applyUnitsVisibility;
|
|
42
44
|
private setDigits;
|
|
43
45
|
private syncDigitGroup;
|
|
46
|
+
private createDigitSlot;
|
|
47
|
+
private createDigitFace;
|
|
48
|
+
private setFaceDigit;
|
|
49
|
+
private ensureDigitTrack;
|
|
50
|
+
private getOrCreateCurrentFace;
|
|
51
|
+
private cleanupTrack;
|
|
52
|
+
private parseTransitionMs;
|
|
53
|
+
private animateDigitChange;
|
|
54
|
+
private syncDigitSlot;
|
|
44
55
|
private tick;
|
|
45
56
|
private fireDoneOnce;
|
|
46
57
|
private scheduleNextSecondBoundary;
|
package/dist/index.es.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
const
|
|
1
|
+
const g = `:host {
|
|
2
2
|
display: block;
|
|
3
3
|
width: 100%;
|
|
4
4
|
container-type: inline-size;
|
|
@@ -35,6 +35,27 @@ const m = `:host {
|
|
|
35
35
|
.zh__digit {
|
|
36
36
|
aspect-ratio: var(--zh-digit-aspect, 9 / 12);
|
|
37
37
|
width: 100%;
|
|
38
|
+
position: relative;
|
|
39
|
+
overflow: hidden;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
.zh__digit-track {
|
|
43
|
+
width: 100%;
|
|
44
|
+
height: 100%;
|
|
45
|
+
display: flex;
|
|
46
|
+
flex-direction: column;
|
|
47
|
+
transform: translateY(0);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
.zh--mode-scroll .zh__digit-track {
|
|
51
|
+
transition: transform var(--zh-scroll-duration, 375ms)
|
|
52
|
+
var(--zh-scroll-timing, cubic-bezier(0.445, 0.05, 0.55, 0.95));
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
.zh__digit-face {
|
|
56
|
+
flex: 0 0 100%;
|
|
57
|
+
width: 100%;
|
|
58
|
+
height: 100%;
|
|
38
59
|
background-image: var(--zh-digits-url);
|
|
39
60
|
background-repeat: no-repeat;
|
|
40
61
|
/* Digits sprite is horizontal (frames left-to-right). */
|
|
@@ -51,113 +72,113 @@ const m = `:host {
|
|
|
51
72
|
white-space: nowrap;
|
|
52
73
|
clip-path: inset(50%);
|
|
53
74
|
}
|
|
54
|
-
`,
|
|
55
|
-
function
|
|
56
|
-
const t =
|
|
75
|
+
`, U = g;
|
|
76
|
+
function F(o) {
|
|
77
|
+
const t = o.charCodeAt(0) - 48;
|
|
57
78
|
return t >= 0 && t <= 9 ? t : 0;
|
|
58
79
|
}
|
|
59
|
-
function
|
|
60
|
-
return
|
|
80
|
+
function d(o) {
|
|
81
|
+
return o < 0 ? 0 : o;
|
|
61
82
|
}
|
|
62
|
-
const
|
|
83
|
+
const p = {
|
|
63
84
|
showDays: !0,
|
|
64
85
|
showHours: !0,
|
|
65
86
|
showMinutes: !0,
|
|
66
87
|
showSeconds: !0
|
|
67
|
-
},
|
|
68
|
-
function
|
|
69
|
-
return "adoptedStyleSheets" in
|
|
88
|
+
}, y = { hours: 0, minutes: 0, seconds: 0 };
|
|
89
|
+
function f(o) {
|
|
90
|
+
return "adoptedStyleSheets" in o;
|
|
70
91
|
}
|
|
71
|
-
function
|
|
92
|
+
function z(o) {
|
|
72
93
|
try {
|
|
73
94
|
const t = new CSSStyleSheet();
|
|
74
|
-
return t.replaceSync(
|
|
95
|
+
return t.replaceSync(o), t;
|
|
75
96
|
} catch {
|
|
76
97
|
return null;
|
|
77
98
|
}
|
|
78
99
|
}
|
|
79
|
-
const
|
|
80
|
-
function
|
|
81
|
-
const t = Math.floor(
|
|
82
|
-
return { d:
|
|
100
|
+
const N = z(g);
|
|
101
|
+
function E(o) {
|
|
102
|
+
const t = Math.floor(o / 1e3), e = t % 60, s = Math.floor(t / 60) % 60, i = Math.floor(t / 3600), n = Math.floor(i / 24), r = i % 24;
|
|
103
|
+
return { d: n, h: r, m: s, s: e, totalSec: t };
|
|
83
104
|
}
|
|
84
|
-
function
|
|
85
|
-
return String(
|
|
105
|
+
function w(o) {
|
|
106
|
+
return String(o).padStart(2, "0");
|
|
86
107
|
}
|
|
87
|
-
function
|
|
88
|
-
return [...String(
|
|
108
|
+
function S(o, t) {
|
|
109
|
+
return [...String(o).padStart(t, "0")];
|
|
89
110
|
}
|
|
90
|
-
function
|
|
91
|
-
if (!
|
|
92
|
-
const
|
|
93
|
-
return
|
|
111
|
+
function T(o, t, e) {
|
|
112
|
+
if (!o.hasAttribute(t)) return e;
|
|
113
|
+
const s = o.getAttribute(t);
|
|
114
|
+
return s == null || s === "" ? !0 : s !== "false";
|
|
94
115
|
}
|
|
95
|
-
function
|
|
96
|
-
if (
|
|
97
|
-
const t =
|
|
116
|
+
function D(o) {
|
|
117
|
+
if (o == null) return null;
|
|
118
|
+
const t = o.trim();
|
|
98
119
|
if (!t) return null;
|
|
99
|
-
const
|
|
100
|
-
if (!
|
|
101
|
-
const
|
|
102
|
-
return !Number.isFinite(
|
|
120
|
+
const e = t.match(/^(\d{4})-(\d{2})-(\d{2})$/);
|
|
121
|
+
if (!e) return null;
|
|
122
|
+
const s = Number(e[1]), i = Number(e[2]), n = Number(e[3]);
|
|
123
|
+
return !Number.isFinite(s) || !Number.isFinite(i) || !Number.isFinite(n) ? null : { year: s, month: i, day: n };
|
|
103
124
|
}
|
|
104
|
-
function
|
|
105
|
-
if (
|
|
106
|
-
const t =
|
|
107
|
-
if (!t) return { ...
|
|
108
|
-
const
|
|
125
|
+
function x(o) {
|
|
126
|
+
if (o == null) return { ...y };
|
|
127
|
+
const t = o.trim();
|
|
128
|
+
if (!t) return { ...y };
|
|
129
|
+
const e = t.split(":"), s = Number(e[0] ?? "0"), i = Number(e[1] ?? "0"), n = Number(e[2] ?? "0");
|
|
109
130
|
return {
|
|
110
|
-
hours: Number.isFinite(
|
|
111
|
-
minutes: Number.isFinite(
|
|
112
|
-
seconds: Number.isFinite(
|
|
131
|
+
hours: Number.isFinite(s) ? s : 0,
|
|
132
|
+
minutes: Number.isFinite(i) ? i : 0,
|
|
133
|
+
seconds: Number.isFinite(n) ? n : 0
|
|
113
134
|
};
|
|
114
135
|
}
|
|
115
|
-
function
|
|
116
|
-
if (
|
|
117
|
-
const t =
|
|
136
|
+
function C(o) {
|
|
137
|
+
if (o == null) return null;
|
|
138
|
+
const t = o.trim();
|
|
118
139
|
if (!t) return null;
|
|
119
|
-
const
|
|
120
|
-
if (!
|
|
121
|
-
const
|
|
122
|
-
return !Number.isFinite(
|
|
140
|
+
const e = t.match(/^(\d{1,2}):(\d{2})(?::(\d{2}))?$/);
|
|
141
|
+
if (!e) return null;
|
|
142
|
+
const s = Number(e[1]), i = Number(e[2]), n = Number(e[3] ?? "0");
|
|
143
|
+
return !Number.isFinite(s) || !Number.isFinite(i) || !Number.isFinite(n) || s < 0 || s > 23 || i < 0 || i > 59 || n < 0 || n > 59 ? null : { hours: s, minutes: i, seconds: n };
|
|
123
144
|
}
|
|
124
|
-
function
|
|
125
|
-
if (
|
|
126
|
-
let t =
|
|
145
|
+
function b(o) {
|
|
146
|
+
if (o == null) return null;
|
|
147
|
+
let t = o.trim();
|
|
127
148
|
if (!t) return null;
|
|
128
149
|
/^utc/i.test(t) && (t = t.slice(3));
|
|
129
|
-
let
|
|
130
|
-
if (t[0] === "+" ? t = t.slice(1) : t[0] === "-" && (
|
|
131
|
-
const [
|
|
132
|
-
if (!Number.isFinite(
|
|
133
|
-
const l =
|
|
134
|
-
return
|
|
150
|
+
let e = 1;
|
|
151
|
+
if (t[0] === "+" ? t = t.slice(1) : t[0] === "-" && (e = -1, t = t.slice(1)), !t) return null;
|
|
152
|
+
const [s, i = "0"] = t.split(":"), n = Number(s), r = Number(i);
|
|
153
|
+
if (!Number.isFinite(n) || !Number.isFinite(r)) return null;
|
|
154
|
+
const l = n * 60 + r;
|
|
155
|
+
return e * l;
|
|
135
156
|
}
|
|
136
|
-
function
|
|
157
|
+
function v() {
|
|
137
158
|
if (typeof window > "u" || !("location" in window)) return null;
|
|
138
|
-
const
|
|
139
|
-
if (!
|
|
140
|
-
const t = new URLSearchParams(
|
|
141
|
-
|
|
142
|
-
const
|
|
143
|
-
|
|
144
|
-
const
|
|
145
|
-
|
|
146
|
-
const
|
|
147
|
-
return
|
|
159
|
+
const o = window.location?.search ?? "";
|
|
160
|
+
if (!o) return null;
|
|
161
|
+
const t = new URLSearchParams(o), e = {}, s = t.get("date")?.trim();
|
|
162
|
+
s && (e.date = s);
|
|
163
|
+
const i = t.get("time")?.trim();
|
|
164
|
+
i && (e.time = i);
|
|
165
|
+
const n = t.get("utc")?.trim();
|
|
166
|
+
n && (e.utc = n);
|
|
167
|
+
const r = t.get("units")?.trim();
|
|
168
|
+
return r && (e.units = r), Object.keys(e).length ? e : null;
|
|
148
169
|
}
|
|
149
|
-
function
|
|
150
|
-
const t = (
|
|
170
|
+
function A(o) {
|
|
171
|
+
const t = (o ?? "").trim().toLowerCase();
|
|
151
172
|
if (!t)
|
|
152
|
-
return
|
|
153
|
-
const
|
|
154
|
-
if (!
|
|
155
|
-
return
|
|
156
|
-
const
|
|
157
|
-
return !
|
|
173
|
+
return p;
|
|
174
|
+
const e = t.split(":").map((a) => a.trim()).filter(Boolean);
|
|
175
|
+
if (!e.length)
|
|
176
|
+
return p;
|
|
177
|
+
const s = new Set(e), i = s.has("d"), n = s.has("h"), r = s.has("m"), l = s.has("s");
|
|
178
|
+
return !i && !n && !r && !l ? p : { showDays: i, showHours: n, showMinutes: r, showSeconds: l };
|
|
158
179
|
}
|
|
159
|
-
class
|
|
160
|
-
static defaultStylesheet =
|
|
180
|
+
class m extends HTMLElement {
|
|
181
|
+
static defaultStylesheet = N;
|
|
161
182
|
static observedAttributes = [
|
|
162
183
|
"digits-url",
|
|
163
184
|
"separator-url",
|
|
@@ -165,7 +186,8 @@ class c extends HTMLElement {
|
|
|
165
186
|
"date",
|
|
166
187
|
"time",
|
|
167
188
|
"utc",
|
|
168
|
-
"units"
|
|
189
|
+
"units",
|
|
190
|
+
"mode"
|
|
169
191
|
];
|
|
170
192
|
shadow = this.attachShadow({ mode: "open" });
|
|
171
193
|
digitsUrl = null;
|
|
@@ -190,16 +212,18 @@ class c extends HTMLElement {
|
|
|
190
212
|
showHours = !0;
|
|
191
213
|
showMinutes = !0;
|
|
192
214
|
showSeconds = !0;
|
|
215
|
+
mode = "static";
|
|
216
|
+
hasDigitsRendered = !1;
|
|
193
217
|
connectedCallback() {
|
|
194
218
|
this.render(), this.readAttributes(), this.autostart ? this.start() : this.renderStaticInitial();
|
|
195
219
|
}
|
|
196
220
|
disconnectedCallback() {
|
|
197
221
|
this.stop();
|
|
198
222
|
}
|
|
199
|
-
attributeChangedCallback(t,
|
|
223
|
+
attributeChangedCallback(t, e, s) {
|
|
200
224
|
if (!this.isConnected) return;
|
|
201
|
-
const
|
|
202
|
-
this.readAttributes(), this.doneFired = !1,
|
|
225
|
+
const i = this.isRunning();
|
|
226
|
+
this.readAttributes(), this.doneFired = !1, i && this.autostart ? this.start() : this.renderStaticInitial();
|
|
203
227
|
}
|
|
204
228
|
start() {
|
|
205
229
|
this.stop(), this.digitsUrl && (this.durationMs = this.targetEpochMs != null ? this.targetEpochMs - Date.now() : 0, this.startEpochMs = Date.now(), this.tick(), this.scheduleNextSecondBoundary());
|
|
@@ -218,36 +242,38 @@ class c extends HTMLElement {
|
|
|
218
242
|
}
|
|
219
243
|
readAttributes() {
|
|
220
244
|
this.rootEl || this.render();
|
|
221
|
-
const t =
|
|
222
|
-
this.digitsUrl = this.getAttribute("digits-url"), this.separatorUrl = this.getAttribute("separator-url"), this.autostart =
|
|
223
|
-
const
|
|
245
|
+
const t = v();
|
|
246
|
+
this.digitsUrl = this.getAttribute("digits-url"), this.separatorUrl = this.getAttribute("separator-url"), this.autostart = T(this, "autostart", !0);
|
|
247
|
+
const e = (this.getAttribute("mode") ?? "").trim().toLowerCase();
|
|
248
|
+
this.mode = e === "scroll" ? "scroll" : "static";
|
|
249
|
+
const s = A(t?.units ?? this.getAttribute("units"));
|
|
224
250
|
this.showDays = s.showDays, this.showHours = s.showHours, this.showMinutes = s.showMinutes, this.showSeconds = s.showSeconds;
|
|
225
|
-
const
|
|
226
|
-
if (!
|
|
251
|
+
const i = D(this.getAttribute("date")), r = D(t?.date ?? null) ?? i, l = x(this.getAttribute("time")), h = C(t?.time ?? null) ?? l, c = b(this.getAttribute("utc")), _ = b(t?.utc ?? null) ?? c ?? 0;
|
|
252
|
+
if (!r)
|
|
227
253
|
this.targetEpochMs = null;
|
|
228
254
|
else {
|
|
229
|
-
const
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
255
|
+
const M = Date.UTC(
|
|
256
|
+
r.year,
|
|
257
|
+
r.month - 1,
|
|
258
|
+
r.day,
|
|
233
259
|
h.hours,
|
|
234
260
|
h.minutes,
|
|
235
261
|
h.seconds
|
|
236
262
|
);
|
|
237
|
-
this.targetEpochMs =
|
|
263
|
+
this.targetEpochMs = M - _ * 60 * 1e3;
|
|
238
264
|
}
|
|
239
265
|
if (this.durationMs = 0, !this.digitsUrl) {
|
|
240
266
|
this.setTextFallback("—:—:—:—");
|
|
241
267
|
return;
|
|
242
268
|
}
|
|
243
|
-
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();
|
|
269
|
+
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(), this.rootEl.classList.toggle("zh--mode-scroll", this.mode === "scroll");
|
|
244
270
|
}
|
|
245
271
|
renderStaticInitial() {
|
|
246
272
|
if (this.targetEpochMs != null) {
|
|
247
|
-
const t = Date.now(),
|
|
248
|
-
this.setDigits({ d:
|
|
273
|
+
const t = Date.now(), e = d(this.targetEpochMs - t), { d: s, h: i, m: n, s: r } = E(e);
|
|
274
|
+
this.setDigits({ d: s, h: i, m: n, s: r }, !1);
|
|
249
275
|
} else
|
|
250
|
-
this.setDigits({ d: 0, h: 0, m: 0, s: 0 });
|
|
276
|
+
this.setDigits({ d: 0, h: 0, m: 0, s: 0 }, !1);
|
|
251
277
|
}
|
|
252
278
|
render() {
|
|
253
279
|
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(
|
|
@@ -259,7 +285,7 @@ class c extends HTMLElement {
|
|
|
259
285
|
this.sep2El,
|
|
260
286
|
this.secondsEl,
|
|
261
287
|
this.a11yEl
|
|
262
|
-
), 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 });
|
|
288
|
+
), 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 }, !1), this.hasDigitsRendered = !1;
|
|
263
289
|
}
|
|
264
290
|
setTextFallback(t) {
|
|
265
291
|
this.a11yEl.textContent = t;
|
|
@@ -270,57 +296,120 @@ class c extends HTMLElement {
|
|
|
270
296
|
this.sep0El.style.display = "none", this.sep1El.style.display = "none", this.sep2El.style.display = "none";
|
|
271
297
|
return;
|
|
272
298
|
}
|
|
273
|
-
const
|
|
274
|
-
for (let
|
|
275
|
-
|
|
276
|
-
this.rootEl.style.setProperty("--zh-groups", String(
|
|
277
|
-
const
|
|
278
|
-
if (
|
|
279
|
-
for (let
|
|
280
|
-
const
|
|
281
|
-
|
|
299
|
+
const e = [this.showDays, this.showHours, this.showMinutes, this.showSeconds], s = [];
|
|
300
|
+
for (let n = 0; n < e.length; n++)
|
|
301
|
+
e[n] && s.push(n);
|
|
302
|
+
this.rootEl.style.setProperty("--zh-groups", String(s.length));
|
|
303
|
+
const i = [!1, !1, !1];
|
|
304
|
+
if (s.length >= 2)
|
|
305
|
+
for (let n = 0; n < s.length - 1; n++) {
|
|
306
|
+
const r = s[n + 1], l = Math.min(2, Math.max(0, r - 1));
|
|
307
|
+
i[l] = !0;
|
|
282
308
|
}
|
|
283
|
-
this.sep0El.style.display =
|
|
284
|
-
}
|
|
285
|
-
setDigits({ d: t, h:
|
|
286
|
-
|
|
287
|
-
this.
|
|
288
|
-
const a = [];
|
|
289
|
-
this.
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
309
|
+
this.sep0El.style.display = i[0] ? "" : "none", this.sep1El.style.display = i[1] ? "" : "none", this.sep2El.style.display = i[2] ? "" : "none";
|
|
310
|
+
}
|
|
311
|
+
setDigits({ d: t, h: e, m: s, s: i }, n = !0) {
|
|
312
|
+
let r = n;
|
|
313
|
+
this.hasDigitsRendered || (r = !1);
|
|
314
|
+
const l = S(Math.min(t, 99), 2), a = S(e, 2), h = [...w(s)], c = [...w(i)];
|
|
315
|
+
this.syncDigitGroup(this.daysEl, l, r), this.syncDigitGroup(this.hoursEl, a, r), this.syncDigitGroup(this.minutesEl, h, r), this.syncDigitGroup(this.secondsEl, c, r);
|
|
316
|
+
const u = [];
|
|
317
|
+
this.showDays && u.push(l.join("")), this.showHours && u.push(a.join("")), this.showMinutes && u.push(h.join("")), this.showSeconds && u.push(c.join("")), this.a11yEl.textContent = u.length ? u.join(":") : "—", this.hasDigitsRendered = !0;
|
|
318
|
+
}
|
|
319
|
+
syncDigitGroup(t, e, s) {
|
|
320
|
+
for (; t.children.length < e.length; ) {
|
|
321
|
+
const i = t.children.length;
|
|
322
|
+
t.appendChild(this.createDigitSlot(e[i]));
|
|
323
|
+
}
|
|
324
|
+
for (; t.children.length > e.length; ) {
|
|
325
|
+
const i = t.lastElementChild;
|
|
326
|
+
if (!i) break;
|
|
327
|
+
t.removeChild(i);
|
|
295
328
|
}
|
|
296
|
-
for (;
|
|
297
|
-
const
|
|
298
|
-
|
|
299
|
-
t.removeChild(e);
|
|
329
|
+
for (let i = 0; i < e.length; i++) {
|
|
330
|
+
const n = t.children[i];
|
|
331
|
+
this.syncDigitSlot(n, e[i], s);
|
|
300
332
|
}
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
333
|
+
}
|
|
334
|
+
createDigitSlot(t) {
|
|
335
|
+
const e = document.createElement("span");
|
|
336
|
+
e.className = "zh__digit";
|
|
337
|
+
const s = document.createElement("span");
|
|
338
|
+
s.className = "zh__digit-track";
|
|
339
|
+
const i = this.createDigitFace(t);
|
|
340
|
+
return i.classList.add("zh__digit-face--current"), s.append(i), e.append(s), e;
|
|
341
|
+
}
|
|
342
|
+
createDigitFace(t) {
|
|
343
|
+
const e = document.createElement("span");
|
|
344
|
+
return e.className = "zh__digit-face", this.setFaceDigit(e, t), e;
|
|
345
|
+
}
|
|
346
|
+
setFaceDigit(t, e) {
|
|
347
|
+
t.dataset.zhDigit = e;
|
|
348
|
+
const s = F(e);
|
|
349
|
+
t.style.setProperty("--zh-sheet-index", String(s));
|
|
350
|
+
}
|
|
351
|
+
ensureDigitTrack(t) {
|
|
352
|
+
let e = t.querySelector(".zh__digit-track");
|
|
353
|
+
return e || (e = document.createElement("span"), e.className = "zh__digit-track", t.innerHTML = "", t.append(e)), e;
|
|
354
|
+
}
|
|
355
|
+
getOrCreateCurrentFace(t, e) {
|
|
356
|
+
let s = t.querySelector(".zh__digit-face--current");
|
|
357
|
+
if (!s)
|
|
358
|
+
s = t.querySelector(".zh__digit-face") ?? this.createDigitFace(e), s.classList.add("zh__digit-face--current"), this.setFaceDigit(s, s.dataset.zhDigit ?? e), t.innerHTML = "", t.append(s);
|
|
359
|
+
else {
|
|
360
|
+
const i = Array.from(t.children);
|
|
361
|
+
for (const n of i)
|
|
362
|
+
n !== s && t.removeChild(n);
|
|
363
|
+
}
|
|
364
|
+
return s;
|
|
365
|
+
}
|
|
366
|
+
cleanupTrack(t, e) {
|
|
367
|
+
if (!t.contains(e)) return;
|
|
368
|
+
const s = Array.from(t.children);
|
|
369
|
+
for (const i of s)
|
|
370
|
+
i !== e && t.removeChild(i);
|
|
371
|
+
e.classList.remove("zh__digit-face--next"), e.classList.add("zh__digit-face--current"), t.style.transition = this.mode === "scroll" ? "" : "none", t.style.transform = "translateY(0)";
|
|
372
|
+
}
|
|
373
|
+
parseTransitionMs(t) {
|
|
374
|
+
const e = window.getComputedStyle(t), s = e.transitionDuration.split(",").map((a) => a.trim()), i = e.transitionDelay.split(",").map((a) => a.trim()), n = (a) => a ? a.endsWith("ms") ? Number.parseFloat(a) : a.endsWith("s") ? Number.parseFloat(a) * 1e3 : Number.parseFloat(a) || 0 : 0, r = s[0] ? n(s[0]) : 0, l = i[0] ? n(i[0]) : 0;
|
|
375
|
+
return r + l;
|
|
376
|
+
}
|
|
377
|
+
animateDigitChange(t, e, s) {
|
|
378
|
+
const i = this.createDigitFace(s);
|
|
379
|
+
i.classList.add("zh__digit-face--next"), t.innerHTML = "", t.append(i, e), t.style.transition = "none", t.style.transform = "translateY(-100%)", t.offsetHeight, t.style.transition = "", t.style.transform = "translateY(0)";
|
|
380
|
+
const n = () => {
|
|
381
|
+
t.removeEventListener("transitionend", n), t.contains(i) && this.cleanupTrack(t, i);
|
|
382
|
+
};
|
|
383
|
+
t.addEventListener("transitionend", n, { once: !0 });
|
|
384
|
+
const r = this.parseTransitionMs(t) + 150;
|
|
385
|
+
window.setTimeout(n, r || 800);
|
|
386
|
+
}
|
|
387
|
+
syncDigitSlot(t, e, s) {
|
|
388
|
+
const i = this.ensureDigitTrack(t), n = this.getOrCreateCurrentFace(i, e), r = n.dataset.zhDigit ?? e;
|
|
389
|
+
if (this.setFaceDigit(n, r), r === e || !s || this.mode !== "scroll") {
|
|
390
|
+
r !== e && this.setFaceDigit(n, e), this.cleanupTrack(i, n);
|
|
391
|
+
return;
|
|
304
392
|
}
|
|
393
|
+
this.animateDigitChange(i, n, e);
|
|
305
394
|
}
|
|
306
395
|
tick() {
|
|
307
396
|
if (!this.digitsUrl) return;
|
|
308
|
-
const t =
|
|
397
|
+
const t = d(this.durationMs);
|
|
309
398
|
if (t === 0) {
|
|
310
399
|
this.setDigits({ d: 0, h: 0, m: 0, s: 0 }), this.fireDoneOnce(), this.stop();
|
|
311
400
|
return;
|
|
312
401
|
}
|
|
313
|
-
const
|
|
314
|
-
this.setDigits({ d:
|
|
402
|
+
const e = this.startEpochMs ?? Date.now(), s = d(Date.now() - e), i = d(t - s), { d: n, h: r, m: l, s: a, totalSec: h } = E(i);
|
|
403
|
+
this.setDigits({ d: n, h: r, m: l, s: a }), h === 0 && (this.fireDoneOnce(), this.stop());
|
|
315
404
|
}
|
|
316
405
|
fireDoneOnce() {
|
|
317
406
|
this.doneFired || (this.doneFired = !0, this.dispatchEvent(new CustomEvent("done")));
|
|
318
407
|
}
|
|
319
408
|
scheduleNextSecondBoundary() {
|
|
320
|
-
const
|
|
409
|
+
const e = 1e3 - Date.now() % 1e3;
|
|
321
410
|
this.nextTickTimeout = window.setTimeout(() => {
|
|
322
411
|
this.tick(), this.isRunning() && this.scheduleNextSecondBoundary();
|
|
323
|
-
},
|
|
412
|
+
}, e);
|
|
324
413
|
}
|
|
325
414
|
adoptStylesheet(t) {
|
|
326
415
|
this.applyStyles(t);
|
|
@@ -330,46 +419,46 @@ class c extends HTMLElement {
|
|
|
330
419
|
}
|
|
331
420
|
applyStyles(t) {
|
|
332
421
|
if (typeof t == "string") {
|
|
333
|
-
if (
|
|
334
|
-
const
|
|
335
|
-
if (
|
|
336
|
-
this.shadow.adoptedStyleSheets = [
|
|
422
|
+
if (f(this.shadow)) {
|
|
423
|
+
const e = z(t);
|
|
424
|
+
if (e) {
|
|
425
|
+
this.shadow.adoptedStyleSheets = [e], this.styleEl = null;
|
|
337
426
|
return;
|
|
338
427
|
}
|
|
339
428
|
}
|
|
340
429
|
this.styleEl || (this.styleEl = document.createElement("style")), this.styleEl.textContent = t;
|
|
341
430
|
return;
|
|
342
431
|
}
|
|
343
|
-
if (t &&
|
|
432
|
+
if (t && f(this.shadow)) {
|
|
344
433
|
this.shadow.adoptedStyleSheets = [t], this.styleEl = null;
|
|
345
434
|
return;
|
|
346
435
|
}
|
|
347
|
-
if (
|
|
348
|
-
this.shadow.adoptedStyleSheets = [
|
|
436
|
+
if (m.defaultStylesheet && f(this.shadow)) {
|
|
437
|
+
this.shadow.adoptedStyleSheets = [m.defaultStylesheet], this.styleEl = null;
|
|
349
438
|
return;
|
|
350
439
|
}
|
|
351
|
-
this.styleEl || (this.styleEl = document.createElement("style")), this.styleEl.textContent =
|
|
440
|
+
this.styleEl || (this.styleEl = document.createElement("style")), this.styleEl.textContent = g;
|
|
352
441
|
}
|
|
353
442
|
}
|
|
354
|
-
customElements.get("countdown-timer") || customElements.define("countdown-timer",
|
|
355
|
-
function
|
|
356
|
-
const { selector: t = "countdown-timer", onDone:
|
|
357
|
-
if (
|
|
358
|
-
const
|
|
359
|
-
|
|
443
|
+
customElements.get("countdown-timer") || customElements.define("countdown-timer", m);
|
|
444
|
+
function L(o = {}) {
|
|
445
|
+
const { selector: t = "countdown-timer", onDone: e, stylesheet: s } = o, i = Array.from(document.querySelectorAll(t));
|
|
446
|
+
if (e) {
|
|
447
|
+
const n = /* @__PURE__ */ new WeakSet(), r = (l) => {
|
|
448
|
+
n.has(l) || (n.add(l), e(l));
|
|
360
449
|
};
|
|
361
|
-
|
|
362
|
-
l.addEventListener("done", () =>
|
|
363
|
-
const
|
|
364
|
-
typeof
|
|
450
|
+
i.forEach((l) => {
|
|
451
|
+
l.addEventListener("done", () => r(l));
|
|
452
|
+
const a = l;
|
|
453
|
+
typeof a.isDone == "function" && a.isDone() && r(l);
|
|
365
454
|
});
|
|
366
455
|
}
|
|
367
|
-
return
|
|
368
|
-
const
|
|
369
|
-
typeof
|
|
370
|
-
}),
|
|
456
|
+
return s && i.forEach((n) => {
|
|
457
|
+
const r = n;
|
|
458
|
+
typeof s == "string" ? r.adoptStyles(s) : r.adoptStylesheet(s);
|
|
459
|
+
}), i;
|
|
371
460
|
}
|
|
372
461
|
export {
|
|
373
|
-
|
|
374
|
-
|
|
462
|
+
L as initCountdownTimers,
|
|
463
|
+
U as zeroHourCssText
|
|
375
464
|
};
|
package/dist/index.umd.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
(function(
|
|
1
|
+
(function(u,c){typeof exports=="object"&&typeof module<"u"?c(exports):typeof define=="function"&&define.amd?define(["exports"],c):(u=typeof globalThis<"u"?globalThis:u||self,c(u.ZeroHour={}))})(this,(function(u){"use strict";const c=`:host {
|
|
2
2
|
display: block;
|
|
3
3
|
width: 100%;
|
|
4
4
|
container-type: inline-size;
|
|
@@ -35,6 +35,27 @@
|
|
|
35
35
|
.zh__digit {
|
|
36
36
|
aspect-ratio: var(--zh-digit-aspect, 9 / 12);
|
|
37
37
|
width: 100%;
|
|
38
|
+
position: relative;
|
|
39
|
+
overflow: hidden;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
.zh__digit-track {
|
|
43
|
+
width: 100%;
|
|
44
|
+
height: 100%;
|
|
45
|
+
display: flex;
|
|
46
|
+
flex-direction: column;
|
|
47
|
+
transform: translateY(0);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
.zh--mode-scroll .zh__digit-track {
|
|
51
|
+
transition: transform var(--zh-scroll-duration, 375ms)
|
|
52
|
+
var(--zh-scroll-timing, cubic-bezier(0.445, 0.05, 0.55, 0.95));
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
.zh__digit-face {
|
|
56
|
+
flex: 0 0 100%;
|
|
57
|
+
width: 100%;
|
|
58
|
+
height: 100%;
|
|
38
59
|
background-image: var(--zh-digits-url);
|
|
39
60
|
background-repeat: no-repeat;
|
|
40
61
|
/* Digits sprite is horizontal (frames left-to-right). */
|
|
@@ -51,4 +72,4 @@
|
|
|
51
72
|
white-space: nowrap;
|
|
52
73
|
clip-path: inset(50%);
|
|
53
74
|
}
|
|
54
|
-
`,D=c;function T(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}isDone(){return!this.digitsUrl||this.targetEpochMs==null?!1:Date.now()>=this.targetEpochMs}readAttributes(){this.rootEl||this.render();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=T(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));if(e){const i=new WeakSet,r=l=>{i.has(l)||(i.add(l),e(l))};n.forEach(l=>{l.addEventListener("done",()=>r(l));const h=l;typeof h.isDone=="function"&&h.isDone()&&r(l)})}return s&&n.forEach(i=>{const r=i;typeof s=="string"?r.adoptStyles(s):r.adoptStylesheet(s)}),n}a.initCountdownTimers=C,a.zeroHourCssText=D,Object.defineProperty(a,Symbol.toStringTag,{value:"Module"})}));
|
|
75
|
+
`,M=c;function T(o){const t=o.charCodeAt(0)-48;return t>=0&&t<=9?t:0}function f(o){return o<0?0:o}const g={showDays:!0,showHours:!0,showMinutes:!0,showSeconds:!0},E={hours:0,minutes:0,seconds:0};function y(o){return"adoptedStyleSheets"in o}function w(o){try{const t=new CSSStyleSheet;return t.replaceSync(o),t}catch{return null}}const F=w(c);function S(o){const t=Math.floor(o/1e3),e=t%60,s=Math.floor(t/60)%60,i=Math.floor(t/3600),n=Math.floor(i/24),r=i%24;return{d:n,h:r,m:s,s:e,totalSec:t}}function b(o){return String(o).padStart(2,"0")}function D(o,t){return[...String(o).padStart(t,"0")]}function N(o,t,e){if(!o.hasAttribute(t))return e;const s=o.getAttribute(t);return s==null||s===""?!0:s!=="false"}function z(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]),i=Number(e[2]),n=Number(e[3]);return!Number.isFinite(s)||!Number.isFinite(i)||!Number.isFinite(n)?null:{year:s,month:i,day:n}}function x(o){if(o==null)return{...E};const t=o.trim();if(!t)return{...E};const e=t.split(":"),s=Number(e[0]??"0"),i=Number(e[1]??"0"),n=Number(e[2]??"0");return{hours:Number.isFinite(s)?s:0,minutes:Number.isFinite(i)?i:0,seconds:Number.isFinite(n)?n:0}}function C(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]),i=Number(e[2]),n=Number(e[3]??"0");return!Number.isFinite(s)||!Number.isFinite(i)||!Number.isFinite(n)||s<0||s>23||i<0||i>59||n<0||n>59?null:{hours:s,minutes:i,seconds:n}}function _(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,i="0"]=t.split(":"),n=Number(s),r=Number(i);if(!Number.isFinite(n)||!Number.isFinite(r))return null;const l=n*60+r;return e*l}function v(){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 i=t.get("time")?.trim();i&&(e.time=i);const n=t.get("utc")?.trim();n&&(e.utc=n);const r=t.get("units")?.trim();return r&&(e.units=r),Object.keys(e).length?e:null}function A(o){const t=(o??"").trim().toLowerCase();if(!t)return g;const e=t.split(":").map(a=>a.trim()).filter(Boolean);if(!e.length)return g;const s=new Set(e),i=s.has("d"),n=s.has("h"),r=s.has("m"),l=s.has("s");return!i&&!n&&!r&&!l?g:{showDays:i,showHours:n,showMinutes:r,showSeconds:l}}class p extends HTMLElement{static defaultStylesheet=F;static observedAttributes=["digits-url","separator-url","autostart","date","time","utc","units","mode"];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;mode="static";hasDigitsRendered=!1;connectedCallback(){this.render(),this.readAttributes(),this.autostart?this.start():this.renderStaticInitial()}disconnectedCallback(){this.stop()}attributeChangedCallback(t,e,s){if(!this.isConnected)return;const i=this.isRunning();this.readAttributes(),this.doneFired=!1,i&&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}isDone(){return!this.digitsUrl||this.targetEpochMs==null?!1:Date.now()>=this.targetEpochMs}readAttributes(){this.rootEl||this.render();const t=v();this.digitsUrl=this.getAttribute("digits-url"),this.separatorUrl=this.getAttribute("separator-url"),this.autostart=N(this,"autostart",!0);const e=(this.getAttribute("mode")??"").trim().toLowerCase();this.mode=e==="scroll"?"scroll":"static";const s=A(t?.units??this.getAttribute("units"));this.showDays=s.showDays,this.showHours=s.showHours,this.showMinutes=s.showMinutes,this.showSeconds=s.showSeconds;const i=z(this.getAttribute("date")),r=z(t?.date??null)??i,l=x(this.getAttribute("time")),h=C(t?.time??null)??l,m=_(this.getAttribute("utc")),L=_(t?.utc??null)??m??0;if(!r)this.targetEpochMs=null;else{const H=Date.UTC(r.year,r.month-1,r.day,h.hours,h.minutes,h.seconds);this.targetEpochMs=H-L*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(),this.rootEl.classList.toggle("zh--mode-scroll",this.mode==="scroll")}renderStaticInitial(){if(this.targetEpochMs!=null){const t=Date.now(),e=f(this.targetEpochMs-t),{d:s,h:i,m:n,s:r}=S(e);this.setDigits({d:s,h:i,m:n,s:r},!1)}else this.setDigits({d:0,h:0,m:0,s:0},!1)}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},!1),this.hasDigitsRendered=!1}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 n=0;n<e.length;n++)e[n]&&s.push(n);this.rootEl.style.setProperty("--zh-groups",String(s.length));const i=[!1,!1,!1];if(s.length>=2)for(let n=0;n<s.length-1;n++){const r=s[n+1],l=Math.min(2,Math.max(0,r-1));i[l]=!0}this.sep0El.style.display=i[0]?"":"none",this.sep1El.style.display=i[1]?"":"none",this.sep2El.style.display=i[2]?"":"none"}setDigits({d:t,h:e,m:s,s:i},n=!0){let r=n;this.hasDigitsRendered||(r=!1);const l=D(Math.min(t,99),2),a=D(e,2),h=[...b(s)],m=[...b(i)];this.syncDigitGroup(this.daysEl,l,r),this.syncDigitGroup(this.hoursEl,a,r),this.syncDigitGroup(this.minutesEl,h,r),this.syncDigitGroup(this.secondsEl,m,r);const d=[];this.showDays&&d.push(l.join("")),this.showHours&&d.push(a.join("")),this.showMinutes&&d.push(h.join("")),this.showSeconds&&d.push(m.join("")),this.a11yEl.textContent=d.length?d.join(":"):"—",this.hasDigitsRendered=!0}syncDigitGroup(t,e,s){for(;t.children.length<e.length;){const i=t.children.length;t.appendChild(this.createDigitSlot(e[i]))}for(;t.children.length>e.length;){const i=t.lastElementChild;if(!i)break;t.removeChild(i)}for(let i=0;i<e.length;i++){const n=t.children[i];this.syncDigitSlot(n,e[i],s)}}createDigitSlot(t){const e=document.createElement("span");e.className="zh__digit";const s=document.createElement("span");s.className="zh__digit-track";const i=this.createDigitFace(t);return i.classList.add("zh__digit-face--current"),s.append(i),e.append(s),e}createDigitFace(t){const e=document.createElement("span");return e.className="zh__digit-face",this.setFaceDigit(e,t),e}setFaceDigit(t,e){t.dataset.zhDigit=e;const s=T(e);t.style.setProperty("--zh-sheet-index",String(s))}ensureDigitTrack(t){let e=t.querySelector(".zh__digit-track");return e||(e=document.createElement("span"),e.className="zh__digit-track",t.innerHTML="",t.append(e)),e}getOrCreateCurrentFace(t,e){let s=t.querySelector(".zh__digit-face--current");if(!s)s=t.querySelector(".zh__digit-face")??this.createDigitFace(e),s.classList.add("zh__digit-face--current"),this.setFaceDigit(s,s.dataset.zhDigit??e),t.innerHTML="",t.append(s);else{const i=Array.from(t.children);for(const n of i)n!==s&&t.removeChild(n)}return s}cleanupTrack(t,e){if(!t.contains(e))return;const s=Array.from(t.children);for(const i of s)i!==e&&t.removeChild(i);e.classList.remove("zh__digit-face--next"),e.classList.add("zh__digit-face--current"),t.style.transition=this.mode==="scroll"?"":"none",t.style.transform="translateY(0)"}parseTransitionMs(t){const e=window.getComputedStyle(t),s=e.transitionDuration.split(",").map(a=>a.trim()),i=e.transitionDelay.split(",").map(a=>a.trim()),n=a=>a?a.endsWith("ms")?Number.parseFloat(a):a.endsWith("s")?Number.parseFloat(a)*1e3:Number.parseFloat(a)||0:0,r=s[0]?n(s[0]):0,l=i[0]?n(i[0]):0;return r+l}animateDigitChange(t,e,s){const i=this.createDigitFace(s);i.classList.add("zh__digit-face--next"),t.innerHTML="",t.append(i,e),t.style.transition="none",t.style.transform="translateY(-100%)",t.offsetHeight,t.style.transition="",t.style.transform="translateY(0)";const n=()=>{t.removeEventListener("transitionend",n),t.contains(i)&&this.cleanupTrack(t,i)};t.addEventListener("transitionend",n,{once:!0});const r=this.parseTransitionMs(t)+150;window.setTimeout(n,r||800)}syncDigitSlot(t,e,s){const i=this.ensureDigitTrack(t),n=this.getOrCreateCurrentFace(i,e),r=n.dataset.zhDigit??e;if(this.setFaceDigit(n,r),r===e||!s||this.mode!=="scroll"){r!==e&&this.setFaceDigit(n,e),this.cleanupTrack(i,n);return}this.animateDigitChange(i,n,e)}tick(){if(!this.digitsUrl)return;const t=f(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=f(Date.now()-e),i=f(t-s),{d:n,h:r,m:l,s:a,totalSec:h}=S(i);this.setDigits({d:n,h:r,m:l,s:a}),h===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(y(this.shadow)){const e=w(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&&y(this.shadow)){this.shadow.adoptedStyleSheets=[t],this.styleEl=null;return}if(p.defaultStylesheet&&y(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 U(o={}){const{selector:t="countdown-timer",onDone:e,stylesheet:s}=o,i=Array.from(document.querySelectorAll(t));if(e){const n=new WeakSet,r=l=>{n.has(l)||(n.add(l),e(l))};i.forEach(l=>{l.addEventListener("done",()=>r(l));const a=l;typeof a.isDone=="function"&&a.isDone()&&r(l)})}return s&&i.forEach(n=>{const r=n;typeof s=="string"?r.adoptStyles(s):r.adoptStylesheet(s)}),i}u.initCountdownTimers=U,u.zeroHourCssText=M,Object.defineProperty(u,Symbol.toStringTag,{value:"Module"})}));
|
package/dist/zero-hour.css
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
:host{width:100%;display:block;overflow:hidden;container-type:inline-size}.zh{box-sizing:border-box;align-items:stretch;gap:0;display:flex;position:relative}.zh__group{flex:1 1 0;grid-template-rows:auto;grid-template-columns:1fr 1fr;gap:0;width:100%;min-width:0;display:grid}.zh__sep{flex:0 0 var(--zh-sep-width,4%);width:var(--zh-sep-width,4%);background-image:var(--zh-sep-url);background-position:50%;background-repeat:no-repeat;background-size:contain}.zh__digit{aspect-ratio:var(--zh-digit-aspect,9/12);background-image:var(--zh-digits-url);background-repeat:no-repeat;background-size:calc(var(--zh-digits-frames,10)*100%)100%;background-position:calc(var(--zh-sheet-index,0)*100%/(var(--zh-digits-frames,10) - 1))0;width:100%}.zh__a11y{clip:rect(0 0 0 0);white-space:nowrap;clip-path:inset(50%);width:1px;height:1px;position:absolute;overflow:hidden}
|
|
1
|
+
:host{width:100%;display:block;overflow:hidden;container-type:inline-size}.zh{box-sizing:border-box;align-items:stretch;gap:0;display:flex;position:relative}.zh__group{flex:1 1 0;grid-template-rows:auto;grid-template-columns:1fr 1fr;gap:0;width:100%;min-width:0;display:grid}.zh__sep{flex:0 0 var(--zh-sep-width,4%);width:var(--zh-sep-width,4%);background-image:var(--zh-sep-url);background-position:50%;background-repeat:no-repeat;background-size:contain}.zh__digit{aspect-ratio:var(--zh-digit-aspect,9/12);width:100%;position:relative;overflow:hidden}.zh__digit-track{flex-direction:column;width:100%;height:100%;display:flex;transform:translateY(0)}.zh--mode-scroll .zh__digit-track{transition:transform var(--zh-scroll-duration,.375s)var(--zh-scroll-timing,cubic-bezier(.445,.05,.55,.95))}.zh__digit-face{background-image:var(--zh-digits-url);background-repeat:no-repeat;background-size:calc(var(--zh-digits-frames,10)*100%)100%;background-position:calc(var(--zh-sheet-index,0)*100%/(var(--zh-digits-frames,10) - 1))0;flex:0 0 100%;width:100%;height:100%}.zh__a11y{clip:rect(0 0 0 0);white-space:nowrap;clip-path:inset(50%);width:1px;height:1px;position:absolute;overflow:hidden}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "zero-hour",
|
|
3
|
-
"version": "1.3.
|
|
3
|
+
"version": "1.3.2",
|
|
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",
|
|
@@ -16,6 +16,7 @@
|
|
|
16
16
|
"scripts": {
|
|
17
17
|
"clean": "rimraf dist",
|
|
18
18
|
"build": "vite build",
|
|
19
|
+
"dev": "vite",
|
|
19
20
|
"verify": "yarn lint && yarn typecheck",
|
|
20
21
|
"lint": "biome check src",
|
|
21
22
|
"lint:fix": "biome check --write src",
|