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

fix(menuButton): fix RTL issues with MenuButton #14565

Merged
Merged
Show file tree
Hide file tree
Changes from 5 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
11 changes: 9 additions & 2 deletions packages/react/src/components/Menu/Menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,15 @@ const Menu = React.forwardRef(function Menu(
focusReturn.current = document.activeElement;

const pos = calculatePosition();
menu.current.style.left = `${pos[0]}px`;
menu.current.style.top = `${pos[1]}px`;
if (document?.dir === 'rtl' && !rest?.id?.includes('MenuButton')) {
menu.current.style.insetInlineStart = `initial`;
menu.current.style.insetInlineEnd = `${pos[0]}px`;
} else {
menu.current.style.insetInlineStart = `${pos[0]}px`;
menu.current.style.insetInlineEnd = `initial`;
}

menu.current.style.insetBlockStart = `${pos[1]}px`;
setPosition(pos);

menu.current.focus();
Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/components/Menu/Menu.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export const Playground = (args) => {
const target = document.getElementById('storybook-root');

return (
<Menu {...args} target={target}>
<Menu {...args} target={target} x={document?.dir === 'rtl' ? '250' : 0}>
<MenuItem label="Share with">
<MenuItemRadioGroup
label="Share with"
Expand Down
28 changes: 22 additions & 6 deletions packages/react/src/components/Menu/MenuItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import cx from 'classnames';
import PropTypes from 'prop-types';
import React, { useContext, useEffect, useRef, useState } from 'react';

import { CaretRight, Checkmark } from '@carbon/react/icons';
import { CaretRight, CaretLeft, Checkmark } from '@carbon/react/icons';
import { keys, match } from '../../internal/keyboard';
import { useControllableState } from '../../internal/useControllableState';
import { useMergedRefs } from '../../internal/useMergedRefs';
Expand Down Expand Up @@ -40,6 +40,7 @@ const MenuItem = React.forwardRef(function MenuItem(
const menuItem = useRef();
const ref = useMergedRefs([forwardRef, menuItem]);
const [boundaries, setBoundaries] = useState({ x: -1, y: -1 });
const [isRtl, setRtl] = useState(false);

const hasChildren = Boolean(children);
const [submenuOpen, setSubmenuOpen] = useState(false);
Expand All @@ -60,10 +61,17 @@ const MenuItem = React.forwardRef(function MenuItem(

function openSubmenu() {
const { x, y, width, height } = menuItem.current.getBoundingClientRect();
setBoundaries({
x: [x, x + width],
y: [y, y + height],
});
if (isRtl) {
setBoundaries({
x: [-x, x - width],
y: [y, y + height],
});
} else {
setBoundaries({
x: [x, x + width],
y: [y, y + height],
});
}

setSubmenuOpen(true);
}
Expand Down Expand Up @@ -126,6 +134,14 @@ const MenuItem = React.forwardRef(function MenuItem(
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

useEffect(() => {
if (document?.dir === 'rtl') {
setRtl(true);
} else {
setRtl(false);
}
}, []);

tay1orjones marked this conversation as resolved.
Show resolved Hide resolved
return (
<li
role="menuitem"
Expand All @@ -150,7 +166,7 @@ const MenuItem = React.forwardRef(function MenuItem(
{hasChildren && (
<>
<div className={`${prefix}--menu-item__shortcut`}>
<CaretRight />
{isRtl ? <CaretLeft /> : <CaretRight />}
</div>
<Menu
label={label}
Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/components/MenuButton/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ const MenuButton = React.forwardRef(function MenuButton(
}

function handleOpen() {
menuRef.current.style.width = `${width}px`;
menuRef.current.style.inlineSize = `${width}px`;
}

const triggerClasses = classNames(`${prefix}--menu-button__trigger`, {
Expand Down