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 id="app"></div>
html {
font-family: 'Orbitron', sans-serif;
box-sizing: border-box;
}
#app {
/* height: 100vh; */
display: flex;
align-items: center;
justify-content: center;
}
.console {
width: 350px;
background-color: black;
margin-top: 100px;
}
.title {
color: white;
text-align: center;
}
.title h2 {
margin: 10px auto;
}
.ops {
font-family: 'Audiowide', monospace;
font-size: 14px;
min-height: 22px;
color: orange;
padding: 4px 8px 2px 4px;
text-align: right;
word-break: break-all;
}
.display-out {
font-family: 'ZCOOL QingKe HuangYou', monospace;
font-size: 1.8em;
min-height: 36px;
color: white;
padding: 2px 8px;
background-color: #0B0C10;
text-align: right;
}
.keyboard {
display: grid;
grid-template-columns: auto auto auto auto;
justify-content: center;
align-content: start;
}
.key {
font-family: 'Orbitron', sans-serif;
border: none;
border-radius: 8px;
color: white;
background-color: #1f2833;
width: 83px;
height: 50px;
text-align: center;
text-decoration: none;
font-size: 16px;
margin: 2px 2px;
cursor: pointer;
}
.key:active {
background-color: #45A29E;
}
.long {
grid-column: 1 / span 2;
width: 170px;
background-color: #64485C;
}
.long.zero {
background-color: #1f2833;
}
.tall {
grid-row: 4 / span 2;
grid-column: 4;
height: 105px;
background-color: #45A29E;
}
.tall:active {
background-color: #C5C6C7;
}
.scaleUp {
font-size: 1.5em
}
.scaleDown {
font-size: 0.8em;
color: #66FCF1;
}
.greyKeys {
background-color: #C5C6C7;
color: black;
}
/* History Section */
.historyBtn {
background-color: black;
color: white;
cursor: pointer;
padding: 10px;
border: none;
border-radius: 8px;
position: relative;
z-index:10;
outline: none;
font-size: 12px;
transition: 0.4s;
margin: 4px ;
}
.active,
.historyBtn:hover {
background-color: #64485C;
}
.panel {
padding: 0 1px 0 5px;
background-color: black;
width: 344px;
overflow: hidden;
position:relative;
z-index:-1;
}
.panel.slidedown{
display:block;
opacity:0;
animation: slideDown 0.5s ease-out forwards;
}
.panel.slideup{
opacity:1;
animation: slideUp 0.5s ease-out forwards;
}
#log {
background-color: black;
/* font-family: Helvetica, monospace; */
font-family: 'DM Sans', sans-serif;
font-size: 0.8em;
min-height: 22px;
color: white;
word-break: break-all;
}
.custom-counter {
margin: 0;
padding: 0;
list-style-type: none;
}
.custom-counter li {
counter-increment: step-counter;
margin-bottom: 5px;
}
.custom-counter li::before {
content: counter(step-counter)" )";
margin-right: 5px;
font-size: 90%;
color: #8c8c8c;
}
.rotate {
-moz-transition: all 0.5s linear;
-webkit-transition: all 0.5s linear;
transition: all 0.5s linear;
}
.rotate.down {
-moz-transform: rotate(180deg);
-webkit-transform: rotate(180deg);
transform: rotate(180deg);
}
@keyframes slideDown {
0% {
transform: translateY(-100%);
}
100% {
transform: translateY(0);
opacity: 1;
}
}
@keyframes slideUp {
0% {
transform: translateY(0);
}
100% {
transform: translateY(-100%);
opacity: 0;
}
}
const keyButtons = [
{name: "clear", clsName: "key long", value: "AC", visual: "AC"},
{name: "divide", clsName: "key greyKeys", value: "/", visual: "/"},
{name: "multiply", clsName: "key greyKeys", value: "x", visual: "x"},
{name: "seven", clsName: "key", value: 7, visual: 7},
{name: "eight", clsName: "key", value: 8, visual: 8},
{name: "nine", clsName: "key", value: 9, visual: 9},
{name: "subtract", clsName: "key scaleUp greyKeys", value: "-", visual: "-"},
{name: "four", clsName: "key", value: 4, visual: 4},
{name: "five", clsName: "key", value: 5, visual: 5},
{name: "six", clsName: "key", value: 6, visual: 6},
{name: "add", clsName: "key scaleUp greyKeys", value: "+", visual: "+"},
{name: "one", clsName: "key", value: 1, visual: 1},
{name: "two", clsName: "key", value: 2, visual: 2},
{name: "three", clsName: "key", value: 3, visual: 3},
{name: "equals", clsName: "key tall scaleUp", value: "=", visual: "="},
{name: "zero", clsName: "key long zero", value: 0, visual: 0},
{name: "decimal", clsName: "key scaleUp", value: ".", visual: "."},
];
function History (props) {
return(
<div id="equation" className="ops" >
{props.value}
</div>
)
}
function Key (props) {
return (
<button
id={props.name}
className={props.clsName}
onClick={(props.onClick)}
value={props.value}>
{props.visual}
</button>
);
}
class Keys extends React.Component {
constructor(props) {
super(props);
}
render() {
const arrKeyBtns = keyButtons.map((item)=>{
return(
<Key
name={item.name}
clsName={item.clsName}
onClick={this.props.onClick}
value={item.value}
visual={item.visual}
/>
);
});
return (
<div className="keyboard">
{arrKeyBtns}
</div>
);
}
}
//------- Calculator class -------------------------
class Calculator extends React.Component {
constructor(props) {
super(props);
this.state = { total: 0,
numPressed: "",
currNumber: "",
preNumber: "",
negNumber: "",
opPressed: "clear",
currEquation: "",
history: [],
operate: false,
evaled: false,
};
this.handleKeyPress = this.handleKeyPress.bind(this);
}
handleKeyPress(e) {
let val;
if(e.type === "click") {
val = e.target.value;
} else if(e.type === "keydown") {
val = e.key;
}else{return; }
let keyVal = val;
let displayStr = display.textContent;
let eqStr = equation.textContent;
let dec = /\.+/g;
let theEquation = "i am number 4";
let totalValue;
let stTotal = this.state.total;
let stNumPressed = this.state.numPressed;
let stCurrNumber = this.state.currNumber;
let stPreNumber = this.state.preNumber;
let stNegNumber = this.state.negNumber;
let stOpPressed = this.state.opPressed;
let stCurrEquation = this.state.currEquation;
let stHistory = this.state.history.slice(0);
let stOperate = this.state.operate;
let stEvaled = this.state.evaled;
switch(val) {
case "Enter":
case "=":
//Do Not Allow double evals
if(this.state.operate &&
(this.state.opPressed === "Enter" || this.state.opPressed === "="))
{return;}
//Is last Num a Neg?
if(this.state.opPressed === "Neg"){
//Get Total
stTotal = this.evalEquation(stCurrEquation + "-" + stNegNumber);
stCurrEquation += "-" + stNegNumber + "=" + stTotal;
//HUD
eqStr = stCurrEquation.replace(/\s/g, "");
displayStr = stTotal;
//State
(Array.isArray(stHistory)) ?
stHistory.push(stCurrEquation) :
stHistory = [stCurrEquation];
stCurrEquation = stTotal;
stEvaled = true;
stOperate = true;
stOpPressed = keyVal;
stPreNumber = "-" + stNegNumber;
stNegNumber = "";
stNumPressed = "";
}else{
//State
stTotal = this.evalEquation(stCurrEquation);
stCurrEquation += "=" + stTotal;
(Array.isArray(stHistory)) ?
stHistory.push(stCurrEquation) :
stHistory = [stCurrEquation];
stOperate = true;
stEvaled = true;
stOpPressed = keyVal;
stCurrEquation = stTotal;
stPreNumber = stCurrNumber;
stCurrNumber = "";
//HUD
eqStr += keyVal + stTotal;
displayStr = stTotal;
}
break;
case ".":
if(this.state.evaled || this.state.opPressed === "clear" ){ // <--- prev keyPress was = or AC
//HUD
eqStr = "0.";
displayStr = "0.";
//State
stCurrEquation = "0.";
stCurrNumber = "0.";
stOpPressed = "";
stEvaled = false;
} else if(this.state.operate){ // <--- prev keyPress was operator
eqStr += "0."
displayStr = "0.";
stCurrEquation += "0.";
stCurrNumber = "0.";
} else if(!dec.test(stCurrNumber)) { // <--- prev keyPress was Number
eqStr += val;
displayStr += val;
stCurrEquation += val;
stCurrNumber += val;
}
break;
case "-":
case "+":
case "x":
case "*":
case "/":
// Do Not Allow Dupe op KeyPress except for -
if(this.state.operate && this.state.opPressed === keyVal && keyVal !== "-"){return;}
if(stOperate && keyVal === "-" && !stEvaled){
//State
stOpPressed = "Neg";
stEvaled = false;
stOperate = false;
//HUD
displayStr = "(-)";
eqStr = stCurrEquation.replace(/\s/g, "") ;
}else{
if(this.state.opPressed === "Neg"){
//State
stTotal = this.evalEquation(stCurrEquation + "-" + stNegNumber);
stCurrEquation += "-" + stNegNumber + " " + keyVal + " ";
stEvaled = false;
stOperate = true;
stOpPressed = keyVal;
stPreNumber = "-" + stNegNumber; stNegNumber = "";
stNumPressed = "";
}else{
//State
stTotal = this.evalEquation(stCurrEquation + " " + keyVal);
stCurrEquation += " " + keyVal + " ";
stEvaled = false;
stOperate = true;
stOpPressed = keyVal;
stPreNumber = stCurrNumber;
stCurrNumber = "";
stNumPressed = "";
//HUD
eqStr += keyVal;
displayStr = stTotal;
}
}
break;
case "Delete":
case "AC":
stTotal = 0;
stNumPressed = "";
stCurrNumber = "";
stPreNumber = "";
stNegNumber = "";
stOpPressed = "clear";
stCurrEquation = "";
stOperate = false;
stEvaled = false;
displayStr = 0;
eqStr = "";
break;
case "Backspace":
// stepBack();
break;
case "0":
case "1":
case "2":
case "3":
case "4":
case "5":
case "6":
case "7":
case "8":
case "9":
// Do Not Allow Num to start with 00
if(/^00/g.test(display.textContent + val)) {return;}
// Was Last keyPress op || = || AC? Yes: Start a new Num
if(this.state.operate || stOpPressed === "clear"){
if(this.state.opPressed === "Neg"){
//HUD
displayStr = "(-" + stNegNumber + keyVal + ")";
eqStr = displayStr;
//state
stNumPressed = keyVal;
stCurrNumber = "";
stPreNumber = stTotal;
stNegNumber = keyVal;
stOperate = false;
stEvaled = false;
stCurrEquation = (stEvaled) ? keyVal : stCurrEquation += keyVal;
// stOpPressed = "Neg";
}else{
//HUD
displayStr = keyVal;
eqStr = (stEvaled) ? keyVal : eqStr + keyVal;
//state
stNumPressed = keyVal;
stCurrNumber += keyVal;
stPreNumber = stTotal;
stNegNumber = "";
stOpPressed = "";
stCurrEquation = (stEvaled) ? keyVal : stCurrEquation += keyVal;
stOperate = false;
stEvaled = false;
}
}else{
if(this.state.opPressed === "Neg"){ // <----Neg Num
//HUD
displayStr = "(-" + stNegNumber + keyVal + ")";
eqStr = stCurrEquation.replace(/\s/g, "") + "(-" + stNegNumber + keyVal + ")" ;
//state
stNumPressed = keyVal;
stPreNumber = stNegNumber + keyVal;
stNegNumber += keyVal;
}else{ //<--- Pos Num
//HUD
displayStr += keyVal;
eqStr += keyVal;
//state
stNumPressed = keyVal;
stPreNumber = stCurrNumber;
stCurrNumber += keyVal;
stCurrEquation += keyVal;
stOpPressed = "";
}
}
break;
}
//*UPDATE HUD
equation.textContent = eqStr;
display.textContent = displayStr;
//*UPDATE STATE
this.setState({
total: stTotal,
numPressed: stNumPressed,
currNumber: stCurrNumber,
preNumber: stPreNumber,
negNumber: stNegNumber,
opPressed: stOpPressed,
currEquation: stCurrEquation,
operate: stOperate,
evaled: stEvaled,
history: stHistory,
});
} // END of handleKeyPress function
evalEquation(equation) {
equation = equation.replace(/\s=\s/g, "");
// console.log(equation);
let parts = equation.split(" ");
let result = 0;
let newNum = 0;
let op = "+"
for(let i = 0;i < parts.length; i++){
if(Number(parts[i]) || parts[i] === "0"){ // <----------- Is It a Number?
if(i === 0){ //<------------------ First loop?
result = Number(parts[i]);
}else{
newNum = Number(parts[i]);
switch(op){
case "+":
result = result + newNum;
break;
case "-":
result = result - newNum;
break;
case "*":
case "x":
result = result * newNum;
break;
case "/":
result = result / newNum;
break;
}
}
}else{
op = parts[i];
}
}
return Number.parseFloat(result.toFixed(8));
}
render() {
let historyList = (Array.isArray(this.state.history)) ? this.state.history.slice(0) : "";
if(Array.isArray(historyList)){
historyList = historyList.map((item, i) => <li key={i}>{item}</li>);
}
return (
<div id="main">
<div className="console" tabindex="0" onKeyDown={(e) => this.handleKeyPress(e)} >
<div className="title"><h2>The Counting Machine</h2></div>
<History />
<div id="display" className="display-out"></div>
<Keys onClick={(e) => this.handleKeyPress(e)} />
<div id="historyBtnRow" >
<button className="historyBtn" onClick={drpdwnFunction} title="History">
<i id="caret" className="fas fa-history rotate"></i>
</button>
</div>
</div>
<div id="history" className="panel slideup">
<ol id="log" class="custom-counter">{historyList}</ol>
</div>
</div>
);
}
}
ReactDOM.render(<Calculator />, document.getElementById("app"));
function drpdwnFunction() {
var e = document.getElementById("history");
if(e.classList.contains("slidedown")){
e.classList.replace("slidedown", "slideup");
}else{
e.classList.replace("slideup", "slidedown");
}
}
Also see: Tab Triggers