Author avatar

Gaurav Singhal

Create a Link that an App will Open in a Popup

Gaurav Singhal

  • Nov 17, 2020
  • 6 Min read
  • 74 Views
  • Nov 17, 2020
  • 6 Min read
  • 74 Views
Web Development
Front End Web Development
Client-side Frameworks
React

Introduction

To retain users of an app, you might want to show them content from an external source via hyperlinks without navigating them away from your app. In such situations, you can implement a React component that opens external links in a popup. This allows the user to stay on your app and still access external content. In this guide, you'll learn how to create a link that the application can conditionally open in a popup.

Implementation

The idea is to create a wrapper component that creates a URL object for any link passed to it and decides if it’s an external link or an internal link by checking the hostname of the link. For an internal link, it simply renders that link, and in all other cases, it renders an iframe in a popup showing that external link's source.

Setup

In an empty React project, install React-Router-Dom

1
npm i react-router-dom
shell

Instead of creating a custom modal component, this example will use React-Bootstrap's modal component. You can create a custom modal component or use any other UI library you are comfortable with.

Install React Bootstrap and Bootstrap.

1
npm i react-bootstrap bootstrap
shell

Create a file in the root folder called environment.js and add a constant that stores the hostname of your app.

1
export const HOSTNAME="localhost";
shell

Since your app is in local development, all routes will be relative to localhost. However, when you push this app to production, you should put the domain name of your app here.

Creating the Wrapper Component

Create a wrapper component for your link called LinkWrapper that takes in the link as props and generates a complete URL object for that link. If the hostname of the URL is the same as HOSTNAME in your environment, then you need to simply render that link. If it's an external website, render a modal component from React Bootstrap with an iframe pointing to the external link. Below is the complete code for the LinkWrapper component.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import React,{useState} from 'react';
import { Modal, Button } from "react-bootstrap";
import {HOSTNAME} from './environment';

export default function LinkWrapper({link}) {    
    const [show, setShow] = useState(false);
    const handleClose = () => setShow(false);
    const handleShow = () => setShow(true);

    const url = new URL(link)
    console.log(url)
    if (url.hostname === HOSTNAME ) return <a target="_blank" href={link}>{link}</a>
    else
    return (
      <>
        <Button variant="primary" onClick={handleShow}>
          Open Link
        </Button>
  
        <Modal show={show} onHide={handleClose}>
            <Modal.Title>Link Popup</Modal.Title>
          <Modal.Body><iframe src={link} style={{width:'100%',height:'400px'}}/></Modal.Body>
          <Modal.Footer>
            <Button variant="secondary" onClick={handleClose}>
              Close
            </Button>
          </Modal.Footer>
        </Modal>
      </>
    );
  }
jsx

You can learn how the React-Bootstrap modal is used here by following the example in the official documentation: https://react-bootstrap.netlify.app/components/modal/#modals.

Conclusion

The underlying concept for creating such a component is conditional rendering. You can use the same concept for opening registration forms in a popup or rendering external images in your app. You can use this approach in situations where you might want to show certain content to the user based on the time they have spent on your app without creating a session, allowing them to log in to access more content by opening a login form popup. You can also open external videos in a popup inside an iframe that can be used in video streaming apps to show quick teasers and trailers to your users. If you have any questions, feel free to contact me at CodeAlphabet.

1