Skip to content

Commit

Permalink
fix(drawing): Fix Safari doesn't focus and blur color picker button (#…
Browse files Browse the repository at this point in the history
…1321)

* fix(drawing): Fix Safari doesn't focus and blur color picker button

* fix(drawing): Address comments
  • Loading branch information
Mingze authored Jan 21, 2021
1 parent 4c33de0 commit bd66999
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/lib/viewers/controls/color-picker/ColorPickerControl.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,24 @@ export default function ColorPickerControl({

const handleClick = (): void => setIsColorPickerToggled(!isColorPickerToggled);

const handleMouseDown = (event: React.MouseEvent<HTMLButtonElement>): void => {
if (event.currentTarget.focus) {
// Buttons do not receive focus in Firefox and Safari on MacOS
event.currentTarget.focus();
// When focus is called within a mousedown handler,
// preventDefault must be called to keep the focus from leaving the target
event.preventDefault();
}
};

return (
<div className="bp-ColorPickerControl">
<button
className={classNames('bp-ColorPickerControl-toggle', { 'bp-is-active': isColorPickerToggled })}
data-testid="bp-ColorPickerControl-toggle"
onBlur={handleBlur}
onClick={handleClick}
onMouseDown={handleMouseDown}
type="button"
{...rest}
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,19 @@ describe('ColorPickerControl', () => {

expect(getColorPickerPalette(wrapper).hasClass('bp-is-open')).toBe(true);
});

test('should call focus and stop propagation when mouse down', () => {
const mockEvent = {
currentTarget: {
focus: jest.fn(),
},
preventDefault: jest.fn(),
};
(getToggleButton(getWrapper()).prop('onMouseDown') as Function)(mockEvent);

expect(mockEvent.currentTarget.focus).toHaveBeenCalled();
expect(mockEvent.preventDefault).toHaveBeenCalled();
});
});

describe('handleSelect', () => {
Expand Down

0 comments on commit bd66999

Please sign in to comment.