Skip to content
Open
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
5 changes: 5 additions & 0 deletions .changeset/ui-elements-focus-visible.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@embeddedchat/ui-elements": patch
---

fix(ui-elements): add visible focus indicators to Button and Menu items, and trap focus in Modal
5 changes: 5 additions & 0 deletions packages/ui-elements/src/components/Button/Button.styles.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ const getButtonStyles = (theme) => {
line-height: 1.25rem;
min-width: 80px;
outline: none;
&:focus-visible {
outline: 2px solid
${theme.colors.ring || theme.colors.primary || 'currentColor'};
outline-offset: 2px;
}
overflow: hidden;
padding-block: calc(18px - 0.625rem);
padding: calc(18px - 0.625rem) 14px;
Expand Down
13 changes: 12 additions & 1 deletion packages/ui-elements/src/components/Menu/Menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,18 @@ const Menu = ({
}, []);

useEffect(() => {
if (!isOpen) return undefined;

const onKeyDown = (e) => {
if (e.key === 'Escape') {
e.preventDefault();
setOpen(false);
triggerRef.current?.focus();
}
};

const onBodyClick = (e) => {
if (
isOpen &&
wrapperRef.current &&
!wrapperRef.current.contains(e.target) &&
!sheetRef.current?.contains(e.target)
Expand All @@ -136,9 +145,11 @@ const Menu = ({
};

document.addEventListener('click', onBodyClick);
document.addEventListener('keydown', onKeyDown);

return () => {
document.removeEventListener('click', onBodyClick);
document.removeEventListener('keydown', onKeyDown);
};
}, [isOpen]);

Expand Down
10 changes: 8 additions & 2 deletions packages/ui-elements/src/components/Menu/Menu.styles.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,13 @@ export const getMenuItemStyles = ({ theme, mode }) => {
white-space: nowrap;
gap: 0.2rem;
color: ${theme.colors.foreground};
&:hover {
&:hover,
&:focus,
&:focus-visible {
background-color: ${mode === 'light'
? darken(theme.colors.background, 0.05)
: lighten(theme.colors.background, 2)};
outline: none;
cursor: pointer;
}
`,
Expand All @@ -95,10 +98,13 @@ export const getMenuItemStyles = ({ theme, mode }) => {
text-align: left;
border: 0;
background: transparent;
&:hover {
&:hover,
&:focus,
&:focus-visible {
background-color: ${mode === 'light'
? darken(theme.colors.background, 0.05)
: lighten(theme.colors.background, 2)};
outline: none;
cursor: pointer;
}
& + & {
Expand Down
57 changes: 52 additions & 5 deletions packages/ui-elements/src/components/Modal/Modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,19 @@ export const Modal = forwardRef(
) => {
const { classNames, styleOverrides } = useComponentOverrides('Modal');
const backDropRef = useRef(null);
const dialogRef = useRef(null);
const { theme } = useTheme();
const styles = getModalstyles(theme);

const setModalRef = (node) => {
dialogRef.current = node;
if (typeof ref === 'function') {
ref(node);
} else if (ref) {
ref.current = node;
}
};

const handleClick = useCallback(
(e) => {
if (e.target === backDropRef.current) {
Expand All @@ -32,22 +42,59 @@ export const Modal = forwardRef(
[onClose]
);

const handleEscKey = useCallback(
const handleKeyDown = useCallback(
(e) => {
if (e.key === 'Escape') {
onClose();
return;
}

if (e.key === 'Tab' && dialogRef.current) {
const focusableElements = dialogRef.current.querySelectorAll(
'a[href], button:not([disabled]), textarea:not([disabled]), input:not([disabled]), select:not([disabled]), [tabindex]:not([tabindex="-1"])'
);

if (focusableElements.length === 0) return;

const firstElement = focusableElements[0];
const lastElement = focusableElements[focusableElements.length - 1];

if (e.shiftKey) {
if (
document.activeElement === firstElement ||
!dialogRef.current.contains(document.activeElement)
) {
e.preventDefault();
lastElement.focus();
}
} else if (
document.activeElement === lastElement ||
!dialogRef.current.contains(document.activeElement)
) {
e.preventDefault();
firstElement.focus();
}
}
},
[onClose]
);

useEffect(() => {
window.addEventListener('keydown', handleEscKey);
if (!open) return undefined;
const previousActiveElement = document.activeElement;

window.addEventListener('keydown', handleKeyDown);

return () => {
window.removeEventListener('keydown', handleEscKey);
window.removeEventListener('keydown', handleKeyDown);
if (
previousActiveElement &&
typeof previousActiveElement.focus === 'function'
) {
previousActiveElement.focus();
}
};
}, [handleEscKey]);
}, [open, handleKeyDown]);

if (!open) {
return null;
Expand All @@ -57,7 +104,7 @@ export const Modal = forwardRef(
<ReactPortal wrapperId="overlay-items">
<ModalBackdrop ref={backDropRef} onClick={handleClick}>
<Box
ref={ref}
ref={setModalRef}
is="dialog"
css={styles.main}
className={`ec-modal ${className} ${classNames}`}
Expand Down