-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
FormFileUpload
: Prevent HEIC and HEIF files from always being uploaded on Safari
#67139
Changes from 5 commits
6d288f1
97b802f
13d6ec4
544b2ec
2b71be4
21a1cb0
aa2c911
460ece4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useRef } from '@wordpress/element'; | ||
import { useRef, useState, useEffect } from '@wordpress/element'; | ||
|
||
/** | ||
* Internal dependencies | ||
|
@@ -50,9 +50,16 @@ export function FormFileUpload( { | |
// @todo: Temporary fix a bug that prevents Chromium browsers from selecting ".heic" files | ||
// from the file upload. See https://core.trac.wordpress.org/ticket/62268#comment:4. | ||
// This can be removed once the Chromium fix is in the stable channel. | ||
const compatAccept = !! accept?.includes( 'image/*' ) | ||
? `${ accept }, image/heic, image/heif` | ||
: accept; | ||
const [ isSafari, setIsSafari ] = useState( false ); | ||
useEffect( () => { | ||
setIsSafari( | ||
/^((?!chrome|android).)*safari/i.test( navigator.userAgent ) | ||
); | ||
}, [] ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If I do a similar thing to get Safari like in
eslint alerts: Also, checking the I'm not really convinced this is a good fix. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems to me like a reasonable and unavoidable bandaid fix in this case 👍 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. May be wrong but moving the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The point of executing that in a
Good find! But that isn't really a public API and the underlying detection mechanisms seem similar anyway. We can always encapsulate it ourselves for reuse if we need this kind of browser detection more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right, browser detection from userAgent is generally not recommended. This seems to be the second place it's needed though, after duotone.js. This may be pretty short-lived considering one of the causes for it is already fixed in Chromium and is in Chrome Canary. Seems WP 6.7.1 may end up with reverting this fix completely. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Seems like a weird hack for SSR :( Btw, using lazy initialization for state silences the warnings. const [ isSafari ] = useState( () =>
/^((?!chrome|android).)*safari/i.test( navigator?.userAgent )
); There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The goal here is to reference The This is OK but const isSafari = typeof window !== 'undefined' ? window.navigator.userAgent.includes(...) : false; And this @Mamaduka's suggestion will crash because the const [ isSafari ] = useState( () => window.navigator.userAgent.includes(...) ); Be careful that this will also crash in Node.js despite the optional chaining: window?.navigator Because the One of good solutions is also using globalThis.window?.navigator.userAgent
globalThis.navigator?.userAgent The I think we shouldn't need So, all you need is to find a solution that:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
const compatAccept = | ||
! isSafari && !! accept?.includes( 'image/*' ) | ||
? `${ accept }, image/heic, image/heif` | ||
: accept; | ||
|
||
return ( | ||
<div className="components-form-file-upload"> | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the changelog for the components package, so we need to describe the change in terms of the FormFileUpload component, not for whatever Gutenberg feature it's fixing. Something like this maybe?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated in 21a1cb0