cssAudio - ActiveCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - Activehtmlicon-personicon-teamoctocatspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

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.

            
              <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
  &.active
    border-bottom-color #fff
    
[role="tabpanel"]
   display none
   &.active
     display block
            
          
!
            
              const Tab = ({children, index, isSelected}) => (
  <div id={`tabpanel_${index}`}
       name={`tabpanel_${index}`}
       role="tabpanel"
       className={isSelected() ? 'active' : ''}
  >{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"
               className={`${tab === this.state.selected ? 'active' : ''}`}
               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
Close

Asset uploading is a PRO feature.

As a PRO member, you can drag-and-drop upload files here to use as resources. Images, Libraries, JSON data... anything you want. You can even edit them anytime, like any other code on CodePen.

Go PRO

Loading ..................

Console