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.
<script src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/6.2.1/rxjs.umd.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/core-js/2.5.7/core.js"/></script>
<script src="https://unpkg.com/@angular/core@6.0.5/bundles/core.umd.js"/></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/zone.js/0.8.26/zone.min.js"></script>
<script src="https://unpkg.com/@angular/common@6.0.5/bundles/common.umd.js"></script>
<script src="https://unpkg.com/@angular/compiler@6.0.5/bundles/compiler.umd.js"></script>
<script src="https://unpkg.com/@angular/platform-browser@6.0.5/bundles/platform-browser.umd.js"></script>
<script src="https://unpkg.com/@angular/platform-browser-dynamic@6.0.5/bundles/platform-browser-dynamic.umd.js"></script>
<my-app></my-app>
@import url('https://fonts.googleapis.com/css?family=Raleway:400,500');
body {
background-color: rgba(8, 211, 67, 0.3);
padding: 2%;
font-family: 'Raleway', sans-serif;
font-size: 1em;
}
input {
display: block;
width: 100%;
box-sizing: border-box;
font-size: 1em;
font-family: 'Raleway', sans-serif;
font-weight: 500;
padding: 12px;
border: 1px solid #ccc;
outline-color: white;
transition: background-color 0.5s ease;
transition: outline-color 0.5s ease;
}
input[name=number] {
border-bottom: 0;
}
input[name=expiry],
input[name=cvc] {
width: 50%;
}
input[name=expiry] {
float: left;
border-right: 0;
}
input::placeholder {
color: #999;
font-weight: 400;
}
input:focus {
background-color: rgba(130, 245, 249, 0.1);
outline-color: #82f5f9;
}
input.is-error {
background-color: rgba(250, 55, 55, 0.1);
}
input.is-error:focus {
outline-color: #ffbdbd;
}
button {
font-size: 1em;
font-family: 'Raleway', sans-serif;
background-color: #08d343;
border: 0;
box-shadow: 0px 1px 3px 1px rgba(51, 51, 51, 0.16);
color: white;
font-weight: 500;
letter-spacing: 1px;
margin-top: 30px;
padding: 12px;
text-transform: uppercase;
width: 100%;
}
button:disabled {
opacity: 0.4;
background-color: #999999;
}
form {
background-color: white;
box-shadow: 0px 17px 22px 1px rgba(51, 51, 51, 0.16);
padding: 40px;
margin: 0 auto;
max-width: 500px;
}
.message {
margin-bottom: 20px;
color: #fa3737;
}
// app.js
const { Component, VERSION } = ng.core;
const {Subject, BehaviorSubject, combineLatest, of, from, concat, pipe} = rxjs;
const {map, merge, scan, mergeAll, multicast, refCount, tap, startWith} = rxjs.operators;
const cleanCardNumber = map( (card) => {
if (card) {
return card.replace(/[\s-]/g, "");
}
});
const expiryParts = map( (expiry) => {
if (expiry) {
return expiry.split("-")
}
});
const validateCard = map( (card)=> {
if (!card) {
return "There is no card"
}
if (card.length !== 16) {
return "There should be 16 characters in a card";
}
});
const validateExpiry = map( (expiry) => {
if (!expiry) {
return "There is no expiry. Format MM-YY";
}
if (expiry.length !== 2 || expiry[0].length !== 2 || expiry[1].length !== 2) {
return "Expirty must be formatted like MM-YY";
}
});
const validateCVC = map( (cvc) => {
if (!cvc) {
return "There is no CVC code";
}
if (cvc.length !== 3) {
return "The CVC must be at least 3 numbers";
}
if (isNaN(parseInt(cvc))) {
return "The CVC must be numbers";
}
});
function showOnlyWhenBlurredOnce( errorObservable, blurredObservable ){
const errorEvent = errorObservable.pipe(
map((error) => {
if (!error) {
return {
type: "valid"
}
} else {
return {
type: "invalid",
message: error
}
}
})
);
const focusEvents = blurredObservable.pipe(
map((isBlurred) => {
if (isBlurred === undefined) {
return {};
}
return isBlurred ? {
type: "blurred"
} : {
type: "focused"
};
})
);
const valueState = scan( (previous, event) => {
switch (event.type) {
case "valid":
return Object.assign({}, previous, {
isValid: true,
showCardError: false
});
case "invalid":
return Object.assign({}, previous, {
isValid: false,
showCardError: previous.hasBeenBlurred
});
case "blurred":
return Object.assign({}, previous, {
hasBeenBlurred: true,
showCardError: !previous.isValid
});
default:
return previous;
}
}, {
hasBeenBlurred: false,
showCardError: false,
isValid: false
});
var merged = errorEvent.pipe(merge(focusEvents));
return merged.pipe( valueState ).pipe( map( state => state.showCardError ) );
}
function isCardInvalid(cardError, expiryError, cvcError){
return combineLatest(cardError, expiryError, cvcError, function(cardError, expiryError, cvcError) {
return !!(cardError || expiryError || cvcError)
})
}
function combineCard(cardNumber, expiry, cvc) {
return combineLatest(cardNumber, expiry, cvc, function(cardNumber, expiry, cvc) {
return {cardNumber , expiry , cvc};
})
}
function paymentPromises(payClicked, card) {
return combineLatest(payClicked, card, (payClicked, card) => {
if (payClicked) {
console.log("Asking for token with", card);
return new Promise(function(resolve) {
setTimeout(function() {
resolve(1000);
}, 2000);
});
}
});
}
const paymentStatusObservable = pipe(
map( (promise) => {
console.log("paymentStatusObservable map", promise);
if (promise) {
// STREAM<STATUS>
var later = from(promise).pipe(
map((value) => {
console.log("resolved promise!")
return {
status: "resolved",
value: value
};
})
);
var res = concat(
of({
status: "pending"
}),
later
);
return res;
} else {
// STREAM
return of({
status: "waiting"
});
}
}),
startWith([{
status: "waiting"
}])
);
function disablePaymentButton(isCardInvalid, paymentStatus){
return combineLatest(isCardInvalid, paymentStatus, function(isCardInvalid, paymentStatus) {
return (isCardInvalid === true) || !paymentStatus || paymentStatus.status === "pending";
})
}
var log = function(name){
return tap(ev => console.log(name, ev))
}
@Component({
selector: 'my-app',
template: `
<form (submit)="pay($event)">
<div class="message"
*ngIf="(showCardError | async)">{{ (cardError | async) }}</div>
<div class="message"
*ngIf="(showExpiryError | async)">{{ (expiryError | async) }}</div>
<div class="message"
*ngIf="(showCVCError | async)">{{ (cvcError | async) }}</div>
<input type="text" name="number" placeholder="Card Number"
(input)="userCardNumber.next( $event.target.value )"
(blur)="userCardNumberBlurred.next( true )"
[class.is-error]="showCardError | async"/>
<input type="text" name="expiry" placeholder="MM-YY"
(input)="userExpiry.next( $event.target.value )"
(blur)="userExpiryBlurred.next( true )"
[class.is-error]="showExpiryError | async"/>
<input type="text" name="cvc" placeholder="CVC"
(input)="userCVC.next( $event.target.value )"
(blur)="userCVCBlurred.next( true )"
[class.is-error]="showCVCError | async"/>
<button [disabled]="disablePaymentButton | async">
{{ ( (paymentStatus | async)?.status === "pending" ) ? "Paying" : "Pay" }}
</button>
</form>
`
})
class AppComponent {
userCardNumber = new BehaviorSubject<String>();
userCardNumberBlurred = new Subject<Boolean>();
userExpiry = new BehaviorSubject<Array>();
userExpiryBlurred = new Subject<Boolean>();
userCVC = new BehaviorSubject<String>();
userCVCBlurred = new Subject<Boolean>();
payClicked = new Subject();
constructor() {
this.cardNumber = this.userCardNumber.pipe(cleanCardNumber);
this.cardError = this.cardNumber.pipe(validateCard);
this.showCardError = showOnlyWhenBlurredOnce(this.cardError, this.userCardNumberBlurred);
this.expiry = this.userExpiry.pipe(expiryParts);
this.expiryError = this.expiry.pipe(validateExpiry);
this.showExpiryError = showOnlyWhenBlurredOnce(this.expiryError, this.userExpiryBlurred);
this.cvc = this.userCVC;
this.cvcError = this.cvc.pipe(validateCVC);
this.showCVCError = showOnlyWhenBlurredOnce(this.cvcError, this.userCVCBlurred);
this.isCardInvalid = isCardInvalid(this.cardError, this.expiryError, this.cvcError);
this.card = combineCard(this.cardNumber, this.expiry, this.cvc)
.pipe(multicast(new Subject()), refCount());
const payments = paymentPromises(this.payClicked, this.card)
.pipe( log("payment") );
//payments.subscribe( (data) => {console.log("subscribe payments",data)})
const paymentsObservables = payments.pipe(paymentStatusObservable);
//paymentsObservables.subscribe( (data) => {console.log("subscribe paymentsObservables",data)})
this.paymentStatus = paymentsObservables.pipe( mergeAll() ).pipe(multicast(new Subject()), refCount());
this.disablePaymentButton = disablePaymentButton(this.isCardInvalid, this.paymentStatus);
}
pay(event){
console.log("PAY CLICKED");
event.preventDefault();
this.payClicked.next(true); //a
console.log("EMITTED");
}
}
// main.js
const { BrowserModule } = ng.platformBrowser;
const { NgModule } = ng.core;
const { CommonModule } = ng.common;
@NgModule({
imports: [
BrowserModule,
CommonModule,
],
declarations: [AppComponent],
bootstrap: [AppComponent],
providers: []
})
class AppModule {}
const { platformBrowserDynamic } = ng.platformBrowserDynamic;
platformBrowserDynamic()
.bootstrapModule(AppModule)
.catch(err => console.error(err));
Also see: Tab Triggers