diff --git a/src/Cell.tsx b/src/Cell.tsx index 89c1e300ea..d18294fd2d 100644 --- a/src/Cell.tsx +++ b/src/Cell.tsx @@ -4,6 +4,7 @@ import { css } from 'ecij'; import { useRovingTabIndex } from './hooks'; import { createCellEvent, getCellClassname, getCellStyle, isCellEditableUtil } from './utils'; import type { CellMouseEventHandler, RenderCellProps, SetActivePositionOptions } from './types'; +import { cellActiveClassname } from './style/cell'; const cellDraggedOver = css` @layer rdg.Cell { @@ -43,6 +44,7 @@ function Cell({ const { cellClass } = column; className = getCellClassname( column, + isCellActive && cellActiveClassname, isDraggedOver && cellDraggedOverClassname, typeof cellClass === 'function' ? cellClass(row) : cellClass, className @@ -101,7 +103,6 @@ function Cell({ role="gridcell" aria-colindex={column.idx + 1} // aria-colindex is 1-based aria-colspan={colSpan > 1 ? colSpan : undefined} - aria-selected={isCellActive} aria-readonly={!isEditable || undefined} tabIndex={tabIndex} className={className} diff --git a/src/EditCell.tsx b/src/EditCell.tsx index a6a81c6d47..d6ea1e43a9 100644 --- a/src/EditCell.tsx +++ b/src/EditCell.tsx @@ -3,6 +3,7 @@ import { css } from 'ecij'; import { createCellEvent, getCellClassname, getCellStyle, onEditorNavigation } from './utils'; import type { EditCellProps } from './types'; +import { cellActiveClassname } from './style/cell'; /* * To check for outside `mousedown` events, we listen to all `mousedown` events at their birth, @@ -187,6 +188,7 @@ export default function EditCell({ const { cellClass } = column; const className = getCellClassname( column, + cellActiveClassname, 'rdg-editor-container', editCell, !column.editorOptions?.displayCellContent && cellEditing, @@ -199,7 +201,6 @@ export default function EditCell({ role="gridcell" aria-colindex={column.idx + 1} // aria-colindex is 1-based aria-colspan={colSpan > 1 ? colSpan : undefined} - aria-selected className={className} style={getCellStyle(column, colSpan)} onKeyDownCapture={handleKeyDownCapture} diff --git a/src/GroupCell.tsx b/src/GroupCell.tsx index 39a8398003..315729f631 100644 --- a/src/GroupCell.tsx +++ b/src/GroupCell.tsx @@ -3,6 +3,7 @@ import { memo } from 'react'; import { useRovingTabIndex } from './hooks'; import { getCellClassname, getCellStyle } from './utils'; import type { CalculatedColumn, GroupRow } from './types'; +import { cellActiveClassname } from './style/cell'; interface GroupCellProps { column: CalculatedColumn; @@ -33,12 +34,11 @@ function GroupCell({ key={column.key} role="gridcell" aria-colindex={column.idx + 1} - aria-selected={isCellActive} // tabIndex={undefined} prevents clicks on the cell // from stealing focus from the row. // onMouseDown={preventDefault} would break mousewheel clicks tabIndex={tabIndex === -1 ? undefined : tabIndex} - className={getCellClassname(column)} + className={getCellClassname(column, isCellActive && cellActiveClassname)} style={{ ...getCellStyle(column, 1), cursor: isLevelMatching ? 'pointer' : 'default' diff --git a/src/GroupedColumnHeaderCell.tsx b/src/GroupedColumnHeaderCell.tsx index 0cb549ff5f..cb3205ed34 100644 --- a/src/GroupedColumnHeaderCell.tsx +++ b/src/GroupedColumnHeaderCell.tsx @@ -2,7 +2,7 @@ import { useRovingTabIndex } from './hooks'; import { classnames, getHeaderCellRowSpan, getHeaderCellStyle } from './utils'; import type { CalculatedColumnParent } from './types'; import type { GroupedColumnHeaderRowProps } from './GroupedColumnHeaderRow'; -import { cellClassname } from './style/cell'; +import { cellActiveClassname, cellClassname } from './style/cell'; type SharedGroupedColumnHeaderRowProps = Pick< GroupedColumnHeaderRowProps, @@ -35,9 +35,12 @@ export default function GroupedColumnHeaderCell({ aria-colindex={index} aria-colspan={colSpan} aria-rowspan={rowSpan} - aria-selected={isCellActive} tabIndex={tabIndex} - className={classnames(cellClassname, column.headerCellClass)} + className={classnames( + cellClassname, + isCellActive && cellActiveClassname, + column.headerCellClass + )} style={{ ...getHeaderCellStyle(column, rowIdx, rowSpan), gridColumnStart: index, diff --git a/src/HeaderCell.tsx b/src/HeaderCell.tsx index 8fff7f8529..53aeff0964 100644 --- a/src/HeaderCell.tsx +++ b/src/HeaderCell.tsx @@ -15,6 +15,7 @@ import { } from './utils'; import type { CalculatedColumn, SortColumn } from './types'; import type { HeaderRowProps } from './HeaderRow'; +import { cellActiveClassname } from './style/cell'; const cellSortableClassname = css` @layer rdg.HeaderCell { @@ -121,6 +122,7 @@ export default function HeaderCell({ const className = getCellClassname( column, + isCellActive && cellActiveClassname, column.headerCellClass, sortable && cellSortableClassname, resizable && cellResizableClassname, @@ -297,7 +299,6 @@ export default function HeaderCell({ aria-colindex={column.idx + 1} aria-colspan={colSpan > 1 ? colSpan : undefined} aria-rowspan={rowSpan} - aria-selected={isCellActive} aria-sort={ariaSort} tabIndex={tabIndex} className={className} diff --git a/src/SummaryCell.tsx b/src/SummaryCell.tsx index b366b298d8..99e08481b7 100644 --- a/src/SummaryCell.tsx +++ b/src/SummaryCell.tsx @@ -3,6 +3,7 @@ import { memo } from 'react'; import { useRovingTabIndex } from './hooks'; import { getCellClassname, getCellStyle } from './utils'; import type { RenderCellProps } from './types'; +import { cellActiveClassname } from './style/cell'; type SharedRenderCellProps = Pick< RenderCellProps, @@ -25,6 +26,7 @@ function SummaryCell({ const { summaryCellClass } = column; const className = getCellClassname( column, + isCellActive && cellActiveClassname, typeof summaryCellClass === 'function' ? summaryCellClass(row) : summaryCellClass ); @@ -37,7 +39,6 @@ function SummaryCell({ role="gridcell" aria-colindex={column.idx + 1} aria-colspan={colSpan > 1 ? colSpan : undefined} - aria-selected={isCellActive} tabIndex={tabIndex} className={className} style={getCellStyle(column, colSpan)} diff --git a/src/style/cell.ts b/src/style/cell.ts index 8d2918fcd9..ea72a6912f 100644 --- a/src/style/cell.ts +++ b/src/style/cell.ts @@ -20,16 +20,21 @@ export const cell = css` overflow: clip; text-overflow: ellipsis; outline: none; - - &[aria-selected='true'] { - outline: var(--rdg-selection-width) solid var(--rdg-selection-color); - outline-offset: calc(var(--rdg-selection-width) * -1); - } } `; export const cellClassname = `rdg-cell ${cell}`; +// Declared after `cell` so its outline overrides `outline: none` in the same layer. +const cellActive = css` + @layer rdg.Cell { + outline: var(--rdg-selection-width) solid var(--rdg-selection-color); + outline-offset: calc(var(--rdg-selection-width) * -1); + } +`; + +export const cellActiveClassname = `rdg-cell-active ${cellActive}`; + // Single shared sticky/z-index rule reused by both edge-frozen variants. // Selectors that need to distinguish start vs end use the marker classes // (`.rdg-cell-frozen-start` / `.rdg-cell-frozen-end`) directly rather than this ecij identifier. diff --git a/test/browser/TreeDataGrid.test.tsx b/test/browser/TreeDataGrid.test.tsx index b66fa693a9..6d6429d688 100644 --- a/test/browser/TreeDataGrid.test.tsx +++ b/test/browser/TreeDataGrid.test.tsx @@ -475,24 +475,24 @@ test('cell navigation in a treegrid', async () => { // focus cell const cells = getCellsAtRowIndex(5); await userEvent.click(cells.nth(1)); - await expect.element(cells.nth(1)).toHaveAttribute('aria-selected', 'true'); + await expect.element(cells.nth(1)).toHaveClass('rdg-cell-active'); await expect.element(cells.nth(1)).toHaveFocus(); await expect.element(cells.nth(1)).toHaveAttribute('tabIndex', '0'); // focus the previous cell await userEvent.keyboard('{arrowleft}'); - await expect.element(cells.nth(1)).toHaveAttribute('aria-selected', 'false'); - await expect.element(cells.nth(0)).toHaveAttribute('aria-selected', 'true'); + await expect.element(cells.nth(1)).not.toHaveClass('rdg-cell-active'); + await expect.element(cells.nth(0)).toHaveClass('rdg-cell-active'); // if the first cell is focused then arrowleft should focus the row await userEvent.keyboard('{arrowleft}'); - await expect.element(cells.nth(0)).toHaveAttribute('aria-selected', 'false'); + await expect.element(cells.nth(0)).not.toHaveClass('rdg-cell-active'); await expect.element(rows.nth(4)).toHaveClass(rowActiveClassname); await expect.element(rows.nth(4)).toHaveFocus(); // if the row is focused then arrowright should focus the first cell on the same row await userEvent.keyboard('{arrowright}'); - await expect.element(cells.nth(0)).toHaveAttribute('aria-selected', 'true'); + await expect.element(cells.nth(0)).toHaveClass('rdg-cell-active'); await userEvent.keyboard('{arrowleft}{arrowup}'); @@ -624,11 +624,11 @@ test('onCellKeyDown and onActivePositionChange receive group rows', async () => // activate the first cell of the group row await userEvent.keyboard('{arrowright}'); - await expect.element(usaGroupRow.getCell().nth(0)).toHaveAttribute('aria-selected', 'true'); + await expect.element(usaGroupRow.getCell().nth(0)).toHaveClass('rdg-cell-active'); // activate a cell of a regular row await userEvent.click(page.getCell({ name: '2020' })); - await expect.element(page.getCell({ name: '2020' })).toHaveAttribute('aria-selected', 'true'); + await expect.element(page.getCell({ name: '2020' })).toHaveClass('rdg-cell-active'); await userEvent.keyboard('{arrowright}'); await expect.element(activeCell).toHaveTextContent('value: 1'); diff --git a/test/browser/events.test.tsx b/test/browser/events.test.tsx index 002c461861..d76321f8d8 100644 --- a/test/browser/events.test.tsx +++ b/test/browser/events.test.tsx @@ -52,7 +52,7 @@ const rows: readonly Row[] = [ ]; describe('Events', () => { - it('should not select cell if onCellMouseDown prevents grid default', async () => { + it('should not activate the cell if onCellMouseDown prevents grid default', async () => { await page.render( { /> ); await userEvent.click(page.getCell({ name: '1' })); - await expect.element(page.getCell({ name: '1' })).toHaveAttribute('aria-selected', 'false'); + await expect.element(page.getCell({ name: '1' })).not.toHaveClass('rdg-cell-active'); await userEvent.click(page.getCell({ name: 'a1' })); - await expect.element(page.getCell({ name: 'a1' })).toHaveAttribute('aria-selected', 'true'); + await expect.element(page.getCell({ name: 'a1' })).toHaveClass('rdg-cell-active'); }); it('should activate and focus the cell if onCellMouseDown calls setActivePosition with shouldFocus', async () => { diff --git a/test/setupBrowser.ts b/test/setupBrowser.ts index afe6027a4c..4a4ef2e7e3 100644 --- a/test/setupBrowser.ts +++ b/test/setupBrowser.ts @@ -65,7 +65,7 @@ locators.extend({ }, getActiveCell() { - return this.getCell({ selected: true }).or(this.getHeaderCell({ selected: true })); + return '.rdg-cell-active'; }, getDragHandle() {