-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Feature: [Enhancement] Site Editor Identity: consider live updates for the admin bar site icon and browser favicon after changing the Site Icon #81501
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
hbhalodia
wants to merge
2
commits into
WordPress:trunk
Choose a base branch
from
hbhalodia:fix/issue-81483
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
packages/editor/src/hooks/use-sync-admin-bar-site-icon.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| import { useSelect, useDispatch } from '@wordpress/data'; | ||
| import { useEffect, useRef } from '@wordpress/element'; | ||
| import { store as coreStore } from '@wordpress/core-data'; | ||
|
|
||
| const SITE_NAME_SELECTOR = '#wp-admin-bar-site-name'; | ||
| const SITE_ICON_SELECTOR = ':scope > img.site-icon'; | ||
| const SUPPORTS_SITE_ICON_CLASS = 'supports-site-icon'; | ||
| const HAS_SITE_ICON_CLASS = 'has-site-icon'; | ||
| const SITE_ICON_SIZE = 20; | ||
|
|
||
| /** | ||
| * Keeps the site icon in the admin bar in sync with the saved Site Icon. | ||
| * | ||
| * The admin bar is rendered by PHP before the editor mounts, so its icon would | ||
| * otherwise keep showing the previous icon until the page is reloaded. The | ||
| * markup mirrored here is built by `gutenberg_admin_bar_site_icon()` in | ||
| * lib/compat/wordpress-7.1/admin-bar.php, which also marks the node with | ||
| * `supports-site-icon` to say whether an icon may be shown at all. | ||
| */ | ||
| export default function useSyncAdminBarSiteIcon() { | ||
| const { savedIconId, iconUrl } = useSelect( ( select ) => { | ||
| const { getEntityRecord } = select( coreStore ); | ||
| return { | ||
| // The persisted icon, which only changes once a save completes. | ||
| savedIconId: getEntityRecord( 'root', 'site' )?.site_icon, | ||
| // The icon's URL is derived server-side, so it lives on the base | ||
| // entity rather than alongside the ID in the site settings. | ||
| iconUrl: getEntityRecord( 'root', '__unstableBase' )?.site_icon_url, | ||
| }; | ||
| }, [] ); | ||
| const { invalidateResolution } = useDispatch( coreStore ); | ||
|
|
||
| // Saving the icon leaves `site_icon_url` stale, because it is derived from | ||
| // `site_icon` but belongs to a different entity that nothing refetches. | ||
| const savedIconIdRef = useRef(); | ||
| useEffect( () => { | ||
| const previousIconId = savedIconIdRef.current; | ||
| savedIconIdRef.current = savedIconId; | ||
|
|
||
| if ( | ||
| previousIconId === undefined || | ||
| savedIconId === undefined || | ||
| previousIconId === savedIconId | ||
| ) { | ||
| return; | ||
| } | ||
|
|
||
| invalidateResolution( 'getEntityRecord', [ 'root', '__unstableBase' ] ); | ||
| }, [ savedIconId, invalidateResolution ] ); | ||
|
|
||
| // Tracks the icon the admin bar is showing. Seeded on the first resolution, | ||
| // where the server-rendered icon is already correct, so that a page load | ||
| // never rewrites the markup it just received. | ||
| const renderedIconUrlRef = useRef(); | ||
| useEffect( () => { | ||
| if ( iconUrl === undefined ) { | ||
| return; | ||
| } | ||
|
|
||
| if ( renderedIconUrlRef.current === undefined ) { | ||
| renderedIconUrlRef.current = iconUrl; | ||
| return; | ||
| } | ||
|
|
||
| if ( renderedIconUrlRef.current === iconUrl ) { | ||
| return; | ||
| } | ||
|
|
||
| renderedIconUrlRef.current = iconUrl; | ||
|
|
||
| const siteName = document.querySelector( SITE_NAME_SELECTOR ); | ||
|
|
||
| // The class is absent when the `wp_admin_bar_show_site_icons` filter | ||
| // turns icons off, which is not otherwise distinguishable from an | ||
| // unset icon. | ||
| if ( ! siteName?.classList.contains( SUPPORTS_SITE_ICON_CLASS ) ) { | ||
| return; | ||
| } | ||
|
|
||
| const link = siteName.querySelector( ':scope > .ab-item' ); | ||
|
|
||
| if ( ! link ) { | ||
| return; | ||
| } | ||
|
|
||
| let image = link.querySelector( SITE_ICON_SELECTOR ); | ||
|
|
||
| if ( ! iconUrl ) { | ||
| image?.remove(); | ||
| siteName.classList.remove( HAS_SITE_ICON_CLASS ); | ||
| return; | ||
| } | ||
|
|
||
| if ( ! image ) { | ||
| image = document.createElement( 'img' ); | ||
| image.className = 'site-icon'; | ||
| image.alt = ''; | ||
| image.width = SITE_ICON_SIZE; | ||
| image.height = SITE_ICON_SIZE; | ||
| link.prepend( image ); | ||
| } | ||
|
|
||
| // `site_icon_url` is the full-size icon, so the 2x `srcset` rendered | ||
| // alongside the initial markup is both unnecessary and stale — leaving | ||
| // it would keep showing the previous icon on high-density screens. | ||
| image.removeAttribute( 'srcset' ); | ||
| image.src = iconUrl; | ||
| siteName.classList.add( HAS_SITE_ICON_CLASS ); | ||
| }, [ iconUrl ] ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
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.
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.
Have worked on this here, Not sure is it the correct thing to change. As it's punted to
7.2not sure if we are creating a new directory incompatand add the changes there?