Fix product editor UI misbehaving in Calypso (#44132)

* Fix product editor UI misbehaving in custom adminmenu widths (e.g. Calypso)

* Extract logic to useAdminSidebarWidth hook
This commit is contained in:
Nathan Silveira 2024-01-29 16:09:06 -03:00 committed by GitHub
parent 487c7a87f0
commit 8e79cfa3ea
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 53 additions and 2 deletions

View File

@ -0,0 +1,4 @@
Significance: minor
Type: add
Add useAdminSidebarWidth hook

View File

@ -0,0 +1 @@
export * from './use-admin-sidebar-width';

View File

@ -0,0 +1,27 @@
/**
* External dependencies
*/
import { useEffect, useState } from '@wordpress/element';
/**
* the admin menu can have different widths in certain scenarios, like when using calypso
* so we need to observe it and adjust the header width and position accordingly
*/
export function useAdminSidebarWidth() {
const [ width, setWidth ] = useState( 0 );
useEffect( () => {
const resizeObserver = new ResizeObserver( ( entries ) => {
setWidth( entries[ 0 ].contentRect.width );
} );
const adminMenu = document.getElementById( 'adminmenu' );
if ( adminMenu ) {
resizeObserver.observe( adminMenu );
}
return () => {
if ( adminMenu ) {
resizeObserver.unobserve( adminMenu );
}
};
}, [] );
return width;
}

View File

@ -1,2 +1,3 @@
export * from './plugins';
export * from './components';
export * from './hooks';

View File

@ -0,0 +1,4 @@
Significance: patch
Type: fix
Fix product editor UI misbehaving in custom adminmenu widths (e.g. Calypso)

View File

@ -1,10 +1,10 @@
/**
* External dependencies
*/
import { WooHeaderItem } from '@woocommerce/admin-layout';
import { WooHeaderItem, useAdminSidebarWidth } from '@woocommerce/admin-layout';
import { useEntityProp } from '@wordpress/core-data';
import { useSelect } from '@wordpress/data';
import { createElement } from '@wordpress/element';
import { createElement, useEffect } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import { Button, Tooltip } from '@wordpress/components';
import { chevronLeft, group, Icon } from '@wordpress/icons';
@ -56,6 +56,20 @@ export function Header( {
'name'
);
const sidebarWidth = useAdminSidebarWidth();
useEffect( () => {
document
.querySelectorAll( '.interface-interface-skeleton__header' )
.forEach( ( el ) => {
if ( ( el as HTMLElement ).style ) {
( el as HTMLElement ).style.width =
'calc(100% - ' + sidebarWidth + 'px)';
( el as HTMLElement ).style.left = sidebarWidth + 'px';
}
} );
}, [ sidebarWidth ] );
if ( ! productId ) {
return null;
}