Open In App

React.js Blueprint Popover2 Portal rendering

Blueprint is a React-based UI toolkit for the web. This library is very optimized and popular for building interfaces that are complex and data-dense for desktop applications.

In this article, we’ll discuss React.js Blueprint Popover2 Portal rendering. Popovers help in displaying the floating content next to the target element. With the help of portal rendering using usePortal prop & specifying the value as true or false, the popover can be rendered inside or outside a portal attached to portalContainer.



BluePrint Popover2 Portal rendering class:

 



React.js Blueprint Popover2 Props:

Syntax:

<Popover2
    usePortal={true || false}
    ...
/>

Creating React Application And Installing Module:

Step 1: Create a React application using the following command:

npm create-react-app appname

Step 2: After creating your project folder i.e. appname, move to it using the following command:

cd appname

Step 3: After creating the ReactJS application, Install the required module using the following command:

npm install @blueprintjs/core

Project Structure:

 

Step 4: Run the project as follows:

npm start

Example 1: The below example demonstrates the usage of usePortal as ‘True’ in popover2.




import React from "react";
import "@blueprintjs/core/lib/css/blueprint.css";
import { Button } from "@blueprintjs/core";
import { Popover2, Classes } from "@blueprintjs/popover2";
import "@blueprintjs/popover2/lib/css/blueprint-popover2.css";
  
function App() {
    return (
        <center>
            <div style={{ padding: 20, 
                 textAlign: "center", color: "green" }}>
                <h1>GeeksforGeeks</h1>
                <h2>ReactJS BluePrint Popover2 Portal Rendering</h2>
            </div>
            <div
                className="custom"
                style={{ backgroundColor: "lightgray"
                 , padding: 50 }}
            >
                <Popover2
                    usePortal={true}
                    position="bottom"
                    interactionKind="click-target"
                    content={
                        <div>
                            <h3>GeeksforGeeks</h3>
                        </div>
                    }
                    renderTarget={({ isOpen, ref, ...targetProps })
                       => (
                        <div style={{ width: 200 }}>
                            <Button
                                {...targetProps}
                                elementRef={ref}
                                text="Click"
                                intent="success"
                            />
                        </div>
                    )}
                />
            </div>
        </center>
    );
}
  
export default App;

Style.css




/* Write CSS Here */
.custom {
      height: 170px;
      max-width: 180px;
      flex: 1 1 auto;
      overflow-x: scroll;
      overflow-y: hidden;
      position: relative;
}

Output:

 

Example 2: The below example demonstrates the usage of usePortal as ‘False’ in popover2.




import React from "react";
import "@blueprintjs/core/lib/css/blueprint.css";
import { Button } from "@blueprintjs/core";
import { Popover2, Classes } from "@blueprintjs/popover2";
import "@blueprintjs/popover2/lib/css/blueprint-popover2.css";
  
function App() {
    return (
        <center>
            <div style={{ padding: 20, 
                 textAlign: "center", color: "green" }}>
                <h1>GeeksforGeeks</h1>
                <h2>
                   ReactJS BluePrint Popover2 Portal Rendering
                </h2>
            </div>
            <div
                className="custom"
                style={{ backgroundColor: "lightgray", padding: 50 }}
            >
                <Popover2
                    usePortal={false}
                    position="bottom"
                    interactionKind="click-target"
                    content={
                        <div>
                            <h3>GeeksforGeeks</h3>
                        </div>
                    }
                    renderTarget={({ isOpen, ref, ...targetProps })
                    => (
                        <div style={{ width: 200 }}>
                            <Button
                                {...targetProps}
                                elementRef={ref}
                                text="Click"
                                intent="success"
                            />
                        </div>
                    )}
                />
            </div>
        </center>
    );
}
  
export default App;

Style.css




/* Write CSS Here */
.custom {
      height: 170px;
      max-width: 180px;
      flex: 1 1 auto;
      overflow-x: scroll;
      overflow-y: hidden;
      position: relative;
}

Output:

 

Reference: https://blueprintjs.com/docs/#popover2-package/popover2.portal-rendering


Article Tags :