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

              
                <h1>Accessible tab component in React</h1>
<div id="mount"></div>
              
            
!

CSS

              
                body
  font-family Menlo, Monaco, Arial, sans-serif
  
h1
  font-size 1.5em
  
h2
  font-size 1.1em
  
[role="tablist"]
  list-style-type none
  padding 0
  border-bottom 1px solid #ccc
  li
    display inline
    
[role="tab"]
  display inline-block
  padding 0.25em 0.75em
  margin 0 0.25em -1px 0.25em
  text-decoration none
  border 1px solid #ccc
  border-radius 3px 3px 0 0
  color #000
  &[aria-selected="true"]
    border-bottom-color #fff
    
[role="tabpanel"]
   &[aria-hidden="true"]
     display none
              
            
!

JS

              
                const Tab = ({children, index, isSelected}) => (
  <div id={`tabpanel_${index}`}
       name={`tabpanel_${index}`}
       role="tabpanel"
       aria-labelledby={`tab_${index}`}
       aria-hidden={!isSelected()}
  >{children}</div>
)

class Tabs extends React.Component {
  constructor(props) {
    super(props)
    this.tabs = props.children
    this.state = {selected: this.tabs.find(tab => tab.props.selected) || this.tabs[0]}
    this.selectTab = this.selectTab.bind(this)
    this.handleClick = this.handleClick.bind(this)
  }
  componentDidUpdate() {
    this.activeLink.focus()
  }
  selectTab (tab) {
    this.setState({selected: tab})
  }
  handleClick (e, tab) {
    e.preventDefault()
    this.selectTab(tab)
  }
  render () {
    return (<div>
      <ul role="tablist">
        {this.tabs.map((tab, i) => (
          <li role="presentation">
            <a id={`tab_${i}`}
               href={`#tabpanel_${i}`}
               role="tab"
               aria-selected={tab === this.state.selected}
               aria-controls={`tabpanel_${i}`}
               onClick={e => this.handleClick(e, tab)}
               ref={link => { if (tab === this.state.selected) this.activeLink = link}}
            >{tab.props.title}</a>
          </li>
        ))}
      </ul>
      <div>
        {this.tabs.map((tab, i) => (
          React.cloneElement(tab, {
            index: i,
            isSelected: () => tab === this.state.selected
          })
        ))}
      </div>
    </div>)
  }
}

const App = () => (
  <Tabs>
    <Tab title="React">
      <h2>Declarative</h2>
      <p>Declarative views make your code more predictable and easier to debug.</p>
      
      <h2>Component-Based</h2>
      <p>Build encapsulated components that manage their own state, then compose them to make complex UIs.</p>
      
      <h2>Learn Once, Write Anywhere</h2>
      <p>We don't make assumptions about the rest of your technology stack, so you can develop new features in React without rewriting existing code.</p>
    </Tab>
    <Tab title="Vue">
      <h2>Approachable</h2>
      <p>Already know HTML, CSS and JavaScript? Read the guide and start building things in no time!</p>

      <h2>Versatile</h2>
      <p>Simple, minimal core with an incrementally adoptable stack that can handle apps of any scale.</p>

      <h2>Performant</h2>
      <p>16kb min+gzip Runtime<br/>
         Blazing Fast Virtual DOM<br/>
         Minimal Optimization Efforts</p>
    </Tab>
    <Tab title="Mithril">
      <h2>Light-weight</h2>
      <p>Only 7.8 kB gzipped, no dependencies<br/>
        Small API, small learning curve</p>

      <h2>Robust</h2>
      <p>Safe-by-default templates<br/>
        Hierarchical MVC via components</p>
      
      <h2>Fast</h2>
      <p>Virtual DOM diffing and compilable templates<br/>
        Intelligent auto-redrawing system</p>
    </Tab>
  </Tabs>
)

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

Console