0.0.8 • Published 5 years ago

the-captains-hooks v0.0.8

Weekly downloads
2
License
ISC
Repository
github
Last release
5 years ago

The Captain's Hooks

Build Status

A collection of React + TypeScript utility hooks, mostly stolen from the wonderful useHooks.

Hooks

useFocus

Returns a boolean value reflecting the focus state and two methods to apply to the component to be watched.

const ExampleComponent: React.FC = props => {
  const [focused, focusMethods] = useFocus();
  return <input {...focusMethods} value={focused.toString()} readOnly />;
}

Alternatively:

const ExampleComponent: React.FC = props => {
  const [focused, focusMethods] = useFocus();
  return (
    <input
      value={focused.toString()}
      readOnly
      onFocus={focusMethods.onFocus}
      onBlur={focusMethods.onBlur}
    />
  );
}

useHover

Almost identical to useFocus, returns a boolean value reflecting the hover state and two methods to apply to the component to be watched.

const ExampleComponent: React.FC = props => {
  const [hovered, hoverMethods] = useHover();
  return (
    <div {...hoverMethods}>
      {hover.toString()}
    </div>
  );
}

Alternatively:

const ExampleComponent: React.FC = props => {
  const [hovered, hoverMethods] = useHover();
  return (
    <div
      mouseOver={hoverMethods.mouseOver}
      mouseOut={hoverMethods.mouseOut}
    >
      {hover.toString()}
    </div>
  );
}

useScrollTo

Returns a method that scrolls a component with a given ref into view.

const ExampleComponent: React.FC = props => {
  const ref = React.useRef<HTMLDivElement>(null);
  const scrollTo = useScrollTo(ref);

  return (
    <>
      <div id="some-div" ref={ref} />
      <button onClick={scrollTo}>Scroll back to div</button>
    </>
  )
}
0.0.8

5 years ago

0.0.7

5 years ago

0.0.6

5 years ago

0.0.5

5 years ago

0.0.4

5 years ago

0.0.3

5 years ago

0.0.1

5 years ago