HTML preprocessors can make writing HTML more powerful or convenient. For instance, Markdown is designed to be easier to write and read for text documents and you could write a loop in Pug.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. So you don't have access to higher-up elements like the <html>
tag. If you want to add classes there that can affect the whole document, this is the place to do it.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. If you need things in the <head>
of the document, put that code here.
The resource you are linking to is using the 'http' protocol, which may not work when the browser is using https.
CSS preprocessors help make authoring CSS easier. All of them offer things like variables and mixins to provide convenient abstractions.
It's a common practice to apply CSS to a page that styles elements such that they are consistent across all browsers. We offer two of the most popular choices: normalize.css and a reset. Or, choose Neither and nothing will be applied.
To get the best cross-browser support, it is a common practice to apply vendor prefixes to CSS properties and values that require them to work. For instance -webkit-
or -moz-
.
We offer two popular choices: Autoprefixer (which processes your CSS server-side) and -prefix-free (which applies prefixes via a script, client-side).
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.
You can apply CSS to your Pen from any stylesheet on the web. Just put a URL to it here and we'll apply it, in the order you have them, before the CSS in the Pen itself.
You can also link to another Pen here (use the .css
URL Extension) and we'll pull the CSS from that Pen and include it. If it's using a matching preprocessor, use the appropriate URL Extension and we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
JavaScript preprocessors can help make authoring JavaScript easier and more convenient.
Babel includes JSX processing.
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.
You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.
If the script you link to has the file extension of a preprocessor, we'll attempt to process it before applying.
You can also link to another Pen here, and we'll pull the JavaScript from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
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.
Using packages here is powered by esm.sh, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ESM usage.
All packages are different, so refer to their docs for how they work.
If you're using React / ReactDOM, make sure to turn on Babel for the JSX processing.
If active, Pens will autosave every 30 seconds after being saved once.
If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.
If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.
Visit your global Editor Settings.
<div class="add-list" id="add-list">
<button class="add-list-button">+</button>
</div>
<div id="app-header">Task Manager</div>
<!-- Application content starts here -->
<div id="container"></div>
body {
background-color: #6BA1C2
}
#container {
display: flex;
width: 100%;
min-width: 1vh;
flex-wrap: wrap;
}
.list-drop-zone {
flex: 0.25 0.5 260px;
margin: 0 10px;
background-color: #ccc;
display: inline-block;
text-align: center;
font-weight: bolder;
margin-bottom: 100px;
}
.list-draggable {
background-color: #b9b4c5;
min-height: 20px;
margin: 5px;
cursor: pointer;
padding: 10px;
border-radius: 5px;
}
.list-hold {
border: solid 1px #ccc;
}
.add-item {
cursor: pointer;
height: 20px;
}
.add-item:hover {
color: #6BA1C2;
}
.item-content {
font-size: 16px;
word-wrap: break-word;
}
.list-header {
font-size: 20px;
font-weight: bold;
padding: 10px;
}
.display-none {
display: none;
}
.list-hovered {
border: 2px dashed black;
height: 200px;
}
.add-list {
width: 60px;
height: 60px;
background-color:#30c77b;
position: fixed;
right: 20px;
bottom: 20px;
border-radius: 100%;
}
button.add-list-button {
background-color:#30c77b;
width: 100%;
height: 100%;
cursor: pointer;
border-radius: 100%;
font-size: 20px;
}
.edit-row-input {
width: 100%;
}
.save-textarea-button {
cursor: pointer;
}
.action-items {
display: inline;
}
div.display-inline:has(div.action-items) {
display: inline;
}
.action-item {
margin: 0 3px;
cursor: pointer;
}
#app-header {
height: 100px;
font-weight: bolder;
text-align: center;
font-size: 30px;
}
.action-item {
position: relative;
float: right;
}
class DragDropContainer {
onDragOverHandler(e) {
// To help execution of onDragLeaveHandler added preventDefault();
e.preventDefault()
}
/*
on entering a drop-zone. highlighting the element.
*/
onDragEnterHandler(e) {
e.preventDefault();
this.classList.add('list-hovered');
}
/*
on leaving the drop-zone. removing the highlighter.
*/
onDragLeaveHandler(e) {
this.classList.remove('list-hovered');
}
/*
Gets in sourceId from dataTransfer. destination id from dataset of the currentTarget element.
once dropped. updates the store. And renders the DOM again. And clears the dataTransfer.
*/
onDropHandler(event) {
const sourceListId = parseInt(event.dataTransfer.getData('sourceListId'));
const itemId = parseInt(event.dataTransfer.getData('itemId'));
const listId = parseInt(event.currentTarget.dataset.listId);
event.currentTarget.classList.remove('list-hovered');
if(listId === sourceListId) return;
const sourceListIndex = this.listStore.findIndex(list => list.id === sourceListId)
const sourceListItems = [...this.listStore[sourceListIndex].items].filter((item) => item.id !== itemId);
const dropItem = [...this.listStore[sourceListIndex].items].find((item) => item.id === itemId);
const destinationListIndex = this.listStore.findIndex(list => list.id === listId);
this.listStore[sourceListIndex].items = sourceListItems;
this.listStore[destinationListIndex].items.push(dropItem);
this.render()
event.dataTransfer.clearData()
}
/*
Sets dataTransfer that is used on onDropHandler.
setTimeout is to avoid flick of disappearance of dragged object from source.
*/
onDragStartHandler(event) {
const { itemId } = event.currentTarget.dataset;
const { listId } = event.currentTarget.closest('.list-drop-zone').dataset;
event.dataTransfer.setData('sourceListId', listId);
event.dataTransfer.setData('itemId', itemId)
this.classList.add('list-hold');
setTimeout(() => (this.classList.add('display-none')));
}
onDragEndHandler(event) {
setTimeout(() => (this.classList.remove('list-hold')));
this.classList.remove('display-none');
}
}
class Main extends DragDropContainer {
/*
Bind the methods here as the context of this used from where it's being called. expected this should point to the present class.
*/
constructor() {
super()
this.listStore = JSON.parse(localStorage.getItem('listStore')) || this.initialStore();
this.listsCount = this.listStore.length;
this.itemsCount = this.listStore.flatMap((list) => list.items).length;
// parent class method. As it has properities that are set in this class. need to bind to this class.
this.onDropHandler = this.onDropHandler.bind(this);
this.addhandleEventListeners = this.addhandleEventListeners.bind(this);
this.addItem = this.addItem.bind(this);
this.onSave = this.onSave.bind(this);
this.addList = this.addList.bind(this);
this.editListText = this.editListText.bind(this);
this.editText = this.editText.bind(this);
this.deleteList = this.deleteList.bind(this);
this.deleteItem = this.deleteItem.bind(this);
}
/*
Saves the list and item input fields value into listStore and updates the DOM.
*/
onSave(event) {
event.preventDefault()
const listId = parseInt(event.currentTarget.closest('.list-drop-zone').dataset.listId);
const listIndex = this.listStore.findIndex(list => list.id === listId);
const { actionType } = event.currentTarget.dataset;
if(actionType === 'item') {
const itemId = parseInt(event.currentTarget.closest('.list-draggable').dataset.itemId);
const itemIndex = this.listStore[listIndex].items.findIndex(item => item.id === itemId);
const text = event.currentTarget.closest('.list-drop-zone').getElementsByTagName('textarea')[0].value;
this.listStore[listIndex].items[itemIndex].text = text;
} else {
const text = event.currentTarget.closest('.list-header').getElementsByTagName('input')[0].value;
this.listStore[listIndex].text = text;
}
this.render()
}
/*
deletes the whole list.
*/
deleteList(event) {
const listId = parseInt(event.currentTarget.closest('.list-drop-zone').dataset.listId);
this.listStore = this.listStore.filter(list => list.id !== listId);
this.render();
}
/*
deletes the selected item/card.
*/
deleteItem(event) {
const itemId = parseInt(event.currentTarget.closest('.list-draggable').dataset.itemId);
const listId = parseInt(event.currentTarget.closest('.list-drop-zone').dataset.listId);
const listIndex = this.listStore.findIndex(list => list.id === listId);
this.listStore[listIndex].items = [...this.listStore[listIndex].items.filter(item => item.id !== itemId)]
this.render()
}
/*
Editing list text/header;
*/
editListText(event) {
const ele = event.currentTarget.closest('.list-header');
const textContent = ele.textContent;
ele.innerHTML = `<input class='edit-row-input' value="${textContent}"></input><button data-action-type='list' class="save-textarea-button">Save</button>`;
const buttonElements = ele.getElementsByTagName('button');
for(let i = 0; i < buttonElements.length; i++) {
buttonElements[i].onclick = this.onSave;
}
}
/*
Editing item/card text.
*/
editText(event) {
const ele = event.currentTarget.closest('.list-draggable');
const textContent = ele.textContent;
ele.innerHTML = `<textarea cols="20" rows="3" class='edit-row-input'>${textContent}</textarea><button data-action-type='item' class="save-textarea-button">Save</button>`;
const buttonElements = ele.getElementsByTagName('button');
for(let i = 0; i < buttonElements.length; i++) {
buttonElements[i].onclick = this.onSave;
}
}
/*
Adds new Item/Card with default Data.
*/
addItem(event) {
const listId = parseInt(event.currentTarget.closest('.list-drop-zone').dataset.listId);
const listIndex = this.listStore.findIndex(list => list.id === listId)
this.itemsCount += 1;
this.listStore[listIndex].items.push({
id: this.itemsCount,
text: ''
})
this.render()
}
/*
Initial listStore, used if localStorage has no value.
*/
initialStore() {
return [{
id: 1,
text: 'In Progress',
items: [{
id: 1,
text: 'Things to be done.'
}]
}, {
id: 2,
text: 'Completed',
items: []
}]
}
/*
Adds event listeners for all the DOM elements rendered. called as callback from render method.
*/
addhandleEventListeners() {
for(let i = 0; i < this.listStore.length; i++) {
const list = this.listStore[i]
const listElem = document.getElementById(`drop-zone-${list.id}`);
listElem.ondragover = this.onDragOverHandler;
listElem.ondrop = this.onDropHandler;
listElem.ondragenter = this.onDragEnterHandler;
listElem.ondragleave = this.onDragLeaveHandler;
// listElem.onmouseover = (event) => {
// event.currentTarget.classList.add('display-inline')
// }
// listElem.onmouseout = (event) => {
// event.currentTarget.classList.remove('display-inline')
// }
const items = list.items;
for(let j = 0; j < items.length; j++) {
const item = document.getElementById(`draggable-${items[j].id}`)
item.ondragstart = this.onDragStartHandler;
item.ondragend = this.onDragEndHandler;
// item.onmouseover = (event) => {
// event.currentTarget.classList.add('display-inline')
// }
// item.onmouseout = (event) => {
// event.currentTarget.classList.remove('display-inline')
// }
}
}
document.getElementById('add-list').onclick = this.addList;
const addItems = document.getElementsByClassName('add-item') //.onclick = this.addItem;
for(let i = 0; i < addItems.length; i++) {
addItems[i].onclick = this.addItem;
}
const listsHeader = document.getElementsByClassName('list-header');
for(let i = 0; i < listsHeader.length; i++) {
const actionItems = listsHeader[i].getElementsByTagName('i')
actionItems[0].onclick = this.editListText
actionItems[1].onclick = this.deleteList;
}
const itemsContent = document.getElementsByClassName('item-content')
for(let i = 0; i < itemsContent.length; i++) {
const actionItems = itemsContent[i].getElementsByTagName('i')
actionItems[0].onclick = this.editText;
actionItems[1].onclick = this.deleteItem;
}
}
/*
Adds in list and an item to store and updates the DOM.
*/
addList() {
this.listsCount += 1
this.itemsCount += 1
this.listStore.push({
id: this.listsCount,
text: 'List Name Here',
items: [{
id: this.itemsCount,
text: 'Text Here'
}]
})
this.render()
}
render() {
let container = document.getElementById('container');
container.innerHTML = '';
for(let i = 0; i < this.listStore.length; i++) {
const list = this.listStore[i]
container.innerHTML += `
<div class="list-drop-zone" id="drop-zone-${list.id}" data-list-id=${list.id}>
<div class="list-header">${list.text}
<div class="action-items">
<i class="fa fa-edit action-item"></i>
<i class="fa fa-trash action-item"></i>
</div></div>
${list.items.map((item) => (`
<div class="list-draggable"
draggable="true" data-item-id=${item.id} id="draggable-${item.id}">
<div class="item-content">${item.text}
<div class="action-items">
<i class="fa fa-edit action-item"></i>
<i class="fa fa-trash action-item"></i>
</div>
</div>
</div>`)
).join('')}
<div class="add-item">${list.items.length ? '+ Add another item' : '+ Add item'}</div>
</div>
`
}
this.addhandleEventListeners()
localStorage.setItem('listStore', JSON.stringify(this.listStore))
}
}
const app = new Main()
app.render()
Also see: Tab Triggers