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 URL's added here will be added as <link>
s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.
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.
If the stylesheet 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 CSS 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.
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 Skypack, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ES6 import
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.
<cc-payment></cc-payment>
@import url('https://fonts.googleapis.com/css?family=Raleway:400,500');
@font-family: 'Raleway', sans-serif;
@font-size: 1em;
@color-accent: #08d343;
@color-disabled: #999;
@color-error: #fa3737;
@color-focus: #82f5f9;
@color-focus-outline: #ffbdbd;
@padding-vertical: 12px;
body {
background-color: fade(@color-accent, 30%);
padding: 2%;
font-family: @font-family;
font-size: @font-size;
}
input {
display: block;
width: 100%;
box-sizing: border-box;
font-size: @font-size;
font-family: @font-family;
font-weight: 500;
padding: @padding-vertical;
border: 1px solid #ccc;
outline-color: white;
transition: background-color 0.5s ease;
transition: outline-color 0.5s ease;
&[name=number]{
border-bottom: 0;
}
&[name=expiry],
&[name=cvc] {
width: 50%;
}
&[name=expiry] {
float: left;
border-right: 0;
}
&::placeholder {
color: #999;
font-weight: 400;
}
&:focus {
background-color: fade(@color-focus, 10%);
outline-color: @color-focus;
}
&.is-error {
background-color: fade(@color-error, 10%);
&:focus {
outline-color: @color-focus-outline;
}
}
}
button {
font-size: @font-size;
font-family: @font-family;
background-color: @color-accent;
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: @padding-vertical;
text-transform: uppercase;
width: 100%;
&:disabled {
opacity: 0.4;
background-color: @color-disabled;
}
}
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: @color-error;
}
import { kefir as Kefir, StacheElement } from "//unpkg.com/can@pre/ecosystem.mjs";
class CCPayment extends StacheElement {
static view = `
<form on:submit="this.pay(scope.event)">
{{# if(this.showCardError.value) }}
<div class="message">{{ this.cardError.value }}</div>
{{/ if }}
{{# if(this.showExpiryError.value) }}
<div class="message">{{ this.expiryError.value }}</div>
{{/ if }}
{{# if(this.showCVCError.value) }}
<div class="message">{{ this.cvcError.value }}</div>
{{/ if }}
<input type="text" name="number" placeholder="Card Number"
on:input:value:to="this.userCardNumber.value"
on:blur="this.userCardNumberBlurred.emitter.value(true)"
{{# if(this.showCardError.value) }}class="is-error"{{/ if }}>
<input type="text" name="expiry" placeholder="MM-YY"
on:input:value:to="this.userExpiry.value"
on:blur="this.userExpiryBlurred.emitter.value(true)"
{{# if(this.showExpiryError.value) }}class="is-error"{{/ if }}>
<input type="text" name="cvc" placeholder="CVC"
on:input:value:to="this.userCVC.value"
on:blur="this.userCVCBlurred.emitter.value(true)"
{{# if(this.showCVCError.value) }}class="is-error"{{/ if }}>
<button disabled:from="this.disablePaymentButton.value">
{{# eq(this.paymentStatus.value.status, "pending") }}Paying{{ else }}Pay{{/ eq }} \${{ this.amount.value }}
</button>
</form>
`;
static props = {
amount: {
get default() {
return Kefir.constant(1000);
}
},
userCardNumber: {
get default() {
return Kefir.emitterProperty();
}
},
userCardNumberBlurred: {
get default() {
return Kefir.emitterProperty();
}
},
userExpiry: {
get default() {
return Kefir.emitterProperty();
}
},
userExpiryBlurred: {
get default() {
return Kefir.emitterProperty();
}
},
userCVC: {
get default() {
return Kefir.emitterProperty();
}
},
userCVCBlurred: {
get default() {
return Kefir.emitterProperty();
}
},
payClicked: {
get default() {
return Kefir.emitterProperty();
}
},
get cardNumber() {
return this.userCardNumber.map(card => {
if (card) {
return card.replace(/[\s-]/g, "");
}
});
},
get cardError() {
return this.cardNumber.map(this.validateCard);
},
get showCardError() {
return this.showOnlyWhenBlurredOnce(
this.cardError,
this.userCardNumberBlurred
);
},
// EXPIRY
get expiry() {
return this.userExpiry.map(expiry => {
if (expiry) {
return expiry.split("-");
}
});
},
get expiryError() {
return this.expiry.map(this.validateExpiry).toProperty();
},
get showExpiryError() {
return this.showOnlyWhenBlurredOnce(
this.expiryError,
this.userExpiryBlurred
);
},
// CVC
get cvc() {
return this.userCVC;
},
get cvcError() {
return this.cvc.map(this.validateCVC).toProperty();
},
get showCVCError() {
return this.showOnlyWhenBlurredOnce(this.cvcError, this.userCVCBlurred);
},
get isCardInvalid() {
return Kefir.combine(
[this.cardError, this.expiryError, this.cvcError],
(cardError, expiryError, cvcError) => {
return !!(cardError || expiryError || cvcError);
}
);
},
get card() {
return Kefir.combine(
[this.cardNumber, this.expiry, this.cvc],
(cardNumber, expiry, cvc) => {
return { cardNumber, expiry, cvc };
}
);
},
// STREAM< Promise<Number> | undefined >
get paymentPromises() {
return Kefir.combine(
[this.payClicked],
[this.card],
(payClicked, card) => {
if (payClicked) {
console.log("Asking for token with", card);
return new Promise(resolve => {
setTimeout(() => {
resolve(1000);
}, 2000);
});
}
}
);
},
// STREAM< STREAM<STATUS> >
// This is a stream of streams of status objects.
get paymentStatusStream() {
return this.paymentPromises.map(promise => {
if (promise) {
// STREAM<STATUS>
return Kefir.concat([
Kefir.constant({
status: "pending"
}),
Kefir.fromPromise(promise).map(value => {
return {
status: "resolved",
value: value
};
})
]);
} else {
// STREAM
return Kefir.constant({
status: "waiting"
});
}
});
},
// STREAM<STATUS> //{status: "waiting"} | {status: "resolved"}
get paymentStatus() {
return this.paymentStatusStream.flatMap().toProperty();
},
get disablePaymentButton() {
return Kefir.combine(
[this.isCardInvalid, this.paymentStatus],
(isCardInvalid, paymentStatus) => {
return (
isCardInvalid === true ||
!paymentStatus ||
paymentStatus.status === "pending"
);
}
).toProperty(() => {
return true;
});
}
};
pay(event) {
event.preventDefault();
this.payClicked.emitter.value(true);
}
validateCard(card) {
if (!card) {
return "There is no card";
}
if (card.length !== 16) {
return "There should be 16 characters in a card";
}
}
validateExpiry(expiry) {
if (!expiry) {
return "There is no expiry. Format MM-YY";
}
if (
expiry.length !== 2 ||
expiry[0].length !== 2 ||
expiry[1].length !== 2
) {
return "Expiry must be formatted like MM-YY";
}
}
validateCVC(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";
}
}
showOnlyWhenBlurredOnce(errorStream, blurredStream) {
const errorEvent = errorStream.map(error => {
if (!error) {
return {
type: "valid"
};
} else {
return {
type: "invalid",
message: error
};
}
});
const focusEvents = blurredStream.map(isBlurred => {
if (isBlurred === undefined) {
return {};
}
return isBlurred
? {
type: "blurred"
}
: {
type: "focused"
};
});
return Kefir.merge([errorEvent, focusEvents])
.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
}
)
.map(state => {
return state.showCardError;
});
}
}
customElements.define("cc-payment", CCPayment);
Also see: Tab Triggers