Skip to content

Commit ce4938f

Browse files
committed
fix: make TabButton close button aria compliant as a button
Make the close button on TabButton a button instead of a div. It looks identical and is aria compliant as a button, supporting screen readers and keyboard navigation with tab to select and enter or space to click. Note that a event handler for onCloseKeyDown was added to stop event propagation when pressing enter or space, so that the button's normal onClick logic is called, and the keyboard event does not propagate to the TabButton keyboard event handler. Allows using the keyboard to select the tab close button by pressing the Tab key to switch between elements on the page, and then pressing "enter" or space " " to select the highlighted button. Signed-off-by: Christian Stewart <[email protected]>
1 parent 51511bf commit ce4938f

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

src/view/TabButton.tsx

+16-3
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,8 @@ export const TabButton = (props: ITabButtonProps) => {
9090
return false;
9191
};
9292

93-
const onClose = (event: React.MouseEvent<HTMLElement>) => {
93+
const onClose = (event: React.MouseEvent<HTMLButtonElement>) => {
94+
event.preventDefault();
9495
if (isClosable()) {
9596
layout.doAction(Actions.deleteTab(node.getId()));
9697
} else {
@@ -102,6 +103,10 @@ export const TabButton = (props: ITabButtonProps) => {
102103
event.stopPropagation();
103104
};
104105

106+
const onCloseKeyDown = (event: React.KeyboardEvent<HTMLButtonElement>) => {
107+
event.stopPropagation();
108+
};
109+
105110
const onTextBoxPointerDown = (event: React.PointerEvent<HTMLInputElement>) => {
106111
event.stopPropagation();
107112
};
@@ -161,9 +166,17 @@ export const TabButton = (props: ITabButtonProps) => {
161166
if (node.isEnableClose() && !isStretch) {
162167
const closeTitle = layout.i18nName(I18nLabel.Close_Tab);
163168
renderState.buttons.push(
164-
<div key="close" data-layout-path={path + "/button/close"} title={closeTitle} className={cm(CLASSES.FLEXLAYOUT__TAB_BUTTON_TRAILING)} onPointerDown={onClosePointerDown} onClick={onClose}>
169+
<button
170+
key="close"
171+
data-layout-path={path + "/button/close"}
172+
title={closeTitle}
173+
className={cm(CLASSES.FLEXLAYOUT__TAB_BUTTON_TRAILING)}
174+
onPointerDown={onClosePointerDown}
175+
onClick={onClose}
176+
onKeyDown={onCloseKeyDown}
177+
>
165178
{typeof icons.close === "function" ? icons.close(node) : icons.close}
166-
</div>,
179+
</button>,
167180
);
168181
}
169182

0 commit comments

Comments
 (0)