Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <main>
	<h1>📚 Reading Planner</h1>
	
	<form id="form" class="form form--pages">
		<div class="form__field">
			<label for="input-total-pages">How many pages are in your book?</label>
			<input id="input-total-pages" name="total_pages" type="number" value="100" min="0" step="1">
		</div>
		
		<div class="form__field">
			<label for="input-current-page">What page are you on?</label>
			<input id="input-current-page" name="current_page" type="number" value="1" min="0" step="1">
		</div>
		
		<div class="form__field">
			<p>Would you rather read some number of pages per day, or finish by some date?</p>
			<input id="input-method-pages-per-day" type="radio" name="method" value="pages" checked>
			<label for="input-method-pages-per-day">Pages per day</label>
			<input id="input-method-finish-by-date" type="radio" name="method" value="date">
			<label for="input-method-finish-by-date">Finish by date</label>
		</div>
		
		<div class="form__field form__field--pages">
			<label for="input-pages-per-day">How many pages do you want to read each day?</label>
			<input id="input-pages-per-day" name="pages_per_day" type="number" value="10" min="0" step="1">
		</div>

		<div class="form__field form__field--date">
			<label for="input-finish-date">When do you want to finish by?</label>
			<input id="input-finish-date" name="finish_date" type="date">
		</div>
		
		<div class="form__field">
			<button type="submit">Update</button>
		</div>
	</form>
	
	<h2>Result</h2>
	<div id="result">If you read 10 pages per day, you’ll finish your book in <strong>10 days</strong>.</div>
</main>

              
            
!

CSS

              
                /* ------------------------------ */
/* Base                           */
/* ------------------------------ */

html {
	box-sizing: border-box;
}

*,
::after,
::before {
	box-sizing: inherit;
}

body {
	margin: 1rem auto 3rem;
	line-height: 1.4;
	tab-size: 4;
}

main {
	display: block; /* Fix IE rendering main as inline */
	margin: 0 auto;
	max-width: 42rem;
	padding-left: 1rem;
	padding-right: 1rem;
}

pre {
	border: 1px solid #ccc;
	padding: 1rem;
	overflow: auto;
	-webkit-overflow-scroll: touch;
}

button,
input,
select {
	font-size: 1rem;
}



/* ------------------------------ */
/* Form                           */
/* ------------------------------ */

.form__field {
	margin-bottom: 1rem;
}

/* 💡 Remove Child Margin Utility */
/* https://codepen.io/tannerhodges/pen/gObaGvw */
.form__field > :first-of-type { margin-top: 0; }
.form__field > :last-of-type  { margin-bottom: 0; }

/* Dim unused fields */
.form--pages .form__field--date,
.form--date .form__field--pages {
	filter: blur(2px);
	opacity: 0.5;
	cursor: not-allowed;
	pointer-events: none;
	user-select: none;
}

              
            
!

JS

              
                /**
 * Update results based on form data.
 * @param   Event  event
 * @return  void
 */
function update(event) {	
	var output = '😵 Whoops! I screwed up. Would you leave a comment to let me know? Thanks!';
	
	// Get form values
	var currentPage = parseInt(form.elements.current_page.value, 10);
	var totalPages = parseInt(form.elements.total_pages.value, 10);
	var method = form.elements.method.value;
	var pagesPerDay = parseInt(form.elements.pages_per_day.value, 10);
	var finishDate = form.elements.finish_date.value;
	
	// Common variables
	var pagesLeft = totalPages - currentPage;

	// Calculate - Pages per day
	// NOTE: Round up so we always return full days
	var daysToRead = Math.ceil(pagesLeft / pagesPerDay);
		
	// Calculate - Finish by date
	// NOTE: Round up so we always return full days
	var today = new Date();
	var daysLeft = Math.ceil((Date.parse(finishDate) - today.getTime()) / (1000 * 3600 * 24));
	// NOTE: Round up so we always return full pages
	var newPagesPerDay = Math.ceil(pagesLeft / daysLeft);

	// Update CSS classes
	form.className = form.className.replace(/form--\w+/, 'form--' + method);
	
	// Output - Finish by date
	if (method === 'date') {
		output = 'You’ll need to read <strong>' + newPagesPerDay + ' pages per day</strong> to finish by ' + finishDate + '.';
	// Output - Pages per day
	} else {
		output = 'If you read ' + pagesPerDay + ' pages per day, you’ll finish your book in <strong>' + daysToRead + ' days</strong>, on ' + getDateFromNow(daysToRead) + '.';
	}
	
	// Save data to localStorage
	localStorage.setItem('readingPlanner', JSON.stringify({
		current_page: currentPage,
		total_pages: totalPages,
		method: method,
		pages_per_day: pagesPerDay,
		finish_date: finishDate,
	}));
	
	result.innerHTML = output;
}

/**
 * Load form data from localStorage.
 * @return void
 */
function loadData() {
	var data = JSON.parse(localStorage.getItem('readingPlanner') || 'false');
	
	if (!data) {
		return;
	}
	
	// Set data for each form input
	Object.entries(data).forEach(function ([key, value]) {
		form.elements[key].value = value;
	});
}

/**
 * Get date N days from now. Defaults to today.
 * @param   Number  Number of days
 * @return  String  Date in mm/dd/yyyy
 */
function getDateFromNow(days) {
	var date = new Date();
	
	// Increment days
	date.setDate(date.getDate() + (days || 0));
	
	// Return formatted string
	return date
		.toISOString()
		.replace(/(\d+-\d+-\d+).*/, '$1');
}

// Init default values
window['input-finish-date'].value = getDateFromNow(30);
loadData();
update();

// Init event listeners
form.addEventListener('input', update);
form.addEventListener('submit', function (event) {
	event.preventDefault();
	update();
});

              
            
!
999px

Console