Compare commits

..
4 changed files with 31 additions and 12 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
@@ -1,5 +0,0 @@
---
"@navikt/ds-css": patch
---
DataGrid: Change overflow hidden to min height and width on root
+1 -4
View File
@@ -6,10 +6,7 @@
display: flex;
flex-direction: column;
width: 100%;
/* Makes DataGridRoot shrink inside a height-limited flex container */
min-width: 0;
min-height: 0;
overflow: hidden;
}
.aksel-data-table__frame {
@@ -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,
};
}