Open In App

React.js Blueprint Popover2 Controlled mode

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 Controlled mode. Popovers help in displaying the floating content next to the target element. In the controlled mode, we can modify the behavior of popover which can be done by using the isOpen property.



React.js BluePrint Popover2 Props:

Syntax:



<Popover2
    isOpen={...}
    ...
/>

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
npm install @blueprintjs/popover2

Project Structure:

 

Step 4: Run the project as follows:

npm start

Example 1: The below example demonstrates the usage of controlled mode popover. 




import React from "react";
import "@blueprintjs/core/lib/css/blueprint.css";
import { Button } from "@blueprintjs/core";
import { Popover2 } from "@blueprintjs/popover2";
import "@blueprintjs/popover2/lib/css/blueprint-popover2.css";
 
class App extends React.Component<{}, { isOpen: boolean }> {
    state = { isOpen: true };
 
    render() {
        return (
            <center>
                <div style={{ textAlign: "center",
                    color: "green" }}>
                    <h1>GeeksforGeeks</h1>
                    <h2>
                        ReactJS BluePrint Popover2 Controlled Mode
                    </h2>
                </div>
                <Popover2
                    content={
                        <div style={{ backgroundColor: "#F0EFEF",
                            padding: 20 }}>
                            <h2>GeeksforGeeks</h2>
                            <p>Popover2</p>
                        </div>
                    }
                    interactionKind="click"
                    isOpen={this.state.isOpen}
                    placement="right"
                >
                    <Button text="Click" intent="success" />
                </Popover2>
            </center>
        );
    }
 
}
 
export default App;

Output:

 

Note: In the above output, the popover is opened by default as isOpen property is true and the popover cannot be reopened after closing as onInteraction property is not used to handle the interactions. 

Example 2: The below example demonstrates the usage of controlled mode along with handlingInteraction (whether to open the popover on click or not and the controlled popover will remain closed even if isOpen={true}) in a popover. 




import React from "react";
import "@blueprintjs/core/lib/css/blueprint.css";
import { Button } from "@blueprintjs/core";
import { Popover2 } from "@blueprintjs/popover2";
import "@blueprintjs/popover2/lib/css/blueprint-popover2.css";
 
class App extends React.Component<{}, { isOpen: boolean }> {
    state = { isOpen: true };
 
    render() {
        return (
            <center>
                <div style={{ textAlign: "center",
                    color: "green" }}>
                    <h1>GeeksforGeeks</h1>
                    <h2>
                        ReactJS BluePrint Popover2 Controlled Mode
                    </h2>
                </div>
                <Popover2
                    content={
                        <div style={{ backgroundColor: "#F0EFEF",
                            padding: 20 }}>
                            <h2>GeeksforGeeks</h2>
                            <p>Popover2</p>
                        </div>
                    }
                    interactionKind="click"
                    isOpen={this.state.isOpen}
                    onInteraction={(state) =>
                        this.handleInteraction(state)}
                    placement="right"
                >
                    <Button text="Click" intent="success" />
                </Popover2>
            </center>
        );
    }
 
    handleInteraction(nextOpenState: boolean) {
        this.setState({ isOpen: nextOpenState });
    }
}
 
export default App;

Output:

 

Note: In the above output, the popover is opened by default as isOpen property is true and the popover may be reopened after closing. 

Reference: https://blueprintjs.com/docs/#popover2-package/popover2.controlled-mode


Article Tags :