everweij / react-laag

Hooks to build things like tooltips, dropdown menu's and popovers in React
https://www.react-laag.com
MIT License
907 stars 47 forks source link

Feature Request: useTooltip() support for focus/blur events #35

Closed TheTedAdams closed 3 years ago

TheTedAdams commented 4 years ago

Apologies if there is an easy way to do this that I'm missing, but I would like my tooltips to show when the target is focused. For example, I like to do this on IconButton's (A button that just has an Icon and an aria-label) so that when the user hovers over the IconButton or tabs to it, they get a helpful tooltip. useTooltip makes supporting hover very easy (since it uses useHover), but I'd love to support both hover and focus.

TheTedAdams commented 4 years ago

Just as a reference, Tippy.js exposes this via a Trigger option: image From their docs

everweij commented 3 years ago

Hi @TheTedAdams , Sorry for the super late response. I know it's been a year since you openen this issue, but I hope I can help you nonetheless. V2 brings a lot more flexibility when it comes you controlling when to show or hide the layer. Here's an example: https://storybook.react-laag.com/?path=/docs/password-validation--page

In essence:

function Example() {
  const [isOpen, setOpen] = React.useState(false);

  const { triggerProps, layerProps, renderLayer } = useLayer({
    isOpen,
  });

  return (
    <>
      <input
        onFocus={() => setOpen(true)}
        onBlur={() => setOpen(false)}
        {...triggerProps}
      />
      {isOpen && renderLayer(<div {...layerProps}>Layer!</div>)}
    </>
  );
}

Hopefully this answers your question

I will close this issue for now. Feel free to re-open this issue if things are not clear enough, or open a new one if you run into other issues.