feat(ui): safe clipboard handling #7626
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
The clipboard API is not available by default on firefox. Until now, we have sometimes ignored (resulting in errors when a FF user attempts to copy something) and sometimes checked for access first. This PR refactors clipboard access across the UI to ensure it cannot cause errors.
The
useClipboard
hook exposes a boolean indicating if clipboard is available and provides a wrapper around the clipboard API.The wrapper methods first check if clipboard access is available before attempting to copy to it. If not, a toast is displayed, linking to the new FAQ entry:
![image](https://private-user-images.githubusercontent.com/4822129/410689605-1610dd3b-0937-460f-b27a-88ebce2068ac.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MzkyODI5MDMsIm5iZiI6MTczOTI4MjYwMywicGF0aCI6Ii80ODIyMTI5LzQxMDY4OTYwNS0xNjEwZGQzYi0wOTM3LTQ2MGYtYjI3YS04OGViY2UyMDY4YWMucG5nP1gtQW16LUFsZ29yaXRobT1BV1M0LUhNQUMtU0hBMjU2JlgtQW16LUNyZWRlbnRpYWw9QUtJQVZDT0RZTFNBNTNQUUs0WkElMkYyMDI1MDIxMSUyRnVzLWVhc3QtMSUyRnMzJTJGYXdzNF9yZXF1ZXN0JlgtQW16LURhdGU9MjAyNTAyMTFUMTQwMzIzWiZYLUFtei1FeHBpcmVzPTMwMCZYLUFtei1TaWduYXR1cmU9N2ZhZTljYjdlOTMwZTY1MGNmNjA0N2EwY2NlZjI1NTdjMzdmNzhlMjcxOTM5NDMyMWFhNmIxYmIzNWViMzMwYyZYLUFtei1TaWduZWRIZWFkZXJzPWhvc3QifQ.sIwBvElXYTUbltnKoCxEif7__eGlPg57Su_IHIJbuB8)
When clipboard access is available, there is no change to behaviour.
An
eslint
rule now prevents direct access tonavigator.clipboard
and points devs to the safe wrapper.Related Issues / Discussions
One report for a copy feature that, via a happy code accident, handled errors from the clipboard API:
https://discord.com/channels/1020123559063990373/1149506274971631688/1337165581131972751
Been a number of issues over time, but I'm not sure if there are tickets for any of them.
QA Instructions
Follow the instructions in the new FAQ entry to disable clipboard on Firefox. You'll need to check the code in this PR for the instructions bc the docs link in the toast won't work until this is merged.
Attempt to copy an image via right-click or canvas. You should get the toast.
Enable clipboard, refresh the page and try again. Should work.
Merge Plan
Should go into v5.6.1
Checklist
What's New
copy (if doing a release after this PR)