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>
h1 {
font-size: 2em;
padding: 1em 0;
}
h2 {
font-size: 1.6em;
padding: 0.5em 0;
}
body {
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
}
.app {
padding: 20px;
}
.section {
padding-top: 30px;
}
.subCollapse {
padding: 20px;
}
.text {
margin: 0;
}
.text p {
margin: 0;
padding: 10px;
}
.config {
padding-bottom: 20px;
}
.label {
padding-right: 20px;
}
.input {
margin-right: 10px;
margin-left: 10px;
vertical-align: middle;
}
.blob {
border-radius: 50vh;
background: rgba(96, 125, 139, 0.6);
}
.log {
padding: 0.5em;
font-size: 0.6em;
font-family: Menlo, Consolas, Monospaced, monospace;
}
.ReactCollapse--collapse {
max-width: 800px;
border: 1px solid rgba(3, 169, 244, 0.3);
border-radius: 10px;
background-color: rgba(100, 255, 100, 0.1);
transition: height 500ms;
}
.ReactCollapse--content {
}
const text = [
"You think water moves fast? You should see ice. It moves like it has a mind. Like it knows it killed the world once and got a taste for murder. After the avalanche, it took us a week to climb out. Now, I don't know exactly when we turned on each other, but I know that seven of us survived the slide... and only five made it out. Now we took an oath, that I'm breaking now. We said we'd say it was the snow that killed the other two, but it wasn't. Nature is lethal but it doesn't hold a candle to man.",
"Your bones don't break, mine do. That's clear. Your cells react to bacteria and viruses differently than mine. You don't get sick, I do. That's also clear. But for some reason, you and I react the exact same way to water. We swallow it too fast, we choke. We get some in our lungs, we drown. However unreal it may seem, we are connected, you and I. We're on the same curve, just on opposite ends.",
"Do you see any Teletubbies in here? Do you see a slender plastic tag clipped to my shirt with my name printed on it? Do you see a little Asian child with a blank expression on his face sitting outside on a mechanical helicopter that shakes when you put quarters in it? No? Well, that's what you see at a toy store. And you must think you're in a toy store, because you're here shopping for an infant named Jeb.",
"You see? It's curious. Ted did figure it out - time travel. And when we get back, we gonna tell everyone. How it's possible, how it's done, what the dangers are. But then why fifty years in the future when the spacecraft encounters a black hole does the computer call it an 'unknown entry event'? Why don't they know? If they don't know, that means we never told anyone. And if we never told anyone it means we never made it back. Hence we die down here. Just as a matter of deductive logic."
];
const {Collapse, UnmountClosed} = ReactCollapse;
const getText = num => text.slice(0, num).map((p, i) => <p key={i}>{p}</p>);
class VariableText extends React.Component {
static propTypes = {
isOpened: PropTypes.bool
};
static defaultProps = {
isOpened: false
};
constructor(props) {
super(props);
const {isOpened} = this.props;
this.state = {isOpened, paragraphs: 0};
}
render() {
const {isOpened, paragraphs} = this.state;
return (
<div>
<div className="config">
<label className="label">
Opened:
<input
className="input"
type="checkbox"
checked={isOpened}
onChange={({target: {checked}}) => this.setState({isOpened: checked})} />
</label>
<label className="label">
Paragraphs:
<input
className="input"
type="range"
value={paragraphs}
step={1}
min={0}
max={4}
onChange={({target: {value}}) => this.setState({paragraphs: parseInt(value, 10)})} />
{paragraphs}
</label>
</div>
<Collapse isOpened={isOpened}>
<div className="text">
{paragraphs ? getText(paragraphs) : <p>No text</p>}
</div>
</Collapse>
</div>
);
}
}
class VariableHeight extends React.PureComponent {
constructor(props) {
super(props);
this.state = {isOpened: false, height: 100};
}
render() {
const {isOpened, height} = this.state;
return (
<div {...this.props}>
<div className="config">
<label className="label">
Opened:
<input
className="input"
type="checkbox"
checked={isOpened}
onChange={({target: {checked}}) => this.setState({isOpened: checked})} />
</label>
<label className="label">
Content height:
<input
className="input"
type="range"
value={height}
step={50}
min={0}
max={500}
onChange={({target: {value}}) => this.setState({height: parseInt(value, 10)})} />
{height}
</label>
</div>
<Collapse isOpened={isOpened}>
<div style={{height}} className="blob" />
</Collapse>
</div>
);
}
}
class InitiallyOpened extends React.PureComponent {
constructor(props) {
super(props);
this.state = {isOpened: true};
}
render() {
const {isOpened} = this.state;
const height = 100;
return (
<div>
<div className="config">
<label className="label">
Opened:
<input
className="input"
type="checkbox"
checked={isOpened}
onChange={({target: {checked}}) => this.setState({isOpened: checked})} />
</label>
</div>
<Collapse isOpened={isOpened}>
<div style={{height}} className="blob" />
</Collapse>
</div>
);
}
}
class Nested extends React.PureComponent {
constructor(props) {
super(props);
this.state = {isOpened: true};
}
render() {
const {isOpened} = this.state;
return (
<div>
<div className="config">
<label className="label">
Opened:
<input
className="input"
type="checkbox"
checked={isOpened}
onChange={({target: {checked}}) => this.setState({isOpened: checked})} />
</label>
</div>
<Collapse isOpened={isOpened} hasNestedCollapse>
<InitiallyOpened className="subCollapse" />
<VariableHeight className="subCollapse" />
<VariableHeight className="subCollapse" />
</Collapse>
</div>
);
}
}
class Hooks extends React.PureComponent {
constructor(props) {
super(props);
this.state = {
isOpened: false,
isResting: false,
paragraphs: 0
};
}
render() {
const {
isResting, isOpened, paragraphs, params
} = this.state;
return (
<div>
<div className="config">
<label className="label">
Opened:
<input
className="input"
type="checkbox"
checked={isOpened}
onChange={({target: {checked}}) => this.setState({isOpened: checked})} />
</label>
<label className="label">
Paragraphs:
<input
className="input"
type="range"
value={paragraphs}
step={1}
min={0}
max={4}
onChange={({target: {value}}) => this.setState({paragraphs: parseInt(value, 10)})} />
{paragraphs}
</label>
</div>
<div className="config">
<span className="label">
Resting:
{' '}
{isResting ? 'true' : 'false'}
</span>
<span className="label">
onRest/onWork arguments:
{' '}
{params}
</span>
</div>
<Collapse
isOpened={isOpened}
onWork={p => this.setState({isResting: false, params: JSON.stringify(p)})}
onRest={p => this.setState({isResting: true, params: JSON.stringify(p)})}>
<div className="text">{paragraphs ? getText(paragraphs) : <p>No text</p>}</div>
</Collapse>
</div>
);
}
}
class Test extends React.PureComponent {
static propTypes = {
onMount: PropTypes.func.isRequired,
onUnmount: PropTypes.func.isRequired
};
componentDidMount() {
const {onMount} = this.props;
onMount();
}
componentWillUnmount() {
const {onUnmount} = this.props;
onUnmount();
}
render() {
return <div>Test</div>;
}
}
class AutoUnmount extends React.PureComponent {
static propTypes = {
isOpened: PropTypes.bool.isRequired
};
constructor(props) {
super(props);
const {isOpened} = this.props;
this.state = {isOpened};
this.counter = 0;
this.messages = [];
}
onRef = ref => {
this.ref = ref;
};
onMount = () => {
if (this.ref) {
this.messages.unshift(`${this.counter}. Mounted`);
this.messages = this.messages.slice(0, 5);
this.ref.innerHTML = this.messages.join('<br />');
this.counter = this.counter + 1;
}
};
onUnmount = () => {
if (this.ref) {
this.messages.unshift(`${this.counter}. Unmounted`);
this.messages = this.messages.slice(0, 5);
this.ref.innerHTML = this.messages.join('<br />');
this.counter = this.counter + 1;
}
};
onChange = ({target: {checked}}) => {
this.setState({isOpened: checked});
};
render() {
const {isOpened} = this.state;
return (
<div>
<div className="config">
<label className="label">
Opened:
<input
className="input"
type="checkbox"
checked={isOpened}
onChange={this.onChange} />
</label>
</div>
<UnmountClosed isOpened={isOpened}>
<Test onMount={this.onMount} onUnmount={this.onUnmount} />
</UnmountClosed>
<div className="log" ref={this.onRef} />
</div>
);
}
}
class ForceInitialAnimation extends React.PureComponent {
constructor(props) {
super(props);
this.state = {isOpened: true};
}
render() {
const {isOpened} = this.state;
const height = 100;
return (
<div>
<div className="config">
<label className="label">
Opened:
<input
className="input"
type="checkbox"
checked={isOpened}
onChange={({target: {checked}}) => this.setState({isOpened: checked})} />
</label>
</div>
<Collapse isOpened={isOpened} initialStyle={{height: 0, overflow: 'hidden'}}>
<div style={{height}} className="blob" />
</Collapse>
</div>
);
}
}
const App = () => (
<div className="app">
<h1>react-collapse</h1>
<section className="section">
<h2>Variable text</h2>
<VariableText />
</section>
<section className="section">
<h2>Variable text (initially opened)</h2>
<VariableText isOpened />
</section>
<section className="section">
<h2>Variable height content</h2>
<VariableHeight />
</section>
<section className="section">
<h2>Initially opened</h2>
<InitiallyOpened />
</section>
<section className="section">
<h2>Force initial animation</h2>
<ForceInitialAnimation />
</section>
<section className="section">
<h2>Nested Collapse</h2>
<Nested />
</section>
<section className="section">
<h2>Hooks</h2>
<Hooks />
</section>
<section className="section">
<h2>Auto-unmount when closed</h2>
<p>closed by default</p>
<AutoUnmount isOpened={false} />
<section className="section">
<p>opened by default</p>
<AutoUnmount isOpened />
</section>
</section>
</div>
);
ReactDOM.render(<App />, document.querySelector('#app'));
Also see: Tab Triggers