diff --git a/.changeset/additional-files-upload-focus.md b/.changeset/additional-files-upload-focus.md new file mode 100644 index 00000000..432ac0e7 --- /dev/null +++ b/.changeset/additional-files-upload-focus.md @@ -0,0 +1,5 @@ +--- +"@javaabu/onu": patch +--- + +`AdditionalFilesUpload` now returns focus to the "Add files" picker after removing a new file at `maxFiles`, where it used to be lost because the picker was still disabled. diff --git a/.changeset/bulk-actions-form-focus.md b/.changeset/bulk-actions-form-focus.md new file mode 100644 index 00000000..1b7abff2 --- /dev/null +++ b/.changeset/bulk-actions-form-focus.md @@ -0,0 +1,5 @@ +--- +"@javaabu/onu": patch +--- + +`BulkActionBar` moves focus to the form control when an action's form opens, and back to the action when the form is cancelled, instead of dropping it to the page body. diff --git a/.changeset/editor-toolbar-tab-stop.md b/.changeset/editor-toolbar-tab-stop.md new file mode 100644 index 00000000..cadc59b8 --- /dev/null +++ b/.changeset/editor-toolbar-tab-stop.md @@ -0,0 +1,5 @@ +--- +"@javaabu/onu": patch +--- + +The editor toolbar keeps its tab stop when the control holding it is disabled (for example "Text style" with the caret in a code block): Tab and Alt+F10 now reach the first enabled control. diff --git a/.changeset/export-menu-focus.md b/.changeset/export-menu-focus.md new file mode 100644 index 00000000..ffc0161c --- /dev/null +++ b/.changeset/export-menu-focus.md @@ -0,0 +1,5 @@ +--- +"@javaabu/onu": patch +--- + +`ExportMenu` keeps focus on its button while a file is being made: the button is now `aria-disabled` instead of `disabled` while exporting, and its menu won't open until the export finishes. diff --git a/.changeset/record-actions-menu-focus.md b/.changeset/record-actions-menu-focus.md new file mode 100644 index 00000000..bfee5870 --- /dev/null +++ b/.changeset/record-actions-menu-focus.md @@ -0,0 +1,5 @@ +--- +"@javaabu/onu": patch +--- + +`RecordActionsMenu` now returns focus to the "More actions" button when a confirmation opened from its menu closes, instead of leaving it on the page body. diff --git a/docs/files-and-media/additional-files-upload.md b/docs/files-and-media/additional-files-upload.md index cd2c13c4..6b73f5bf 100644 --- a/docs/files-and-media/additional-files-upload.md +++ b/docs/files-and-media/additional-files-upload.md @@ -49,7 +49,7 @@ Set `name` to also render native fields: `name[removed][]` (hidden ids), `name[a - Each file's buttons are named with the file ("Remove Lease agreement.pdf", "Restore …"). - Marking a file for removal shows "Will be removed" as text (not only a strike-through), announces it, and moves focus to the Restore button. -- Removing a new file returns focus to the "Add files" picker. +- Removing a new file returns focus to the "Add files" picker, also when the picker was disabled at `maxFiles`. | Key | Action | | -------------------------------------------- | ------------------------------------------------------ | diff --git a/docs/listing-pages/bulk-actions.md b/docs/listing-pages/bulk-actions.md index 0d0323be..e89177cb 100644 --- a/docs/listing-pages/bulk-actions.md +++ b/docs/listing-pages/bulk-actions.md @@ -47,4 +47,4 @@ const actions: BulkAction[] = [ ## Accessibility -The bar is a region named "Bulk actions"; the count is a polite live region, so "3 selected" is announced. Confirmations are alert dialogs that return focus where they were opened. After success, focus moves to the result message so it isn't lost when the bar goes away; errors are `role="alert"`. +The bar is a region named "Bulk actions"; the count is a polite live region, so "3 selected" is announced. Confirmations are alert dialogs that return focus where they were opened. Opening an action's form moves focus to its control, and Cancel returns focus to the action. After success, focus moves to the result message so it isn't lost when the bar goes away; errors are `role="alert"`. diff --git a/docs/listing-pages/export-menu.md b/docs/listing-pages/export-menu.md index 5b9bca10..c8039039 100644 --- a/docs/listing-pages/export-menu.md +++ b/docs/listing-pages/export-menu.md @@ -3,7 +3,7 @@ title: ExportMenu sidebar_position: 16 --- -An **Export** button with a menu of formats (CSV, Excel, PDF). The menu does not build the file: your `onExport(format)` does, either by asking the server with the current filters or in the browser with [`exportRows`](export-rows) from `@javaabu/onu/export`. While the returned promise is pending the button shows a spinner and is disabled. +An **Export** button with a menu of formats (CSV, Excel, PDF). The menu does not build the file: your `onExport(format)` does, either by asking the server with the current filters or in the browser with [`exportRows`](export-rows) from `@javaabu/onu/export`. While the returned promise is pending the button shows a spinner and won't open the menu. ```tsx import { ExportMenu } from "@javaabu/onu" @@ -33,4 +33,4 @@ const exportRows = useExportRows() ## Accessibility -A menu button. "Exporting…" and "Export ready" are announced through a polite live region; the button has `aria-busy` while working. +A menu button. "Exporting…" and "Export ready" are announced through a polite live region; the button has `aria-busy` and `aria-disabled` while working. It uses `aria-disabled` rather than `disabled`, so focus returns to it when the menu closes. diff --git a/docs/rich-content/wysiwyg-editor.md b/docs/rich-content/wysiwyg-editor.md index 30af724d..d18b3d02 100644 --- a/docs/rich-content/wysiwyg-editor.md +++ b/docs/rich-content/wysiwyg-editor.md @@ -102,7 +102,7 @@ Other `
` props go to the root, and `ref` points at the root. `editorVariant ## Accessibility - The content area is a `role="textbox"` with `aria-multiline`. Inside a `FormField` it is labelled by the field label and described by help and error text; `aria-invalid`, `aria-required`, `aria-readonly` and `aria-disabled` are set. -- The toolbar follows the WAI-ARIA toolbar pattern: one tab stop, arrow keys move between controls (mirrored in RTL), toggles expose `aria-pressed`. +- The toolbar follows the WAI-ARIA toolbar pattern: one tab stop, arrow keys move between controls (mirrored in RTL), toggles expose `aria-pressed`. When the control holding the tab stop is disabled (for example "Text style" in a code block), the tab stop moves to the first enabled control. - Link and image dialogs return focus to the button that opened them. - Toolbar buttons are 44px on touch screens. diff --git a/packages/onu/src/components/files/additional-files-upload.test.tsx b/packages/onu/src/components/files/additional-files-upload.test.tsx index fb04576c..005cf481 100644 --- a/packages/onu/src/components/files/additional-files-upload.test.tsx +++ b/packages/onu/src/components/files/additional-files-upload.test.tsx @@ -90,6 +90,15 @@ describe("AdditionalFilesUpload", () => { expect(screen.getByText("b.pdf was not added: only 2 files are allowed.")).toBeInTheDocument() }) + it("moves focus to the picker after removing a file at maxFiles", async () => { + const { container } = renderWithOnu() + await user().upload(picker(container), [file("a.pdf")]) + expect(picker(container)).toBeDisabled() + await user().click(screen.getByRole("button", { name: "Remove a.pdf" })) + expect(picker(container)).toBeEnabled() + expect(picker(container)).toHaveFocus() + }) + it("disables every action when disabled", () => { renderWithOnu() expect(screen.getByRole("button", { name: "Remove Lease agreement.pdf" })).toBeDisabled() diff --git a/packages/onu/src/components/files/additional-files-upload.tsx b/packages/onu/src/components/files/additional-files-upload.tsx index a9edc206..d462897d 100644 --- a/packages/onu/src/components/files/additional-files-upload.tsx +++ b/packages/onu/src/components/files/additional-files-upload.tsx @@ -16,6 +16,7 @@ import { syncInputFiles } from "./file-utils" import { registerEnglish } from "../../i18n/english" import { file as en_file } from "../../locales/en/file" import { useControllableState } from "../../hooks/use-controllable-state" +import { useFocusAfterRender } from "../../hooks/use-focus-after-render" registerEnglish({ file: en_file }) @@ -104,6 +105,8 @@ export function AdditionalFilesUpload({ const { t } = useTranslation() const [state, setState] = useControllableState(value, defaultValue ?? EMPTY, onValueChange) const pickerRef = React.useRef(null) + // The picker may be disabled (at maxFiles) until the removal re-renders it. + const focusPicker = useFocusAfterRender(pickerRef) const rootRef = React.useRef(null) const [announcement, setAnnouncement] = React.useState("") const kept = existing.filter((a) => !state.removed.includes(a.id)).length @@ -143,7 +146,7 @@ export function AdditionalFilesUpload({ const removeAdded = (item: NewAttachment) => { setState({ ...state, added: state.added.filter((a) => a.key !== item.key) }) setAnnouncement(t("file.removedFile", { name: item.file.name })) - pickerRef.current?.focus() + focusPicker() } return ( diff --git a/packages/onu/src/components/files/file-input.tsx b/packages/onu/src/components/files/file-input.tsx index c1d72ecd..7d0e0b3c 100644 --- a/packages/onu/src/components/files/file-input.tsx +++ b/packages/onu/src/components/files/file-input.tsx @@ -8,6 +8,7 @@ import { cn } from "../../lib/utils" import { useTranslation } from "../../providers/onu-provider" import { Button, buttonVariants } from "../../ui/button" import { useFormField, useFormFieldControl } from "../form/form-field" +import { useFocusAfterRender } from "../../hooks/use-focus-after-render" import { useMergedRef } from "../../hooks/use-merged-ref" import { FileRow } from "./_file-row" import type { ExistingAttachment } from "./additional-files-upload" @@ -227,7 +228,7 @@ export function FileInput({ const [announcement, setAnnouncement] = React.useState("") const [dragActive, setDragActive] = React.useState(false) // Focus moves back to the input after a removal, once it has re-rendered (it may have been disabled). - const [focusRequest, setFocusRequest] = React.useState(0) + const refocus = useFocusAfterRender(local) const options: FileValidationOptions = { accept, maxSize, minSize, maxFiles } const message = useRejectionMessage(options) const statusId = React.useId() @@ -301,10 +302,6 @@ export function FileInput({ syncInputFiles(local.current, uploads ? NO_FILES : entries.filter(isLocalFile)) }, [entries, uploads]) - React.useEffect(() => { - if (focusRequest) local.current?.focus() - }, [focusRequest]) - function setEntries(next: FileInputEntry[]) { entriesRef.current = next setEntriesState(next) @@ -382,8 +379,6 @@ export function FileInput({ if (!blocked) setDragActive(true) } - const refocus = () => setFocusRequest((n) => n + 1) - const removeEntry = (entry: FileInputEntry) => { setEntries(entriesRef.current.filter((e) => e !== entry)) setRejections([]) diff --git a/packages/onu/src/components/listing/bulk-actions.test.tsx b/packages/onu/src/components/listing/bulk-actions.test.tsx index 9f29b3a5..1099c7f8 100644 --- a/packages/onu/src/components/listing/bulk-actions.test.tsx +++ b/packages/onu/src/components/listing/bulk-actions.test.tsx @@ -115,6 +115,27 @@ describe("BulkActionBar", () => { expect(run).not.toHaveBeenCalled() }) + it("moves focus into an input form when it opens and back to its action when cancelled", async () => { + const user = userEvent.setup() + renderWithOnu( + , run: vi.fn() }, + ]} + />, + ) + const action = screen.getByRole("button", { name: "Change status" }) + action.focus() + await user.keyboard("{Enter}") + expect(screen.getByRole("textbox", { name: "Change status" })).toHaveFocus() + await user.tab() + await user.tab() + expect(screen.getByRole("button", { name: "Cancel" })).toHaveFocus() + await user.keyboard("{Enter}") + expect(screen.getByRole("button", { name: "Change status" })).toHaveFocus() + }) + it.each(directions)("works in %s with Dhivehi strings", async (dir) => { renderWithOnu(, { locale: "dv", diff --git a/packages/onu/src/components/listing/bulk-actions.tsx b/packages/onu/src/components/listing/bulk-actions.tsx index dd27b081..a3a79aa1 100644 --- a/packages/onu/src/components/listing/bulk-actions.tsx +++ b/packages/onu/src/components/listing/bulk-actions.tsx @@ -86,6 +86,16 @@ export function BulkActionBar({ const [confirming, setConfirming] = React.useState<{ action: BulkAction; value: unknown } | null>(null) const [message, setMessage] = React.useState<{ text: string; tone: "success" | "error" } | null>(null) const messageRef = React.useRef(null) + const formRef = React.useRef(null) + const actionRefs = React.useRef(new Map()) + // Opening or cancelling a form swaps the buttons out of the bar: once it has re-rendered, move + // focus into the form or back to the action, so it doesn't drop to the page. + const focusAfterRender = React.useRef<(() => void) | null>(null) + React.useEffect(() => { + const focus = focusAfterRender.current + focusAfterRender.current = null + focus?.() + }) // The bar goes away with the selection: keep keyboard focus on the result instead of losing it. React.useEffect(() => { if (message && (selected.length === 0 || message.tone === "error")) messageRef.current?.focus() @@ -142,8 +152,23 @@ export function BulkActionBar({ } const choose = (action: BulkAction) => { - if (action.form) setPhase({ kind: "form", action, value: action.defaultValue }) - else proceed(action, undefined) + if (!action.form) { + proceed(action, undefined) + return + } + setPhase({ kind: "form", action, value: action.defaultValue }) + focusAfterRender.current = () => { + const form = formRef.current + const control = + form?.querySelector(`[id="${CSS.escape(formId)}"]`) ?? + form?.querySelector("input, select, textarea, button") + control?.focus() + } + } + + const cancelForm = (action: BulkAction) => { + setPhase({ kind: "idle" }) + focusAfterRender.current = () => actionRefs.current.get(action.id)?.focus() } const confirmOptions = (action: BulkAction): ConfirmOptions => { @@ -216,6 +241,7 @@ export function BulkActionBar({ {phase.kind === "form" ? (
{ @@ -239,7 +265,7 @@ export function BulkActionBar({ size="sm" variant="ghost" className="touch:min-h-11" - onClick={() => setPhase({ kind: "idle" })} + onClick={() => cancelForm(phase.action)} > {t("listing.cancel")} @@ -249,6 +275,10 @@ export function BulkActionBar({ {visible.map((action) => (