npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-tab-state

v1.1.0

Published

state-aware tab components in react

Downloads

73

Readme

react-tab

state-aware tab components in react built with Next.js useRouter() hook.

Why?

This started off from working with the Tab component in ChakraUI. Although, there's a "managing active tab state" section in their docs and that may suffice for some of your use-cases.

With react-tabs, you might not need to write the logic to manage just the active state of your tab items or use a state management library. The state of the tab component is preserved in the browser URL when you switch between routes.

Say, for example, there are four tabItems, you procced to select the second tab item and navigate to another route or you refresh the current page, React re-renders the component and the state is lost, hence the active component goes back to being the first tabItem.

This becomes an issue when you're working in large applications, where tiny details — like preserving the tab state — that improves the overall UX of your app depends on these side effects.

The browser URL can be used as a global state store which we'll use to keep the state of this component.

Usage

Currently, react-tab has support for barebones react projects — that do not depend on a component UI library like Chakra UI etc. —

Support for chakra-ui is going on though. It should be live soon — or never. it all depends on the bandwidth i can give to it 🥲

To use the component, you can install it with your preferred package manager. The command below ascertains that.

yarn add react-tab-state

The component expects you to have an array of tabItems you wish to render, something like the one below;

import React from "react";

const Component = ({ text: string }) => <h1>{text}</h1>;

export const data = [
  {
    name: "first tab",
    component: <Component text="Component One" />,
  },
  {
    name: "second tab",
    component: <Component text="Component Two" />,
  },
  {
    name: "third tab",
    component: <Component text="Component Three" />,
  },
];

It should have the name and component properties in it. The way you chose to structure it is completely up to you. But, it is important that you pass an array of objects to the tabItems props like so:

import { Tab } from "react-tab-state";

export default function HomePage() {
  return (
    <>
      <Tab tabItems={data} />
    </>
  );
}

Because you might wan to customize the look and feel of tabHeader. react-tab exposes a theme prop that accepts any color as a string depending on the design tokens that you're working with.

<Tab tabItems={data} theme="brown" />

perks

  • state preservation any tabItem when you navigate away from and back to the current route.
  • you can copy the link in the URL share it and the state is shared across devices
  • I can't remember the last one....

setbacks

Because, the component uses next/router when you try using it outside of a Next.js project, you'll get an error indicating that "NextRouter is not mounted".

Support for react projects without Next.js should be up soon.

Want to contribute?

See the CONTRIBUTING guide.