cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

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.

Quick-add: + add another resource

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.

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.

            
              @white: #ffffff;
@black: #000000;
@brand: #1666af;
@dusty--x2: #ededed;
@dusty--x6: #333333;

.transition(@property @duration @type) {
    -webkit-transition: @property @duration @type;
       -moz-transition: @property @duration @type;
         -o-transition: @property @duration @type;
        -ms-transition: @property @duration @type;
            transition: @property @duration @type;
}

.display-flex(@type: flex){

	& when (@type = flex){
		display: -webkit-box;
		display: -webkit-flex;
		display: -moz-box;
		display: -ms-flexbox;
	}

	& when (@type = inline-flex){
		display: -webkit-inline-box;
		display: -webkit-inline-flex;
		display: -moz-inline-box;
		display: -ms-inline-flexbox;
	}

	display: @type;
}

.flex-direction(@direction : row){

	& when (@direction = row){
		-webkit-box-orient: horizontal;
		-webkit-box-direction: normal;
		-moz-box-orient: horizontal;
		-moz-box-direction: normal;
	}

	& when (@direction = row-reverse){
		-webkit-box-orient: horizontal;
		-webkit-box-direction: reverse;
		-moz-box-orient: horizontal;
		-moz-box-direction: reverse;
	}

	& when (@direction = column){
		-webkit-box-orient: vertical;
		-webkit-box-direction: normal;
		-moz-box-orient: vertical;
		-moz-box-direction: normal;
	}

	& when (@direction = column-reverse){
		-webkit-box-orient: vertical;
		-webkit-box-direction: reverse;
		-moz-box-orient: vertical;
		-moz-box-direction: reverse;
	}

	-webkit-flex-direction: @direction;
	-ms-flex-direction: @direction;
	flex-direction: @direction;
}

.flex-grow(@grow : 0){
	-webkit-box-flex: @grow;
	-webkit-flex-grow: @grow;
	-moz-box-flex: @grow;
	-ms-flex-positive: @grow;
	flex-grow: @grow;
}

.flex-shrink(@shrink : 1){
	-webkit-flex-shrink: @shrink;
	-ms-flex-negative: @shrink;
	flex-shrink: @shrink;
}

body { 
  padding: 30px;
  font-family: Arial, Helvetica, Sans-Serif;
}

// ---------------------------------------------------
// TAB COMPONENT STYLES 
// ---------------------------------------------------
.tabs {
  .display-flex();
  .flex-direction(column);
}

.tabs-header {
  .display-flex();
}

.tabs-header > *,
.tab-panel-header {
  background: @brand;
  color: @white;
  cursor: pointer;
  text-align: center;
  padding: 16px;
  font-weight: bold;
  .transition(background, 0.15s, linear);
  .flex-shrink(0);
}

.tabs-justified .tabs-header > * {
  .flex-grow(1);
}

.tabs-header > * + * {
  border-left: 1px solid darken(@brand, 7%);
}

.tabs-header > :hover {
  background-color: darken(@brand, 7%);
}

.tabs-header > .isActive,
.tabs-header > .isActive:hover {
  background-color: @dusty--x2;
  color: @dusty--x6;
}

.tab-panel-header {
  text-align: left;
  padding: 16px;
}

.tab-panel-header.isActive {
  background-color: darken(@brand, 10%);
}

.tab-panel {
  background-color: @dusty--x2;
  padding: 16px;
  line-height: 1.5;
  .flex-grow(1);
}

.tabs-onSide {
  .flex-direction(row);
}

.tabs-onSide .tabs-header {
  display: block;
}

.tabs-onSide .tabs-header > * {
  border-left: 0;
  text-align: left;
}

.tabs-onSide .tabs-header > * + *,
.tab-panel-header {
  border-top: 1px solid darken(@brand, 7%);
}

// Responsive tabs / Accordion
.tabs-responsive,
.tabs-responsive .isActive .tab-panel,
.tabs-accordion .isActive .tab-panel,
.tabs-accordion,
.tabs-accordion .tab-panel-wrapper,
.tabs-accordion .tab-panel-header {
  display: block;
}

.tabs-responsive .tabs-header,
.tabs-responsive .tab-panel,
.tabs-accordion .tab-panel {
  display: none;
}

.tabs-accordion .tab-panel-header,
.tabs-responsive .tab-panel-header {
  background-image: url('data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmLTgiPz4NCjwhLS0gR2VuZXJhdG9yOiBBZG9iZSBJbGx1c3RyYXRvciAxOC4xLjEsIFNWRyBFeHBvcnQgUGx1Zy1JbiAuIFNWRyBWZXJzaW9uOiA2LjAwIEJ1aWxkIDApICAtLT4NCjxzdmcgdmVyc2lvbj0iMS4xIiBpZD0iTGF5ZXJfMSIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIiB4bWxuczp4bGluaz0iaHR0cDovL3d3dy53My5vcmcvMTk5OS94bGluayIgeD0iMHB4IiB5PSIwcHgiDQoJIHZpZXdCb3g9IjAgMCA4NSAzNyIgZW5hYmxlLWJhY2tncm91bmQ9Im5ldyAwIDAgODUgMzciIHhtbDpzcGFjZT0icHJlc2VydmUiPg0KPHBhdGggZmlsbD0iI0ZGRkZGRiIgZD0iTTIwLjgsMS43bDI3LjYsMjcuN2wtNS44LDUuOUwxNC45LDcuNkwyMC44LDEuN3ogTTcwLjEsNy42TDQyLjUsMzUuMmwtNS44LTUuOUw2NC4yLDEuN0w3MC4xLDcuNnoiLz4NCjwvc3ZnPg0K');
}

.tabs-accordion .tab-panel-header.isActive,
.tabs-responsive .tab-panel-header.isActive {
  background-image: url('data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmLTgiPz4NCjwhLS0gR2VuZXJhdG9yOiBBZG9iZSBJbGx1c3RyYXRvciAxOC4xLjEsIFNWRyBFeHBvcnQgUGx1Zy1JbiAuIFNWRyBWZXJzaW9uOiA2LjAwIEJ1aWxkIDApICAtLT4NCjxzdmcgdmVyc2lvbj0iMS4xIiBpZD0iTGF5ZXJfMSIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIiB4bWxuczp4bGluaz0iaHR0cDovL3d3dy53My5vcmcvMTk5OS94bGluayIgeD0iMHB4IiB5PSIwcHgiDQoJIHZpZXdCb3g9IjAgMCA4NSAzNyIgZW5hYmxlLWJhY2tncm91bmQ9Im5ldyAwIDAgODUgMzciIHhtbDpzcGFjZT0icHJlc2VydmUiPg0KPHBhdGggZmlsbD0iI0ZGRkZGRiIgZD0iTTY0LjIsMzUuM0wzNi42LDcuNmw1LjgtNS45bDI3LjcsMjcuN0w2NC4yLDM1LjN6IE0xNC45LDI5LjRMNDIuNSwxLjhsNS44LDUuOUwyMC44LDM1LjNMMTQuOSwyOS40eiIvPg0KPC9zdmc+DQo=');
}

.tabs-accordion .tab-panel-header,
.tabs-responsive .tab-panel-header {
  background-size: 20px;
  background-repeat: no-repeat;
  background-position: 16px center;
}

.tabs-accordion .tab-panel-header,
.tabs-responsive .tab-panel-header {
  padding-left: 45px;  
}

@media (min-width: 768px) {
  .tabs-responsive,
  .tabs-responsive .tabs-header {
    .display-flex();
  }

  .tab-panel-wrapper {
    .display-flex();
    .flex-grow(0);
  }

  .tab-panel-header {
    display: none;
  }

  .tabs-onSide .tabs-header {
    display: block;
  }
}
            
          
!
            
              const { Component, PropTypes } = React;
const { render } = ReactDOM;

const generateRandomId = () => {
  return `tabcordion-${Math.round(Math.random() * 10000)}`;
};

let activeTabs;

class Tabs extends Component {
  constructor (props) {
    super(props);

    if (this.props.checkboxAccordion) {
      this.state = { selectedIndex: [+props.selectedIndex || 0] };
    } else {
      this.state = { selectedIndex: +props.selectedIndex || 0 };
    }
  }
  
  static propTypes = {
    orientation: PropTypes.string,
    responsive: PropTypes.bool,
  };

  static defaultProps = {
    orientation: 'top',
    responsive: false,
  };
  
  componentDidMount () {
    this._handleAccessibility();
  }

  _handleAccessibility = () => {
    
    const { selectedIndex } = this.state;
    const { children, orientation } = this.props;
    
    const self = this,
        maxLength = children.length - 1,
        hasSideNav = orientation.match(/left|right/);
    
    let newSelectedIndex = selectedIndex;
    
    function handleKeypressTabNavigation (e) {

      e.preventDefault();

      const 
        isFirst = newSelectedIndex === 0,
        isLast = newSelectedIndex === maxLength,
        next = hasSideNav
          ? e.keyCode === 39 || e.keyCode === 40
          : e.keyCode === 39 || e.keyCode === 40,
        previous = hasSideNav
          ? e.keyCode === 37 || e.keyCode === 38
          : e.keyCode === 37 || e.keyCode === 38
      ;

      // Next tab
      if (next) {
        if (isLast) {
          newSelectedIndex = 0;
        } else {
          newSelectedIndex += 1;
        }
      }
    
      // Previous tab
      if (previous) {
        if (isFirst) {
          newSelectedIndex = maxLength;
        } else {
          newSelectedIndex -= 1;
        }
      }

      self.setState({ selectedIndex: newSelectedIndex });
    }

    document.addEventListener('keydown', handleKeypressTabNavigation);

  };

  _handleClick = (key, e) => {
    e.preventDefault();

    let {selectedIndex} = this.state,
        isArray = Array.isArray(selectedIndex);

    if (isArray && selectedIndex.indexOf(key) !== -1) {
      selectedIndex.splice(selectedIndex.indexOf(key), 1);
    } else if (isArray && selectedIndex.indexOf(key) === -1) {
      selectedIndex.push(key);
    } else {
      selectedIndex = key;
    }

    this.setState({ selectedIndex: selectedIndex });
  };

  _renderTabs = () => {

    const { checkboxAccordion, children } = this.props;
    const { selectedIndex } = this.state;

    const tabHeaderAttrs = {
      'className': 'tabs-header',
      'role': 'tablist'
    };

    function tabs (tab, key) {
      
      const { title } = tab.props;
      
      let activeClass = selectedIndex === key ? 'isActive' : '',
          ariaSelected = activeClass === 'isActive' ? 'true' : 'false';

      let tabAttrs = {
        'id': `tab-${key}`,
        'key': key,
        'onClick': this._handleClick.bind(this, key),
        'className': `isTab ${activeClass}`,
        'role': 'tab',
        'aria-selected': ariaSelected,
        'aria-controls': `panel-${key}`
      };

      return (
        <li { ...tabAttrs }>{ title }</li>
      );
    }

    if (this.props.accordion) {
      return null;
    } else {
      return (
        <ul { ...tabHeaderAttrs }>
          { children.map(tabs.bind(this)) }
        </ul>
      );
    }
  };

  _renderContent = () => {

    const { checkboxAccordion } = this.props;
    const { selectedIndex } = this.state;
    const { _handleClick } = this;

    const panelAttrs = {
      'id': `panel-${selectedIndex}`,
      'className': 'tab-panel',
      'role': 'tabpanel',
      'aria-hidden': 'false',
      'aria-labelledby': 'tab-' + this.state.selectedIndex
    };

    function tabPanels (panel, key) {
      
      const { title } = panel.props;

      const isActive = checkboxAccordion 
                      ? this.state.selectedIndex.indexOf(key) > -1 
                      : this.state.selectedIndex === key;

      const activeClass = isActive ? 'isActive' : '',
            ariaHidden = activeClass === 'isActive' ? 'false' : 'true',
            ariaSelected = activeClass === 'isActive' ? 'true' : 'false';

      const panelHeaderAttrs = {
        'className': `isTab tab-panel-header ${activeClass}`,
        'aria-selected': ariaSelected,
        'onClick': (e) => _handleClick(key, e)
      };

      panelAttrs['id'] = `panel-${key}`;
      panelAttrs['aria-hidden'] = ariaHidden;
      panelAttrs['aria-labelledby'] = `tab-${key}`;

      return (
        <div key={ key } className={ classNames(
            `tab-panel-wrapper`,
            activeClass
          ) }>
          <div { ...panelHeaderAttrs }>{ title }</div>
          <div { ...panelAttrs }>
            { panel }
          </div>
        </div>
      );
    }

    if (this.props.responsive || this.props.accordion) {
      return Array.isArray(this.props.children) 
        ? this.props.children.map(tabPanels.bind(this))
      : [this.props.children].map(tabPanels.bind(this));
    }

    return (
      <div {...panelAttrs}>
        {this.props.children[this.state.selectedIndex]}
      </div>
    );
  };

  render () {
    
    const { className, responsive, accordion, orientation } = this.props;
    
    const classes = classNames(
      `tabs`,
      className,
      { 'tabs-responsive': responsive },
      { 'tabs-accordion': accordion },
      { 'tabs-onSide': orientation === 'left' || orientation === 'right' }
    );
        
    return (
      <div className={ classes }>
        {this._renderTabs()}
        {this._renderContent()}
      </div>
    );
    
  }
};

class TabPanel extends React.Component {
  constructor (props) {
    super(props);
  }
  render () {
    
    const { children } = this.props;
    
    return (
      <div>{ children }</div>
    );
  }
};

const App = () => {
  return (
    <div className="tabs-wrapper">
      <Tabs className="tabs-justified" responsive>
        <TabPanel title="Tab Header">
          Tab content 1...
        </TabPanel>
        <TabPanel title="Tab Header">
          Tab content 2...
        </TabPanel>
        <TabPanel title="Tab Header">
          Tab content 3...
        </TabPanel>
        <TabPanel title="Tab Header">
          Tab content 4...
        </TabPanel>
      </Tabs>
    </div>
  );
};

render(<App />, document.body);
            
          
!
999px
Loading ..................

Console