Reusable way to create smooth tab highlights.

Overview

useTabs

Reusable way to create smooth tab highlights.

Logo

Installation

Install my-project with npm

  npm install @olivieralexander/usetabs

Usage/Examples

To get started you will need a couple things:

  • A component or element to be used as the highlighter.
  • A container element with a ref and position relative that will wrap your tabs.

useTabs will return 2 things:

  • setHightlight(): function to trigger the hightlight
  • hightlightStyles: object containing styles that will need to be applied on your highlight element.

Note: These examples use TailwindCSS but you're free to use any styling library you want.

setHightlight

<li onMouseEnter={setHightlight}>
    Tab 1
</li>

hightlightStyles

<div
style={highlightStyles}
className="bg-gray-500 bg-opacity-10 rounded-sm"
/>

Full Example:

import { useRef } from "react";
import useTabs from "../hooks/useTabs";

export default function Home() {
  const containerRef = useRef(null);
  const defaultRef = useRef(null);

  const { setHightlight, highlightStyles } = useTabs({
    container: containerRef,
    // defaultTab: defaultRef,
    // duration: 150,
  });

  const tabs = [
    {
      name: "Tab 1",
      id: "tab-1",
    },
    {
      name: "Tab 2",
      id: "tab-2",
    },
    {
      name: "Tab 3",
      id: "tab-3",
    },
  ];

  return (
    <main className="w-screen h-screen grid place-items-center">
      <ul className="w-[50%] flex justify-between relative" ref={containerRef}>
        {tabs.map((tab, i) => (
          <li
            key={tab.id}
            className="py-2 px-8 border rounded cursor-pointer"
            ref={i === 1 ? defaultRef : null}
            onMouseEnter={setHightlight}
          >
            {tab.name}
          </li>
        ))}

        <div
          style={highlightStyles}
          className="bg-gray-500 bg-opacity-10 rounded-sm"
        />
      </ul>
    </main>
  );
}

API Reference

useTabs

Parameter Type Description
container ref Required. Ref of your tab container
defaultTab ref Ref of a tab you would like to set as the default highlighted tab
duration number Number to represent the amount of time in ms how long the animations should take

Acknowledgements

  • Emil Kowalksi

License

MIT

You might also like...

WalkThru is a better way of explaining code on the web.

WalkThru WalkThru is a better way of explaining code on the web. Check out the live demo to see how it works. See the live demo source code if you wan

Dec 28, 2022

A complete habits manager, where you can track your progress and complete each daily activity in an organized way.

TrackIt Habit manager in a dynamic, clear and simple way. TackIt is an application that seeks to make it simple and accessible for any user to control

Dec 31, 2022

⚪ SVG-Powered component to easily create skeleton loadings.

⚪ SVG-Powered component to easily create skeleton loadings.

SVG-Powered component to easily create placeholder loadings (like Facebook's cards loading). Features ⚙️ Customizable: Feel free to change the colors,

Jan 4, 2023

✨ Create server-rendered universal JavaScript applications with no configuration

✨ Create server-rendered universal JavaScript applications with no configuration

Universal JavaScript applications are tough to setup. Either you buy into a framework like Next.js or Nuxt, fork a boilerplate, or set things up yours

Jan 7, 2023

📄 Create PDF files using React

📄  Create PDF files using React

React renderer for creating PDF files on the browser and server Lost? This package is used to create PDFs using React. If you wish to display existing

Jan 7, 2023

Mobile app development framework and SDK using HTML5 and JavaScript. Create beautiful and performant cross-platform mobile apps. Based on Web Components, and provides bindings for Angular 1, 2, React and Vue.js.

Mobile app development framework and SDK using HTML5 and JavaScript. Create beautiful and performant cross-platform mobile apps. Based on Web Components, and provides bindings for Angular 1, 2, React and Vue.js.

Onsen UI - Cross-Platform Hybrid App and PWA Framework Onsen UI is an open source framework that makes it easy to create native-feeling Progressive We

Jan 8, 2023

🎥 Create videos programmatically in React

🎥      Create videos programmatically in React

Join the Discord Remotion is a suite of libraries building a fundament for creating videos programmatically using React. Why create videos in React? L

Dec 31, 2022

This project was bootstrapped with Chakra UI & Create React App

This project was bootstrapped with Chakra UI & Create React App

Getting Started with Create React App This project was bootstrapped with Chakra UI & Create React App. ScreenShots Available Scripts In the project di

Dec 11, 2022

This command line helps you create components, pages and even redux implementation for your react project

This command line helps you create components, pages and even redux implementation for your react project

react-help-create This command line helps you create components, pages and even redux implementation for your react project. How to install it? To ins

Dec 10, 2022
Releases(v0.0.5)
  • v0.0.5(Jan 13, 2022)

    • when passing null, false or undefined to setHightlight(), the highlight is now automatically hidden.
    • updated README on setHighlight() function

    Full Changelog: https://github.com/OlivierDijkstra/useTabs/compare/v0.0.4...v0.0.5

    Source code(tar.gz)
    Source code(zip)
  • 0.0.2(Jan 10, 2022)

  • 0.0.1(Jan 10, 2022)

    Initial release

    What's Changed

    • added typescript support by @OlivierDijkstra in https://github.com/OlivierDijkstra/useTabs/pull/1

    New Contributors

    • @OlivierDijkstra made their first contribution in https://github.com/OlivierDijkstra/useTabs/pull/1

    Full Changelog: https://github.com/OlivierDijkstra/useTabs/commits/0.0.1

    Source code(tar.gz)
    Source code(zip)
Owner
OlivierAlexander
OlivierAlexander
A reusable react hook that preserves a components semantic accessibility to create a visual block link.

useAccessibleBlockLink is a reusable react hook that preserves a components semantic accessibility to create a visual block link. This hook supports multiple links within the same block.

Wayfair Tech – Incubator 4 Nov 28, 2022
Create a performant distributed context state by synergyzing atomar context pieces and composing reusable state logic.

Synergies Create a performant distributed context state by synergyzing atomar context pieces and composing reusable state logic. synergies is a tiny (

Lukas Bach 8 Nov 8, 2022
Customizable plugin, smooth scroll up button, on native JavaScript

up-button Customizable plugin, smooth scroll up button, on native JavaScript Description Up button initialization, call up() function, in your js file

Alexandr Zabolotny 2 May 24, 2022
A new way to create navigation in react-native

React Native Url Router Documentation page Motivation React Native Url Router aims to simplify native navigation patterns. It allows for native naviga

Software Mansion – Labs 130 Dec 14, 2022
TryShape is an open-source platform to create shapes of your choice using a simple, easy-to-use interface. You can create banners, circles, polygonal shapes, export them as SVG, PNG, and even as CSS.

Create, Export, Share, and Use any Shapes of your choice. View Demo · Report Bug · Request Feature ?? Introducing TryShape TryShape is an opensource p

TryShape 148 Dec 26, 2022
An application that has a frontend (user interface) that allows you to create, read, update or delete (CRUD) products using an API in which you can also create, read, update or delete products.

CRUD app with React and Firebase 9 An application that has a frontend (user interface) that allows you to create, read, update or delete (CRUD) produc

Júlio Bem 3 Sep 28, 2021
Bdc-walkthrough - An Angular Material library for displaying walk-through pop-ups and dialogs using a declarative way.

Material walk-through library An Angular Material library for displaying walk-through pop-ups and dialogs using a declarative way. Show Demo Prerequis

Broadcom Inc 65 Dec 14, 2022
a more intuitive way of defining private, public and common routes for react applications using react-router-dom v6

auth-react-router is a wrapper over react-router-dom v6 that provides a simple API for configuring public, private and common routes (React suspense r

Pasecinic Nichita 12 Dec 3, 2022
The easiest way to check who hasn't followed you back on Instagram

Instagram Unfollowers The easiest way to check who hasn't followed you back on Instagram is to do it manually, keeping track of the exact number of fo

Andrei Voicu 26 Dec 14, 2022
🐥 A very simple way to transform antd table to xlsx

antd-table-2-xlsx antd-table-2-xlsx A very simple way to transform antd table to xlsx . Get Start! Install pnpm add @sudongyuer/antd-table-2-xlsx Usa

Frozen FIsh 18 Aug 15, 2022