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

@olivieralexander/usetabs

v1.0.3

Published

Reusable implementation of Emil Kowalskis smooth tab hover effect

Downloads

7

Readme

useTabs

Reusable way to create smooth tab highlights.

Logo

Installation

Install with npm

  npm install @olivieralexander/usetabs

Yarn

  yarn add @olivieralexander/usetabs

Pnpm

  pnpm add @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(ref | e): function to trigger the hightlight, takes either a react ref, focus or mouseEnter event.
  • 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} onFocus={setHighlight}>
    Tab 1
</li>

It is also advised to add a onMouseLeave event on the container to hide the highlight when the user finished hovering.

<div className="container" ref={containerRef} onMouseLeave={() => setHighlight(null)} />

hightlightStyles

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

Full Example:

import { useRef } from "react";
import useTabs from "@olivieralexander/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 md:flex-row gap-2 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

License

MIT