Skip to content

Commit 90c488b

Browse files
Add example solution for Interactive Calendar (#321)
1 parent 8574bc2 commit 90c488b

4 files changed

Lines changed: 807 additions & 0 deletions

File tree

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
# Interactive Calendar App
2+
3+
A dynamic, interactive calendar application that demonstrates date manipulation, DOM updates, and event-driven programming in JavaScript. The calendar allows users to navigate through months and years, highlights the current date, and includes event management functionality.
4+
5+
## Features
6+
7+
- **Dynamic Calendar Display**: Shows all days of the selected month and year
8+
- **Current Date Highlighting**: Today's date is visually distinguished
9+
- **Month/Year Navigation**: Intuitive buttons for browsing through time
10+
- **Quick Navigation**: Dropdown for months and input for years
11+
- **Responsive Design**: Adapts to different screen sizes
12+
- **Event Management**: Add, view, and remove events for specific dates
13+
- **Interactive Tooltips**: Hover to see event previews
14+
- **Modal Interface**: Detailed event management for selected dates
15+
16+
## How It Works
17+
18+
### HTML Structure (`index.html`)
19+
- **Header Section**: Navigation buttons and month/year display
20+
- **Calendar Grid**: Weekday headers and dynamic day cells
21+
- **Controls**: Today button and quick month/year selectors
22+
- **Modal**: Event management interface with form and event list
23+
- **Tooltip**: Hover preview for dates with events
24+
25+
### CSS Styling (`style.css`)
26+
- Modern, responsive design with gradient backgrounds
27+
- Smooth animations and transitions
28+
- Mobile-first approach with media queries
29+
- Visual feedback for interactions (hover, active states)
30+
- Modal and tooltip styling with fade-in animations
31+
32+
### JavaScript Implementation (`script.js`)
33+
34+
#### Core Calendar Logic
35+
36+
**Date State Management**
37+
```javascript
38+
let currentDate = new Date(); // Currently displayed month/year
39+
let selectedDate = null; // Date selected for event management
40+
let events = {}; // Event storage: {'YYYY-MM-DD': ['event1', 'event2']}
41+
```
42+
43+
**Calendar Rendering**
44+
The `renderCalendar()` function dynamically generates the calendar grid:
45+
1. Calculate first and last days of the month
46+
2. Determine start/end dates for full week display
47+
3. Create day elements with appropriate styling
48+
4. Add event indicators and click handlers
49+
50+
**Date Manipulation**
51+
```javascript
52+
// Navigate months
53+
function navigateMonth(delta) {
54+
currentDate.setMonth(currentDate.getMonth() + delta);
55+
renderCalendar();
56+
}
57+
58+
// Get date key for event storage
59+
function getDateKey(date) {
60+
return `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, '0')}-${String(date.getDate()).padStart(2, '0')}`;
61+
}
62+
```
63+
64+
#### Event Handling
65+
66+
**Navigation Events**
67+
- Month/Year buttons: `navigateMonth()`, `navigateYear()`
68+
- Today button: `goToToday()` - resets to current date
69+
- Quick selectors: `goToSpecificMonthYear()` - direct month/year input
70+
71+
**Day Interactions**
72+
- Click: Opens event modal for selected date
73+
- Hover: Shows tooltip with event preview (if events exist)
74+
75+
**Modal Management**
76+
- Add events: `addEvent()` - validates input and updates storage
77+
- Remove events: `removeEvent()` - deletes specific events
78+
- Display events: `displayEvents()` - renders event list in modal
79+
80+
#### Key Concepts Demonstrated
81+
82+
**Date Object Manipulation**
83+
- Creating and modifying Date objects
84+
- Extracting date components (year, month, day)
85+
- Calculating date ranges and day-of-week
86+
- Formatting dates for display and storage
87+
88+
**DOM Manipulation**
89+
- Dynamic element creation and removal
90+
- Class manipulation for styling
91+
- Event listener attachment and management
92+
- Real-time UI updates
93+
94+
**Event-Driven Programming**
95+
- User interaction handling (clicks, hovers, form submission)
96+
- State management and updates
97+
- Callback functions and closures
98+
99+
**Data Structures**
100+
- Object-based event storage
101+
- Array manipulation for event lists
102+
- Date string keys for efficient lookups
103+
104+
## Usage
105+
106+
1. **Navigation**: Use arrow buttons or dropdown/input to change months/years
107+
2. **Today Button**: Quickly return to the current date
108+
3. **Add Events**: Click any date to open the event modal and add events
109+
4. **View Events**: Hover over dates with events to see tooltips, or click to edit
110+
5. **Remove Events**: Use the × button in the modal to delete events
111+
112+
## Sample Events
113+
114+
The calendar includes sample events for demonstration:
115+
- Today's date: "Meeting with team"
116+
- Tomorrow: "Doctor appointment", "Project deadline"
117+
- Next week: "Conference call"
118+
119+
## Browser Compatibility
120+
121+
Works in all modern browsers supporting ES6 features including:
122+
- Date object methods
123+
- Template literals
124+
- Arrow functions
125+
- Array methods (forEach, splice)
126+
127+
## Responsive Features
128+
129+
- **Desktop**: Full navigation and layout
130+
- **Tablet**: Adjusted button sizes and spacing
131+
- **Mobile**: Stacked controls, smaller text, touch-friendly interface
132+
133+
This implementation provides a solid foundation for calendar-based applications and demonstrates essential JavaScript concepts in an interactive, practical way.
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>Interactive Calendar</title>
7+
<link rel="stylesheet" href="style.css">
8+
</head>
9+
<body>
10+
<div class="calendar-container">
11+
<div class="calendar-header">
12+
<button id="prev-year" class="nav-btn">&laquo;</button>
13+
<button id="prev-month" class="nav-btn">&lt;</button>
14+
<h1 id="month-year">October 2025</h1>
15+
<button id="next-month" class="nav-btn">&gt;</button>
16+
<button id="next-year" class="nav-btn">&raquo;</button>
17+
</div>
18+
19+
<div class="calendar-grid">
20+
<div class="weekdays">
21+
<div>Sun</div>
22+
<div>Mon</div>
23+
<div>Tue</div>
24+
<div>Wed</div>
25+
<div>Thu</div>
26+
<div>Fri</div>
27+
<div>Sat</div>
28+
</div>
29+
<div id="calendar-days" class="days"></div>
30+
</div>
31+
32+
<div class="calendar-controls">
33+
<button id="today-btn" class="control-btn">Today</button>
34+
<div class="quick-nav">
35+
<select id="month-select">
36+
<option value="0">January</option>
37+
<option value="1">February</option>
38+
<option value="2">March</option>
39+
<option value="3">April</option>
40+
<option value="4">May</option>
41+
<option value="5">June</option>
42+
<option value="6">July</option>
43+
<option value="7">August</option>
44+
<option value="8">September</option>
45+
<option value="9">October</option>
46+
<option value="10">November</option>
47+
<option value="11">December</option>
48+
</select>
49+
<input type="number" id="year-input" min="1900" max="2100">
50+
</div>
51+
</div>
52+
</div>
53+
54+
<!-- Event Modal -->
55+
<div id="event-modal" class="modal">
56+
<div class="modal-content">
57+
<span class="close">&times;</span>
58+
<h2 id="modal-date">Event Details</h2>
59+
<div id="event-list"></div>
60+
<div class="event-form">
61+
<input type="text" id="event-title" placeholder="Event title" maxlength="50">
62+
<button id="add-event-btn">Add Event</button>
63+
</div>
64+
</div>
65+
</div>
66+
67+
<!-- Tooltip -->
68+
<div id="tooltip" class="tooltip"></div>
69+
70+
<script src="script.js"></script>
71+
</body>
72+
</html>

0 commit comments

Comments
 (0)