question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to have "onClickOutside" ?

See original GitHub issue

Hello here 👋

I have a specific use case where I want to display the Tippy component only on desktop views like following:

const defaultTippyProps: Omit<TippyProps, "content" | "children"> = {
  animation: "shift-away-subtle",
  arrow: true,
  interactive: true,
  trigger: "manual",
  interactiveBorder: 10,
  placement: "bottom-end",
  delay: [0, 0],
  duration: [200, 150],
  maxWidth: 500,
  appendTo: document.body,
  sticky: true,
  plugins: [sticky],
  boundary: "viewport",
  theme: "light",
  distance: 24,
  inertia: true,
}

const [showPopover, setShowPopover] = useState(false)
const open = () => setShowPopover(true)
const close = () => setShowPopover(false)

return isMobile ? (
    <>
      <MyMobileView content={content} visible={showPopover} .../>
    </>
  ) : (
    <Tippy
      content={content}
      {...defaultTippyProps}
      visible={showPopover}
      onHide={() => {
        close()
      }}
    >
      <span tabIndex={0} onClick={showPopover ? close : open}>
        {children}
      </span>
    </Tippy>
  )

The display of the tooltip depend on the showPopover state. Here are my needs:

  • When I click outside of the Tippy component, it should call close() to update the state, it works using onHide/onHidden here
  • When I resize the window, I should use MyMobileView so Tippy will be unmounted but I don’t want the state to be updated so it will display/open MyMobileView but using onHide/onHidden still update the state here

Ideally, I should use something like onClickOutside instead of onHide to handle my use case, do you see another way to do it with the current API?

Thanks by advance and feel free to ask if you need a PR or more informations 😃

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:2
  • Comments:20 (17 by maintainers)

github_iconTop GitHub Comments

1reaction
atomikscommented, Mar 16, 2020

Released, and I’ve added an example to the docs. Do you think we should make hideOnClick: false default if we detect the visible prop?

1reaction
atomikscommented, Feb 4, 2020

Are you able to make a CodeSandbox demo?

Read more comments on GitHub >

github_iconTop Results From Across the Web

react-onclickoutside - npm
An onClickOutside wrapper for React components. Latest version: 6.12.2, last published: 7 months ago. Start using react-onclickoutside in ...
Read more >
react-onclickoutside examples - CodeSandbox
Learn how to use react-onclickoutside by viewing and forking example apps that make use of react-onclickoutside on CodeSandbox.
Read more >
How to use the react-onclickoutside function in react ... - Snyk
To help you get started, we've selected a few react-onclickoutside examples, based on popular ways it is used in public projects.
Read more >
How can I pass a ref to HOC that uses onClickOutside('react ...
I use onClickOutside('react-onclickoutside') for my HOC and I can't pass ref for this HOC, I have something like below and an error appears:...
Read more >
How to detect a click outside a React component
First, we'll create a new React app to get started. ... Hence, the onClickOutside callback will be executed if a click event occurs...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found