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. You can use the CSS from another Pen by using it's URL and the proper URL extention.
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 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.
%div.container
%h1 a11y React.js accordions!
%h3 Click or use 'Enter' to expand/collapse
%h5 The attributes on the accordions refer to the state of the content area inside
#accordions
@import "bourbon";
// Colors
$slate: #222;
$red: #BA394C;
$blue: #1F6190;
$lightBlue: #3683BA;
$yellow: #FFAD28;
body {
min-height: 100vh;
@include linear-gradient(to top, #f5f5f5, #fff);
@include display(flex);
@include justify-content(center);
}
.container {
width: 100%;
max-width: 500px;
}
// Self contained components
// Semantically structured
.accordion {
width: 100%;
height: auto;
margin: 10px 0px;
// Default Theme
.accordion-item {
// Elements
header {
padding: 20px;
background: #222;
color: #fff;
cursor: pointer;
font-size: 0.9em;
border-bottom: solid 1px #111;
@include display(flex);
@include justify-content(space-between);
&:hover {
background: #111;
}
}
section {
background: #fff;
padding: 40px;
}
// State
&.is-expanded {
section {
display: block;
}
}
&.is-collapsed {
section {
display: none;
}
}
} // End Accordion Item
// Themes
@mixin accordionTheme($color) {
$itemColor: $color;
$shades: 5;
.accordion-item {
@for $i from 1 through $shades {
&:nth-child(#{$i}) {
header {
$amount: $i * 3%;
background: darken($itemColor, $amount );
border-bottom: solid 5px darken($itemColor, $amount * 2);
&:hover {
background: darken($itemColor, 10%);
}
}
}
}
section {
h2 {
color: $color;
}
}
}
}
&.dark {
@include accordionTheme($slate);
}
&.blue {
@include accordionTheme($blue);
}
&.light-blue {
@include accordionTheme($lightBlue);
}
&.red {
@include accordionTheme($red);
}
&.yellow {
@include accordionTheme($yellow);
}
} // End Accordion
// Here is a little example for everyone
// Let's build an accordion that is a11y compliant!
// If there are any React purist out there, go easy on me, I am learning too :)
// I have to say, every change I have had to make so far has been SIGNIFICANTLY less painful than maintaining this through the jQuery spaghetti version
// Select the node to render the React component into
// The "Parent Element" or "Container"
// This is just an HTML element that React will render the state of our accordion into
var renderNode = document.getElementById("accordions");
// This enables react components to work well on touch devices
React.initializeTouchEvents(true);
// In react everything is organized as a self contained object.
/////////////////////////////////////
// Accordion Component
/////////////////////////////////////
var Accordion = React.createClass({
render: function() {
// Look into this vvvv. I believe there is a better way than concatenating a string to dynamic properties. Probably some sort of binding?
var classes = "accordion " + this.props.theme;
return(
<div className={classes}>
{this.props.children}
</div>
);
}
});
/////////////////////////////////////
// Accordion Item Component
////////////////////////////////////
var AccordionItem = React.createClass({
// Initial setup
getInitialState: function() {
return {
expanded: this.props.expanded
};
},
// Actions
expand: function() {
this.setState({expanded: true});
},
collapse: function() {
this.setState({expanded: false});
},
toggleExpansion: function() {
this.setState({expanded: !this.state.expanded});
},
// Events
handleClick: function() {
this.toggleExpansion();
},
handleKeyPress: function(ev) {
var key = ev.keyCode;
console.log(ev);
// Enter
if (key === 13) {
this.toggleExpansion();
}
// Esc
if (key === 27) {
this.collapse();
}
},
// Render
render: function() {
// You can think of the render function as the "Template"
// Here is where we set our state on the actual DOM element
// We detect via the expanded state of the component what state
// things like aria-hidden and tab-indexs should be in as well as
// adjusting class names to change appearance.
// A REAL nice way to build UI, give it a try.
// DO NOTE: This is not the render function that is responsible for actually placing the element on the screen. That comes later :)
var expandState = this.state.expanded ? 'is-expanded' : 'is-collapsed',
hiddenState = this.state.expanded ? '' : 'hidden',
ariaHiddenState = this.state.expanded ? 'false' : 'true',
tabState = this.state.expanded ? '0' : '-1',
classes = "accordion-item " + expandState;
// OK, now take all that state and tell React how to render it into a template
return (
<div className={classes}>
<header onClick={this.handleClick} onKeyDown={this.handleKeyPress} tabIndex="0">
<span>Tab-Index = {tabState}</span><span>Aria-Hidden = {ariaHiddenState}</span><span>Hidden = {hiddenState} </span>
</header>
<section aria-hidden={ariaHiddenState} hidden={hiddenState} tabIndex={tabState} onKeyDown={this.handleKeyPress}>
<h2>Content</h2>
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Enim inventore velit sint quod blanditiis, sapiente voluptatibus, molestiae, dolore ipsam labore quaerat veritatis fuga libero! Explicabo aperiam sapiente optio consectetur placeat.
</section>
</div>
);
}
});
// Where the "Magic" happens.
React.render(
<div>
<Accordion theme="red" toggleType="all">
<AccordionItem />
<AccordionItem expanded="true" />
<AccordionItem />
<AccordionItem />
</Accordion>
<Accordion theme="light-blue" toggleType="single">
<AccordionItem />
<AccordionItem />
<AccordionItem />
<AccordionItem />
</Accordion>
</div>
, renderNode);
Also see: Tab Triggers