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

useisopen

v1.1.1

Published

useIsOpen is a react hook for managing open and close states, designed to simplify the management of open/close states

Downloads

4

Readme

useIsOpen

useIsOpen is a react hook for managing open and close states, designed to simplify the management of open/close states

Installation

Install the package using npm:

npm install useisopen

Or with yarn:

yarn add useisopen

Usage:

import useIsOpen from 'use-is-open'

function App() {
  const [isOpen, { open, manualClose }] = useIsOpen({
    open(e) {
      log(e)
    },
    manualClose([state, setState, reset], anchor) {
      log(anchor)
      setState(null)
    },
  })

  return (
    <React.Fragment>
      <Button onClick={open}>Open Modal</Button>
      <Modal open={isOpen} onClose={manualClose} />
    </React.Fragment>
  )
}

Configuration Options

  • initialState (optional): The initial state of your component (default: false).
  • openIndicator (optional): Indicator for the open state (default: true).
  • closeIndicator (optional): Indicator for the close state (default: false).
  • open (optional): Custom logic to execute when opening.
  • close (optional): Custom logic to execute when closing.
  • manualOpen (optional): Custom logic for manual opening, you have to set the open state yourself.
  • manualClose (optional): Custom logic for manual closing, you have to set the close state yourself.

When useIsOpen is called, it will return an array of two members, the first member is the current opening state, initially, it's defined in your configuration object (which you can provide to useIsOpen(config)), it's the initialState property. by default the initialState is config.closeIndicator, which is false. you can tweak that of course, using the config object as well.

the second member in the returned array is an object holding four methods, open, close, manualOpen, manualClose.

open and close, you can invoke them when you want to open/close something. Both methods can be called with one argument. these arguments can be used within your config object as parameters. You can have any argument you want.

In case you wanted to pass multiple arguments, you pass them as one argument in an array or object. for example:

open([a, b, c])

you can then use a, b and c as follows in the config object as follows:

const [isOpen, { open, manualClose }] = useIsOpen({
  open(args) {
    args[0] // the variable a
    args[1] // the variable b
    args[2] // the variable c
  },
})

When the function open is invoked, the state is changed to indicate that the modal, menu, or whatever you're working on is now opened.

close is to set the state to close that thing.

in case you want to have more control, use the manualOpen and manualClose methods instead.

They give you the freedom to set the state yourself, so you can for example set the state as follows:

const [isOpen, { open, manualClose }] = useIsOpen({
  manualOpen(stateToolkit, args) {
    const [state, setState, reset] = stateToolkit
    setState((previousState) => !previousState) // a react thing, nothing fancy. useful for setting the state synchronously
  },
})

manualOpen and manualClose gets called with two arguments, the stateToolkit argument, which is an array holding three members, [state, setState, reset].

  • state and setState are obvious, they're the react useState() returned values.
  • reset is a method to set the state (setState) back to the initialState.

The second argument manualOpen and manualClose gets called with is the argument you provide when you call these methods.

Contributing

Contributions are welcome! Please see our Contributing Guidelines for more details.

Issues

If you encounter any issues or have questions, please open an issue on the GitHub repository.