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

use-advanced-toggle

v1.0.2

Published

A custom React hook for cycling through multiple states with ease. (not limited to just true and false).

Downloads

30

Readme

use-advanced-toggle

use-advanced-toggle is a custom React hook that allows you to easily cycle through multiple states (not limited to just true and false). It's designed to enhance the functionality of toggling between values, making it perfect for use cases such as switching themes, controlling animations, or any scenario where you need to cycle through different options.

Table of Contents

Features

  • Cycle through multiple states using a simple API.
  • Supports any type of values, including strings and objects.
  • Handles edge cases such as empty states and single states gracefully.
  • Easy to integrate into both TypeScript and JavaScript projects.

Installation

To install the use-advanced-toggle package, run:

npm install use-advanced-toggle

or if you are using Yarn:

yarn add use-advanced-toggle

Usage

Here’s how to use the use-advanced-toggle hook in your React component:

import React from 'react';
import useAdvancedToggle from 'use-advanced-toggle';

const MyComponent = () => {
  const [color, toggleColor] = useAdvancedToggle(['red', 'green', 'blue']);

  return (
    <div>
      <p>The current color is: {color}</p>
      <button onClick={toggleColor}>Toggle Color</button>
    </div>
  );
};

export default MyComponent;

API

useAdvancedToggle(states: string[])

Parameters

  • states: An array of states you want to toggle between (can be strings, numbers, or objects).

Returns

  • An array where:
    • The first element is the current state.
    • The second element is a function to toggle to the next state.

Example

const [currentState, toggle] = useAdvancedToggle(['state1', 'state2', 'state3']);

Testing

To run tests for the use-advanced-toggle hook, ensure you have the testing dependencies installed:

npm install --save-dev @testing-library/react @testing-library/jest-dom

Then you can run your tests using:

npm test

Example Test Case

Here's an example of how you might test your hook:

import { renderHook, act } from '@testing-library/react';
import useAdvancedToggle from '../hooks/useAdvancedToggle';

describe('useAdvancedToggle', () => {
  it('should toggle between states', () => {
    const states = ['red', 'green', 'blue'];
    const { result } = renderHook(() => useAdvancedToggle(states));

    expect(result.current[0]).toBe('red');
    act(() => result.current[1]());
    expect(result.current[0]).toBe('green');
    act(() => result.current[1]());
    expect(result.current[0]).toBe('blue');
    act(() => result.current[1]());
    expect(result.current[0]).toBe('red');
  });
});

Contributing

Contributions are welcome! If you have suggestions for improvements, feel free to open an issue or submit a pull request.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/NewFeature)
  3. Commit your changes (git commit -m 'Add some feature')
  4. Push to the branch (git push origin feature/NewFeature)
  5. Open a pull request

License

This project is licensed under the MIT License.


Feel free to adjust any sections to better fit your project!