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

Tabs: Fix indicator misalignment when the browser width changes in RTL languages #64965

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
### Bug Fixes

- `TimePicker`: use ToggleGroupControl for AM/PM toggle ([#64800](https://github.com/WordPress/gutenberg/pull/64800)).
- `Tabs`: Fix indicator misalignment when the browser width changes in RTL languages ([#64965](https://github.com/WordPress/gutenberg/pull/64965)).

### Internal

Expand Down
4 changes: 2 additions & 2 deletions packages/components/src/tabs/styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export const TabListWrapper = styled.div`

@media not ( prefers-reduced-motion: reduce ) {
&.is-animation-enabled::after {
transition-property: left, top, width, height;
transition-property: inset-inline-start, top, width, height;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This change is necessary so that the indicator animations continue to work as before.

transition-duration: 0.2s;
transition-timing-function: ease-out;
}
Expand All @@ -40,7 +40,7 @@ export const TabListWrapper = styled.div`
}
&:not( [aria-orientation='vertical'] )::after {
bottom: 0;
left: var( --indicator-left );
inset-inline-start: var( --indicator-inset-inline-start );
width: var( --indicator-width );
height: 0;
border-bottom: var( --wp-admin-border-width-focus ) solid
Expand Down
5 changes: 4 additions & 1 deletion packages/components/src/tabs/tablist.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { useStoreState } from '@ariakit/react';
*/
import warning from '@wordpress/warning';
import { forwardRef, useState } from '@wordpress/element';
import { isRTL } from '@wordpress/i18n';

/**
* Internal dependencies
Expand Down Expand Up @@ -78,7 +79,9 @@ export const TabList = forwardRef<
onBlur={ onBlur }
{ ...otherProps }
style={ {
'--indicator-left': `${ indicatorPosition.left }px`,
'--indicator-inset-inline-start': `${
isRTL() ? indicatorPosition.right : indicatorPosition.left
}px`,
'--indicator-top': `${ indicatorPosition.top }px`,
'--indicator-width': `${ indicatorPosition.width }px`,
'--indicator-height': `${ indicatorPosition.height }px`,
Expand Down
40 changes: 30 additions & 10 deletions packages/components/src/utils/element-rect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,11 @@ export type ElementOffsetRect = {
* the element.
*/
left: number;
/**
* The distance from the right edge of the offset parent to the right edge of
* the element.
*/
right: number;
Comment on lines +128 to +132
Copy link
Contributor Author

Choose a reason for hiding this comment

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

In this PR, the right value is calculated in the getElementOffsetRect() function, but if the right value is inappropriate for the getElementOffsetRect() function itself, we can also calculate the right value directly in the Tabs component.

/**
* The distance from the top edge of the offset parent to the top edge of
* the element.
Expand All @@ -145,6 +150,7 @@ export type ElementOffsetRect = {
*/
export const NULL_ELEMENT_OFFSET_RECT = {
left: 0,
right: 0,
top: 0,
width: 0,
height: 0,
Expand All @@ -163,17 +169,31 @@ export const NULL_ELEMENT_OFFSET_RECT = {
export function getElementOffsetRect(
element: HTMLElement
): ElementOffsetRect {
// This is a workaround to obtain these values with a sub-pixel precision,
// since `offsetWidth` and `offsetHeight` are rounded to the nearest pixel.
const width = parseFloat( getComputedStyle( element ).width );
const height = parseFloat( getComputedStyle( element ).height );
const parentElementWidth = element?.parentElement
? parseFloat( getComputedStyle( element.parentElement ).width )
: 0;

// The adjustments mentioned in the documentation above are necessary
// because `offsetLeft` and `offsetTop` are rounded to the nearest pixel,
// which can result in a position mismatch that causes unwanted overflow.
// For context, see: https://github.com/WordPress/gutenberg/pull/61979
const left = Math.max( element.offsetLeft - 1, 0 );
const top = Math.max( element.offsetTop - 1, 0 );
const right = Math.max(
Math.round( parentElementWidth - width ) - element.offsetLeft - 1,
0
);

return {
// The adjustments mentioned in the documentation above are necessary
// because `offsetLeft` and `offsetTop` are rounded to the nearest pixel,
// which can result in a position mismatch that causes unwanted overflow.
// For context, see: https://github.com/WordPress/gutenberg/pull/61979
left: Math.max( element.offsetLeft - 1, 0 ),
top: Math.max( element.offsetTop - 1, 0 ),
// This is a workaround to obtain these values with a sub-pixel precision,
// since `offsetWidth` and `offsetHeight` are rounded to the nearest pixel.
width: parseFloat( getComputedStyle( element ).width ),
height: parseFloat( getComputedStyle( element ).height ),
left,
top,
right,
width,
height,
};
}

Expand Down
Loading