Skip to content

Commit

Permalink
docs: add min-width styles to Table and DataGrid stories, add best pr…
Browse files Browse the repository at this point in the history
…actices (microsoft#31721)
  • Loading branch information
smhigley authored Jun 17, 2024
1 parent 1fb910e commit 41835e7
Show file tree
Hide file tree
Showing 36 changed files with 113 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,13 @@ const columns: TableColumnDefinition<Item>[] = [

export const CompositeNavigation = () => {
return (
<DataGrid selectionMode="multiselect" items={items} columns={columns} focusMode="composite">
<DataGrid
selectionMode="multiselect"
items={items}
columns={columns}
focusMode="composite"
style={{ minWidth: '550px' }}
>
<DataGridHeader>
<DataGridRow selectionCell={{ checkboxIndicator: { 'aria-label': 'Select all rows' } }}>
{({ renderHeaderCell }) => <DataGridHeaderCell>{renderHeaderCell()}</DataGridHeaderCell>}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ export const CustomRowId = () => {
selectedItems={selectedRows}
onSelectionChange={onSelectionChange}
getRowId={item => item.file.label}
style={{ minWidth: '550px' }}
>
<DataGridHeader>
<DataGridRow selectionCell={{ checkboxIndicator: { 'aria-label': 'Select all rows' } }}>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
## Best Practices

### Do

- Always enclude a `DataGridHeader` row.
- When the DataGrid is preceded by a heading or other visible labelling text, use `aria-labelledby` to point to the heading's `id`.
- When the DataGrid does not have any visible text label, use `aria-label` to give it an accessible name.
- Set a `min-width` style to ensure the DataGrid displays properly at high zoom levels or small screens.

### Don't

- Use DataGrid to display single-column content.
- Override the `role` attribute of DataGrid controls.
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ export const Default = () => {
selectionMode="multiselect"
getRowId={item => item.file.label}
focusMode="composite"
style={{ minWidth: '550px' }}
>
<DataGridHeader>
<DataGridRow selectionCell={{ checkboxIndicator: { 'aria-label': 'Select all rows' } }}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ export const FocusableElementsInCells = () => {
selectionMode="multiselect"
getRowId={item => item.file.label}
onSelectionChange={(e, data) => console.log(data)}
style={{ minWidth: '550px' }}
>
<DataGridHeader>
<DataGridRow selectionCell={{ checkboxIndicator: { 'aria-label': 'Select all rows' } }}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,13 @@ export const MultipleSelect = () => {
const defaultSelectedItems = React.useMemo(() => new Set([1]), []);

return (
<DataGrid items={items} columns={columns} selectionMode="multiselect" defaultSelectedItems={defaultSelectedItems}>
<DataGrid
items={items}
columns={columns}
selectionMode="multiselect"
defaultSelectedItems={defaultSelectedItems}
style={{ minWidth: '550px' }}
>
<DataGridHeader>
<DataGridRow selectionCell={{ checkboxIndicator: { 'aria-label': 'Select all rows' } }}>
{({ renderHeaderCell }) => <DataGridHeaderCell>{renderHeaderCell()}</DataGridHeaderCell>}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ export const MultipleSelectControlled = () => {
selectionMode="multiselect"
selectedItems={selectedRows}
onSelectionChange={onSelectionChange}
style={{ minWidth: '550px' }}
>
<DataGridHeader>
<DataGridRow selectionCell={{ checkboxIndicator: { 'aria-label': 'Select all rows' } }}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ export const SelectionAppearance = () => {
selectionMode="multiselect"
selectionAppearance="neutral"
defaultSelectedItems={defaultSelectedItems}
style={{ minWidth: '550px' }}
>
<DataGridHeader>
<DataGridRow selectionCell={{ checkboxIndicator: { 'aria-label': 'Select all rows' } }}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,13 @@ export const SingleSelect = () => {
const defaultSelectedItems = React.useMemo(() => new Set([1]), []);

return (
<DataGrid items={items} columns={columns} selectionMode="single" defaultSelectedItems={defaultSelectedItems}>
<DataGrid
items={items}
columns={columns}
selectionMode="single"
defaultSelectedItems={defaultSelectedItems}
style={{ minWidth: '550px' }}
>
<DataGridHeader>
<DataGridRow>
{({ renderHeaderCell }) => <DataGridHeaderCell>{renderHeaderCell()}</DataGridHeaderCell>}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ export const SingleSelectControlled = () => {
selectionMode="single"
selectedItems={selectedRows}
onSelectionChange={onSelectionChange}
style={{ minWidth: '550px' }}
>
<DataGridHeader>
<DataGridRow>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ export const Sort = () => {
);

return (
<DataGrid items={items} columns={columns} defaultSortState={defaultSortState}>
<DataGrid items={items} columns={columns} defaultSortState={defaultSortState} style={{ minWidth: '500px' }}>
<DataGridHeader>
<DataGridRow>
{({ renderHeaderCell }) => <DataGridHeaderCell>{renderHeaderCell()}</DataGridHeaderCell>}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,14 @@ export const SortControlled = () => {
};

return (
<DataGrid items={items} columns={columns} sortable sortState={sortState} onSortChange={onSortChange}>
<DataGrid
items={items}
columns={columns}
sortable
sortState={sortState}
onSortChange={onSortChange}
style={{ minWidth: '500px' }}
>
<DataGridHeader>
<DataGridRow>
{({ renderHeaderCell }) => <DataGridHeaderCell>{renderHeaderCell()}</DataGridHeaderCell>}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ export const SubtleSelection = () => {
selectionMode="multiselect"
subtleSelection
defaultSelectedItems={defaultSelectedItems}
style={{ minWidth: '550px' }}
>
<DataGridHeader>
<DataGridRow selectionCell={{ checkboxIndicator: { 'aria-label': 'Select all rows' } }}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
DataGridSelectionCell,
} from '@fluentui/react-components';
import descriptionMd from './DataGridDescription.md';
import bestPracticesMd from './DataGridBestPractices.md';

export { Default } from './Default.stories';
export { CompositeNavigation } from './CompositeNavigation.stories';
Expand Down Expand Up @@ -39,7 +40,7 @@ export default {
parameters: {
docs: {
description: {
component: [descriptionMd].join('\n'),
component: [descriptionMd, bestPracticesMd].join('\n'),
},
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ const columns = [

export const CellActions = () => {
return (
<Table aria-label="Table with cell actions">
<Table aria-label="Table with cell actions" style={{ minWidth: '500px' }}>
<TableHeader>
<TableRow>
{columns.map(column => (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,12 @@ export const CellNavigation = () => {
const keyboardNavAttr = useArrowNavigationGroup({ axis: 'grid' });

return (
<Table {...keyboardNavAttr} role="grid" aria-label="Table with grid keyboard navigation">
<Table
{...keyboardNavAttr}
role="grid"
aria-label="Table with grid keyboard navigation"
style={{ minWidth: '600px' }}
>
<TableHeader>
<TableRow>
{columns.map(column => (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ export const CompositeNavigation = () => {
aria-label="Table with grid keyboard navigation"
noNativeElements
onKeyDown={onTableKeyDown}
style={{ minWidth: '500px' }}
{...tableTabsterAttribute}
>
<TableHeader>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,13 @@ export const DataGrid = () => {
const keyboardNavAttr = useArrowNavigationGroup({ axis: 'grid' });

return (
<Table {...keyboardNavAttr} role="grid" sortable aria-label="DataGrid implementation with Table primitives">
<Table
{...keyboardNavAttr}
role="grid"
sortable
aria-label="DataGrid implementation with Table primitives"
style={{ minWidth: '550px' }}
>
<TableHeader>
<TableRow>
<TableSelectionCell
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ const columns = [

export const Default = () => {
return (
<Table arial-label="Default table">
<Table arial-label="Default table" style={{ minWidth: '510px' }}>
<TableHeader>
<TableRow>
{columns.map(column => (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ const columns = [
{ columnKey: 'file', label: 'File' },
{ columnKey: 'author', label: 'Author' },
{ columnKey: 'lastUpdated', label: 'Last updated' },
{ columnKey: 'lastUpdate', label: 'Last update' },
{ columnKey: 'actions', label: 'Actions' },
];

Expand All @@ -75,13 +76,17 @@ export const FocusableElementsInCells = () => {
const focusableGroupAttr = useFocusableGroup({ tabBehavior: 'limited-trap-focus' });

return (
<Table {...keyboardNavAttr} role="grid" aria-label="Table with grid keyboard navigation">
<Table
{...keyboardNavAttr}
role="grid"
aria-label="Table with grid keyboard navigation"
style={{ minWidth: '620px' }}
>
<TableHeader>
<TableRow>
{columns.map(column => (
<TableHeaderCell key={column.columnKey}>{column.label}</TableHeaderCell>
))}
<TableHeaderCell />
</TableRow>
</TableHeader>
<TableBody>
Expand All @@ -106,6 +111,9 @@ export const FocusableElementsInCells = () => {
<TableCell tabIndex={0} role="gridcell">
{item.lastUpdated.label}
</TableCell>
<TableCell tabIndex={0} role="gridcell">
<TableCellLayout media={item.lastUpdate.icon}>{item.lastUpdate.label}</TableCellLayout>
</TableCell>
<TableCell role="gridcell" tabIndex={0} {...focusableGroupAttr}>
<TableCellLayout>
<Button icon={<EditRegular />} aria-label="Edit" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,13 @@ export const Memoization = () => {
const keyboardNavAttr = useArrowNavigationGroup({ axis: 'grid' });

return (
<Table {...keyboardNavAttr} role="grid" sortable aria-label="DataGrid implementation with Table primitives">
<Table
{...keyboardNavAttr}
role="grid"
sortable
aria-label="DataGrid implementation with Table primitives"
style={{ minWidth: '550px' }}
>
<TableHeader>
<TableRow>
<TableSelectionCell
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ export const MultipleSelect = () => {
);

return (
<Table aria-label="Table with multiselect">
<Table aria-label="Table with multiselect" style={{ minWidth: '550px' }}>
<TableHeader>
<TableRow>
<TableSelectionCell
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ export const MultipleSelectControlled = () => {
);

return (
<Table aria-label="Table with controlled multiselect">
<Table aria-label="Table with controlled multiselect" style={{ minWidth: '550px' }}>
<TableHeader>
<TableRow>
<TableSelectionCell
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ const columns = [

export const NonNativeElements = () => {
return (
<Table noNativeElements aria-label="Table without semantic HTML elements">
<Table noNativeElements aria-label="Table without semantic HTML elements" style={{ minWidth: '500px' }}>
<TableHeader>
<TableRow>
{columns.map(column => (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ const columns = [

export const PrimaryCell = () => {
return (
<Table aria-label="Table with primary cell layout">
<Table aria-label="Table with primary cell layout" style={{ minWidth: '500px' }}>
<TableHeader>
<TableRow>
{columns.map(column => (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ export const SelectionWithCellActions = () => {
const onKeyDownCellActions = (e: React.KeyboardEvent<HTMLDivElement>) => e.key === ' ' && e.preventDefault();

return (
<Table aria-label="Table with multiselect">
<Table aria-label="Table with multiselect" style={{ minWidth: '550px' }}>
<TableHeader>
<TableRow>
<TableSelectionCell
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ export const SingleSelect = () => {
});

return (
<Table aria-label="Table with single selection">
<Table aria-label="Table with single selection" style={{ minWidth: '550px' }}>
<TableHeader>
<TableRow>
<TableSelectionCell type="radio" invisible />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ export const SingleSelectControlled = () => {
});

return (
<Table aria-label="Table with controlled single selection">
<Table aria-label="Table with controlled single selection" style={{ minWidth: '550px' }}>
<TableHeader>
<TableRow>
<TableSelectionCell type="radio" invisible />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ const columns = [

export const SizeExtraSmall = () => {
return (
<Table size="extra-small" aria-label="Table with extra-small size">
<Table size="extra-small" aria-label="Table with extra-small size" style={{ minWidth: '400px' }}>
<TableHeader>
<TableRow>
{columns.map(column => (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ const columns = [

export const SizeSmall = () => {
return (
<Table size="small" aria-label="Table with small size">
<Table size="small" aria-label="Table with small size" style={{ minWidth: '475px' }}>
<TableHeader>
<TableRow>
{columns.map(column => (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ export const Sort = () => {
const rows = sort(getRows());

return (
<Table sortable aria-label="Table with sort">
<Table sortable aria-label="Table with sort" style={{ minWidth: '500px' }}>
<TableHeader>
<TableRow>
<TableHeaderCell {...headerSortProps('file')}>File</TableHeaderCell>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ export const SortControlled = () => {
const rows = sort(getRows());

return (
<Table sortable aria-label="Table with controlled sort">
<Table sortable aria-label="Table with controlled sort" style={{ minWidth: '500px' }}>
<TableHeader>
<TableRow>
<TableHeaderCell {...headerSortProps('file')}>File</TableHeaderCell>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ export const SubtleSelection = () => {
);

return (
<Table aria-label="Table with subtle selection">
<Table aria-label="Table with subtle selection" style={{ minWidth: '550px' }}>
<TableHeader>
<TableRow>
<TableSelectionCell
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
## Best Practices

### Do

- Always enclude a `TableHeader` row.
- When the Table is preceded by a heading or other visible labelling text, use `aria-labelledby` to point to the heading's `id`.
- When the Table does not have any visible text label, use `aria-label` to give it an accessible name.
- Set a `min-width` style to ensure the Table displays properly at high zoom levels or small screens.

### Don't

- Use Table to display single-column content.
- Override the `role` attribute of Table controls.
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ export const Virtualization = () => {
);

return (
<Table noNativeElements aria-label="Table with selection" aria-rowcount={rows.length}>
<Table noNativeElements aria-label="Table with selection" aria-rowcount={rows.length} style={{ minWidth: '650px' }}>
<TableHeader>
<TableRow aria-rowindex={1}>
<TableSelectionCell
Expand Down
Loading

0 comments on commit 41835e7

Please sign in to comment.