-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalendar.html
More file actions
174 lines (163 loc) · 7.5 KB
/
calendar.html
File metadata and controls
174 lines (163 loc) · 7.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<!doctype html>
<html>
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<script src="https://cdn.tailwindcss.com"></script>
<script>
tailwind.config = {
darkMode: "class",
};
</script>
<script src="https://unpkg.com/phosphor-icons"></script>
</head>
<body class="transition-colors duration-300">
<div
class="bg-white dark:bg-gray-800 md:py-8 px-4 lg:max-w-7xl lg:mx-auto lg:px-8"
>
<div class="flex items-center justify-between mb-8">
<p
id="month-year"
class="text-4xl font-bold text-gray-800 dark:text-gray-100"
></p>
<div class="flex items-center space-x-4">
<button
id="dark-mode-toggle"
class="p-2 rounded-full bg-gray-100 dark:bg-gray-700 focus:outline-none"
>
<i
class="ph ph-sun-dim text-xl text-yellow-500 dark:hidden"
></i>
<i
class="ph ph-moon text-xl text-blue-300 hidden dark:block"
></i>
</button>
<button
id="prev-month"
class="px-4 py-2 bg-gray-200 text-gray-800 dark:bg-gray-700 dark:text-gray-200 rounded-md hover:bg-gray-300 dark:hover:bg-gray-600"
>
Prev
</button>
<button
id="next-month"
class="px-4 py-2 bg-gray-200 text-gray-800 dark:bg-gray-700 dark:text-gray-200 rounded-md hover:bg-gray-300 dark:hover:bg-gray-600"
>
Next
</button>
</div>
</div>
<div
class="inline-flex flex-col space-y-1 items-start justify-start h-full w-full"
>
<div
class="inline-flex space-x-28 items-start justify-start pr-24 h-full w-full"
>
<p
class="w-12 h-full text-sm font-medium text-gray-800 dark:text-gray-200 uppercase"
>
M
</p>
<p
class="w-12 h-full text-sm font-medium text-gray-800 dark:text-gray-200 uppercase"
>
T
</p>
<p
class="w-12 h-full text-sm font-medium text-gray-800 dark:text-gray-200 uppercase"
>
W
</p>
<p
class="w-12 h-full text-sm font-medium text-gray-800 dark:text-gray-200 uppercase"
>
T
</p>
<p
class="w-12 h-full text-sm font-medium text-gray-800 dark:text-gray-200 uppercase"
>
F
</p>
<p
class="w-12 h-full text-sm font-medium text-gray-800 dark:text-gray-200 uppercase"
>
S
</p>
<p
class="w-12 h-full text-sm font-medium text-gray-800 dark:text-gray-200 uppercase"
>
S
</p>
</div>
<div id="calendar-grid" class="grid grid-cols-7 gap-px"></div>
</div>
</div>
<script>
document.addEventListener("DOMContentLoaded", function () {
const monthYearElement = document.getElementById("month-year");
const calendarGrid = document.getElementById("calendar-grid");
const prevMonthButton = document.getElementById("prev-month");
const nextMonthButton = document.getElementById("next-month");
const darkModeToggle =
document.getElementById("dark-mode-toggle");
// Check for saved theme preference or default to user's system preference
if (
localStorage.getItem("darkMode") === "dark" ||
(!localStorage.getItem("darkMode") &&
window.matchMedia("(prefers-color-scheme: dark)")
.matches)
) {
document.documentElement.classList.add("dark");
} else {
document.documentElement.classList.remove("dark");
}
let currentDate = new Date();
function generateCalendar(date) {
calendarGrid.innerHTML = "";
const year = date.getFullYear();
const month = date.getMonth();
const firstDay = new Date(year, month, 1);
const lastDay = new Date(year, month + 1, 0);
const daysInMonth = lastDay.getDate();
const startDay = (firstDay.getDay() + 6) % 7; // Monday is 0
monthYearElement.textContent = `${date.toLocaleString("default", { month: "long" })} ${year}`;
for (let i = 0; i < startDay; i++) {
const emptyCell = document.createElement("div");
emptyCell.className =
"w-40 h-24 border border-gray-200 dark:border-gray-700";
calendarGrid.appendChild(emptyCell);
}
for (let i = 1; i <= daysInMonth; i++) {
const dayCell = document.createElement("div");
dayCell.className =
"flex items-start justify-start w-40 h-24 pl-2 pt-2.5 border border-gray-200 dark:border-gray-700";
const dayNumber = document.createElement("p");
dayNumber.className =
"text-sm font-medium text-gray-800 dark:text-gray-200";
dayNumber.textContent = i.toString().padStart(2, "0");
dayCell.appendChild(dayNumber);
calendarGrid.appendChild(dayCell);
}
}
prevMonthButton.addEventListener("click", () => {
currentDate.setMonth(currentDate.getMonth() - 1);
generateCalendar(currentDate);
});
// Dark mode toggle functionality
darkModeToggle.addEventListener("click", () => {
if (document.documentElement.classList.contains("dark")) {
document.documentElement.classList.remove("dark");
localStorage.setItem("darkMode", "light");
} else {
document.documentElement.classList.add("dark");
localStorage.setItem("darkMode", "dark");
}
});
nextMonthButton.addEventListener("click", () => {
currentDate.setMonth(currentDate.getMonth() + 1);
generateCalendar(currentDate);
});
generateCalendar(currentDate);
});
</script>
</body>
</html>