diff --git a/.changeset/compact-table-footer.md b/.changeset/compact-table-footer.md new file mode 100644 index 000000000..cd131d964 --- /dev/null +++ b/.changeset/compact-table-footer.md @@ -0,0 +1,5 @@ +--- +"@tailor-platform/app-shell": patch +--- + +Make the `DataTable` footer more compact at phone widths. When the footer is narrower than 480px, `DataTable.Pagination` hides the page-size switcher, shortens the page text to `1/5`, and shrinks the page buttons from 36px to 28px, so the row count and the page buttons fit on one line. The selection count is shortened to `3/240 selected` in the bulk-action bar and in `DataTable.Pagination`; screen readers still get the full text. With rows selected, the footer now takes two lines at 375px instead of three. The English row count now uses `row` or `rows` by count instead of `row(s)`. diff --git a/docs-manifest.json b/docs-manifest.json index 2c68be482..257afb8bf 100644 --- a/docs-manifest.json +++ b/docs-manifest.json @@ -477,9 +477,9 @@ ], "hashes": { "typeSurface": "413c66caec0a0bf8", - "outline": "a4288a1435b46ed4", + "outline": "6d76610bcbb15d98", "snapshot": "a97e067c66e46ef6", - "outputMd": "c32c7ccf13528bf1", + "outputMd": "ea6d433dbee15988", "examples": "8adc119ac0d7152c" } }, @@ -1840,7 +1840,7 @@ "packages/core/skills/app-shell-patterns/references/components/combobox.md": "c79cd0fe175187e3", "packages/core/skills/app-shell-patterns/references/components/command-palette.md": "7e5bc675a593791a", "packages/core/skills/app-shell-patterns/references/components/csv-importer.md": "10c04c78bb846c9a", - "packages/core/skills/app-shell-patterns/references/components/data-table.md": "c32c7ccf13528bf1", + "packages/core/skills/app-shell-patterns/references/components/data-table.md": "ea6d433dbee15988", "packages/core/skills/app-shell-patterns/references/components/date-picker.md": "376e7d4ad14c2934", "packages/core/skills/app-shell-patterns/references/components/default-header.md": "375e55f4b12fa2be", "packages/core/skills/app-shell-patterns/references/components/default-sidebar.md": "9b1672c71b6db3ff", diff --git a/docs-src/components/data-table.docs.outline.md b/docs-src/components/data-table.docs.outline.md index 9d6eafcc9..442ef5103 100644 --- a/docs-src/components/data-table.docs.outline.md +++ b/docs-src/components/data-table.docs.outline.md @@ -261,16 +261,22 @@ Add a name, minimum-quantity, or status filter, then edit or remove its chip. Th `DataTable.Pagination` automatically displays a row count and selection info text on the left side of the pagination bar based on context state: -| Condition | Displayed text | -| ----------------------------------------- | ------------------------ | -| `total` is provided | `X row(s)` | -| Rows selected and `total` is provided | `Y of X row(s) selected` | -| Rows selected and `total` is not provided | `Y row(s) selected` | -| No selection enabled and no `total` | _(nothing displayed)_ | +| Condition | Displayed text | +| ----------------------------------------- | --------------------------------------------------- | +| `total` is provided | `X rows` (`1 row`) | +| Rows selected and `total` is provided | `Y of X rows selected` (`Y/X selected` when narrow) | +| Rows selected and `total` is not provided | `Y rows selected` (`1 row selected`) | +| No selection enabled and no `total` | _(nothing displayed)_ | Row selection is enabled by providing `rowSelection` or `onSelectionChange` to `useDataTable`. The `total` value comes from `DataTableData.total`. While the footer shows the bulk-action bar (see [Selection actions](#selection-actions)), the bar owns the selection count and this text is omitted. -In a narrow footer (under 480px wide, such as on a phone), the controls wrap onto more lines instead of being clipped. The **Rows per page** label is hidden, and the page text is shortened to `1 / 5`. Both stay available to screen readers. This only works inside `DataTable.Footer`, because the footer's own width is measured, not the viewport's. +In a narrow footer (under 480px wide, such as on a phone), the footer is compacted so the row count and the page buttons fit on one line: + +- The page-size switcher is hidden. +- The page text is shortened from `Page 1 / 5` to `1/5`, and the selection text from `Y of X rows selected` to `Y/X selected`. Screen readers still get the full text. +- The page buttons shrink from 36px to 28px, which stays above the 24px minimum target size. + +Controls that still do not fit wrap onto another line instead of being clipped. This only works inside `DataTable.Footer`, because the footer's own width is measured, not the viewport's. When pagination changes page or page size, `DataTable.Table` resets its own scroll container to the top automatically. That applies whether navigation comes from the built-in `DataTable.Pagination` or from custom controls using the same table context. @@ -720,7 +726,7 @@ Confirmation, eligibility checks, and execution remain consumer-owned. A real ap - **Return the promise from `onClick`.** While it is pending, the bar disables every action and shows a spinner on the running one, so a slow request can't be fired twice. Neither success nor rejection changes selection; there is no `keepSelection` option. A rejection is logged as `[DataTable] Selection action "…" failed`; showing it to the user is up to your handler. - **Confirm dialogs can hand a later request back with `run`.** An `onClick` that only opens a dialog returns nothing. Keep the `run` helper and call it from the dialog's confirm button — `await run(deleteVendors(ids))` — to track that request in the bar. This only tracks pending state; it does not clear selection. Clear is disabled while a request is pending, and pending state survives the bar closing and reopening. Use controlled `rowSelection` to remove only successful ids, or call the explicit `clearSelection` helper if clearing every page is intentional. - **Selection spans pages.** The header checkbox adds or removes only the current page; **Clear** empties everything. -- **Three actions stay inline.** The fourth onward collapse into a **More actions** menu, in array order. Put the most frequent first; a destructive action placed last sits safely in the menu. When the footer is narrower than 480px (for example, on a phone), every action moves into the menu, so the bar keeps to one line and the table rows stay visible. The menu button then gets an outline, like the inline actions. +- **Three actions stay inline.** The fourth onward collapse into a **More actions** menu, in array order. Put the most frequent first; a destructive action placed last sits safely in the menu. When the footer is narrower than 480px (for example, on a phone), every action moves into the menu, so the bar keeps to one line and the table rows stay visible. The menu button then gets an outline, like the inline actions, and the count is shortened to `3/240 selected` (screen readers still get `3 of 240 selected`). - **Confirm destructive actions.** `variant: "destructive"` only styles the action. Open a confirm dialog from `onClick` before deleting — see the [confirm pattern](../patterns/interaction-confirm.md). - **The bar stays reachable.** In `` the footer is already pinned. On a page that scrolls, the bar sticks to the bottom of the viewport while rows are selected and settles back into place at the end of the table. It sticks to the nearest scrolling container: inside a scrolling drawer or panel it rides that container instead, and inside a wrapper that clips its overflow (such as a card with `overflow: hidden`) it simply stays in the footer. - **Share presentation with `rowActions`.** Both action types extend the non-generic [`DataTableAction`](#datatableaction) (`id`, `label`, `icon`, `variant`). Keep row predicates on `RowAction` and bulk business rules in the consumer's ID-based handler. diff --git a/docs/components/data-table.md b/docs/components/data-table.md index d89a645a0..61056070a 100644 --- a/docs/components/data-table.md +++ b/docs/components/data-table.md @@ -447,16 +447,22 @@ function Filtering() { `DataTable.Pagination` automatically displays a row count and selection info text on the left side of the pagination bar based on context state: -| Condition | Displayed text | -| ----------------------------------------- | ------------------------ | -| `total` is provided | `X row(s)` | -| Rows selected and `total` is provided | `Y of X row(s) selected` | -| Rows selected and `total` is not provided | `Y row(s) selected` | -| No selection enabled and no `total` | _(nothing displayed)_ | +| Condition | Displayed text | +| ----------------------------------------- | --------------------------------------------------- | +| `total` is provided | `X rows` (`1 row`) | +| Rows selected and `total` is provided | `Y of X rows selected` (`Y/X selected` when narrow) | +| Rows selected and `total` is not provided | `Y rows selected` (`1 row selected`) | +| No selection enabled and no `total` | _(nothing displayed)_ | Row selection is enabled by providing `rowSelection` or `onSelectionChange` to `useDataTable`. The `total` value comes from `DataTableData.total`. While the footer shows the bulk-action bar (see [Selection actions](#selection-actions)), the bar owns the selection count and this text is omitted. -In a narrow footer (under 480px wide, such as on a phone), the controls wrap onto more lines instead of being clipped. The **Rows per page** label is hidden, and the page text is shortened to `1 / 5`. Both stay available to screen readers. This only works inside `DataTable.Footer`, because the footer's own width is measured, not the viewport's. +In a narrow footer (under 480px wide, such as on a phone), the footer is compacted so the row count and the page buttons fit on one line: + +- The page-size switcher is hidden. +- The page text is shortened from `Page 1 / 5` to `1/5`, and the selection text from `Y of X rows selected` to `Y/X selected`. Screen readers still get the full text. +- The page buttons shrink from 36px to 28px, which stays above the 24px minimum target size. + +Controls that still do not fit wrap onto another line instead of being clipped. This only works inside `DataTable.Footer`, because the footer's own width is measured, not the viewport's. When pagination changes page or page size, `DataTable.Table` resets its own scroll container to the top automatically. That applies whether navigation comes from the built-in `DataTable.Pagination` or from custom controls using the same table context. @@ -1884,7 +1890,7 @@ Confirmation, eligibility checks, and execution remain consumer-owned. A real ap - **Return the promise from `onClick`.** While it is pending, the bar disables every action and shows a spinner on the running one, so a slow request can't be fired twice. Neither success nor rejection changes selection; there is no `keepSelection` option. A rejection is logged as `[DataTable] Selection action "…" failed`; showing it to the user is up to your handler. - **Confirm dialogs can hand a later request back with `run`.** An `onClick` that only opens a dialog returns nothing. Keep the `run` helper and call it from the dialog's confirm button — `await run(deleteVendors(ids))` — to track that request in the bar. This only tracks pending state; it does not clear selection. Clear is disabled while a request is pending, and pending state survives the bar closing and reopening. Use controlled `rowSelection` to remove only successful ids, or call the explicit `clearSelection` helper if clearing every page is intentional. - **Selection spans pages.** The header checkbox adds or removes only the current page; **Clear** empties everything. -- **Three actions stay inline.** The fourth onward collapse into a **More actions** menu, in array order. Put the most frequent first; a destructive action placed last sits safely in the menu. When the footer is narrower than 480px (for example, on a phone), every action moves into the menu, so the bar keeps to one line and the table rows stay visible. The menu button then gets an outline, like the inline actions. +- **Three actions stay inline.** The fourth onward collapse into a **More actions** menu, in array order. Put the most frequent first; a destructive action placed last sits safely in the menu. When the footer is narrower than 480px (for example, on a phone), every action moves into the menu, so the bar keeps to one line and the table rows stay visible. The menu button then gets an outline, like the inline actions, and the count is shortened to `3/240 selected` (screen readers still get `3 of 240 selected`). - **Confirm destructive actions.** `variant: "destructive"` only styles the action. Open a confirm dialog from `onClick` before deleting — see the [confirm pattern](../patterns/interaction-confirm.md). - **The bar stays reachable.** In `` the footer is already pinned. On a page that scrolls, the bar sticks to the bottom of the viewport while rows are selected and settles back into place at the end of the table. It sticks to the nearest scrolling container: inside a scrolling drawer or panel it rides that container instead, and inside a wrapper that clips its overflow (such as a card with `overflow: hidden`) it simply stays in the footer. - **Share presentation with `rowActions`.** Both action types extend the non-generic [`DataTableAction`](#datatableaction) (`id`, `label`, `icon`, `variant`). Keep row predicates on `RowAction` and bulk business rules in the consumer's ID-based handler. diff --git a/packages/core/src/components/data-table/i18n.ts b/packages/core/src/components/data-table/i18n.ts index 0686ed69f..9e4737324 100644 --- a/packages/core/src/components/data-table/i18n.ts +++ b/packages/core/src/components/data-table/i18n.ts @@ -49,12 +49,18 @@ export const dataTableLabels = defineI18nLabels({ // Clear button's accessible name extends its visible text, so it is still // unambiguous when read outside the bar. bulkActionsLabel: "Bulk actions", - // Shorter than Pagination's "N of M row(s) selected": the bar shares the + // Shorter than Pagination's "N of M rows selected": the bar shares the // footer line with the pagination controls. selectionCount: (props: { selected: number; total: number | null }) => props.total === null ? `${props.selected} selected` : `${props.selected} of ${props.total} selected`, + // Shown in place of `selectionCount` in a footer under 480px wide; the + // full text stays for screen readers. + selectionCountShort: (props: { selected: number; total: number | null }) => + props.total === null + ? `${props.selected} selected` + : `${props.selected}/${props.total} selected`, selectionMoreActions: "More actions", selectionClear: "Clear", selectionClearLabel: "Clear selection", @@ -77,10 +83,15 @@ export const dataTableLabels = defineI18nLabels({ paginationLast: "Last page", paginationRowsPerPage: "Rows per page", paginationPage: "Page", - paginationTotalRows: (props: { total: number }) => `${props.total} row(s)`, + paginationTotalRows: (props: { total: number }) => + `${props.total} ${props.total === 1 ? "row" : "rows"}`, paginationSelectedOfTotal: (props: { selected: number; total: number }) => - `${props.selected} of ${props.total} row(s) selected`, - paginationSelectedRows: (props: { selected: number }) => `${props.selected} row(s) selected`, + `${props.selected} of ${props.total} ${props.total === 1 ? "row" : "rows"} selected`, + // Shown in place of `paginationSelectedOfTotal` in a footer under 480px wide. + paginationSelectedOfTotalShort: (props: { selected: number; total: number }) => + `${props.selected}/${props.total} selected`, + paginationSelectedRows: (props: { selected: number }) => + `${props.selected} ${props.selected === 1 ? "row" : "rows"} selected`, // Filters addFilter: "Add filter", @@ -188,6 +199,10 @@ export const dataTableLabels = defineI18nLabels({ props.total === null ? `${props.selected} 行を選択中` : `${props.total} 行中 ${props.selected} 行を選択中`, + selectionCountShort: (props: { selected: number; total: number | null }) => + props.total === null + ? `${props.selected} 行を選択中` + : `${props.selected}/${props.total} 行を選択中`, selectionMoreActions: "その他の操作", selectionClear: "解除", selectionClearLabel: "選択を解除", @@ -210,6 +225,8 @@ export const dataTableLabels = defineI18nLabels({ paginationTotalRows: (props: { total: number }) => `${props.total} 行`, paginationSelectedOfTotal: (props: { selected: number; total: number }) => `${props.total} 行中 ${props.selected} 行を選択中`, + paginationSelectedOfTotalShort: (props: { selected: number; total: number }) => + `${props.selected}/${props.total} 行を選択中`, paginationSelectedRows: (props: { selected: number }) => `${props.selected} 行を選択中`, // Filters diff --git a/packages/core/src/components/data-table/pagination.test.tsx b/packages/core/src/components/data-table/pagination.test.tsx index 1417ae03c..896e9ed2b 100644 --- a/packages/core/src/components/data-table/pagination.test.tsx +++ b/packages/core/src/components/data-table/pagination.test.tsx @@ -125,10 +125,33 @@ describe("DataTable.Pagination", () => { wrapper, }); - // A narrow footer shows "1 / 5" in place of "Page 1 / 5"; only the full + // A narrow footer shows "1/5" in place of "Page 1 / 5"; only the full // text should be announced. - expect(screen.getByText("1 / 5").getAttribute("aria-hidden")).toBe("true"); - expect(screen.getByText("Page 1 / 5").getAttribute("aria-hidden")).toBeNull(); + const short = screen.getByText("1/5"); + const full = screen.getByText("Page 1 / 5"); + expect(short.getAttribute("aria-hidden")).toBe("true"); + expect(full.getAttribute("aria-hidden")).toBeNull(); + // jsdom has no container queries, so check the classes that swap them. + expect(short.className).toContain("astw:hidden"); + expect(short.className).toContain("astw:@max-[480px]/data-table-footer:inline"); + expect(full.className).toContain("astw:@max-[480px]/data-table-footer:sr-only"); + }); + + it("drops the page-size switcher and shrinks the page buttons in a narrow footer", () => { + const { container } = render( + , + { wrapper }, + ); + + // jsdom has no container queries, so check the classes that apply them. + const select = screen.getByRole("combobox", { name: "Rows per page" }); + const switcher = container.querySelector('[class*="data-table-footer:hidden"]'); + expect(switcher?.contains(select)).toBe(true); + for (const name of ["First page", "Previous page", "Next page", "Last page"]) { + expect(screen.getByLabelText(name).className).toContain( + "astw:@max-[480px]/data-table-footer:size-7", + ); + } }); it("still shows Previous/Next buttons when total is not provided", () => { @@ -314,7 +337,14 @@ describe("row info text", () => { render(, { wrapper, }); - expect(screen.getByText("50 row(s)")).not.toBeNull(); + expect(screen.getByText("50 rows")).not.toBeNull(); + }); + + it("uses the singular for a single row", () => { + render(, { + wrapper, + }); + expect(screen.getByText("1 row")).not.toBeNull(); }); it("shows total rows when total is provided and selection enabled but nothing selected", () => { @@ -322,10 +352,13 @@ describe("row info text", () => { , { wrapper }, ); - expect(screen.getByText("50 row(s)")).not.toBeNull(); + expect(screen.getByText("50 rows")).not.toBeNull(); }); - it("shows 'Y of X row(s) selected' when rows are selected and total is provided", async () => { + it.each([ + { locale: "en", full: "1 of 50 rows selected", short: "1/50 selected" }, + { locale: "ja", full: "50 行中 1 行を選択中", short: "1/50 行を選択中" }, + ])("shows the selected count with a short form ($locale)", async ({ locale, full, short }) => { const user = userEvent.setup(); function TestWithTable() { @@ -345,17 +378,24 @@ describe("row info text", () => { ); } - render(, { wrapper }); + render(, { wrapper: createAppShellWrapper(locale) }); // Click the first row's checkbox to select it const checkboxes = screen.getAllByRole("checkbox"); // checkboxes[0] is the "select all" checkbox, [1] is the first row await user.click(checkboxes[1]); - expect(screen.getByText("1 of 50 row(s) selected")).not.toBeNull(); + // A narrow footer shows the short form; only the full text is announced. + const fullText = screen.getByText(full); + const shortText = screen.getByText(short); + expect(fullText.getAttribute("aria-hidden")).toBeNull(); + expect(fullText.className).toContain("astw:@max-[480px]/data-table-footer:sr-only"); + expect(shortText.getAttribute("aria-hidden")).toBe("true"); + expect(shortText.className).toContain("astw:hidden"); + expect(shortText.className).toContain("astw:@max-[480px]/data-table-footer:inline"); }); - it("shows 'Y row(s) selected' when rows are selected and no total", async () => { + it("shows 'Y row(s) selected' by count when rows are selected and no total", async () => { const user = userEvent.setup(); function TestWithTable() { @@ -379,8 +419,10 @@ describe("row info text", () => { const checkboxes = screen.getAllByRole("checkbox"); await user.click(checkboxes[1]); + expect(screen.getByText("1 row selected")).not.toBeNull(); - expect(screen.getByText("1 row(s) selected")).not.toBeNull(); + await user.click(checkboxes[2]); + expect(screen.getByText("2 rows selected")).not.toBeNull(); }); it("shows nothing when no total and no selection enabled", () => { diff --git a/packages/core/src/components/data-table/pagination.tsx b/packages/core/src/components/data-table/pagination.tsx index b59051b4c..6a87ae5d0 100644 --- a/packages/core/src/components/data-table/pagination.tsx +++ b/packages/core/src/components/data-table/pagination.tsx @@ -5,15 +5,14 @@ import { Button } from "@/components/button"; import { Select } from "@/components/select"; import { useDataTableContext } from "./context"; import { useDataTableT } from "./i18n"; -import { isSelectionBarOpen } from "./selection-bar"; +import { isSelectionBarOpen, NARROW_FOOTER_ONLY, NARROW_FOOTER_SR_ONLY } from "./selection-bar"; /** - * Visually hides a label while the footer is narrower than - * `NARROW_FOOTER_WIDTH` (see `selection-bar.tsx`), keeping it for screen - * readers. Outside `DataTable.Footer` the container never matches, so the - * label stays visible. + * In a narrow footer the page buttons shrink from 36px to 28px, which still + * clears the 24px minimum target size, so the row count and the page buttons + * fit on one line at phone widths. */ -const NARROW_FOOTER_SR_ONLY = "astw:@max-[480px]/data-table-footer:sr-only"; +const NARROW_PAGE_BUTTON = "astw:@max-[480px]/data-table-footer:size-7"; export interface DataTablePaginationProps { /** @@ -50,16 +49,20 @@ export function DataTablePagination({ pageSizeOptions }: DataTablePaginationProp // While the footer shows the bulk-action bar, the bar owns the count. const barOpen = isSelectionBarOpen(ctx); - const rowInfoText = (() => { + // `short` replaces `full` in a narrow footer; `full` stays for screen readers. + const rowInfo = ((): { full: string; short?: string } | null => { if (barOpen) return null; if (selectionEnabled && selectedCount > 0 && total !== null) { - return t("paginationSelectedOfTotal", { selected: selectedCount, total }); + return { + full: t("paginationSelectedOfTotal", { selected: selectedCount, total }), + short: t("paginationSelectedOfTotalShort", { selected: selectedCount, total }), + }; } if (selectionEnabled && selectedCount > 0) { - return t("paginationSelectedRows", { selected: selectedCount }); + return { full: t("paginationSelectedRows", { selected: selectedCount }) }; } if (total !== null) { - return t("paginationTotalRows", { total }); + return { full: t("paginationTotalRows", { total }) }; } return null; })(); @@ -77,20 +80,27 @@ export function DataTablePagination({ pageSizeOptions }: DataTablePaginationProp barOpen ? "astw:flex-1" : "astw:w-full", )} > - {rowInfoText && ( + {rowInfo && (
- {rowInfoText} + {rowInfo.short ? ( + <> + {rowInfo.full} + + + ) : ( + rowInfo.full + )}
)} + {/* A narrow footer drops the page-size switcher, as MUI does: on a phone + it costs a line of its own. */} {pageSizeOptions && pageSizeOptions.length > 0 && ( -
- {/* Names the select even while a narrow footer hides it visually. */} +
{t("paginationRowsPerPage")} @@ -107,11 +117,11 @@ export function DataTablePagination({ pageSizeOptions }: DataTablePaginationProp )} {/* The page text and its buttons wrap as one unit, so the buttons never split across lines. */} -
+
{totalPages !== null && ( <> {/* The full "Page 1 / 5" stays one text node for screen readers - and text queries; a narrow footer shows "1 / 5" in its place. */} + and text queries; a narrow footer shows "1/5" in its place. */} )}