Skip to content

Commit 23373ad

Browse files
feat: show current date in axenda
1 parent 13133a4 commit 23373ad

2 files changed

Lines changed: 45 additions & 1 deletion

File tree

src/components/home/CalendarSection.astro

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,12 +110,35 @@ const calendarApiUrl = withBase('/api/calendar.json')
110110
})
111111

112112
const weekdayFormatter = new Intl.DateTimeFormat(locale, { weekday: 'short' })
113+
const vigoTimezone = 'Europe/Madrid'
113114

114115
const getDateKey = (date) => `${date.getFullYear()}-${date.getMonth()}-${date.getDate()}`
115116
const keyToDate = (key) => {
116117
const [year, month, day] = key.split('-').map(Number)
117118
return new Date(year, month, day)
118119
}
120+
const getTimezoneDateParts = (date, timeZone) => {
121+
const parts = new Intl.DateTimeFormat('en-CA', {
122+
timeZone,
123+
year: 'numeric',
124+
month: '2-digit',
125+
day: '2-digit',
126+
}).formatToParts(date)
127+
128+
return {
129+
year: Number(parts.find((part) => part.type === 'year')?.value),
130+
month: Number(parts.find((part) => part.type === 'month')?.value) - 1,
131+
day: Number(parts.find((part) => part.type === 'day')?.value),
132+
}
133+
}
134+
const getTimezoneDateKey = (date, timeZone) => {
135+
const { year, month, day } = getTimezoneDateParts(date, timeZone)
136+
return `${year}-${month}-${day}`
137+
}
138+
const getDayStamp = (key) => {
139+
const [year, month, day] = key.split('-').map(Number)
140+
return Date.UTC(year, month, day)
141+
}
119142

120143
const getEventsByDay = (events) => {
121144
const map = new Map()
@@ -197,6 +220,9 @@ const calendarApiUrl = withBase('/api/calendar.json')
197220

198221
const eventsByDay = getEventsByDay(events)
199222
const eventDays = new Set(eventsByDay.keys())
223+
const now = new Date()
224+
const todayKey = getTimezoneDateKey(now, vigoTimezone)
225+
const todayStamp = getDayStamp(todayKey)
200226

201227
calendarGrid.innerHTML = ''
202228

@@ -223,6 +249,8 @@ const calendarApiUrl = withBase('/api/calendar.json')
223249
if (inMonth) {
224250
const key = getDateKey(currentDate)
225251
const hasEvents = hasEventsOnDate(eventsByDay, key)
252+
const isToday = key === todayKey
253+
const isPastDate = getDayStamp(key) < todayStamp
226254
dayCell.textContent = String(currentDate.getDate())
227255
dayCell.classList.remove('text-[var(--ink-soft)]')
228256
dayCell.classList.add('text-[var(--ink)]')
@@ -240,14 +268,27 @@ const calendarApiUrl = withBase('/api/calendar.json')
240268
}
241269

242270
if (eventDays.has(key)) {
243-
dayCell.classList.add('bg-primary', 'font-semibold', 'text-white')
271+
if (isPastDate) {
272+
dayCell.classList.add('bg-primary-past', 'font-semibold', 'text-white')
273+
} else {
274+
dayCell.classList.add('bg-primary', 'font-semibold', 'text-white')
275+
}
244276
} else {
245277
dayCell.classList.add('bg-[var(--bg-panel)]/65')
246278
}
247279

248280
if (selectedDateKey === key) {
249281
dayCell.classList.add('ring-2', 'ring-primary/40', 'ring-offset-2', 'ring-offset-[var(--bg-elevated)]')
250282
}
283+
284+
if (isToday) {
285+
dayCell.classList.add(
286+
'outline',
287+
'outline-2',
288+
'outline-[var(--accent)]',
289+
'outline-offset-2',
290+
)
291+
}
251292
} else {
252293
dayCell.textContent = ''
253294
dayCell.classList.add('opacity-50')

src/styles/global.css

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
@theme {
3030
--color-primary: var(--accent);
3131
--color-primary-strong: var(--accent-strong);
32+
--color-primary-past: var(--accent-past);
3233
--color-surface: var(--bg);
3334
--color-surface-panel: var(--bg-panel);
3435
--color-ink: var(--ink);
@@ -55,6 +56,7 @@
5556
--line: #d9e4e9;
5657
--accent: #e84a5f;
5758
--accent-strong: #d33b51;
59+
--accent-past: color-mix(in srgb, var(--accent-strong) 55%, black);
5860

5961
--shell-width: 1200px;
6062
}
@@ -70,6 +72,7 @@
7072
--line: #2a3b4d;
7173
--accent: #e84a5f;
7274
--accent-strong: #ff3559;
75+
--accent-past: color-mix(in srgb, var(--accent-strong) 62%, black);
7376
}
7477

7578
html,

0 commit comments

Comments
 (0)