<!-- #CodePenChallenge: Timekeeping -->
<div id="root"></div>
@import url("https://fonts.googleapis.com/css2?family=Poppins:wght@400;500&display=swap");

* {
	box-sizing: border-box;
	margin: 0;
	padding: 0;
	font-family: "Poppins", sans-serif;
}

#root {
	background-color: #cdffbf;
}

.app {
	min-height: 100vh;
	background: repeating-linear-gradient(
			45deg,
			#78bd76b5 0px 50px,
			#6aad358c 50px 100px,
			#149d648c 100px 150px
		),
		repeating-linear-gradient(
			-45deg,
			#78bd76 0px 50px,
			#6aad35 50px 100px,
			#149d648c 100px 150px
		);
}

header {
	padding: 20px 0;
	margin-bottom: 20px;
	background-color: hsl(0deg 0% 0% / 25%);
	text-align: center;
	font-size: calc(20px + 4vw);
	color: #fffd;
	text-transform: uppercase;
}

.tasks {
	display: flex;
	flex-direction: column;
	align-items: center;
	position: relative;
}

.task-list {
	width: 100%;
	position: relative;
}

.add-task {
	padding: 12px 20px;
	margin-top: 20px;
	margin-bottom: 40px;
	border-radius: 8px;
	background: hsl(113deg 61% 26%);
	color: #fff;
	font-size: calc(14px + 0.4vw);
	cursor: pointer;
}

.add-task:hover {
	background: hsl(113deg 71% 30%);
}

.task {
	width: 90%;
	max-width: 1000px;
	min-height: 200px;
	background-color: #fffd;
	border-radius: 25px;
	margin: 20px auto;
	padding: 20px;
	display: grid;
	grid-template-columns: 5fr 2fr 50px;
	grid-template-rows: auto 2fr 1fr;
	gap: 2px 10px;
	align-items: center;
	justify-items: center;
	grid-template-areas:
		"title time close"
		"description time close"
		"buttons buttons buttons";
}

@media only screen and (max-width: 700px) {
	.task {
		grid-template-areas:
			"title close"
			"description description"
			"time time"
			"buttons buttons";
		grid-template-columns: 1fr 50px;
		grid-template-rows: 1fr 1fr 1fr 1fr;
		gap: 10px 10px;
	}
}

@media only screen and (max-width: 500px) {
	.task {
		grid-template-areas:
			"close"
			"title"
			"description"
			"time"
			"buttons";
		grid-template-columns: 1fr;
		grid-template-rows: 50px 1fr 1fr 1fr 50px;
		gap: 10px 10px;
	}
}

.task-delete {
	display: inline-block;
	padding: 5px 14px;
	border-radius: 6px;
	background-color: hsl(0deg 70% 47%);
	font-size: calc(15px + 1vw);
	color: #fff;
	grid-area: close;
	align-self: start;
}

.task-delete:hover {
	background-color: hsl(0deg 70% 57%);
}

.task textarea {
	width: 100%;
	height: 100%;
	padding: 10px 20px;
	border: none;
	background: #00000003;
	justify-self: start;
	align-self: start;
	resize: none;
}

.task-title {
	grid-area: title;
	font-size: 24px;
	color: #ff8d00;
}

.task-description {
	grid-area: description;
	font-size: 14px;
	color: #333;
}

.task-time-left {
	grid-area: time;
	text-align: center;
	color: hsl(113deg 61% 26%);
}

.task-time-left-p {
	font-size: 34px;
}

.task-time-left-m {
	font-size: 18px;
}

.task-buttons {
	grid-area: buttons;
}

.toggle-running,
.task-reset {
	display: inline-block;
	padding: 8px 12px;
	margin: 0 10px;
	border-radius: 12px;
	background: hsl(113deg 61% 26%);
	color: #fff;
	font-size: calc(14px + 0.4vw);
	cursor: pointer;
}

.toggle-running:hover,
.task-reset:hover {
	background: hsl(113deg 71% 30%);
}
const INTERVAL_SIZE = 10000;
const TOTAL_TIME = 1000 * 60 * 60;
let id = 1;

const tempTask = {
	id: 0,
	title: "#CodePenChallenge: Timekeeping",
	description:
		"This week, your challenge is to create a Pen that shows us your favorite way of keeping track of time.",
	timeLeft: TOTAL_TIME,
	isRunning: false
};

ReactDOM.render(
	<React.StrictMode>
		<App />
	</React.StrictMode>,
	document.getElementById("root")
);

function App() {
	return (
		<div className="app">
			<header>1 hour tasks</header>
			<Tasks />
		</div>
	);
}

function addNewTask(setTasks, title, description) {
	const newTask = {
		id: id,
		title: title || `Task ${id}`,
		description: description || "Task description",
		timeLeft: TOTAL_TIME,
		isRunning: false
	};
	id++;
	setTasks((tasks) => {
		return [...tasks, newTask];
	});
}

function updateTimer(setTasks) {
	setTasks((tasks) => {
		return tasks.map((task) => {
			if (!task.isRunning) return task;
			if (task.timeLeft - INTERVAL_SIZE < 0)
				return {
					...task,
					timeLeft: 0,
					isRunning: false
				};
			return {
				...task,
				timeLeft: task.timeLeft - INTERVAL_SIZE
			};
		});
	});
}

function Tasks() {
	const [tasks, setTasks] = React.useState([tempTask]);

	React.useEffect(() => {
		const interval = setInterval(() => updateTimer(setTasks), INTERVAL_SIZE);
		return () => {
			clearInterval(interval);
		};
	});

	return (
		<div className="tasks">
			<div className="task-list">
				{tasks.map((task) => (
					<Task task={task} setTasks={setTasks} />
				))}
			</div>
			<div className="add-task" onClick={() => addNewTask(setTasks)}>
				Add new task
			</div>
		</div>
	);
}

function toggleTimmer(id, setTasks) {
	setTasks((tasks) =>
		tasks.map((task) => {
			if (task.id !== id) return task;
			return {
				...task,
				isRunning: !task.isRunning
			};
		})
	);
}

function deleteTask(id, setTasks) {
	setTasks((tasks) => tasks.filter((task) => task.id !== id));
}

function editTitle(event, id, setTasks) {
	setTasks((tasks) =>
		tasks.map((task) => {
			if (task.id !== id) return task;
			return {
				...task,
				title: event.target.value
			};
		})
	);
}

function editDescription(event, id, setTasks) {
	setTasks((tasks) =>
		tasks.map((task) => {
			if (task.id !== id) return task;
			return {
				...task,
				description: event.target.value
			};
		})
	);
}

function Task({ task, setTasks }) {
	const titleRef = React.createRef();
	const descRef = React.createRef();

	React.useEffect(() => {
		setHeight(titleRef);
		setHeight(descRef);
	});

	return (
		<div className="task">
			<div className="task-delete" onClick={() => deleteTask(task.id, setTasks)}>
				X
			</div>
			<textarea
				className="task-title"
				ref={titleRef}
				type="text"
				value={task.title}
				placeholder="Task title"
				onChange={(event) => editTitle(event, task.id, setTasks)}
			></textarea>
			<textarea
				className="task-description"
				ref={descRef}
				type="text"
				value={task.description}
				placeholder="Task description"
				onChange={() => editDescription(event, task.id, setTasks)}
			></textarea>
			<div className="task-time-left">
				<div className="task-time-left-p">
					{parseInt((100 * (TOTAL_TIME - task.timeLeft)) / TOTAL_TIME)}%
				</div>
				<div className="task-time-left-m">
					{parseInt(task.timeLeft / 1000 / 60)}min
				</div>
			</div>
			<div className="task-buttons">
				<div
					className="toggle-running"
					onClick={() => toggleTimmer(task.id, setTasks)}
				>
					{task.isRunning ? "STOP" : "START"}
				</div>
				<div className="task-reset" onClick={() => resetTime(task.id, setTasks)}>
					RESET
				</div>
			</div>
		</div>
	);
}

function setHeight(elem) {
	let e = elem.current;
	e.style.height = "";
	e.style.height = `${e.scrollHeight}px`;
}
View Compiled

External CSS

This Pen doesn't use any external CSS resources.

External JavaScript

  1. https://cdnjs.cloudflare.com/ajax/libs/react/17.0.2/umd/react.production.min.js
  2. https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.2/umd/react-dom.production.min.js