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.
<el-toast></el-toast>
<template id="elToastItem">
<div class="el-toast__icon"></div>
<div class="el-toast__close">
<button>
<i class="fa fa-fw fa-times"></i>
</button>
</div>
<div class="el-toast__description"></div>
<div class="el-toast__action"></div>
<div class="el-toast__progress">
<div class="el-toast__bar" style="width: 100%;"></div>
</div>
</template>
<template id="elToast">
<ul class="el-toast"></ul>
</template>
<!-- Example -->
<!--- ul class="el-toast el-toast--example">
<li class="el-toast__item el-toast__item--info">
<div class="el-toast__icon"><i class="fa fa-exclamation-circle"></i></div>
<div class="el-toast__close">
<button><i class="fa fa-fw fa-times"></i></button>
</div>
<div class="el-toast__description">What is going to happen?</div>
<div class="el-toast__action">
<button>OK</button>
<button>Something wonderful.</button>
</div>
<div class="el-toast__progress">
<div class="el-toast__bar" style="width: 80%;"></div>
</div>
</li>
</ul -->
@import url("https://fonts.googleapis.com/css?family=Open+Sans:400,400i,700");
body,
html {
font-family: 'Open Sans', sans-serif;
font-size: 10px;
background: linear-gradient(90deg, #e3ffe7 0%, #d9e7ff 100%);
height: 100vh;
}
/*
** Component
*/
$root: ".el-toast";
$transition-default: cubic-bezier(0.5, 0, 0.1, 1);
$transition-in: cubic-bezier(0, 0, 0.25, 1);
$transition-out: cubic-bezier(0.25, 0, 1, 1);
#{$root} {
align-items: center;
bottom: 0;
display: flex;
flex-direction: column;
list-style: none;
margin: 16px;
opacity: 1;
padding: 0 0 10px;
position: fixed;
right: 0;
transition: opacity 150ms $transition-default;
width: 350px;
&--paused {
opacity: 0.9;
}
button {
&,
&:focus,
&:hover {
background-color: transparent;
border: 0;
cursor: pointer;
outline: none;
}
}
&__item:not(:first-child) {
margin-top: 8px;
}
&__item:nth-child(n + 4) {
bottom: 0;
max-height: 40px;
position: absolute;
#{$root}__icon,
#{$root}__progress,
#{$root}__description,
#{$root}__action {
opacity: 0;
}
}
&__item:nth-child(n + 6) {
opacity: 0;
transform: translateY(0) scale(0.85);
z-index: 1970;
}
&__item:nth-child(4) {
background-color: #595959;
transform: translateY(-4px) scale(0.95);
z-index: 1990;
}
&__item:nth-child(5) {
background-color: #767676;
transform: translateY(0) scale(0.9);
z-index: 1980;
}
&__item {
animation: append-in 300ms $transition-in forwards;
background-color: #222222;
border-radius: 2px;
color: #fff;
display: grid;
grid-template: [header] "icon description close"auto [body] "icon action close"auto [footer] "progress progress progress"auto / 38px 1fr 38px;
max-height: 400px;
overflow: hidden;
position: relative;
transform-origin: center bottom;
transition: transform 300ms $transition-in, background 300ms $transition-in,
opacity 300ms $transition-in, max-height 300ms $transition-in;
user-select: none;
width: 100%;
z-index: 2000;
@keyframes append-in {
from {
max-height: 0;
opacity: 0;
transform: scale(0.9);
}
}
@keyframes append-out {
to {
max-height: 0;
opacity: 0;
transform: scale(0.9);
}
}
&--danger {
#{$root} {
&__action,
&__description,
&__icon {
button,
a, i {
color: #DF6962;
}
}
&__bar {
background-color: #D64038;
}
}
}
&--alert {
#{$root} {
&__action,
&__description,
&__icon {
button,
a, i {
color: #F3CC6B;
}
}
&__bar {
background-color: #EFBC3C;
}
}
}
&--success {
#{$root} {
&__action,
&__description,
&__icon {
button,
a, i {
color: #54A668;
}
}
&__bar {
background-color: #54A668;
}
}
}
&--info {
#{$root} {
&__action,
&__description,
&__icon {
button,
a, i {
color: #71C3DD;
}
}
&__bar {
background-color: #48B1D3;
}
}
}
&--removing {
animation: append-out 300ms $transition-out forwards;
}
}
&__icon {
display: flex;
font-size: 1.4rem;
grid-area: icon;
justify-content: center;
padding: 12px 8px 12px 12px;
}
&__description {
font-size: 1.3rem;
grid-area: description;
line-height: 1.5;
margin: 10px 0;
max-height: 200px;
overflow-x: auto;
padding: 0 8px;
button {
&:hover,
&:focus {
text-decoration: underline;
}
}
}
&__close {
display: flex;
font-size: 1.4rem;
grid-area: close;
justify-content: center;
button {
color: #fff;
display: block;
height: 38px;
padding: 10px 8px 8px;
text-align: center;
width: 100%;
}
}
&__action {
grid-column: action;
button,
a {
display: inline-block;
font-size: 1.3rem;
padding: 0 8px 10px;
text-transform: uppercase;
&:hover,
&:focus {
text-decoration: underline;
}
}
}
&__progress {
background-color: transparentize(#E1E3E6, 0.85);
grid-column: progress;
height: 4px;
width: 100%;
z-index: 20;
&--hide {
display: none;
}
}
&__bar {
bottom: 0;
display: block;
height: 4px;
position: absolute;
transition: width 100ms $transition-default;
width: 100%;
z-index: 10;
}
// Ignore
&--example {
left: calc(50% - 175px);
right: 0;
top: calc(50% - 33px);
}
}
@media only screen and (max-width: 720px) {
#{$root} {
margin: 16px 0;
max-height: 400px;
padding: 10px 0px;
width: 100%;
&__item {
width: calc(100% - 30px);
}
&__item:not(:first-child) {
margin-top: 8px;
}
&__item:nth-child(n + 2) {
bottom: 0;
max-height: 40px;
position: absolute;
#{$root}__icon,
#{$root}__progress,
#{$root}__description,
#{$root}__action {
opacity: 0;
}
}
&__item:nth-child(n + 4) {
opacity: 0;
transform: translateY(0) scale(0.9);
z-index: 1970;
}
&__item:nth-child(2) {
background-color: #595959;
transform: translateY(-4px) scale(0.95);
width: calc(100% - 60px);
z-index: 1990;
}
&__item:nth-child(3) {
background-color: #767676;
transform: translateY(0) scale(0.9);
width: calc(100% - 60px);
z-index: 1980;
}
// Ignore
&--example {
left: 0;
top: calc(50% - 33px);
}
}
}
const NAME_ITEM = "el-toast",
ICONS = {
SUCCESS: {
CLASSES: ["fa", "fa-check-circle"]
},
DANGER: {
CLASSES: ["fa", "fa-exclamation-triangle"]
},
INFO: {
CLASSES: ["fa", "fa-exclamation-circle"]
},
ALERT: {
CLASSES: ["fa", "fa-exclamation-triangle"]
}
},
MOBILE_RESOLUTION = 720;
let $templateItem = document.querySelector("#elToastItem");
$templateItem = $templateItem.cloneNode(true).content;
class ToastItem extends HTMLLIElement {
constructor() {
super();
this._eventOnRemove =
new CustomEvent("TOAST_REMOVED") || document.createEvent("TOAST_REMOVED");
this.isVisible = false;
}
connectedCallback() {
let type = this.getAttribute("type").toUpperCase(),
description = this.getAttribute("description"),
timeout = Number(this.getAttribute("timeout")),
hideClose = Boolean(JSON.parse(this.getAttribute("hide-close")));
if (!ICONS.hasOwnProperty(type)) {
throw new Error(`[${NAME_ITEM}] Tipo inválido de toast`);
}
let $item = $templateItem.cloneNode(true),
$itemDescription = $item.querySelector(`.${NAME_ITEM}__description`),
$itemIcon = $item.querySelector(`.${NAME_ITEM}__icon`),
$icon = document.createElement("i"),
$itemProgress = $item.querySelector(`.${NAME_ITEM}__progress`),
$itemProgressBar = $item.querySelector(`.${NAME_ITEM}__bar`),
$itemClose = $item.querySelector(`.${NAME_ITEM}__close`),
$itemCloseAction = $item.querySelector(`.${NAME_ITEM}__close button`),
$container = this.parentElement.parentElement;
this.addEventListener("TOAST_REMOVED", this.checkVisible, false);
if (hideClose) {
$itemClose.remove();
} else {
$itemCloseAction.addEventListener("click", event =>
$container.remove(this.id)
);
}
$icon.classList.add(...ICONS[type].CLASSES);
$itemIcon.appendChild($icon);
this.classList.add(`${NAME_ITEM}__item`);
this.classList.add(`${NAME_ITEM}__item--${type.toLowerCase()}`);
if (description.length > 100) {
setShortDescription();
} else {
setFullDescription();
}
function setFullDescription(event) {
$itemDescription.innerText = description;
if (!event) return;
let viewLess = document.createElement("button");
viewLess.innerText = "View less";
viewLess.addEventListener("click", setShortDescription);
$itemDescription.appendChild(viewLess);
}
function setShortDescription() {
let viewMore = document.createElement("button");
viewMore.innerText = "View more";
viewMore.addEventListener("click", setFullDescription);
$itemDescription.innerText = description.slice(0, 99) + "...";
$itemDescription.appendChild(viewMore);
}
this.appendChild($item);
this.checkVisible();
if (timeout === 0) {
$itemProgress.classList.add(`${NAME_ITEM}__progress--hide`);
return;
}
let countdownValue = 100,
size = timeout - countdownValue;
let checking = setInterval(() => {
if (this.isVisible && !$container.isMouseOver()) {
$itemProgressBar.style.width =
size * 100 / (timeout - countdownValue) + "%";
size -= countdownValue;
}
if (size < -countdownValue) {
$container.remove(this.id);
clearInterval(checking);
}
}, countdownValue);
}
checkVisible() {
if (this.parentNode) {
let numCards = document.body.clientWidth > MOBILE_RESOLUTION ? 3 : 1;
this.isVisible =
Array.from(this.parentNode.children).indexOf(this) < numCards;
}
}
remove() {
let parent = this.parentElement;
this.classList.add(`${NAME_ITEM}__item--removing`);
if (
!parent ||
parent.nodeType !== Node.ELEMENT_NODE ||
!this ||
this.nodeType !== Node.ELEMENT_NODE
)
return false;
setTimeout(() => {
if (parent.hasChildNodes() && parent.contains(this)) {
parent.removeChild(this);
parent.parentElement.dispatchEvent(this._eventOnRemove);
return true;
}
return false;
}, 300);
}
setOnClose(action) {
let $itemCloseAction = this.querySelector(`.${NAME_ITEM}__close button`);
$itemCloseAction.addEventListener("click", action);
}
setActions(actions) {
if (!actions.length) {
return;
}
let $itemActions = this.querySelector(`.${NAME_ITEM}__action`);
actions.map(action => {
if (typeof action !== "object") {
throw new Error(`[${NAME_ITEM}] Ação inválida`);
return;
}
let $action = document.createElement("button");
if (typeof action.title !== "string") {
throw new Error(`[${NAME_ITEM}] Informe um título válido para a ação`);
}
$action.innerText = action.title;
if (typeof action.onClick !== "function") {
throw new Error(`[${NAME_ITEM}] Informe um função válida para a ação`);
}
$action.addEventListener("click", action.onClick);
$itemActions.appendChild($action);
});
}
static is() {
return `${NAME_ITEM}-item`;
}
}
customElements.define(ToastItem.is(), ToastItem, { extends: "li" });
const NAME = "el-toast",
TIMEOUT_REMOVE = 8000,
DOM_LIMIT = 5;
let $template = document.querySelector("#elToast");
$template = $template.cloneNode(true).content.querySelector(`.${NAME}`);
class Toast extends HTMLElement {
constructor() {
super();
this._eventOnRemove =
new CustomEvent("TOAST_REMOVED") || document.createEvent("TOAST_REMOVED");
this._mouseOver = false;
this._list = {};
this._listCache = {};
}
isMouseOver() {
return this._mouseOver;
}
setMouseOver(value) {
this._mouseOver = Boolean(value);
}
connectedCallback() {
this.appendChild($template);
this.addEventListener("TOAST_REMOVED", broadcastToastRemoved, false);
this.addEventListener("mouseenter", handleMouseOver);
this.addEventListener("touchstart", handleMouseOver);
this.addEventListener("touchend", handleMouseOut);
document.body.addEventListener("touchend", handleMouseOut);
this.addEventListener("mouseleave", handleMouseOut);
function handleMouseOver() {
$template.classList.add(`${NAME}--paused`);
this.setMouseOver(true);
}
function handleMouseOut() {
$template.classList.remove(`${NAME}--paused`);
this.setMouseOver(false);
}
function broadcastToastRemoved() {
Object.values(this._list)
.slice(0, 5)
.map(toast => {
toast.dispatchEvent(this._eventOnRemove);
});
}
}
publish(config) {
let type = String(config.type || "info"),
description = String(config.description || ""),
timeout =
config.timeout < 0 || typeof config.timeout !== "number"
? TIMEOUT_REMOVE
: config.timeout,
onClose =
typeof config.onClose === "function" ? config.onClose : function() {},
hideClose = Boolean(config.hideClose),
actions = typeof config.actions === "object" ? config.actions : [],
$toast = document.createElement("li", { is: `${NAME}-item` }),
id = "toast_" + generateId();
if (!description) {
throw new Error(`[${NAME}] É necessário informar uma descrição`);
}
$toast.id = id;
$toast.setAttribute("type", type);
$toast.setAttribute("description", description);
$toast.setAttribute("timeout", timeout);
$toast.setAttribute("hide-close", hideClose);
$toast = $toast.cloneNode(true);
if (Object.values(this._list).length > DOM_LIMIT) {
this._listCache[id] = {
el: $toast,
hideClose: hideClose,
onClose: onClose,
actions: actions
};
} else {
$toast = $template.appendChild($toast);
if (!$toast) {
return;
}
if (!hideClose) {
$toast.setOnClose(onClose);
}
if (actions.length) {
$toast.setActions(actions);
}
this._list[id] = $toast;
}
function generateId() {
return Math.random()
.toString(36)
.substr(2, 9);
}
return id;
}
remove(id) {
if (this._list.hasOwnProperty(id)) {
this._list[id].remove();
delete this._list[id];
} else if (this._listCache.hasOwnProperty(id)) {
delete this._listCache[id];
} else {
return;
}
if (Object.keys(this._listCache).length) {
let keyCached = Object.keys(this._listCache)[0],
valueCached = Object.values(this._listCache)[0];
valueCached.el = $template.appendChild(valueCached.el);
if (!valueCached.hideClose) {
valueCached.el.setOnClose(valueCached.onClose);
}
if (valueCached.actions.length) {
valueCached.el.setActions(valueCached.actions);
}
this._list[keyCached] = valueCached.el;
delete this._listCache[keyCached];
}
}
static is() {
return NAME;
}
}
customElements.define(Toast.is(), Toast);
let toast = document.querySelector("el-toast");
for (let i = 0; i < 100; i++) {
toast.publish({
type: "success",
description:
i +
" Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.",
timeout: 8000
});
toast.publish({
type: "danger",
description:
i +
" Lorem Ipsum is simply dummy text of the printing and typesetting industry.",
timeout: 8000
});
toast.publish({
type: "info",
description: i + " Lorem Ipsum is simply.",
timeout: 0,
actions: [
{
title: "OK",
onClick: function() {
alert("OK");
}
}
]
});
let idToast = undefined;
idToast = toast.publish({
type: "info",
description:
i +
" Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.Lorem Ipsum is simply dummy text of the printing and typesetting industry.",
timeout: 0,
hideClose: true,
actions: [
{
title: "OK",
onClick: function() {
toast.remove(idToast);
}
}
]
});
toast.publish({
type: "info",
description: i + " Lorem Ipsum is simply.",
timeout: 20000
});
toast.publish({
type: "info",
description: i + " Lorem Ipsum is simply.",
timeout: 20000,
actions: [
{
title: "Try again",
onClick: function() {
alert("Try again");
}
},
{
title: "Ignore",
onClick: function() {
alert("Ignore");
}
},
{
title: "Custom",
onClick: function() {
alert("Custom Button");
}
}
]
});
toast.publish({
type: "alert",
description:
i +
" Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book."
});
toast.publish({
type: "alert",
description:
i +
" Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book"
});
}
Also see: Tab Triggers