Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

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

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

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

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

CSS

              
                $tablet: 768px;
$white: #ffffff;
$scrollbar-color: #d2d9dc;
$grey1: #f9f9f9;
$grey2: #dddddd;
$grey3: #f1f1f1;
$dark-grey1: #888888;
$dark-grey2: #666666;
$dark-grey3: #444444;
$purple: #8e54e9;

/****** no tabs styles ******/
body {
  margin: 0;
  padding: 0;
}

.container {
  width: calc(100vw - 1rem);
  max-width: 950px;
  margin: 10px auto;

  h2 {
    text-align: center;
  }

  hr {
    margin: 30px auto;
  }
}

/****** tabs styles ******/
@keyframes rightToLeft {
  0% {
    transform: translateX(100%);
  }
  100% {
    transform: translateX(0%);
  }
}

@keyframes leftToRight {
  0% {
    transform: translateX(-100%);
  }
  100% {
    transform: translateX(0%);
  }
}

@keyframes bottomToTop {
  0% {
    transform: translateY(100%);
  }
  100% {
    transform: translateY(0%);
  }
}

@keyframes topToBottom {
  0% {
    transform: translateY(-100%);
  }
  100% {
    transform: translateY(0%);
  }
}

//for the fancy scrollbar
::-webkit-scrollbar {
  width: 8px;
  height: 8px;
}
::-webkit-scrollbar-track {
  background: transparent;
}
::-webkit-scrollbar-thumb {
  background-color: $scrollbar-color;
  border-radius: 6px;
}

// tabs component
.tabs {
  display: flex;
  flex-direction: column;
  width: 100%;

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

  .labels-wrapper {
    display: flex;
    flex-wrap: wrap;

    .tab-label {
      background: $grey1;
      border-radius: 0.25rem 0.25rem 0 0;
      color: $dark-grey1;
      font-size: 1rem;
      text-align: center;
      line-height: 2rem;
      margin-right: 0;
      margin-bottom: 0.25rem;
      padding: 0 1.5rem;
      user-select: none;
      cursor: pointer;
      flex-basis: 50%;
      white-space: nowrap;

      &:hover {
        background: $grey2;
        color: $dark-grey2;
      }

      &:nth-of-type(odd) {
        border-right: 2px solid $white;
      }

      &:nth-of-type(even) {
        border-left: 2px solid $white;
      }

      @media (min-width: $tablet) {
        flex-basis: auto;
        margin-right: 0.25rem;
        margin-bottom: 0;

        &:nth-of-type(odd),
        &:nth-of-type(even) {
          border: none;
        }
      }
    }

    .active-tab-label {
      background: $grey3;
      color: $dark-grey3;
      z-index: 6;

      &:hover {
        background: $grey3;
        color: $dark-grey3;
        cursor: initial;
      }
    }
  }

  .fancy-labels-wrapper {
    display: flex;
    justify-content: space-between;
    align-items: center;

    .labels {
      display: flex;
      overflow: auto;
      padding-bottom: 5px;
    }
    .tab-additional-icon {
      padding-bottom: 10px;
      margin-inline-start: 20px;
    }

    .tab-label {
      background: $grey1;
      border-radius: 20px;
      border: 1px solid transparent;
      color: $dark-grey1;
      font-size: 14px;
      text-align: center;
      line-height: 2rem;
      margin-inline-end: 0.25rem;
      margin-bottom: 0.25rem;
      padding: 0 1.5rem;
      user-select: none;
      cursor: pointer;
      white-space: nowrap;

      &:hover {
        background: $grey2;
        color: $dark-grey2;
      }
    }

    .active-tab-label {
      background-color: transparent;
      color: $dark-grey3;
      z-index: 6;
      border: 1px solid $purple;

      &:hover {
        background-color: transparent;
        color: $dark-grey3;
        cursor: initial;
      }
    }
  }

  .tabs-content-wrapper {
    position: relative;
    overflow: hidden;
    display: flex;
    background-color: $grey3;

    .tab-content {
      opacity: 0;
      padding: 1.5rem;
      transition: all linear 0.1s;
      top: 0;
      left: 0;
      right: 0;
      bottom: 0;
      position: absolute;
      display: block;
      overflow: hidden;
      flex-basis: 100%;
    }

    .active-tab-content {
      opacity: 1;
      position: relative;
      overflow-x: hidden;
      overflow-y: auto;
      z-index: 1;
      flex-grow: 1;

      &.right-to-left-animation {
        animation: rightToLeft 0.5s;
      }

      &.left-to-right-animation {
        animation: leftToRight 0.5s;
      }
    }
  }

  &.vertical-tabs {
    flex-direction: row;

    .labels,
    .labels-wrapper {
      flex-direction: column;
    }

    .labels-wrapper {
      .tab-label {
        margin: 0 0 4px 0;
        border-radius: 0.25rem 0 0 0.25rem;
        flex-basis: auto;
        border: none;
      }
    }

    .tabs-content-wrapper {
      flex: 1;

      .active-tab-content {
        &.bottom-to-top-animation {
          animation: bottomToTop 0.5s;
        }

        &.top-to-bottom-animation {
          animation: topToBottom 0.5s;
        }
      }
    }
  }
}

              
            
!

JS

              
                const { useState, useEffect, useMemo } = React,
  { BrowserRouter, useHistory } = ReactRouterDOM;

//conditional wrapper component
const ConditionalWrapper = ({ initialWrapper, condition, wrapper, children }) =>
  condition ? wrapper(children) : initialWrapper(children);

//tabs component
const Tabs = ({ activeTab, tabsList, isFancyTabs = true, isVertical, isRightToLeftLanguage }) => {
  const history = useHistory(),
    [isUrlTabs, setIsUrlTabs] = useState(false),
    [currentActiveTab, setCurrentActiveTab] = useState(),
    [previousActiveTab, setPreviousActiveTab] = useState(0),
    animationClassName = useMemo(() => {
      if (currentActiveTab !== undefined) {
        if (isVertical) {
          if (currentActiveTab < previousActiveTab) {
            return 'bottom-to-top-animation';
          } else {
            return 'top-to-bottom-animation';
          }
        } else {
          if (isRightToLeftLanguage) {
            if (currentActiveTab < previousActiveTab) {
              return 'left-to-right-animation';
            } else {
              return 'right-to-left-animation';
            }
          } else {
            if (currentActiveTab < previousActiveTab) {
              return 'right-to-left-animation';
            } else {
              return 'left-to-right-animation';
            }
          }
        }
      }
    }, [isVertical, isRightToLeftLanguage, currentActiveTab, previousActiveTab]);

  useEffect(() => {
    if (tabsList.find((el) => el.tabUrl)) {
      setIsUrlTabs(true);
    } else {
      if (activeTab !== 0 && activeTab && activeTab < tabsList.length) {
        setCurrentActiveTab(activeTab);
      } else {
        setCurrentActiveTab(0);
      }
    }
    // eslint-disable-next-line
  }, []);

  useEffect(() => {
    if (isUrlTabs) {
      const currentActiveTabUrlIndex = tabsList.findIndex(
        (el) => el.tabUrl === history.location.pathname
      );
      setCurrentActiveTab(currentActiveTabUrlIndex !== -1 ? currentActiveTabUrlIndex : 0);
      setPreviousActiveTab(currentActiveTab ?? 0);
    }
    // eslint-disable-next-line
  }, [history.location.pathname, isUrlTabs]);

  const onChangeTab = (index, tabUrl) => {
    if (tabUrl) history.push(tabUrl);
    else {
      setPreviousActiveTab(currentActiveTab ?? 0);
      setCurrentActiveTab(index);
    }
  };

  return (
    <div className={`tabs ${isVertical ? 'vertical-tabs' : ''}`}>
      <div className={`${isFancyTabs ? 'fancy-labels-wrapper' : 'labels-wrapper'}`}>
        <ConditionalWrapper
          initialWrapper={(children) => <>{children}</>}
          wrapper={(children) => <div className="labels">{children}</div>}
          condition={isFancyTabs}
        >
          {tabsList.map((el, i) => (
            <div
              className={`tab-label ${currentActiveTab === i ? ' active-tab-label' : ''}`}
              key={i}
              onClick={() => {
                onChangeTab(i, el.tabUrl);
              }}
            >
              {el.label}
            </div>
          ))}
        </ConditionalWrapper>
      </div>
      <div className="tabs-content-wrapper" style={{ borderRadius: isFancyTabs ? 10 : 0 }}>
        {tabsList.map((el, i) => (
          <div
            className={`tab-content ${
              currentActiveTab === i ? ' active-tab-content' : ''
            } ${animationClassName}`}
            key={i}
          >
            {currentActiveTab === i && el.content}
          </div>
        ))}
      </div>
    </div>
  );
};

const App = () => {
  const tabsList = [
    {
      label: 'First',
      // use the following if you want to bind active tab with URL
      // tabUrl: '/first',
      content: (
        <div>
          <p>First tab content</p>
          <p>
            Lorem ipsum dolor sit amet, consectetur adipisicing elit. Aliquid cupiditate deserunt
            eos maiores maxime molestiae odit vitae!
          </p>
        </div>
      ),
    },
    {
      label: 'Second',
      content: (
        <div>
          <p>Second tab content</p>
          <p>
            Lorem ipsum dolor sit amet, consectetur adipisicing elit. Aliquid cupiditate deserunt
            eos maiores maxime molestiae odit vitae!
          </p>
        </div>
      ),
    },
    {
      label: 'Third',
      content: (
        <div>
          <p>Third tab content</p>
          <p>
            Lorem ipsum dolor sit amet, consectetur adipisicing elit. Aliquid cupiditate deserunt
            eos maiores maxime molestiae odit vitae!
          </p>
        </div>
      ),
    },
    {
      label: 'Fourth',
      content: (
        <div>
          <p>Fourth tab content</p>
          <p>
            Lorem ipsum dolor sit amet, consectetur adipisicing elit. Aliquid cupiditate deserunt
            eos maiores maxime molestiae odit vitae!
          </p>
        </div>
      ),
    },
    {
      label: 'Fifth',
      content: (
        <div>
          <p>Fifth tab content</p>
          <p>
            Lorem ipsum dolor sit amet, consectetur adipisicing elit. Aliquid cupiditate deserunt
            eos maiores maxime molestiae odit vitae!
          </p>
        </div>
      ),
    },
    {
      label: 'Sixth',
      content: (
        <div>
          <p>Sixth tab content</p>
          <p>
            Lorem ipsum dolor sit amet, consectetur adipisicing elit. Aliquid cupiditate deserunt
            eos maiores maxime molestiae odit vitae!
          </p>
        </div>
      ),
    },
    {
      label: 'Seventh',
      content: (
        <div>
          <p>Seventh tab content</p>
          <p>
            Lorem ipsum dolor sit amet, consectetur adipisicing elit. Aliquid cupiditate deserunt
            eos maiores maxime molestiae odit vitae!
          </p>
        </div>
      ),
    },
  ];

  return (
    <div className="container">
      {/*Tabs properties:
    - isFancyTabs => boolean for fancy tabs styles (default true)
    - isVertical => boolean to display tabs vertically (default false)
    - isRightToLeftLanguage => boolean to update animation direction based on language direction (default: false)
    - activeTab => receives a number which represents the active tab (default is 0)
    - tabsList => array of tabs*/}
      <h2>Fancy tabs</h2>
      <Tabs
        // activeTab={0}
        tabsList={tabsList}
      />
      <hr />
      <h2>Classic tabs</h2>
      <Tabs isFancyTabs={false} tabsList={tabsList} />
      <hr />
      <h2>Fancy vertical tabs</h2>
      <Tabs isVertical tabsList={tabsList} />
      <hr />
      <h2>Classic vertical tabs</h2>
      <Tabs isFancyTabs={false} isVertical tabsList={tabsList} />
    </div>
  );
};

ReactDOM.render(
  <BrowserRouter>
    <App />
  </BrowserRouter>,
  document.querySelector('#app')
);

              
            
!
999px

Console