Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 8716

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 60k
Author
Asked: November 28, 20242024-11-28T03:44:05+00:00 2024-11-28T03:44:05+00:00

Mounting a React component in a picture in picture window

  • 60k

Google has introduced the documentPictureInPicture API in chrome 116.

In this article we explore how to mount a simple react component in the picture in picture window without the need to first mount it on our main application.

Step 1 — Set up component structure
We make two components. MainComponent.js and Counter.js. In MainComponent.js we set up a simple button that will open up the Counter.js component in the PIP window.

MainComponent.js

import React from "react";  const MainComponent = () => {  async function openPictureInPicture() {   //   }   return (   <div>    <div onClick={openPictureInPicture}>Open counter</div>   </div>  ); };  export default MainComponent; 
Enter fullscreen mode Exit fullscreen mode

Counter.js

import React, { useState, useEffect } from "react"; const Counter = () => {  const [count, setCount] = useState(0);   useEffect(() => {   setCount(1);  }, []);   const addNumber = () => {   setCount((prevCount) => prevCount + 1);  };  const subtractNumber = () => {   setCount((prevCount) => prevCount - 1);  };   try {   return (    <div>     <div onClick={subtractNumber}>-</div>     <button>{count}</button>     <div onClick={addNumber}>+</div>    </div>   );  } catch (error) {   console.error("ERROR_CODE: _RENDER
", error);   return <></>;  } };  export default Counter; 
Enter fullscreen mode Exit fullscreen mode

Step 2 — Add the picture in picture functionality
In the openPictureInPicture() function we request for a picture in picture window.

const pipWindow = await window.documentPictureInPicture.requestWindow(); 
Enter fullscreen mode Exit fullscreen mode

We create a div element in the body of the pip window. On this div we will mount our Counter.js

const pipDiv = pipWindow.document.createElement("div"); pipDiv.setAttribute("id", "pip-root"); pipWindow.document.body.append(pipDiv); 
Enter fullscreen mode Exit fullscreen mode

Now we mount our Counter.js component on the div with id “pip-root”.

const PIP_ROOT = ReactDOM.createRoot(         pipWindow.document.getElementById("pip-root") ); PIP_ROOT.render(<Counter />);  
Enter fullscreen mode Exit fullscreen mode

Step 3 — Combining it all
The final MainComponent.js code should look something like this.

import React from "react"; import Counter from "./Counter"; import ReactDOM from "react-dom/client";  const MainComponent = () => {  async function openPictureInPicture() {   const pipWindow = await window.documentPictureInPicture.requestWindow();   const pipDiv = pipWindow.document.createElement("div");   pipDiv.setAttribute("id", "pip-root");   pipWindow.document.body.append(pipDiv);   const PIP_ROOT = ReactDOM.createRoot(pipWindow.document.getElementById("pip-root"));   PIP_ROOT.render(<Counter />);  }   return (   <div>    <div onClick={openPictureInPicture}>Open counter</div>   </div>  ); };  export default MainComponent; 
Enter fullscreen mode Exit fullscreen mode

Now we have our own react component mounting on a picture in picture window!

frontendjavascriptwebdev
  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 1k
  • Popular
  • Answers
  • Author

    How to ensure that all the routes on my Symfony ...

    • 0 Answers
  • Author

    Insights into Forms in Flask

    • 0 Answers
  • Author

    Kick Start Your Next Project With Holo Theme

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.