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.
<body class="bg-indigo-dark p-5 font-mono">
<div id="root" class="max-w-5xl mx-auto"></div>
</body>
body {
&:before {
display: block;
content: "";
top: 0; left: 0;
width: 100%;
height: 60%;
background: rgba(0, 0, 0, 0.1);
position: absolute;
transform: skewY(-12deg);
transform-origin: 0;
z-index: -1;
}
}
const useState = React.useState;
const useEffect = React.useEffect;
let MAIL_DATA = [
{
title: "Does this app looks good?",
sender: "Unknown User <user@mail.com>",
date: Date.now() - 10000,
read: false,
content: 'Guys, I found this app called Google https://google.com.\n\nHas anyone used it yet? How does it compared to DuckDuckGo?'
},
{
title: "New comment on thefullsnack.com",
sender: "Unknown User <user@mail.com>",
date: Date.now() - 20000,
read: false,
content: 'Huy has left a new comment on thefullsnack.com:\n\n> "Wow, what a great article!!! Thanks dude!"'
},
{
title: "Want to try a new mail app?",
sender: "Huy <huy@mail.com>",
date: Date.now(),
read: true,
selected: true,
content: 'Hi all,\n\nI\'ve published my new email client called nocrapinbox and it\'s open sourced as well!\n\nIf you\'re interested, please check my repo.\n\nThanks,\nHuy'
},
{
title: "Your Amazon order has been shipped",
sender: "Unknown User <user@mail.com>",
date: Date.now() - 20000,
read: true,
content: 'Your order has been shipped.'
},
{
title: "Re: Does this app looks good?",
sender: "Unknown User <user@mail.com>",
date: Date.now() - 10000,
read: true,
content: 'You must be from the moon.'
},
{
title: "New comment on thefullsnack.com",
sender: "Unknown User <user@mail.com>",
date: Date.now() - 20000,
read: false,
content: 'Huy has replied to a comment on thefullsnack.com:\n\n> "Thanks dude!"'
},
{
title: "Re: Your Amazon order has been shipped",
sender: "Unknown User <user@mail.com>",
date: Date.now() - 20000,
read: true,
content: 'Your order has been shipped.'
}
];
const formatDateTime = time => {
let parsed = new Date(time);
return `${parsed.toLocaleString()}`;
};
const formatEmailContent = str => {
return str.replace(/\n/g, '<br/>');
};
const formatEmailReplyContent = str => {
return str.split("\n").map(line => "> " + line).join("\n");
};
const readStyle = read => read ? 'text-grey' : 'font-semibold';
const selectedStyle = (selected, index) => (selected === index) ? 'text-white bg-purple-light' : '';
const markedAsDeletedStyle = marked => marked ? 'line-through text-red' : '';
const EmptyInboxItem = props => {
if (props.count) return null;
return <tr>
<td colSpan="4" className="text-center bg-grey-lightest p-5">Wow! You emptied your inbox!</td>
</tr>;
};
const InboxItem = props => {
return <tr className={`bg-white text-left ${readStyle(props.email.read)} ${selectedStyle(props.selected, props.index)} ${markedAsDeletedStyle(props.email.deleted)}`}>
<td className="p-2 w-1/5">{formatDateTime(props.email.date)}</td>
<td className="p-2">{props.email.sender.split(/\s</)[0]}</td>
<td className="p-2">{props.email.title}</td>
</tr>;
};
const EmailContent = props => {
if (!props.email || !props.email.content) return null;
return <div className="p-2">
<p><span className="font-semibold">Date:</span> {formatDateTime(props.email.date)}</p>
<p><span className="font-semibold">From:</span> {props.email.sender}</p>
<p><span className="font-semibold">Subject:</span> {props.email.title}</p>
<p></p>
<p className="mt-5" dangerouslySetInnerHTML={{__html: formatEmailContent(props.email.content)}}></p>
</div>;
};
let emailReplyTextArea = null;
const EmailReply = props => {
if (!props.isReply) return null;
return <div className="mt-3">
<textarea className="w-full p-2 resize-none h-64"
ref={(input) => { emailReplyTextArea = input; }}>
{"\n\n" + formatEmailReplyContent(props.email.content)}
</textarea>
</div>;
};
const Inbox = props => {
const [selectedIndex, setSelectedIndex] = useState(0);
const [isReplyState, setReplyState] = useState(false);
const increase = val => val + 1;
const decrease = val => val - 1;
const getSelectedEmail = () => {
if (selectedIndex < MAIL_DATA.length)
return MAIL_DATA[selectedIndex];
return null;
};
const selectFirst = () => {
setSelectedIndex(0);
};
const selectNext = () => {
if (selectedIndex < MAIL_DATA.length-1) {
setSelectedIndex(increase(selectedIndex));
}
};
const selectPrev = () => {
if (selectedIndex > 0) {
setSelectedIndex(decrease(selectedIndex));
}
};
const toggleReadStatus = () => {
getSelectedEmail().read = !getSelectedEmail().read;
};
const toggleDeleteStatus = () => {
getSelectedEmail().deleted = !getSelectedEmail().deleted;
};
const deleteAllMarked = () => {
MAIL_DATA = MAIL_DATA.filter(m => !m.deleted);
};
const startReplying = () => {
setReplyState(true);
if (emailReplyTextArea) {
emailReplyTextArea.focus();
}
};
const stopReplying = () => {
setReplyState(false);
};
useEffect(() => {
const onKeyPress = e => {
if (!isReplyState) {
switch (e.key) {
case "j":
selectNext();
stopReplying()
break;
case "k":
selectPrev();
stopReplying()
break;
case "m":
toggleReadStatus();
selectNext();
stopReplying()
break;
case "d":
toggleDeleteStatus();
selectNext();
stopReplying()
break;
case "X":
deleteAllMarked();
selectFirst();
stopReplying()
break;
case "r":
startReplying();
break;
}
} else {
switch (e.key) {
case "Escape":
stopReplying();
break;
}
}
};
window.addEventListener('keypress', onKeyPress);
return function cleanup() {
window.removeEventListener('keypress', onKeyPress);
};
});
return <div>
<div className="text-white text-xl font-medium mb-2">Inbox</div>
<div className="bg-grey-lightest rounded-lg shadow-lg overflow-hidden text-xs">
<table className="bg-grey-light w-full">
<tr className="text-left">
<th className="p-2">Date</th>
<th className="p-2">From</th>
<th className="p-2">Subject</th>
</tr>
<tbody>
{ MAIL_DATA.map((email, idx) => <InboxItem index={idx} key={idx} selected={selectedIndex} email={email}/>) }
<EmptyInboxItem count={MAIL_DATA.length} />
</tbody>
</table>
<EmailContent email={getSelectedEmail()}/>
<EmailReply email={getSelectedEmail()} isReply={isReplyState}/>
</div>
<div className="text-xs text-indigo-lighter mt-3">j / k: move up or down<br/>r: reply - esc: exit reply mode<br/>d: mark as deleted - X: delete all marked<br/>m: mark as read or unread<br/>/: search</div>
</div>;
};
ReactDOM.render(<Inbox/>, document.querySelector("#root"));
Also see: Tab Triggers