Skip to content

Commit

Permalink
fix(ModalClose): don't s show focus outline if it's not focus by Tab (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
HelenaIsh authored Oct 27, 2021
1 parent fbc9ced commit b589014
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
12 changes: 7 additions & 5 deletions packages/react-ui/components/Modal/Modal.styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,10 @@ export const styles = memoizeStyle({
padding: ${t.modalCloseButtonClickArea};
margin: -${t.modalCloseButtonClickArea};
&:focus,
&:hover {
color: ${t.modalCloseButtonHoverColor};
}
&:focus {
outline: 2px solid ${t.borderColorFocus};
}
& > svg {
width: ${t.modalCloseIconSize};
height: ${t.modalCloseIconSize};
Expand Down Expand Up @@ -133,6 +128,13 @@ export const styles = memoizeStyle({
`;
},

focus(t: Theme) {
return css`
color: ${t.modalCloseButtonHoverColor};
outline: 2px solid ${t.borderColorFocus};
`;
},

headerWrapper() {
return css`
position: relative;
Expand Down
20 changes: 20 additions & 0 deletions packages/react-ui/components/Modal/ModalClose.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,41 @@ import React, { useContext } from 'react';
import { ThemeContext } from '../../lib/theming/ThemeContext';
import { CrossIcon } from '../../internal/icons/CrossIcon';
import { cx } from '../../lib/theming/Emotion';
import { keyListener } from '../../lib/events/keyListener';

import { CloseProps } from './ModalContext';
import { styles } from './Modal.styles';

export function ModalClose({ disableClose, requestClose }: CloseProps) {
const theme = useContext(ThemeContext);
const [focusedByTab, setFocusedByTab] = React.useState(false);

const handleFocus = () => {
// focus event fires before keyDown eventlistener
// so we should check tabPressed in async way
requestAnimationFrame(() => {
if (keyListener.isTabPressed) {
setFocusedByTab(true);
}
});
};

const handleBlur = () => {
setFocusedByTab(false);
};

return (
<button
className={cx({
[styles.close(theme)]: true,
[styles.disabled(theme)]: disableClose,
[styles.focus(theme)]: focusedByTab,
})}
onClick={requestClose}
onFocus={handleFocus}
onBlur={handleBlur}
data-tid="modal-close"
tabIndex={disableClose ? -1 : 0}
>
<CrossIcon />
</button>
Expand Down

0 comments on commit b589014

Please sign in to comment.