Compare commits

..
3 Commits
6 changed files with 42 additions and 6 deletions

No files matched your search

+5
View File
@@ -0,0 +1,5 @@
---
"@navikt/ds-react": patch
---
DataGrid: Keyboard navigation in table no longer prevents shift+tabbing away from the table
+5
View File
@@ -0,0 +1,5 @@
---
"@navikt/ds-react": patch
---
Button: Preserve keyboard modifiers during native Space activation.
+1 -1
View File
@@ -6,7 +6,7 @@ const metadata: ComponentMetadata = {
components: {
Button,
},
keywords: ["button", "knapp", "cta", "action", "submit", "test-change"],
keywords: ["button", "knapp", "cta", "action", "submit"],
};
export { metadata };
@@ -317,7 +317,6 @@ export const Chromatic: Story = {
<VStack gap="space-24" align="center">
<div>
<h2>Medium</h2>
test change
<ButtonGrid size="medium" />
</div>
<div>
+6 -1
View File
@@ -89,7 +89,12 @@ export const Button: OverridableComponent<ButtonProps, HTMLButtonElement> =
disabled || loading ? omit(rest, ["href"]) : rest;
const handleKeyUp = (e: React.KeyboardEvent<HTMLButtonElement>) => {
if (e.key === " " && !disabled && !loading) {
if (
e.key === " " &&
e.currentTarget.tagName !== "BUTTON" &&
!disabled &&
!loading
) {
e.currentTarget.click();
}
};
@@ -22,6 +22,7 @@ type UseTableKeyboardNavOptions = {
function useTableKeyboardNav({ enabled }: UseTableKeyboardNavOptions) {
const [tableRef, setTableRef] = useState<HTMLTableElement | null>(null);
const [isFocusWithin, setIsFocusWithin] = useState(false);
const { getTableGrid, activeCell, setActiveCell } = useGridCache(
tableRef,
enabled,
@@ -122,6 +123,7 @@ function useTableKeyboardNav({ enabled }: UseTableKeyboardNavOptions) {
* Make sure td/th elements have tabIndex={-1} so they can receive focus on click
*/
const handleTableFocusIn = useEventCallback((event: FocusEvent): void => {
setIsFocusWithin(true);
const target = event.target as Element | null;
if (tableRef && target === tableRef) {
@@ -146,6 +148,15 @@ function useTableKeyboardNav({ enabled }: UseTableKeyboardNavOptions) {
}
});
const handleTableFocusOut = useEventCallback((event: FocusEvent): void => {
if (
!(event.relatedTarget instanceof Node) ||
!tableRef?.contains(event.relatedTarget)
) {
setIsFocusWithin(false);
}
});
/**
* Attach event listeners for keyboard navigation and focus management.
*/
@@ -156,16 +167,27 @@ function useTableKeyboardNav({ enabled }: UseTableKeyboardNavOptions) {
tableRef.addEventListener("keydown", handleTableKeyDown);
tableRef.addEventListener("focusin", handleTableFocusIn);
tableRef.addEventListener("focusout", handleTableFocusOut);
return () => {
tableRef.removeEventListener("keydown", handleTableKeyDown);
tableRef.removeEventListener("focusin", handleTableFocusIn);
tableRef.removeEventListener("focusout", handleTableFocusOut);
};
}, [tableRef, handleTableKeyDown, handleTableFocusIn, enabled]);
}, [
tableRef,
handleTableKeyDown,
handleTableFocusIn,
handleTableFocusOut,
enabled,
]);
return {
/* Table should only have tabIndex until the focus is moved inside and is enabled */
tabIndex: enabled ? 0 : undefined,
/*
* Table should only have tabIndex until the focus is moved inside and is enabled
* This allows focus to "escape" the table and move to other elements on the page.
*/
tabIndex: enabled ? (isFocusWithin ? -1 : 0) : undefined,
setTableRef,
};
}