Skip to content

Commit

Permalink
fix(dropdown): Fix the dropdown closing on the second button click in… (
Browse files Browse the repository at this point in the history
  • Loading branch information
artemtrusov-cengage authored Sep 30, 2024
1 parent 5783946 commit d109847
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/dropdown-safari.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'react-magma-dom': patch
---

fix(dropdown): Fix the dropdown closing on the second button click in `Safari`
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ describe('Dropdown', () => {

expect(getByTestId('dropdownContent')).toHaveStyleRule('display', 'block');

userEvent.click(document.body);
fireEvent.blur(getByText('Toggle me'));

act(jest.runAllTimers);

Expand Down Expand Up @@ -785,7 +785,7 @@ describe('Dropdown', () => {
'block'
);

userEvent.click(document.body);
fireEvent.blur(getByText('Toggle me'));
act(jest.runAllTimers);

expect(onClose).toHaveBeenCalled();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,11 @@ export const DropdownButton = React.forwardRef<
}
}

// Necessary for the proper opening and closing of the menu in Safari
function handleMouseDown(event: React.MouseEvent) {
event.preventDefault();
}

const iconPositionToUse = props.icon
? iconPosition
? iconPosition
Expand All @@ -141,6 +146,7 @@ export const DropdownButton = React.forwardRef<
id={context.dropdownButtonId.current}
isInverse={context.isInverse}
onClick={handleClick}
onMouseDown={handleMouseDown}
ref={ref}
theme={theme}
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,18 @@ export const DropdownSplitButton = React.forwardRef<
}
}

// Necessary for the proper opening and closing of the menu in Safari
function handleMouseDown(event: React.MouseEvent) {
event.preventDefault();
}

const i18n = React.useContext(I18nContext);

function buildIconButtonStyles(props) {
if (props.color === ButtonColor.secondary || props.color === ButtonColor.subtle) {
if (
props.color === ButtonColor.secondary ||
props.color === ButtonColor.subtle
) {
return '0';
}
return theme.spaceScale.spacing01;
Expand All @@ -107,6 +115,7 @@ export const DropdownSplitButton = React.forwardRef<
icon={buttonIcon}
isInverse={resolvedContext.isInverse}
onClick={handleClick}
onMouseDown={handleMouseDown}
shape={ButtonShape.rightCap}
style={{
marginLeft: buildIconButtonStyles(resolvedProps),
Expand Down

2 comments on commit d109847

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

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

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

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

Please sign in to comment.