mdn / yari

The platform code behind MDN Web Docs

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Copy to clipboard not working for interactive examples

tannerdolby opened this issue · comments

Summary

The copy-to-clipboard logic is failing in the interactive example <iframe> due to lack of clipboard-write permissions for the Clipboard API.

To use the API in iframes, you need to enable it with Permissions Policy, which defines a mechanism that allows for selectively enabling and disabling various browser features and APIs. Concretely, you need to pass either or both of clipboard-read or clipboard-write, depending on the needs of your app.

URL

Reproduction steps

  1. Go to https://developer.mozilla.org/en-US/docs/Web/CSS/clip-path
  2. Click into one of the clip-path demo values, then click the copy-to-clipboard icon
  3. See error in console regarding lack of permissions
Screen Shot 2024-04-13 at 1 50 05 AM

Expected behavior

Expected content to be copied to clipboard within interactive examples

Actual behavior

Fails to copy content to clipboard and logs error to the browser console

Device

Desktop

Browser

Chrome

Browser version

Stable

Operating system

Mac OS

Screenshot

copy-to-clipboard-iframe-error.mov

Anything else?

No response

Validations

maybe we could make the {{EmbedInteractiveExample}} macros able to passing a parameter to specified allow attribute on the <iframe> element, just like the {{EmbedLiveSample}} does

https://github.com/mdn/yari/blob/main/kumascript/macros/EmbedLiveSample.ejs

@skyclouds2001 that could be an option, since the EmbedLiveSample macro supports a parameter for allowed features ($6) it makes sense that the EmbedInteractiveExample macro might do something similar. I'm just not sure if the interactive examples need any other permissions currently other than clipboard API. I added allow="clipboard-write" directly to the <iframe> in EmbedInteractiveExample.ejs for my initial PR (#10910) to quickly validate that the copy-to-clipboard worked again in interactive examples without having to update the actual EmbedInteractiveExample() macro calls throughout the various pages.

I searched mdn/content and found 1256 results in 1253 files of EmbedInteractiveExample( which would mean doing a mass search-and-replace to add a third argument with the "clipboard-write" permission, that is assuming all the interactive examples have code snippets which can be copied to clipboard. If the mdn folks are cool with that extra work of adding another parameter to the EmbedInteractiveExample macro to account for allowed features and updating those 1253 files in mdn/content then I can update my PR here to reflect that and submit one in mdn/content, otherwise I think simply adding the clipboard-write permission to the iframes directly gets the job done.