Skip to content

a11y(swiftui): announce visible text changes in live regions - #4021

Merged
pinieb merged 17 commits into
a2ui-project:mainfrom
karolkulesza:a11y/swiftui-live-region-text
Nov 12, 2026
Merged

pinieb merged 17 commits into
a2ui-project:mainfrom
karolkulesza:a11y/swiftui-live-region-text

Conversation

@karolkulesza

@karolkulesza karolkulesza commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

Description

Part of #3147 (part 1, live regions).

In SwiftUI, a component with accessibility.live set to polite or assertive only announced its
explicit accessibility.label. A live status Text, or a live Column/Card banner without a
label, said nothing when its text changed.

Following the design in #3147:

  • A2UIAccessibleTextKey (in A2UISwiftUI) collects visible text in tree order. Components report
    it with the new .a2uiAccessibleText(_:) modifier.
  • A2UIText reports its text without Markdown syntax. Block Markdown is joined with line breaks,
    and ordered list items keep their numbers. Text is only built inside a live region, so surfaces
    without one do no extra work. A2UIText.swift adds one line and makes attributedMarkdown(_:)
    internal again, so the extension can use it.
  • A2UIAccessibilityModifier reads the subtree text on a live node and, when it changes,
    announces the explicit label followed by the joined text. It no longer depends on any catalog
    property names.
  • A label on a .combine element (such as Text) or on a Button replaces its visible text for
    VoiceOver, so that text isn't reported and the label is reported instead. A labeled live Text
    reads its label once, and a labeled live Card or Column reads its label and the updated text.
  • A hidden subtree reports no text, and a live region inside a hidden ancestor announces nothing.
    hidden only adds accessibilityHidden(true) when set, so a hidden child of a visible
    container stays hidden. On iOS 18 and macOS 15 it uses accessibilityHidden(_:isEnabled:), so
    a bound hidden keeps the component's state. Showing a live region again announces its text.
    A live region doesn't pass its text to its ancestors, so a nested live region announces an
    update once.

Not in this PR: part 2 of #3147 (description on .contain groups). Only Text reports text for
now; TextField, CheckBox, Slider and ChoicePicker could follow.

Testing:

  • New LiveRegionTextTests host a surface inside a live region and check the reported text:
    inline and block Markdown (including a multi-paragraph ordered list item), tree order, labels,
    a Button label replacing its content, hidden and nested live subtrees, a data model update,
    and nothing reported outside a live region. Unit tests cover the announced text (label
    first, joined text, live: off, hidden).
  • AccessibilityModifierTests checks the native AppKit accessibility tree: a hidden child of a
    visible Column is not in it.
  • An @_spi(Testing) a2uiAnnouncer environment value lets the tests record the posted
    announcements: nothing on mount, one assertive announcement per data model update, a labeled
    live Text read once, showing a hidden live region, and silence under a hidden ancestor.
  • swift test --enable-swift-testing --no-parallel: 967 tests pass.
  • xcodebuild build of A2UISwiftCore-Package and the sample app for the iOS Simulator.
  • xcrun swift-format lint --strict on the changed files: clean.

Pre-launch Checklist

One time:

For this PR:

  • I have updated the relevant CHANGELOG.md file.
  • I updated/added relevant documentation. (Doc comments, and the accessibility section of swift/swiftui/README.md.)
  • My code changes (if any) have tests.
  • If my branch is on a fork, I have verified that scripts/e2e_test.sh passes.

If you need help, consider asking for advice on the discussion board.

A live component announced only its explicit accessibility label, so a
live Text or a live Column/Card banner without a label stayed silent when
its text changed.

Components now report their visible text through A2UIAccessibleTextKey
(.a2uiAccessibleText(_:)). A2UIText reports its text without Markdown
syntax. A live, non-hidden node announces its explicit label if set,
otherwise the joined subtree text, whenever it changes. Hidden subtrees
report no text, and a live region doesn't pass its text to ancestors, so
nested live regions announce once.

Part 1 of a2ui-project#3147.
…bers

Components now build and report their visible text only inside a live
region, so surfaces without one don't parse Markdown a second time.
Ordered list items keep their numbers in the announced text.
A list item with several paragraphs repeated its number before each one.
Number only the item's first block, as the screen shows it.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a mechanism for SwiftUI components to report their visible text to enclosing live regions using a custom preference key (A2UIAccessibleTextKey) and environment values. This allows live regions to announce updates to their visible text dynamically without needing to read catalog properties directly. The changes include helper modifiers, Markdown text parsing for accessibility, and comprehensive unit tests to verify the behavior. There are no review comments to address, and the implementation looks solid.

@github-actions github-actions Bot added the status: needs-triage auto-managed: https://github.com/a2ui-project/a2ui/blob/main/scripts/triage.mjs label Nov 6, 2026
@pinieb pinieb added lang: swift Affects SDKs or apps in the Swift programming language component: SwiftUI adapter Swift UI framework adapter labels Nov 9, 2026
@github-actions github-actions Bot removed the status: needs-triage auto-managed: https://github.com/a2ui-project/a2ui/blob/main/scripts/triage.mjs label Nov 9, 2026
…le text extension

a2ui-project#3887 made it private, so A2UIText+AccessibleText.swift no longer compiled after the merge from main.
…unted when hidden

- A live region announces its explicit label followed by its subtree text. A label on a
  `.combine` element replaces the visible text it reports, and a labeled component inside a
  live region reports its label.
- `hidden` no longer swaps the view tree, so a bound `hidden` keeps component state and
  showing a live region again announces its text. A live region under a hidden ancestor
  announces nothing.
- Add an `@_spi(Testing)` `a2uiAnnouncer` environment value, and test the posted
  announcements. LiveRegionTextTests now tear down their window.
@github-actions github-actions Bot added status: needs-triage auto-managed: https://github.com/a2ui-project/a2ui/blob/main/scripts/triage.mjs and removed status: waiting-for-author-response labels Nov 11, 2026
@pinieb

pinieb commented Nov 11, 2026

Copy link
Copy Markdown
Collaborator

Thanks for the clean updates — A2UIAnnouncer, LiveRegionHostModifier, .combine label replacement, and HostedSurface.withHost all look great!

Left one blocking catch in hidden(_:) (.accessibilityHidden(false) overrides .accessibilityHidden(true) on descendants in SwiftUI) along with a couple of small notes inline.

Could you also:

  1. Add an entry under ## Unreleased in swift/CHANGELOG.md (recently added on main for the Swift packages, wrapped at 100 characters and ending with [#4021](https://github.com/a2ui-project/a2ui/pull/4021))?
  2. Update swift/swiftui/README.md (lines 82–83 and 106–108) to mention .a2uiAccessibleText(_:) for custom components and refresh the [Swift] SwiftUI accessibility limitations on container descriptions (accessibilityHint) and container live regions #3147 limitation note?

@pinieb pinieb added status: waiting-for-author-response and removed status: needs-triage auto-managed: https://github.com/a2ui-project/a2ui/blob/main/scripts/triage.mjs labels Nov 11, 2026
…Button's label

Apply accessibilityHidden(true) only when hidden is set. On iOS 18 and macOS 15 use
accessibilityHidden(_:isEnabled:) so a bound hidden keeps the view tree. A Button with an
explicit label no longer reports its content text to live regions, and labelText reports
through a2uiAccessibleText(nil) instead of switching branches.
@karolkulesza

Copy link
Copy Markdown
Contributor Author

Thanks for the second pass. The three inline notes are addressed in b87a5b9. In b3b4079 I added a ## Unreleased entry to swift/CHANGELOG.md and updated the accessibility section of swift/swiftui/README.md: the live bullet now mentions visible text, there's a paragraph on .a2uiAccessibleText(_:) for custom components, and the limitation note now says that only Text reports its text so far and that a bound hidden resets state on iOS 17 / macOS 14. I also merged main into the branch.

@github-actions github-actions Bot added status: needs-triage auto-managed: https://github.com/a2ui-project/a2ui/blob/main/scripts/triage.mjs and removed status: waiting-for-author-response labels Nov 12, 2026

@pinieb pinieb left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, thank you!

@pinieb
pinieb merged commit b4365c1 into a2ui-project:main Nov 12, 2026
50 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

component: SwiftUI adapter Swift UI framework adapter lang: swift Affects SDKs or apps in the Swift programming language status: needs-triage auto-managed: https://github.com/a2ui-project/a2ui/blob/main/scripts/triage.mjs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants