Skip to content
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

Gap block support: force gap change to cause the block to re-render (fix Safari issue) #34567

Merged
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions packages/block-editor/src/hooks/gap.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
/**
* Internal dependencies
*/
import { __unstableUseBlockRef as useBlockRef } from '../components/block-list/use-block-props/use-block-refs';
import useSetting from '../components/use-setting';
import { SPACING_SUPPORT_KEY } from './dimensions';
import { cleanEmptyObject } from './utils';
Expand Down Expand Up @@ -79,6 +80,7 @@ export function useIsGapDisabled( { name: blockName } = {} ) {
*/
export function GapEdit( props ) {
const {
clientId,
attributes: { style },
setAttributes,
} = props;
Expand All @@ -93,6 +95,8 @@ export function GapEdit( props ) {
],
} );

const ref = useBlockRef( clientId );

if ( useIsGapDisabled( props ) ) {
return null;
}
Expand All @@ -109,6 +113,13 @@ export function GapEdit( props ) {
setAttributes( {
style: cleanEmptyObject( newStyle ),
} );

// In Safari, changing the `gap` CSS value on its own will not trigger the layout
// to be recalculated / re-rendered. To force the updated gap to re-render, here
// we replace the block's node with itself.
if ( ref.current ) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there a way to limit it to only Safari cause it sounds a bit aggressive :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Very good point! I'll add in some user agent sniffing tomorrow 🙂

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@youknowriad I've added in some user agent sniffing here, consistent with this MDN article for detecting Safari (string contains "Safari" but doesn't contain "Chrome" or "Chromium").

It appears to work okay in my testing. I thought of abstracting the check out to somewhere else (perhaps in a package somewhere), but it doesn't look like we're using an explicit UA check for Safari anywhere else, so I thought I'd leave it inline here for now, and until we have a second use for it somewhere. But, do let me know if you can think of a better way to handle the check. Thanks!

ref.current.parentNode?.replaceChild( ref.current, ref.current );
}
};

return Platform.select( {
Expand Down