write me a self-container html file using fontawesome, tailwindcss CDNs, that enables me to drag-and-drop and create calendar entries on a 5minute interval basis. i want it to he graphically perfect, with muted blue tones.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Okay, this is a fun challenge! Creating a self-contained, graphically pleasing, drag-and-drop calendar with 5-minute intervals using CDNs for Tailwind and FontAwesome involves a fair bit of HTML, CSS (via Tailwind), and JavaScript.
Here's the complete, self-contained HTML file.
Key Features:
Self-Contained: All code is within a single .html file.
CDNs: Uses Tailwind CSS and FontAwesome CDNs.
Muted Blue Tones: Custom Tailwind colors are defined for a consistent, calming palette.
Drag-and-Drop Creation: Click and drag in the event area to create new entries.
Drag-to-Move: Click and drag an existing event to move it.
Resize: Drag the bottom edge of an event to resize its duration.
5-Minute Intervals: All creation, moving, and resizing snaps to 5-minute increments.
Time Labels: Sticky time labels on the left for easy reference.
Date Navigation: Buttons to navigate previous/next day.
Event Display: Shows title and duration within the event block.
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Muted Blue Calendar</title>
<!-- Tailwind CSS CDN -->
<script src="https://cdn.tailwindcss.com"></script>
<!-- FontAwesome CDN -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css" integrity="sha512-..." crossorigin="anonymous" referrerpolicy="no-referrer" />
<style>
/* Define custom colors for Tailwind using JIT mode configuration */
:root {
--color-primary-100: #E0F2F7; /* Lightest Blue */
--color-primary-200: #B3E0EE; /* Lighter Blue */
--color-primary-300: #81C7D4; /* Accent Blue */
--color-neutral-text: #2C3E50; /* Darker Text */
--color-neutral-bg: #F0F5F7; /* Muted Background */
--color-border: #D1E9F1; /* Light Border */
}
/* Override default Tailwind colors with custom muted blues */
/* This section is handled by the inline Tailwind config block below */
/* Custom scrollbar for a more refined look */
.calendar-grid-container::-webkit-scrollbar {
width: 8px;
}
.calendar-grid-container::-webkit-scrollbar-track {
background: var(--color-neutral-bg);
border-radius: 4px;
}
.calendar-grid-container::-webkit-scrollbar-thumb {
background: var(--color-primary-200);
border-radius: 4px;
}
.calendar-grid-container::-webkit-scrollbar-thumb:hover {
background: var(--color-primary-300);
}
/* Specific styles for event blocks */
.calendar-event {
position: absolute;
width: calc(100% - 4px); /* Account for padding/margin */
left: 2px;
background-color: var(--color-primary-300);
border-radius: 6px; /* slightly more rounded */
padding: 4px 6px;
overflow: hidden;
color: white;
font-size: 0.75rem; /* text-xs */
line-height: 1rem; /* leading-tight */
box-shadow: 0 1px 3px rgba(0,0,0,0.1);
z-index: 10; /* Above grid lines */
cursor: grab;
transition: background-color 0.15s ease-in-out, box-shadow 0.15s ease-in-out;
}
.calendar-event:active {
cursor: grabbing;
box-shadow: 0 4px 10px rgba(0,0,0,0.2);
}
/* Resize handle */
.calendar-event-resize-handle {
position: absolute;
bottom: 0;
left: 0;
width: 100%;
height: 8px; /* h-2 */
cursor: ns-resize;
background: var(--color-primary-300); /* Slightly darker accent */
opacity: 0;
transition: opacity 0.1s ease-in-out;
border-bottom-left-radius: 6px;
border-bottom-right-radius: 6px;
/* Optional: Add a subtle indicator */
display: flex;
justify-content: center;
align-items: center;
}
.calendar-event:hover .calendar-event-resize-handle {
opacity: 0.7;
}
.calendar-event-resize-handle:hover {
opacity: 1; /* Fully opaque on hover */
}
.calendar-event-resize-handle::after {
content: '';
display: block;
width: 20px;
height: 2px;
background-color: rgba(255, 255, 255, 0.8);
border-radius: 1px;
}
/* Ghost event for drag-to-create */
.ghost-event {
position: absolute;
background-color: var(--color-primary-300);
opacity: 0.4;
border-radius: 6px;
left: 2px;
width: calc(100% - 4px);
pointer-events: none; /* Allows mouse events to pass through to grid */
z-index: 5; /* Below actual events */
}
</style>
<!-- Tailwind CSS configuration block for custom colors -->
<script>
tailwind.config = {
theme: {
extend: {
colors: {
primary: {
100: 'var(--color-primary-100)',
200: 'var(--color-primary-200)',
300: 'var(--color-primary-300)', // Accent
},
neutral: {
text: 'var(--color-neutral-text)',
bg: 'var(--color-neutral-bg)',
border: 'var(--color-border)',
}
}
}
}
}
</script>
</head>
<body class="bg-neutral-bg font-sans text-neutral-text antialiased">
<div class="max-w-4xl mx-auto p-6 bg-white rounded-lg shadow-xl my-8">
<!-- Header: Date Navigation -->
<div class="flex items-center justify-between mb-6 border-b pb-4 border-neutral-border">
<button id="prevDayBtn" class="bg-primary-200 hover:bg-primary-300 text-white p-3 rounded-full transition-colors duration-200 shadow-md">
<i class="fas fa-chevron-left"></i>
</button>
<h2 id="currentDateDisplay" class="text-3xl font-extrabold text-neutral-text tracking-tight"></h2>
<button id="nextDayBtn" class="bg-primary-200 hover:bg-primary-300 text-white p-3 rounded-full transition-colors duration-200 shadow-md">
<i class="fas fa-chevron-right"></i>
</button>
</div>
<!-- Calendar Grid -->
<div class="grid grid-cols-[80px_1fr] gap-x-4 h-[70vh] min-h-[500px]">
<!-- Time Labels Column -->
<div id="timeLabels" class="relative overflow-hidden pt-1">
<!-- Time labels will be generated here by JS -->
</div>
<!-- Event Grid Column -->
<div id="eventGrid" class="relative border border-neutral-border rounded-lg overflow-y-auto calendar-grid-container bg-primary-100/30">
<!-- 5-minute slots and events will be generated here by JS -->
</div>
</div>
</div>
<script>
// IIFE for scope encapsulation
(function() {
const timeLabelsContainer = document.getElementById('timeLabels');
const eventGridContainer = document.getElementById('eventGrid');
const currentDateDisplay = document.getElementById('currentDateDisplay');
const prevDayBtn = document.getElementById('prevDayBtn');
const nextDayBtn = document.getElementById('nextDayBtn');
let currentDate = new Date(); // Tracks the currently displayed day
let events = []; // Stores all calendar entries { id, start, end, title, top, height (in px) }
// Drag-and-drop state
let isDragging = false;
let isResizing = false;
let isMoving = false;
let dragStartTimeSlot = null; // The DOM element of the slot where drag started
let dragStartY = 0; // Mouse Y position when drag started
let currentEventId = null; // ID of the event being resized/moved
let initialEventHeight = 0; // For resizing
let initialEventTop = 0; // For moving
let ghostEvent = null; // Temporary div for new event creation
// Configuration for 5-minute intervals
const SLOT_DURATION_MINUTES = 5;
const SLOTS_PER_HOUR = 60 / SLOT_DURATION_MINUTES;
const PIXELS_PER_HOUR = 96; // Adjust this value to control overall height of slots
const PIXELS_PER_SLOT = PIXELS_PER_HOUR / SLOTS_PER_HOUR;
// --- Helper Functions ---
/**
* Formats a Date object to "HH:MM AM/PM"
*/
function formatTime(date) {
const hours = date.getHours();
const minutes = date.getMinutes();
const ampm = hours >= 12 ? 'PM' : 'AM';
const formattedHours = hours % 12 === 0 ? 12 : hours % 12;
const formattedMinutes = minutes < 10 ? '0' + minutes : minutes;
return `${formattedHours}:${formattedMinutes} ${ampm}`;
}
/**
* Formats a Date object to "Day, Month DD, YYYY"
*/
function formatDate(date) {
const options = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' };
return date.toLocaleDateString(undefined, options);
}
/**
* Adds minutes to a Date object.
*/
function addMinutes(date, minutes) {
const newDate = new Date(date.getTime());
newDate.setMinutes(newDate.getMinutes() + minutes);
return newDate;
}
/**
* Converts a Date object to its absolute minutes from midnight (00:00).
*/
function getMinutesOfDay(date) {
return date.getHours() * 60 + date.getMinutes();
}
/**
* Converts pixels from the top of the grid to a Date object (for the current day).
*/
function pixelsToTime(pixels) {
const totalMinutes = Math.round(pixels / PIXELS_PER_SLOT) * SLOT_DURATION_MINUTES;
const hours = Math.floor(totalMinutes / 60);
const minutes = totalMinutes % 60;
const newDate = new Date(currentDate); // Use current day to set date
newDate.setHours(hours, minutes, 0, 0);
return newDate;
}
/**
* Calculates the top pixel position for a given Date object.
*/
function timeToPixels(date) {
const minutesOfDay = getMinutesOfDay(date);
return (minutesOfDay / SLOT_DURATION_MINUTES) * PIXELS_PER_SLOT;
}
/**
* Snaps a given date object to the nearest 5-minute interval.
*/
function snapToFiveMinutes(date) {
const minutes = date.getMinutes();
const remainder = minutes % SLOT_DURATION_MINUTES;
const roundedMinutes = remainder > SLOT_DURATION_MINUTES / 2
? minutes + (SLOT_DURATION_MINUTES - remainder)
: minutes - remainder;
const snappedDate = new Date(date);
snappedDate.setMinutes(roundedMinutes, 0, 0);
return snappedDate;
}
// --- Rendering Functions ---
function renderTimeLabels() {
timeLabelsContainer.innerHTML = ''; // Clear existing labels
for (let hour = 0; hour < 24; hour++) {
const labelDiv = document.createElement('div');
const time = new Date(currentDate);
time.setHours(hour, 0, 0, 0);
labelDiv.textContent = formatTime(time);
labelDiv.className = `absolute text-sm text-neutral-text/70 transform -translate-y-1/2`;
labelDiv.style.top = `${timeToPixels(time)}px`; // Position at the hour mark
timeLabelsContainer.appendChild(labelDiv);
}
}
function renderCalendarGrid() {
eventGridContainer.innerHTML = ''; // Clear existing slots
// Create 5-minute slots for the full 24 hours
for (let i = 0; i < (24 * SLOTS_PER_HOUR); i++) {
const slotDiv = document.createElement('div');
const slotTime = new Date(currentDate);
slotTime.setHours(0, i * SLOT_DURATION_MINUTES, 0, 0);
slotDiv.className = `border-b border-neutral-border/50 h-[${PIXELS_PER_SLOT}px]`; // Tailwind custom height via []
slotDiv.dataset.time = slotTime.toTimeString().substring(0, 5); // Store HH:MM
eventGridContainer.appendChild(slotDiv);
}
}
function renderEvents() {
// Filter events for the current day
const currentDayEvents = events.filter(event =>
event.start.toDateString() === currentDate.toDateString()
);
// Remove existing event elements from the DOM
document.querySelectorAll('.calendar-event').forEach(el => el.remove());
currentDayEvents.forEach(event => {
const eventEl = document.createElement('div');
eventEl.className = 'calendar-event group'; // Add group for hover effects
eventEl.dataset.id = event.id;
const eventTop = timeToPixels(event.start);
const eventBottom = timeToPixels(event.end);
const eventHeight = eventBottom - eventTop;
eventEl.style.top = `${eventTop}px`;
eventEl.style.height = `${Math.max(eventHeight, PIXELS_PER_SLOT * 2)}px`; // Min height of two slots for visibility
eventEl.innerHTML = `
<div class="font-semibold">${event.title}</div>
<div class="text-xs text-white/80">${formatTime(event.start)} - ${formatTime(event.end)}</div>
<div class="calendar-event-resize-handle"></div>
`;
eventGridContainer.appendChild(eventEl);
// Add event listeners for moving and resizing
eventEl.addEventListener('mousedown', (e) => handleEventMouseDown(e, event));
});
}
/**
* Main render function, updates all parts of the calendar display.
*/
function renderCalendar() {
currentDateDisplay.textContent = formatDate(currentDate);
renderTimeLabels();
renderCalendarGrid();
renderEvents();
}
// --- Event Handlers ---
function handleGridMouseDown(e) {
// Check if target is part of an existing event or resize handle
if (e.target.closest('.calendar-event') || e.target.closest('.calendar-event-resize-handle')) {
return; // Let handleEventMouseDown take over
}
e.preventDefault(); // Prevent text selection
isDragging = true;
dragStartY = e.clientY;
// Create a temporary ghost event div
ghostEvent = document.createElement('div');
ghostEvent.className = 'ghost-event';
eventGridContainer.appendChild(ghostEvent);
// Calculate initial top based on mouse Y relative to grid
const gridRect = eventGridContainer.getBoundingClientRect();
const startYInGrid = e.clientY - gridRect.top + eventGridContainer.scrollTop;
const snappedStartPixels = Math.round(startYInGrid / PIXELS_PER_SLOT) * PIXELS_PER_SLOT;
ghostEvent.style.top = `${snappedStartPixels}px`;
ghostEvent.style.height = `${PIXELS_PER_SLOT * 2}px`; // Minimum height for 2 slots (10 mins)
dragStartTimeSlot = pixelsToTime(snappedStartPixels);
}
function handleMouseMove(e) {
if (isDragging) {
const gridRect = eventGridContainer.getBoundingClientRect();
const currentYInGrid = e.clientY - gridRect.top + eventGridContainer.scrollTop;
const snappedCurrentY = Math.round(currentYInGrid / PIXELS_PER_SLOT) * PIXELS_PER_SLOT;
const startTop = parseFloat(ghostEvent.style.top);
let newHeight = snappedCurrentY - startTop;
// Ensure minimum height
if (newHeight < PIXELS_PER_SLOT * 2) {
newHeight = PIXELS_PER_SLOT * 2;
}
ghostEvent.style.height = `${newHeight}px`;
} else if (isResizing) {
e.preventDefault();
const eventEl = document.querySelector(`.calendar-event[data-id="${currentEventId}"]`);
if (!eventEl) return;
const gridRect = eventGridContainer.getBoundingClientRect();
const currentYInGrid = e.clientY - gridRect.top + eventGridContainer.scrollTop;
const eventObj = events.find(ev => ev.id === currentEventId);
if (!eventObj) return;
const eventTop = timeToPixels(eventObj.start);
let newHeight = currentYInGrid - eventTop;
// Snap new height to 5-minute interval
newHeight = Math.round(newHeight / PIXELS_PER_SLOT) * PIXELS_PER_SLOT;
// Ensure minimum height (e.g., 2 slots for visibility)
newHeight = Math.max(newHeight, PIXELS_PER_SLOT * 2);
eventEl.style.height = `${newHeight}px`;
// Update event object's end time
eventObj.end = pixelsToTime(eventTop + newHeight);
} else if (isMoving) {
e.preventDefault();
const eventEl = document.querySelector(`.calendar-event[data-id="${currentEventId}"]`);
if (!eventEl) return;
const gridRect = eventGridContainer.getBoundingClientRect();
const currentYInGrid = e.clientY - gridRect.top + eventGridContainer.scrollTop;
const deltaY = currentYInGrid - dragStartY;
let newTop = initialEventTop + deltaY;
// Snap newTop to 5-minute interval
newTop = Math.round(newTop / PIXELS_PER_SLOT) * PIXELS_PER_SLOT;
eventEl.style.top = `${newTop}px`;
// Update event object's start and end times
const eventObj = events.find(ev => ev.id === currentEventId);
if (!eventObj) return;
const durationMinutes = (getMinutesOfDay(eventObj.end) - getMinutesOfDay(eventObj.start));
eventObj.start = pixelsToTime(newTop);
eventObj.end = addMinutes(eventObj.start, durationMinutes);
}
}
function handleMouseUp(e) {
if (isDragging) {
const gridRect = eventGridContainer.getBoundingClientRect();
const endYInGrid = e.clientY - gridRect.top + eventGridContainer.scrollTop;
const snappedEndY = Math.round(endYInGrid / PIXELS_PER_SLOT) * PIXELS_PER_SLOT;
const startPixels = parseFloat(ghostEvent.style.top);
const endPixels = Math.max(startPixels + PIXELS_PER_SLOT * 2, snappedEndY); // Ensure minimum 10 mins
const startTime = pixelsToTime(startPixels);
const endTime = pixelsToTime(endPixels);
const title = prompt('Enter event title:');
if (title) {
events.push({
id: Date.now(),
start: startTime,
end: endTime,
title: title,
});
saveEvents();
renderCalendar();
}
ghostEvent.remove();
ghostEvent = null;
} else if (isResizing || isMoving) {
saveEvents();
renderCalendar(); // Re-render to ensure positions are final and persistent
}
// Reset all states
isDragging = false;
isResizing = false;
isMoving = false;
currentEventId = null;
dragStartY = 0;
}
function handleEventMouseDown(e, event) {
e.preventDefault(); // Prevent text selection on drag
e.stopPropagation(); // Prevent grid mousedown from firing
currentEventId = event.id;
dragStartY = e.clientY;
const eventEl = e.currentTarget; // The .calendar-event div
if (e.target.classList.contains('calendar-event-resize-handle')) {
isResizing = true;
initialEventHeight = eventEl.offsetHeight;
} else {
isMoving = true;
initialEventTop = eventEl.offsetTop;
}
}
// --- Data Persistence ---
function saveEvents() {
// Store only necessary event data (convert Date objects to ISO strings)
const serializableEvents = events.map(event => ({
id: event.id,
start: event.start.toISOString(),
end: event.end.toISOString(),
title: event.title
}));
localStorage.setItem('calendarEvents', JSON.stringify(serializableEvents));
}
function loadEvents() {
const storedEvents = localStorage.getItem('calendarEvents');
if (storedEvents) {
const parsedEvents = JSON.parse(storedEvents);
events = parsedEvents.map(event => ({
id: event.id,
start: new Date(event.start),
end: new Date(event.end),
title: event.title
}));
}
}
// --- Event Listeners Setup ---
function setupEventListeners() {
eventGridContainer.addEventListener('mousedown', handleGridMouseDown);
document.addEventListener('mousemove', handleMouseMove);
document.addEventListener('mouseup', handleMouseUp);
prevDayBtn.addEventListener('click', () => {
currentDate.setDate(currentDate.getDate() - 1);
renderCalendar();
});
nextDayBtn.addEventListener('click', () => {
currentDate.setDate(currentDate.getDate() + 1);
renderCalendar();
});
}
// --- Initial Load ---
// Load events first, then render
loadEvents();
renderCalendar();
setupEventListeners();
// Initial scroll to current time (optional, but good for UX)
const now = new Date();
const currentHourPixels = timeToPixels(now) - (PIXELS_PER_HOUR * 2); // Scroll a bit before current time
eventGridContainer.scrollTop = Math.max(0, currentHourPixels);
})(); // End of IIFE
</script>
</body>
</html>