Pen Settings

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

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

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.

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.

            
              <h1>Accessible tab component in React</h1>
<div id="mount"></div>
            
          
!
            
              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"]
  padding 0 0.5em
  &[aria-hidden="true"]
    display none
            
          
!
            
              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.previousTab = this.previousTab.bind(this)
    this.nextTab = this.nextTab.bind(this)
    this.handleClick = this.handleClick.bind(this)
    this.handleKeyup = this.handleKeyup.bind(this)
  }
  componentDidUpdate() {
    this.activeLink.focus()
  }
  selectTab (tab) {
    this.setState({selected: tab})
  }
  previousTab (tab) {
    const index = this.tabs.indexOf(tab)
    if (index > 0) this.selectTab(this.tabs[index - 1])
  }
  nextTab (tab) {
    const index = this.tabs.indexOf(tab)
    if (index < this.tabs.length - 1) this.selectTab(this.tabs[index + 1])
  }
  handleClick (e, tab) {
    e.preventDefault()
    this.selectTab(tab)
  }
  handleKeyup (e, tab) {
    e.preventDefault()
    if (e.which === 13) this.selectTab(tab)
    else if (e.which === 37) this.previousTab(tab)
    else if (e.which === 39) this.nextTab(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-controls={`tab_${i}`}
               aria-selected={tab === this.state.selected}
               tabIndex={tab === this.state.selected ? 0 : -1}
               onClick={e => this.handleClick(e, tab)}
               onKeyUp={e => this.handleKeyup(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
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.

Console