Create a Link that an App will Open 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.
Nov 17, 2020 • 6 Minute Read
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
npm i react-router-dom
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.
npm i react-bootstrap bootstrap
Create a file in the root folder called environment.js and add a constant that stores the hostname of your app.
export const HOSTNAME="localhost";
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.
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>
</>
);
}
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.
Creating a Relative Link
In order to test local links, you need to create a relative route for your app. Create a simple About component, as shown below.
import React from 'react'
export const About = () => {
return (
<div>
<h1>This is the about component</h1>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Explicabo quae distinctio provident vero rerum expedita earum eius sint quos dolorum illum temporibus quidem in ipsa, ad beatae, repudiandae facilis aliquid?</p>
</div>
)
}
Inside App.js, add the route and render the LinkWrapper component created in the previous section.
import 'bootstrap/dist/css/bootstrap.min.css';
import './App.css';
import LinkWrapper from './LinkWrapper';
import {
BrowserRouter,
Route,
} from "react-router-dom";
import { About } from './About';
function App() {
return (
<div className="App">
<BrowserRouter>
<LinkWrapper/>
<Route path="/about">
<About/>
</Route>
</BrowserRouter>
</div>
);
}
export default App;
To test it out, create two links, an external link and an internal link, as shown below.
const external_link="https://reactrouter.com/web/guides/quick-start";
const internal_link="http://localhost:3001/about"
The external_link should open in a popup and the internal_link should render as a regular link. Pass both these links one by one to the LinkWrapper component as props.
<LinkWrapper link={external_link}/>
The LinkWrapper component renders an Open Link button that opens the link in a popup. Changing the link props to internal_link simply renders the link that opens in a new tab.
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.