Coder Social home page Coder Social logo

Comments (4)

diegohaz avatar diegohaz commented on May 31, 2024 1

My solution for this was to create nested portals in the DOM:

import * as React from "react";
import * as ReactDOM from "react-dom";

const PortalContext = React.createContext(
  typeof document !== "undefined" ? document.body : null
);

export function Portal({ children }) {
  // if it's a nested portal, context is the parent portal
  // otherwise it's document.body
  const context = React.useContext(PortalContext);
  const [container] = React.useState(() => {
    if (typeof document !== "undefined") {
      return document.createElement("div");
    }
    // ssr
    return null;
  });

  React.useLayoutEffect(() => {
    if (!container || !context) return undefined;
    context.appendChild(container);
    return () => {
      context.removeChild(container);
    };
  }, [container, context]);

  if (container) {
    const portal = ReactDOM.createPortal(children, container);
    return (
      <PortalContext.Provider value={container}>
        {portal}
      </PortalContext.Provider>
    );
  }

  // ssr
  return null;
}

Then you can pass the portal element to useOnClickOutside.

from use-onclickoutside.

Andarist avatar Andarist commented on May 31, 2024

I'm afraid that's not possible without hacks. React doesn't give access to its tree and we can't just traverse it.

from use-onclickoutside.

Andarist avatar Andarist commented on May 31, 2024

@diegohaz this is interesting, would you be able to prepare a simple codesandbox showcasing how this could be used? I'm not sure if I understand how this is supposed to work with DOM and outside clicks

from use-onclickoutside.

diegohaz avatar diegohaz commented on May 31, 2024

@Andarist There you go: https://codesandbox.io/s/o9086wk8j5

It doesn't close nested portals when closing the parent ones though. But I think it's doable. I just made it really fast.

from use-onclickoutside.

Related Issues (14)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.