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>
<div id="root"></div>
</body>
$theme-bg: darkslateblue;
$theme-primary: honeydew;
$theme-secondary: gold;
$color-active: pink;
$color-inactive: brown;
$color-off: #222;
$size-clock: 80px;
$size-control-label: 36px;
$size-control-number: 40px;
$font-digital: 'Source Code Pro', monospace;
$font-main: 'Gamja Flower', serif;
/* Page Setup */
html {
overflow: hidden;
}
body {
background-color: $theme-bg;
}
#root {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
/* Components */
#App {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
height: 100vh;
user-select: none;
}
//Title
.Title {
font-family: $font-main;
font-size: 80px;
color: $color-off;
}
//Clock
.Clock-container {
height: 200px;
width: 350px;
border-radius: 50px;
border: 6px solid $theme-primary;
padding: 10px;
margin-top: 20px;
display: flex;
flex-direction: column;
justify-content: space-between;
align-items: center;
color: $theme-primary;
font-family: $font-digital;
background-color: rgba(0,0,0,0.2);
transition: box-shadow 500ms;
}
.Clock-container-on{
box-shadow: inset 0 0 20px $color-active;
}
.Clock-display {
font-size: $size-clock;
padding-bottom: 5px;
line-height: $size-clock;
letter-spacing: 5px;
}
.Clock-mode {
font-size: 20px;
font-weight: bold;
letter-spacing: 2px;
color: $color-off;
transition: 500ms;
}
.Clock-mode-inactive {
text-shadow: 0 0 1px $theme-secondary;
}
.Clock-mode-on {
color: $theme-secondary;
text-shadow: 0 0 3px $theme-primary;
}
//Control
#Control-box {
padding: 4px;
border-top: none;
display: flex;
flex-direction: row;
justify-content: center;
}
.Control-button {
height: 30px;
width: 30px;
color: $theme-primary;
display: flex;
justify-content: center;
align-items: center;
font-size: 20px;
cursor: pointer;
margin: 2px;
&:hover {
color: $theme-secondary;
}
}
//Settings
.Settings-container {
display: flex;
flex-direction: row;
justify-content: space-around;
align-items: center;
padding: 20px;
font-family: $font-main;
}
.Settings-node {
display: flex;
flex-direction: row;
align-items: center;
font-size: $size-control-label;
color: $theme-primary;
margin: 10px 40px;
text-align: center;
}
.Settings-label {
width: 150px;
}
.Settings-node:hover .Settings-label {
color: $theme-secondary;
}
.Settings-knob {
display: flex;
flex-direction: column;
align-items: center;
}
.Settings-knob-arrow{
height: 0;
width: 0;
border-right: 15px solid transparent;
border-left: 15px solid transparent;
cursor: pointer;
}
.arrow-up{
border-top: none;
border-bottom: 12px solid $theme-primary;
}
.arrow-down{
border-top: 12px solid $theme-primary;
border-bottom: none;
}
.Settings-knob-arrow:hover {
border-bottom-color: $theme-secondary;
border-top-color: $theme-secondary;
}
.Settings-knob-value {
font-family: $font-main;
font-size: $size-control-number;
}
const SESSION = 'session';
const BREAK = 'break';
function requestNotificationPermission(appPermissions) {
console.log('init request code')
// Check if the browser supports notifications
if (!("Notification" in window)) {
return;
}
// Check whether notification permissions have already been granted
else if (Notification.permission === "granted") {
appPermissions = true;
}
// Otherwise, we need to ask the user for permission
else if (Notification.permission !== "denied") {
Notification.requestPermission().then(function (permission) {
// If the user accepts, let's create a notification
appPermissions = permission === "granted"
});
}
}
class App extends React.Component {
constructor(props){
super(props);
this.state = {
reset: true,
on: false,
settings: {
session: 20,
break: 5
},
time: {
min: 20,
sec: 0
},
mode: SESSION,
permissions: {
notifications: false
}
};
this.setTime = this.setTime.bind(this);
this.play = this.play.bind(this);
this.pause = this.pause.bind(this);
this.changeMode = this.changeMode.bind(this);
this.reset = this.reset.bind(this);
this.tick = this.tick.bind(this);
requestNotificationPermission(this.state.permissions.notifications);
}
setTime(mode, inc){
//update = the new break/session time
let update = Object.assign({}, this.state.settings);
update[mode] = update[mode] + inc;
var time = Object.assign({}, this.state.time);
if (update[mode] < 1) update[mode] = 1;
else if (update[mode] > 60) update[mode] = 60;
//if the clock is in reset mode, the clock time will mirror the settings
if (this.state.reset){
if (this.state.mode == mode) {
time.min = update[mode];
time.sec = 0;
}
}
this.setState({
settings: update,
time: time
});
}
play(){
if (!this.intervalID) this.intervalID = setInterval(()=>{this.tick()}, 1000);
this.setState({
on: true,
reset: false
});
}
pause(){
if (this.intervalID) {
clearInterval(this.intervalID);
this.intervalID = null;
}
this.setState({
on: false
});
}
changeMode(){
if (this.intervalID) {
clearInterval(this.intervalID);
this.intervalID = setInterval(()=>{this.tick()}, 1000);
}
var mode;
if (this.state.mode == SESSION){
mode = BREAK;
} else {
mode = SESSION;
}
var time = {};
time.min = this.state.settings[mode];
time.sec = 0;
// notification
console.log(this.state.permissions.notifications);
if (this.state.permissions.notifications){
var notifText;
if (mode == 'session') {
notifText = 'work'
} else {
notifText = mode
}
new Notification(`It's ${notifText} time!`)
}
this.setState({
mode: mode,
time: time
});
}
reset(){
if (this.intervalID) {
clearInterval(this.intervalID);
this.intervalID = null;
}
let time = {
min: 0,
sec: 0
};
if (this.state.mode == SESSION) time.min = this.state.settings.session;
else time.min = this.state.settings.break;
this.setState({
on: false,
reset: true,
time: time
});
}
tick(){
let time = Object.assign({}, this.state.time);
var mode = this.state.mode.slice();
time.sec = time.sec - 1;
if (time.sec < 0) {
if (time.min > 0){
time.min = time.min - 1;
time.sec = 59;
} else {
if (mode == SESSION){
mode = BREAK;
} else {
mode = SESSION;
}
time.min = this.state.settings[mode];
time.sec = 0;
}
}
this.setState({
time: time,
mode: mode
});
}
render(){
return(
<div id='App'>
<div className='Title'>pomodoro clock</div>
<ClockContainer
time={this.state.time}
mode={this.state.mode}
on={this.state.on}
onStyle={this.state.on? ' Clock-container-on' : ''}
/>
<Control
on={this.state.on}
play={this.play}
pause={this.pause}
changeMode={this.changeMode}
reset={this.reset}
/>
<Settings
session={this.state.settings.session}
break={this.state.settings.break}
setTime={this.setTime}
/>
</div>
);
}
}
// Clock display
class ClockContainer extends React.Component {
constructor(props){
super(props);
}
digitize(num){
if (num < 10){
return '0' + num;
} else {
return num.toString();
}
}
render(){
return(
<Clock
onStyle={this.props.onStyle}
min={this.digitize(this.props.time.min)}
sec={this.digitize(this.props.time.sec)}
workClass={
this.props.mode != SESSION ?
'' : this.props.on ?
' Clock-mode-on' : ' Clock-mode-inactive'}
breakClass={
this.props.mode == BREAK ?
(this.props.on ?
' Clock-mode-on' : ' Clock-mode-inactive')
: ''}
/>
);
}
}
class Clock extends React.Component {
constructor(props){
super(props);
}
render(){
return(
<div
id='Clock'
className={'Clock-container' + this.props.onStyle}>
<div
id='Clock-work'
className={'Clock-mode' + this.props.workClass}
>WORK</div>
<div className='Clock-display'>
{this.props.min}:{this.props.sec}
</div>
<div
id='Clock-break'
className={'Clock-mode' + this.props.breakClass}
>BREAK</div>
</div>
);
}
}
// Control buttons
class Control extends React.Component {
constructor(props){
super(props);
}
render(){
return(
<div id='Control-box'>
<ControlButton
name='play'
icon={this.props.on ? 'fa fa-pause' : 'fa fa-play'}
caption={this.props.on ? 'Pause' : 'Play'}
function={this.props.on ? this.props.pause : this.props.play}
/>
<ControlButton
name='mode'
icon='fa fa-random'
caption='Change mode'
function={this.props.changeMode}
/>
<ControlButton
name='reset'
icon='fa fa-undo-alt'
caption='Reset'
function={this.props.reset}
/>
</div>
);
}
}
class ControlButton extends React.Component {
constructor(props){
super(props);
}
render(){
return(
<div
id={'Control-button-' + this.props.name}
className='Control-button'
title={this.props.caption}
onClick={this.props.function}>
<i className={this.props.icon} />
</div>
);
}
}
// Settings
class Settings extends React.Component {
constructor(props){
super(props);
}
render(){
return(
<div
id='Settings'
className='Settings-container'>
<SettingsNodeContainer
side='left'
name='session'
value={this.props.session}
function={this.props.setTime}
/>
<SettingsNodeContainer
side='right'
name='break'
value={this.props.break}
function={this.props.setTime}
/>
</div>
);
}
}
class SettingsNodeContainer extends React.Component {
constructor(props){
super(props);
this.handleWheel = this.handleWheel.bind(this);
}
colorDegree(num){
let hue = num*6-1;
return {
color: 'hsl('+ hue +',90%,70%)'
};
}
handleWheel(e){
this.props.function(this.props.name, Math.abs(e.deltaY)/(-e.deltaY));
}
render(){
let label =
<div id={this.props.name + '-label'}
className='Settings-label'>
{this.props.name}
</div>;
return(
<SettingsNode
name={this.props.name}
left={this.props.side == 'left' ? label : null}
right={this.props.side == 'right' ? label : null}
value={this.props.value}
colorDegree={this.colorDegree(this.props.value)}
increment={()=>this.props.function(this.props.name, 1)}
decrement={()=>this.props.function(this.props.name, -1)}
handleWheel={this.handleWheel}
/>
);
}
}
class SettingsNode extends React.Component {
constructor(props){
super(props);
}
render(){
return(
<div
id={'Settings-' + this.props.name}
className='Settings-node'
onWheel={this.props.handleWheel}>
{this.props.left}
<div className='Settings-knob'>
<div
id={this.props.name + '-increment'}
onClick={this.props.increment}
className='Settings-knob-arrow arrow-up'
/>
<div
className='Settings-knob-value'
style={this.props.colorDegree}>
{this.props.value}
</div>
<div
id={this.props.name + '-decrement'}
onClick={this.props.decrement}
className='Settings-knob-arrow arrow-down'
/>
</div>
{this.props.right}
</div>
);
}
}
ReactDOM.render(<App />, document.getElementById('root'));
Also see: Tab Triggers