Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

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.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

Add External Scripts/Pens

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.

+ add another resource

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

HTML

              
                <h1 class="title">React collapsible</h1>
<p class="lead">An accessible collasable content panel built using <a href="https://reactjs.org/"> React </a>.</p>

<div id="app"></div>
              
            
!

CSS

              
                @import url('https://fonts.googleapis.com/css?family=Barlow:400,400i,700');

:root {
    --panel-border: 2px solid rgba(23, 24, 87, 0.2);
}

html * {
    box-sizing: border-box;
}

body {
    padding: 3rem;
    font-family: "Barlow", sans-serif;
    font-size: 120%;
    line-height: 1.5;
    color: navy;
}

img {
    width: 100%;
    display: block;
}

h1, h2, h3 {
    margin-top: 2rem;
    margin-bottom: 0;
}

.title {
    margin-bottom: 0;
}

.lead {
    margin-top: 0;
    margin-bottom: 4rem;
}

a {
    color: crimson;
}

.panel {
    margin-bottom: 1rem;
    
    h2 {
        font-size: 1.25rem;
        margin: 0;
    }
}

.panel__header {
    display: flex;
    align-items: center;
    width: 100%;
    text-align: left;
    border: var(--panel-border);
    font-weight: 700;
    color: inherit;
}

.panel__header-icon {
    width: 1.25rem;
    height: 1.25rem;
    margin-left: auto;
}

button.panel__header {
    cursor: pointer;
    border-radius: 0;
    
    &:focus,
    &:hover {
        outline: none;
        background-color: navy;
        color: white;
        border-color: navy;
    }
}

.panel__body {
    border-right: var(--panel-border);
    border-bottom: var(--panel-border);
    border-left: var(--panel-border);
    
    > *:first-child {
        margin-top: 0;    
    }
    
    > *:last-child {
        margin-bottom: 0;
    }
}

.panel__body,
.panel__header {
    padding: 1rem;   
}

.panel__body[aria-hidden="true"] {
    display: none;
}

.panel-group {
    .panel {
        margin-bottom: 0;
    }
    
    .panel:not(:first-child) .panel__header {
        border-top: none;
    }
}
              
            
!

JS

              
                const PlusIcon = () => {
    return (
        <span className="panel__header-icon">
            <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 16 16">
                <path fill="currentColor" d="M14,7H9V2A1,1,0,0,0,7,2V7H2A1,1,0,0,0,2,9H7v5a1,1,0,0,0,2,0V9h5a1,1,0,0,0,0-2Z"/>
            </svg>
        </span>
    )
}

const MinusIcon = () => {
    return (
        <span className="panel__header-icon">
            <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 16 16">
                <path fill="currentColor" d="M14,9H2A1,1,0,0,1,2,7H14a1,1,0,0,1,0,2Z"/>
            </svg>
        </span>
    )    
}

const PanelHeader = props => {
    return (
        <button
            className="panel__header"
            onClick={props.handleToggle}
            onKeyDown={props.handleKeyDown}
            aria-expanded={props.isExpanded}
        >
            {props.children}   
            {props.isExpanded ? <MinusIcon/> : <PlusIcon/>}
        </button>
    )
}

const PanelBody = props => {
    return (
        <div className="panel__body" aria-hidden={props.isExpanded}>
            {props.children}
        </div>
    )
}

const PanelGroup = props => {
    return (
        <div className="panel-group" role="group">
            {props.children}
        </div>
    )
}

class Panel extends React.Component {
    constructor(props) {
        super(props)
        
        this.state = {
            isExpanded: props.openDefault
        }
        
        this.handleToggle = this.handleToggle.bind(this)
        this.handleKeyDown = this.handleKeyDown.bind(this)
    }
    
    handleToggle() {
        this.setState({
            isExpanded: !this.state.isExpanded 
        })
    }
    
    handleKeyDown(event) {
        if(event.keyCode == 40) {
            event.preventDefault();
            this.setState({
                isExpanded: true
            })
        }
        
        if(event.keyCode == 38) {
            event.preventDefault();
            this.setState({
                isExpanded: false
            })
        }
    }
    
    render() {
        return (
            <div className="panel">
                <PanelHeader
                    handleToggle={this.handleToggle}
                    handleKeyDown={this.handleKeyDown}
                    isExpanded={this.state.isExpanded}
                >
                    {this.props.title}
                </PanelHeader>
                <PanelBody isExpanded={!this.state.isExpanded}>
                    {this.props.children}
                </PanelBody>
            </div>
        )
    }
}

const App = () => {
    return (
        <div className="wrapper">
            <Panel title="Single panel">
                <p>Optio asperiores sit et dolores est in qui. Et dolores aut. Sunt non sint optio vero esse reiciendis velit aliquam. Exercitationem minus id corporis. Facere qui nisi asperiores laboriosam ea nihil. Fugiat facilis suscipit voluptatem recusandae sit quia quisquam molestiae.</p>
                <p>At quae sint temporibus recusandae velit <a href="#0">non officia</a>. A optio suscipit non aliquam aut exercitationem ipsam. Autem rerum aperiam eos. Dolore necessitatibus harum nisi reprehenderit ea quia molestiae sint et. Cupiditate nostrum quia iure hic nulla vel. Quisquam ducimus nulla explicabo labore cum.</p>
            </Panel>
            <h2>Panel group</h2>
            <p className="lead">You can also goup a bunch of panels together to make an accordiion-like thingy.</p>
            <PanelGroup>
                <Panel title="New panel">
                    <p>Optio asperiores sit et dolores est in qui. Et dolores aut. Sunt non sint optio vero esse reiciendis velit aliquam. Exercitationem minus id corporis. Facere qui nisi asperiores laboriosam ea nihil. Fugiat facilis suscipit voluptatem recusandae sit quia quisquam molestiae.</p>
                    <p>At quae sint temporibus recusandae velit <a href="#0">non officia</a>. A optio suscipit non aliquam aut exercitationem ipsam. Autem rerum aperiam eos. Dolore necessitatibus harum nisi reprehenderit ea quia molestiae sint et. Cupiditate nostrum quia iure hic nulla vel. Quisquam ducimus nulla explicabo labore cum.</p>
                </Panel>
                <Panel title="Another panel panel" openDefault>
                    <p>Optio asperiores sit et dolores est in qui. Et dolores aut. Sunt non sint optio vero esse reiciendis velit aliquam. Exercitationem minus id corporis. Facere qui nisi asperiores laboriosam ea nihil. Fugiat facilis suscipit voluptatem recusandae sit quia quisquam molestiae.</p>
                    <img src={'https://www.fillmurray.com/g/800/450'} />
                    <p>At quae sint temporibus recusandae velit non officia. A optio suscipit non aliquam aut exercitationem ipsam. Autem rerum aperiam eos. Dolore necessitatibus harum nisi reprehenderit ea quia molestiae sint et. Cupiditate nostrum quia iure hic nulla vel. Quisquam ducimus nulla explicabo labore cum.</p>
                </Panel>
                <Panel title="Third panel">
                    <p>Optio asperiores sit et dolores est in qui. Et dolores aut. Sunt non sint optio vero esse reiciendis velit aliquam. Exercitationem minus id corporis. Facere qui nisi asperiores laboriosam ea nihil. Fugiat facilis suscipit voluptatem recusandae sit quia quisquam molestiae.</p>
                    <p>At quae sint temporibus recusandae velit non officia. A optio suscipit non aliquam aut exercitationem ipsam. Autem rerum aperiam eos. Dolore necessitatibus harum nisi reprehenderit ea quia molestiae sint et. Cupiditate nostrum quia iure hic nulla vel. Quisquam ducimus nulla explicabo labore cum.</p>
                </Panel>
                <Panel title="Fourth panel">
                    <p>Optio asperiores sit et dolores est in qui. Et dolores aut. Sunt non sint optio vero esse reiciendis velit aliquam. Exercitationem minus id corporis. Facere qui nisi asperiores laboriosam ea nihil. Fugiat facilis suscipit voluptatem recusandae sit quia quisquam molestiae.</p>
                    <p>At quae sint temporibus recusandae velit non officia. A optio suscipit non aliquam aut exercitationem ipsam. Autem rerum aperiam eos. Dolore necessitatibus harum nisi reprehenderit ea quia molestiae sint et. Cupiditate nostrum quia iure hic nulla vel. Quisquam ducimus nulla explicabo labore cum.</p>
                </Panel>
            </PanelGroup>
        </div>
    )
}

ReactDOM.render(
    <App />,
    document.getElementById('app')
)
              
            
!
999px

Console