Skip to content

lightweight, client-only calendar web application. All data persists in the URL hash for instant sharing, No backend required. Optional AES-GCM password protection keeps shared links locked without a server

JavaScript

timer

Supun Lakmal0d7b01c0f6a6
diff --git a/index.html b/index.html
index 205ed0e..b906c54 100644
--- a/index.html
+++ b/index.html
@@ -96,6 +96,15 @@
</div>
</header>
+ <div id="countdown-widget" class="countdown-container hidden" aria-live="polite">
+ <div class="countdown-label">Up Next</div>
+ <div class="countdown-content">
+ <div id="nextEventTitle" class="countdown-title">--</div>
+ <div id="countdownTimer" class="countdown-timer">00:00:00</div>
+ <div id="nextEventTime" class="countdown-meta">--</div>
+ </div>
+ </div>
+
<main class="layout">
<section class="calendar-panel">
<div class="panel-header">
diff --git a/modules/countdownManager.js b/modules/countdownManager.js
new file mode 100644
index 0000000..3919c31
--- /dev/null
+++ b/modules/countdownManager.js
@@ -0,0 +1,155 @@
+import { expandEvents } from "./recurrenceEngine.js";
+
+const LOOKAHEAD_DAYS = 30;
+const URGENCY_MS = 5 * 60 * 1000;
+
+let countdownInterval = null;
+let latestEvents = [];
+let cachedElements = null;
+let lastEventKey = null;
+
+function addDays(date, days) {
+ const next = new Date(date.getTime());
+ next.setDate(next.getDate() + days);
+ return next;
+}
+
+function isSameDay(a, b) {
+ return a.getFullYear() === b.getFullYear() && a.getMonth() === b.getMonth() && a.getDate() === b.getDate();
+}
+
+function formatTime(date) {
+ return date.toLocaleTimeString(undefined, { hour: "2-digit", minute: "2-digit" });
+}
+
+function formatDateLabel(date) {
+ return date.toLocaleDateString(undefined, { weekday: "short", month: "short", day: "numeric" });
+}
+
+function formatDuration(ms) {
+ const totalSeconds = Math.max(0, Math.floor(ms / 1000));
+ const days = Math.floor(totalSeconds / 86400);
+ const hours = Math.floor((totalSeconds % 86400) / 3600);
+ const minutes = Math.floor((totalSeconds % 3600) / 60);
+ const seconds = totalSeconds % 60;
+
+ const pad = (value) => String(value).padStart(2, "0");
+
+ if (days > 0) {
+ return `${days}d ${pad(hours)}:${pad(minutes)}:${pad(seconds)}`;
+ }
+
+ return `${pad(hours)}:${pad(minutes)}:${pad(seconds)}`;
+}
+
+function formatMeta(event, now) {
+ if (!event) return "";
+ const startDate = event.startDate || new Date(event.start);
+ if (event.isAllDay) {
+ if (isSameDay(startDate, now)) return "All day";
+ return `All day ${formatDateLabel(startDate)}`;
+ }
+
+ const timeLabel = formatTime(startDate);
+ if (isSameDay(startDate, now)) {
+ return `Starts at ${timeLabel}`;
+ }
+ return `Starts ${formatDateLabel(startDate)} at ${timeLabel}`;
+}
+
+function getElements() {
+ if (cachedElements && cachedElements.widget && document.body.contains(cachedElements.widget)) {
+ return cachedElements;
+ }
+ const widget = document.getElementById("countdown-widget");
+ if (!widget) return null;
+
+ cachedElements = {
+ widget,
+ titleEl: document.getElementById("nextEventTitle"),
+ timerEl: document.getElementById("countdownTimer"),
+ metaEl: document.getElementById("nextEventTime"),
+ };
+
+ return cachedElements;
+}
+
+function findNextEvent(events) {
+ const now = new Date();
+ const lookAheadEnd = addDays(now, LOOKAHEAD_DAYS);
+ const expanded = expandEvents(events, now, lookAheadEnd);
+
+ const upcoming = expanded
+ .filter((event) => event.start > now.getTime())
+ .map((event) => ({
+ ...event,
+ title: String(event.title || "Untitled"),
+ startDate: new Date(event.start),
+ endDate: new Date(event.end),
+ }))
+ .sort((a, b) => {
+ const diff = a.start - b.start;
+ if (diff !== 0) return diff;
+ return a.title.localeCompare(b.title);
+ });
+
+ return upcoming.length ? upcoming[0] : null;
+}
+
+function updateWidgetUI(event) {
+ const elements = getElements();
+ if (!elements) return;
+
+ const { widget, titleEl, timerEl, metaEl } = elements;
+ if (countdownInterval) {
+ window.clearInterval(countdownInterval);
+ countdownInterval = null;
+ }
+
+ widget.classList.remove("urgent");
+ widget.classList.remove("inactive");
+ widget.classList.remove("hidden");
+
+ if (!event) {
+ widget.classList.add("hidden");
+ if (titleEl) titleEl.textContent = "--";
+ if (timerEl) timerEl.textContent = "00:00:00";
+ if (metaEl) metaEl.textContent = "--";
+ return;
+ }
+
+ if (titleEl) titleEl.textContent = event.title || "Untitled";
+ if (metaEl) metaEl.textContent = formatMeta(event, new Date());
+
+ const tick = () => {
+ const now = new Date();
+ const diff = event.start - now.getTime();
+
+ if (diff <= 0) {
+ initCountdownWidget(latestEvents);
+ return;
+ }
+
+ if (timerEl) timerEl.textContent = formatDuration(diff);
+ if (metaEl) metaEl.textContent = formatMeta(event, now);
+
+ if (diff < URGENCY_MS) {
+ widget.classList.add("urgent");
+ } else {
+ widget.classList.remove("urgent");
+ }
+ };
+
+ tick();
+ countdownInterval = window.setInterval(tick, 1000);
+}
+
+export function initCountdownWidget(events = []) {
+ latestEvents = Array.isArray(events) ? events : [];
+ const nextEvent = findNextEvent(latestEvents);
+ const eventKey = nextEvent ? `${nextEvent.start}|${nextEvent.title}|${nextEvent.isAllDay ? 1 : 0}` : "none";
+
+ if (eventKey === lastEventKey) return;
+ lastEventKey = eventKey;
+ updateWidgetUI(nextEvent);
+}
diff --git a/script.js b/script.js
index 0b14694..1c23014 100644
--- a/script.js
+++ b/script.js
@@ -3,6 +3,7 @@ import { expandEvents } from "./modules/recurrenceEngine.js";
import { renderAgendaView } from "./modules/agendaRender.js";
import { FocusMode } from "./modules/focusMode.js";
import { initQRCodeManager } from "./modules/qrCodeManager.js";
+import { initCountdownWidget } from "./modules/countdownManager.js";
import {
formatDateKey,
getMonthGridRange,
@@ -160,8 +161,9 @@ function normalizeState(raw) {
return next;
}
-function cacheElements() {
- ui.titleInput = document.getElementById("calendar-title");
+function cacheElements() {
+ ui.topbar = document.querySelector(".topbar");
+ ui.titleInput = document.getElementById("calendar-title");
ui.prevMonth = document.getElementById("prev-month");
ui.nextMonth = document.getElementById("next-month");
ui.todayBtn = document.getElementById("today-btn");
@@ -282,12 +284,17 @@ function updateUrlLength() {
}
}
-function updateTheme() {
- document.body.dataset.theme = state.s.d ? "dark" : "light";
- if (ui.themeToggle) {
- ui.themeToggle.textContent = `Theme: ${state.s.d ? "Dark" : "Light"}`;
- }
-}
+function updateTheme() {
+ document.body.dataset.theme = state.s.d ? "dark" : "light";
+ if (ui.themeToggle) {
+ ui.themeToggle.textContent = `Theme: ${state.s.d ? "Dark" : "Light"}`;
+ }
+}
+
+function syncTopbarHeight() {
+ if (!ui.topbar) return;
+ document.documentElement.style.setProperty("--topbar-height", `${ui.topbar.offsetHeight}px`);
+}
function updateWeekStartLabel() {
if (!ui.weekstartToggle) return;
@@ -467,11 +474,12 @@ function render() {
renderEventList();
updateUrlLength();
- if (ui.jsonModal && !ui.jsonModal.classList.contains("hidden")) {
- updateJsonModal();
- }
- updateLockUI();
-}
+ if (ui.jsonModal && !ui.jsonModal.classList.contains("hidden")) {
+ updateJsonModal();
+ }
+ updateLockUI();
+ initCountdownWidget(state.e);
+}
function renderEventList() {
if (!ui.eventList || !ui.selectedDateLabel) return;
@@ -1025,13 +1033,15 @@ function bindEvents() {
if (ui.jsonClose) ui.jsonClose.addEventListener("click", closeJsonModal);
if (ui.jsonCopy) ui.jsonCopy.addEventListener("click", handleCopyJson);
if (ui.jsonCopyHash) ui.jsonCopyHash.addEventListener("click", handleCopyHash);
- if (ui.jsonDownload) ui.jsonDownload.addEventListener("click", handleExportJson);
-
- window.addEventListener("hashchange", handleHashChange);
-}
+ if (ui.jsonDownload) ui.jsonDownload.addEventListener("click", handleExportJson);
+
+ window.addEventListener("hashchange", handleHashChange);
+ window.addEventListener("resize", syncTopbarHeight);
+}
async function init() {
cacheElements();
+ syncTopbarHeight();
qrManager = initQRCodeManager({
onBeforeOpen: persistStateToHash,
onCopyLink: handleCopyLink,
diff --git a/styles.css b/styles.css
index b6b87e5..ec128b4 100644
--- a/styles.css
+++ b/styles.css
@@ -14,6 +14,7 @@
--accent-strong: #0b57d0;
--danger: #d93025;
--danger-ink: #ffffff;
+ --topbar-height: 64px;
--focus-good: #34a853;
--focus-warn: #f9ab00;
--focus-urgent: #ea4335;
@@ -94,19 +95,100 @@ button {
flex-direction: column;
}
-.topbar {
- display: flex;
- align-items: center;
- justify-content: space-between;
- gap: 16px;
- padding: 0 20px;
- height: 64px;
- background: var(--panel);
- border-bottom: 1px solid var(--border);
- position: sticky;
- top: 0;
- z-index: 10;
-}
+.topbar {
+ display: flex;
+ align-items: center;
+ justify-content: space-between;
+ gap: 16px;
+ padding: 0 20px;
+ height: 64px;
+ background: var(--panel);
+ border-bottom: 1px solid var(--border);
+ position: sticky;
+ top: 0;
+ z-index: 10;
+}
+
+.countdown-container {
+ position: sticky;
+ top: var(--topbar-height, 64px);
+ z-index: 9;
+ margin: 12px 20px 8px;
+ padding: 12px 18px;
+ border-radius: 12px;
+ border: 1px solid var(--border-soft);
+ background: linear-gradient(120deg, var(--accent-soft), transparent);
+ box-shadow: var(--shadow-sm);
+ display: flex;
+ align-items: center;
+ justify-content: space-between;
+ gap: 16px;
+}
+
+.countdown-label {
+ font-size: 0.72rem;
+ text-transform: uppercase;
+ letter-spacing: 0.24em;
+ color: var(--muted);
+ font-weight: 600;
+ white-space: nowrap;
+}
+
+.countdown-content {
+ display: flex;
+ align-items: center;
+ gap: 14px;
+ flex-wrap: wrap;
+}
+
+.countdown-title {
+ font-size: 1rem;
+ font-weight: 600;
+ max-width: 320px;
+ white-space: nowrap;
+ overflow: hidden;
+ text-overflow: ellipsis;
+}
+
+.countdown-timer {
+ font-family: "SFMono-Regular", "Menlo", "Consolas", "Liberation Mono", monospace;
+ font-size: 1.1rem;
+ font-weight: 700;
+ letter-spacing: 0.08em;
+ padding: 4px 10px;
+ border-radius: 8px;
+ background: var(--panel);
+ border: 1px solid var(--border-soft);
+ color: var(--text);
+}
+
+.countdown-meta {
+ font-size: 0.82rem;
+ color: var(--muted);
+}
+
+.countdown-container.urgent {
+ background: linear-gradient(120deg, rgba(234, 67, 53, 0.2), rgba(234, 67, 53, 0.05));
+ border-color: rgba(234, 67, 53, 0.4);
+}
+
+.countdown-container.urgent .countdown-timer {
+ color: var(--focus-urgent);
+ border-color: rgba(234, 67, 53, 0.3);
+}
+
+.countdown-container.inactive {
+ background: var(--panel-2);
+ border-color: var(--border);
+ color: var(--muted);
+ box-shadow: none;
+}
+
+.countdown-container.inactive .countdown-timer {
+ background: transparent;
+ border-color: var(--border-soft);
+ color: var(--muted);
+}
.brand {
display: flex;
@@ -1108,9 +1190,24 @@ button:disabled {
height: auto;
padding: 12px 16px;
flex-direction: column;
- align-items: flex-start;
- gap: 12px;
- }
+ align-items: flex-start;
+ gap: 12px;
+ }
+
+ .countdown-container {
+ margin: 8px 16px 6px;
+ flex-direction: column;
+ align-items: flex-start;
+ }
+
+ .countdown-content {
+ width: 100%;
+ }
+
+ .countdown-title {
+ max-width: 100%;
+ white-space: normal;
+ }
.layout {
grid-template-columns: 1fr;