From 72215b928ff5f40ec73bd080983a63202cc0c231 Mon Sep 17 00:00:00 2001 From: Sanyam Date: Thu, 16 Oct 2025 23:00:00 +0530 Subject: [PATCH] Improve UI of Todo List: alignment, spacing, and accessibility --- projects/todo/main.js | 50 +++++++++++++++++++++++++++++++++++++--- projects/todo/styles.css | 16 ++++++------- 2 files changed, 55 insertions(+), 11 deletions(-) diff --git a/projects/todo/main.js b/projects/todo/main.js index d8d78ec..a2b5a8c 100644 --- a/projects/todo/main.js +++ b/projects/todo/main.js @@ -1,6 +1,50 @@ -const form = document.getElementById('form'); const input = document.getElementById('input'); const list = document.getElementById('list'); +const form = document.getElementById('form'); +const input = document.getElementById('input'); +const list = document.getElementById('list'); + let items = []; -form.addEventListener('submit', e => { e.preventDefault(); items.push({ id: crypto.randomUUID(), text: input.value, done: false }); input.value = ''; render(); }); -function render() { list.innerHTML = ''; for (const it of items) { const li = document.createElement('li'); li.innerHTML = ` `; li.querySelector('input').addEventListener('change', e => { it.done = e.target.checked; }); li.querySelector('.del').addEventListener('click', () => { items = items.filter(x => x.id !== it.id); render(); }); list.appendChild(li); } } + +function escapeHTML(text) { + const div = document.createElement('div'); + div.textContent = text; + return div.innerHTML; +} + +form.addEventListener('submit', (e) => { + e.preventDefault(); + items.push({ id: crypto.randomUUID(), text: input.value.trim(), done: false }); + input.value = ''; + render(); +}); + +function render() { + list.innerHTML = ''; + for (const it of items) { + const li = document.createElement('li'); + if (it.done) li.classList.add('done'); + + li.innerHTML = ` + + + `; + + const checkbox = li.querySelector('input'); + checkbox.addEventListener('change', (e) => { + it.done = e.target.checked; + li.classList.toggle('done', it.done); + }); + + li.querySelector('.del').addEventListener('click', () => { + items = items.filter((x) => x.id !== it.id); + render(); + }); + + list.appendChild(li); + } +} + render(); // TODOs: save to localStorage; add filter (all/active/done); sort; theme switcher diff --git a/projects/todo/styles.css b/projects/todo/styles.css index d7ab385..50aea8d 100644 --- a/projects/todo/styles.css +++ b/projects/todo/styles.css @@ -130,22 +130,22 @@ ul { margin: 1rem 0; } -/* FIXED: Todo Item Alignment */ +/* Improved: Todo Item Alignment */ li { display: flex; align-items: center; gap: 12px; - padding: 1rem; + padding: 0.75rem 0.5rem; border-bottom: 1px solid var(--item-border); transition: background-color 0.2s; - min-height: 60px; + min-height: 52px; } li:hover { background-color: rgba(255, 255, 255, 0.05); } -/* FIXED: Label alignment */ +/* Improved: Label alignment */ label { display: flex; align-items: center; @@ -153,10 +153,10 @@ label { cursor: pointer; flex: 1; margin: 0; - padding: 4px 0; + padding: 2px 0; } -/* FIXED: Checkbox alignment */ +/* Improved: Checkbox alignment */ input[type="checkbox"] { width: 1.2rem; height: 1.2rem; @@ -165,7 +165,7 @@ input[type="checkbox"] { flex-shrink: 0; } -/* FIXED: Text alignment */ +/* Improved: Text alignment */ label span { flex: 1; word-break: break-word; @@ -178,7 +178,7 @@ li.done label span { color: var(--completed-text); } -/* FIXED: Delete button alignment */ +/* Improved: Delete button alignment */ .del { background: #ef4444; color: white;