1.0.4 • Published 2 years ago

react-tabbed-area v1.0.4

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

react-tabbed-area

Easy to use tabbing component for React

Installation

npm install react-tabbed-area

Usage

An example of basic usage, tabs on top with some simple content

import React, { useState } from "react";
import TabbedArea from "react-tabbed-area";

const MyContent = () => {
  const tabs = ["Tab 1", "Tab 2", "Tab 3"];
  const [selectedTab, setSelectedTab] = useState(tabs[0]);

  return (
    <TabbedArea
      tabs={tabs}
      selectedTab={selectedTab}
      onSelectTab={(tab) => setSelectedTab(tab)}
    >
      {selectedTab === "Tab 1" ? (
        <>Tab One Content</>
      ) : selectedTab === "Tab 2" ? (
        <>Tab Two Content</>
      ) : selectedTab === "Tab 3" ? (
        <>Tab Three Content</>
      ) : null}
    </TabbedArea>
  );
};

Example1

Props

Prop NameDescriptionExample Values
tabsArray of tab names, or Array of Objects with the following properties: name, icon, hideName. Tab names must be unique to each TabbedArea"Tab 1", "Tab 2", {name: "Tab 1", icon: , hideName: true}, {name: "Tab 2"}
selectedTabstring or object with a name property. This item should use the state of your parent component to force the rerender upon tab switch
onSelectTabcallback function passed when a user clicks a tab, will pass back the tab name or entire tab object. Use this to set the state of selectedTab(tab) => setSelectedTab(tab)
childrenany content that you wish to display within the tabbed area. Use a check for the selectedTab to display different content for each tabAny Component(s)
tabPlacementthe position of the tabs, either "top", "left", or "right", default is "top"
backgroundColorthe main background color of the tabbed area and the selected tabany hex or html color
colorthe main text color for the selected tabany hex or html color
inactiveBackgroundColorthe background color of a tab when it is inactiveany hex or html color
inactiveTextColorthe color of the text on a tab when it is inactiveany hex or html color
hoverBackgroundColorthe background color of a tab when it is not selected and is hovered overany hex or html color
hoverTextColorthe text color of a tab when it is not selected and is hovered overany hex or html color

Using a custom component for the tab name

Rather than pass an array of strings as your tabs you can use an array of objects. This will allow you to provide icons or simply a custom component as the label for each tab.

If using the object method you must include the name property in each tab, as that is how the selectedTab check will be performed. Other available properties include icon, which is where you will provide your custom component, and hideName, a self explanatory boolean.

You are able to pass any combination of text or objects to the tabs prop. Below is an example of using an image as the first tab label, but regular text as the second label, with some custom coloring.

You will need to use a slightly more complex selectedTab checking function to render your content correctly.

import React, { useState } from "react";
import TabbedArea from "react-tabbed-area";

const MyContent = () => {
  const tabs = [
    {
      name: "Tab 1",
      icon: (
        <img
          src="https://upload.wikimedia.org/wikipedia/commons/thumb/a/a7/React-icon.svg/2300px-React-icon.svg.png"
          width={35}
          height={25}
        />
      ),
      hideName: true,
    },
    "Tab 2",
  ];
  const [selectedTab, setSelectedTab] = useState(tabs[0]);

  const isSelected = (tab) => {
    const tabName = selectedTab.name || selectedTab;
    return tabName === tab;
  };

  return (
    <TabbedArea
      tabs={tabs}
      selectedTab={selectedTab}
      onSelectTab={(tab) => setSelectedTab(tab)}
    >
      {isSelected("Tab 1") ? (
        <>Tab One Content</>
      ) : selectedTab === "Tab 2" ? (
        <>Tab Two Content</>
      ) : null}
    </TabbedArea>
  );
};

Example2

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago