475

useBoolean

Hook provides opportunity to manage boolean state

statenecessarytest coverage

Toggle password visibility

The current state is hidden

import { useBoolean } from '@siberiacancode/reactuse';
import { Eye, EyeOff } from 'lucide-react';

const Demo = () => {
  const [visible, toggle] = useBoolean();

  return (
    <section className='flex min-w-sm flex-col gap-4'>
      <p>Toggle password visibility</p>

      <div className='relative'>
        <input
          className='pr-10'
          data-id='password'
          defaultValue='mysecretpassword'
          placeholder='Enter your password'
          type={visible ? 'text' : 'password'}
        />

        <button
          className='absolute top-1/2 right-0 -translate-y-1/2'
          data-variant='unstyled'
          type='button'
          onClick={() => toggle()}
        >
          {visible ? <EyeOff className='size-4' /> : <Eye className='size-4' />}
        </button>
      </div>

      <p className='text-xs'>
        The current state is <code>{visible ? 'shown' : 'hidden'}</code>
      </p>
    </section>
  );
};

export default Demo;

Installation

pnpm add @siberiacancode/reactuse

Usage

const [on, toggle] = useBoolean()

Type Declarations

export type UseBooleanReturn = [
  /** The current boolean state value */
  value: boolean,
  /** Function to toggle the boolean state */
  toggle: (value?: boolean) => void
];

API

Parameters

NameTypeDefaultNote
initialValuebooleanfalseThe initial boolean value

Returns

UseBooleanReturn

Contributors

ddebabinbbabin

Last updated on