UNPKG

uikit

Version:

UIkit is a lightweight and modular front-end framework for developing fast and powerful web interfaces.

2 lines (1 loc) 1.82 kB
/*! UIkit 3.23.10 | https://www.getuikit.com | (c) 2014 - 2025 YOOtheme | MIT License */(function(t,i){typeof exports=="object"&&typeof module<"u"?module.exports=i(require("uikit-util")):typeof define=="function"&&define.amd?define("uikitcountdown",["uikit-util"],i):(t=typeof globalThis<"u"?globalThis:t||self,t.UIkitCountdown=i(t.UIkit.util))})(this,function(t){"use strict";var i={connected(){t.addClass(this.$el,this.$options.id)}};const r=["days","hours","minutes","seconds"];var d={mixins:[i],props:{date:String,clsWrapper:String,role:String,reload:Boolean},data:{date:"",clsWrapper:".uk-countdown-%unit%",role:"timer",reload:!1},connected(){this.$el.role=this.role,this.date=t.toFloat(Date.parse(this.$props.date)),this.started=this.end=!1,this.start()},disconnected(){this.stop()},events:{name:"visibilitychange",el:()=>document,handler(){document.hidden?this.stop():this.start()}},methods:{start(){this.stop(),this.update()},stop(){this.timer&&(clearInterval(this.timer),t.trigger(this.$el,"countdownstop"),this.timer=null)},update(){const o=a(this.date);o.total?this.timer||(this.started=!0,this.timer=setInterval(this.update,1e3),t.trigger(this.$el,"countdownstart")):(this.stop(),this.end||(t.trigger(this.$el,"countdownend"),this.end=!0,this.reload&&this.started&&window.location.reload()));for(const e of r){const s=t.$(this.clsWrapper.replace("%unit%",e),this.$el);if(!s)continue;let n=Math.trunc(o[e]).toString().padStart(2,"0");s.textContent!==n&&(n=n.split(""),n.length!==s.children.length&&t.html(s,n.map(()=>"<span></span>").join("")),n.forEach((h,l)=>s.children[l].textContent=h))}}}};function a(o){const e=Math.max(0,o-Date.now())/1e3;return{total:e,seconds:e%60,minutes:e/60%60,hours:e/60/60%24,days:e/60/60/24}}return typeof window<"u"&&window.UIkit&&window.UIkit.component("countdown",d),d});