Skip to content

Add example demonstrating navigator.presentation usage to property documentation #39053

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 12 commits into
base: main
Choose a base branch
from

Conversation

low-perry
Copy link
Contributor

@low-perry low-perry commented Apr 9, 2025

Description

This pull request adds an example to demonstrate the usage of the navigator.presentation property in the documentation. The example checks if the browser supports the Presentation API, retrieves the navigator.presentation object, and logs it to the console. If the API is unavailable, an error message is displayed.

Motivation

Additional details

Related issues and pull requests

@low-perry low-perry requested a review from a team as a code owner April 9, 2025 16:12
@low-perry low-perry requested review from sideshowbarker and removed request for a team April 9, 2025 16:12
@github-actions github-actions bot added Content:WebAPI Web API docs size/s [PR only] 6-50 LoC changed labels Apr 9, 2025
low-perry and others added 2 commits April 9, 2025 18:13
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Copy link
Contributor

github-actions bot commented Apr 9, 2025

Preview URLs

(comment last updated: 2025-04-09 17:31:17)

Copy link
Member

@Josh-Cena Josh-Cena left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks but I don't think this is a useful demo. I would prefer an actual usage example that invokes at least one method on the presentation object.

@low-perry
Copy link
Contributor Author

@Josh-Cena I'll try to improve the example.

low-perry and others added 7 commits April 9, 2025 19:02
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
@low-perry low-perry requested a review from Josh-Cena April 9, 2025 17:28
@Josh-Cena
Copy link
Member

IMO this is still not a useful example because it doesn't pertain to navigator.presentation: you are just using it for feature testing but then going to something else. I understand all of this is very hard because MDN's documentation on Presentation API is extremely bad (#4025). Is there a motivation for adding this example? A lot of these global singletons don't have examples anyway because you can just go to the Presentation interface page.

@low-perry
Copy link
Contributor Author

low-perry commented Apr 9, 2025

Thanks for your feedback! From what I understand, navigator.presentation is just a read-only attribute of the Navigator interface that serves as a gateway to the Presentation API, with no methods to use on it. That’s why my first example focused on checking if the browser supports the Presentation API. The idea was simply to show navigator.presentation in action—demonstrating that it exists and providing a basic example of its usage.

@Josh-Cena
Copy link
Member

The Presentation object seems to contain two properties. I have no idea what they do though.

@low-perry
Copy link
Contributor Author

low-perry commented Apr 9, 2025

The defaultRequest represents a PresentationRequest object that can be used to start a presentation, mainly to establish a connection to an external display. However, it’s not fundamentally different from directly instantiating a PresentationRequest (like in my second example), since you still need to create the object before setting it as the defaultRequest. This redundancy might explain why the W3C specification doesn't emphasize defaultRequest much—it’s simply another way to associate a PresentationRequest with navigator.presentation.

@low-perry
Copy link
Contributor Author

low-perry commented Apr 10, 2025

I explored a little bit more and I understand the attributes of the presentation object a little bit better. Here i found the following statement:

In a controlling browsing context, the default presentation request, which is initially set to null, represents the request to use when the user wishes to initiate a presentation connection from the browser chrome.

The defaultRequest property is particularly useful for launching a presentation through the browser’s cast menu. In the example below, initiating the presentation from the browser menu will open presentation.html, whereas clicking the UI button with an id of "startPresentation" will launch presentation2.html.

const presentationRequest = new PresentationRequest(["presentation.html"]);
const presentationRequest2 = new PresentationRequest(["presentation2.html"]);

navigator.presentation.defaultRequest = presentationRequest;

document.getElementById("startPresentation").addEventListener("click", () => {
  presentationRequest2.start().then(session => {
    console.log("Presentation started with session:", session);
  }).catch(error => {
    console.error("Error starting presentation:", error);
  });
});

The receiver attribute is exclusively available in the receiving browsing context. This means it can only be used on the device or window that is displaying the presentation, not on the controlling side where the presentation is initiated.

For the purpose of the navigator.presentation example, I think the code I presented the first time is good enough to show a use case. Alternatively we can just use the following code:

if ("presentation" in navigator) {
      const presentationRequest = new PresentationRequest(["https://example.com/presentation.html"]);
      // Use the default presentation if available, otherwise manually create one
       navigator.presentation.defaultRequest = presentationRequest;
    } else {
      console.error("Presentation API is not available in this browser.");
    }

This will launch a presentation by using the browser's cast menu. Let me know what you think! (I tested all the code I'm presenting on chrome and edge).

@sideshowbarker sideshowbarker removed their request for review April 13, 2025 05:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Content:WebAPI Web API docs size/s [PR only] 6-50 LoC changed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants