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.
<center id="react">
<div id="root"></div>
<div id="centered-container">
<br>
<div id="top">
<div id="semiCircle" class="semiCircle"></div>
<div id="semiCircle2" class="semiCircle"></div>
</div>
<div id ="middle">
<div id="circle2" class="circle"></div>
<div id="circle" class="circle">
<!-- renders the current time use {formattedMinutes} : {formattedSeconds} in React-->
<div id="time-left">6 : 00</div>
</div>
</div>
<div id="bottom">
<div id="leg" class="leg"></div>
</div>
<div id="leg2" class="leg"></div> <!-- leave this out of bottom, otherwise you will face skewing problems (skew of one element will influence the other element)-->
</div>
<div id="bottomRoot"></div>
</center>
#root {
margin-top: 50px;
}
#bottomRoot {
margin-top: 50px;
margin-bottom: 50px;
}
#centered-container {
/*background-color: green;*/
width: 500px;
}
/*this class is only rendered when the time on the clock is below 1 minute*/
.red {
color: red;
}
/*
#top {
height: 100px;
width: ;
background-color: black;
}*/
.semiCircle {
height: 150px;
width: 150px;
border-radius: 50%;
}
#semiCircle {
margin-right: 200px;
margin-top: 50px;
background: linear-gradient(135deg, grey 50%, white 50%);
}
#semiCircle2 {
margin-top: -150px;
margin-left: 200px;
background: linear-gradient(225deg, grey 50%, white 50%);
}
#middle {
margin-top: -100px;
}
.circle {
border-radius: 50%;
}
#circle {
height: 200px;
width: 200px;
background-color: white;
margin-top: -225px;
}
#circle2 {
height: 250px;
width: 250px;
background-color: grey;
}
#time-left {
padding-top: 70px; /*50px works with font-size 24px*/
font-size: 50px;
}
#leg {
margin-left: 275px; /*margin-right on leg2 + 2 * width*/
width: 100px;
height: 50px;
border-left: 30px solid grey;
transform: skew(45deg);
}
#leg2 {
margin-top: -50px;
margin-right: 75px;
width: 100px;
height: 50px;
border-left: 30px solid grey;
transform: skew(-45deg);
}
//this component displays the time of the clock
class Clock extends React.Component {
constructor(props) {
super(props);
this.state = {
minutes: this.props.startTime, //pass in the initial time from the SetTime component, alternatively just set equal to 25
seconds: 0,
running: "off",
break: 5,
current: "session"
};
this.handleClick = this.handleClick.bind(this);
this.incrementTime = this.incrementTime.bind(this);
this.decrementTime = this.decrementTime.bind(this);
this.resetTime = this.resetTime.bind(this);
this.incrementBreak = this.incrementBreak.bind(this);
this.decrementBreak = this.decrementBreak.bind(this);
}
//creates a pause/play button
handleClick() {
if (this.state.running == 'off') {
this.setState((state) => ({
running: 'on'
}));
} else {
this.setState((state) => (
{running: 'off'}));
}
}
//increase the number of minutes the clock will run for, on button click
incrementTime() {
if (this.state.minutes < 59 && this.state.running == "off") { //the second part of this if statement is optional, you can remove it so that the code will be able to increase the time allotted while running (which may be more functional). If altering make sure to adjust all increment/decrement methods
this.setState((state) => ({
minutes: state.minutes + 1
}));
}
}
decrementTime() {
if (this.state.minutes > 1 && this.state.running == "off") {
this.setState((state) => ({
minutes: state.minutes - 1
}));
}
}
incrementBreak() {
if (this.state.break < 59 && this.state.running == "off") {
this.setState((state) => ({
break: state.break + 1
}));
}
}
decrementBreak() {
if (this.state.break > 1 && this.state.running == "off") {
this.setState((state) => ({
break: state.break - 1
}));
}
}
resetTime() {
this.setState((state) => ({
minutes: 25,
seconds: 0,
running: "off",
current: "session"
}));
}
//re-renders the entire component every second, to count down the time
componentDidMount() {
this.timerID = setInterval(
() => this.tick(),
1000
);
}
componentWillUnmount() {
clearInterval(this.timerID);
}
tick() {
//add audio here later
//var audio = new Audio('audio_file.mp3');
//audio.play();
//reduces seconds
if (this.state.seconds != 0 && this.state.running == 'on') {
this.setState((state) => ({
seconds: state.seconds - 1
}));
}
//reduce minutes
if (this.state.seconds == 0 && this.state.minutes != 0 && this.state.running == 'on') {
this.setState((state) => ({
minutes: state.minutes - 1,
seconds: 59
}));
}
//make the code enter break mode when the session time hits zero
if (this.state.seconds == 0 && this.state.minutes == 0 && this.state.current == "session") {
this.setState((state) => ({
minutes: this.state.break,
current: "break"
}));
}
//make the code enter session mode when the break time hits zero
if (this.state.seconds == 0 && this.state.minutes == 0 && this.state.current == "break") {
this.setState((state) => ({
minutes: 25,
current: "session"
}));
}
}
render() {
//this formatting code helps to render times below 10 seconds, so that they are still formatted like this "06 : 05" rather than "6 : 5"
let formattedSeconds = "";
if (this.state.seconds < 10) {
formattedSeconds = "0" + String(this.state.seconds);
} else {
formattedSeconds = String(this.state.seconds);
}
let formattedMinutes = "";
if (this.state.minutes < 10) {
formattedMinutes = "0" + String(this.state.minutes);
} else {
formattedMinutes = String(this.state.minutes);
}
//its worth noting that for this project, I developed the project without meeting some of the specific code requirements that would have made the code worse. I skipped separating the certain lines of text from their labels, and avoided adding some uncessary IDs. Requirements 5-7, 22 worsen the project. 22 would be beneficial if I displayed whether we are currently in break or session --> to alter this simply add a variable to the return and if a conditional is met (i.e. time hits zero), swap the variable. Then use this variable to display the current string (session or break) & go into the tick method and add an additional conditional to evaluate the current break/session to alternate the two. I also didn't bother adding the audio, because to be honest I didn't feel like doing it. I might add it later though.
return (
<div>
<div id="root">
<h2>On/Off: {this.state.running}</h2>
<button id="reset" onClick={this.handleClick}>Pause/Play</button>
<h2 id="session-label">Session Length: {formattedMinutes}</h2>
<button id="session-increment" onClick={this.incrementTime}>Increase Time</button>
<button id="session-decrement" onClick={this.decrementTime}>Decrease Time</button>
<br />
<button onClick={this.resetTime}>Reset Button</button>
</div>
<div id="centered-container">
<br />
<div id="top">
<div id="semiCircle" class="semiCircle"></div>
<div id="semiCircle2" class="semiCircle"></div>
</div>
<div id ="middle">
<div id="circle2" class="circle"></div>
<div id="circle" class="circle">
{this.state.minutes >= 1 && this.state.current == "session" ? (
<div id="time-left">{formattedMinutes} : {formattedSeconds}</div>
) : (
<div id="time-left" class="red">{formattedMinutes} : {formattedSeconds}</div>
)}
</div>
</div>
<div id="bottom">
<div id="leg" class="leg"></div>
</div>
<div id="leg2" class="leg"></div>
</div>
<div id="bottomRoot">
<h2 id="break-label">Break Length: {this.state.break}</h2>
<button id="break-increment" onClick={this.incrementBreak}>Increase Break</button>
<button id="break-decrement" onClick={this.decrementBreak}>Decrease Break</button>
</div>
</div>
);
}
}
//run the Clock component
ReactDOM.render(
<Clock startTime={25}/>,
document.getElementById('react')
);
Also see: Tab Triggers