Skip to content

feat(swift): add A2UICardStyle, A2UIButtonStyle, and Surface.makeWithBasicCatalogs - #4024

Merged
pinieb merged 4 commits into
a2ui-project:mainfrom
peterfriese:feat/swiftui-stratos-progressive-disclosure
Nov 13, 2026
Merged

pinieb merged 4 commits into
a2ui-project:mainfrom
peterfriese:feat/swiftui-stratos-progressive-disclosure

Conversation

@peterfriese

@peterfriese peterfriese commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

Description

Adds SwiftUI component style protocols (A2UICardStyle, A2UIButtonStyle) and Surface.makeWithBasicCatalogs(viewModel:) to BasicCatalogSwiftUI, and requires an explicit catalogs argument on Surface.init(viewModel:catalogs:defaultCatalogID:), addressing #3257.

Changes

  • Surface Catalog Configuration:
    • Drops the = [] default argument on Surface.init(viewModel:catalogs:defaultCatalogID:) in A2UISwiftUI so passing an empty catalog array is an explicit choice rather than an accidental omission.
    • Adds Surface.makeWithBasicCatalogs(viewModel:) in BasicCatalogSwiftUI (Surface+BasicCatalog.swift) to create a Surface preconfigured with BasicCatalogImplementation.allCatalogs (v0.9, v0.9.1, and v1.0), falling back to BasicCatalog.catalogURI(version: viewModel.protocolVersion) when viewModel.defaultCatalogID is nil.
  • Component Style Protocols (BasicCatalogSwiftUI):
    • A2UICardStyle (refining DynamicProperty, Sendable) and A2UICardStyleConfiguration, with built-in .automatic (DefaultA2UICardStyle), .elevated (ElevatedA2UICardStyle), and .outlined (OutlinedA2UICardStyle) styles, applied via .a2uiCardStyle(_:).
    • A2UIButtonStyle (refining DynamicProperty, Sendable) and A2UIButtonStyleConfiguration (exposing variant, isPressed, isDisabled, and node), with .automatic (DefaultA2UIButtonStyle), applied via .a2uiButtonStyle(_:).
  • Tests:
    • Adds ComponentStyleTests covering custom A2UICardStyle / A2UIButtonStyle configurations, @Environment resolution (e.g. \.colorScheme in Dark Mode) via DynamicProperty, and built-in card/button styles.
    • Adds makeWithBasicCatalogsConfiguresAllVersionsAndProtocolFallback in SurfaceTests and updates SurfaceTests for explicit catalogs: [].

Related to #3257 and #4195.

Pre-launch Checklist

One time:

For this PR:

  • I have updated the relevant CHANGELOG.md file.
  • I updated/added relevant documentation.
  • My code changes (if any) have tests.
  • If my branch is on a fork, I have verified that scripts/e2e_test.sh passes.

@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 customizable styling system for A2UI components in SwiftUI, refactoring A2UIButton and A2UICard to delegate rendering to style protocols (A2UIButtonStyle and A2UICardStyle) and providing default, elevated, and outlined style implementations. It also adds environment-based catalog and theme propagation utilities alongside comprehensive unit tests. The review feedback suggests preserving the existing default catalog ID in a2uiCatalogs when not explicitly overridden, using configuration.isDisabled instead of !configuration.node.isValid for button opacity calculations, and replacing iOS 17+ specific .rect shapes with RoundedRectangle to maintain backward compatibility and consistency.

@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
@peterfriese

Copy link
Copy Markdown
Contributor Author

cc @pinieb for review when you get a chance!

@peterfriese
peterfriese force-pushed the feat/swiftui-stratos-progressive-disclosure branch from 5347608 to 919316c Compare November 6, 2026 19:27
@peterfriese

Copy link
Copy Markdown
Contributor Author

/gemini review

@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 customizable styling capabilities for A2UI SwiftUI components, specifically adding A2UIButtonStyle and A2UICardStyle protocols along with several default implementations. It also adds environment modifiers to easily register catalogs, themes, and styles. The feedback focuses on improving environment integration and backward compatibility: specifically, A2UIButton should respect the ancestor isEnabled state, and the code should avoid using the @entry macro and newer shape/overlay APIs (like .rect and overlay(_:in:)) to maintain compatibility with Swift 5.9 and older OS versions (iOS 15/16).

@peterfriese
peterfriese force-pushed the feat/swiftui-stratos-progressive-disclosure branch from 919316c to 147c0eb Compare November 6, 2026 20:59
@peterfriese

Copy link
Copy Markdown
Contributor Author

/gemini review

@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 customizable styling capabilities for A2UI SwiftUI components by defining A2UIButtonStyle and A2UICardStyle protocols, along with their default, elevated, and outlined implementations. It also updates Surface to support environment-based resolution of catalogs, themes, and default catalog IDs. The feedback highlights critical compilation and backward compatibility issues (iOS 15/16) in the style definitions due to the use of .rect(cornerRadius:) and .stroke(lineWidth:) inside overlays, recommending a fallback to RoundedRectangle and .strokeBorder. Additionally, it suggests caching basic catalogs to avoid performance bottlenecks from redundant recreations during view initialization.

@peterfriese

Copy link
Copy Markdown
Contributor Author

All automated review feedback has been addressed — ready for your review whenever you get a chance, @pinieb!

@peterfriese
peterfriese force-pushed the feat/swiftui-stratos-progressive-disclosure branch from 53750ae to f60b3de Compare November 7, 2026 08:38
@pinieb

pinieb commented Nov 10, 2026

Copy link
Copy Markdown
Collaborator

I need a bit more time with this PR. I'm working to resolve how some of these new APIs interact the blueprint's requirements and haven't quite settled on that yet. It is on my radar though, and I have started poking at it.

@pinieb
pinieb self-requested a review November 10, 2026 19:33
@pinieb pinieb added status: needs review lang: swift Affects SDKs or apps in the Swift programming language component: SwiftUI adapter Swift UI framework adapter and removed status: needs-triage auto-managed: https://github.com/a2ui-project/a2ui/blob/main/scripts/triage.mjs labels Nov 10, 2026
@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 10, 2026
@peterfriese
peterfriese force-pushed the feat/swiftui-stratos-progressive-disclosure branch from f60b3de to 37becc3 Compare November 11, 2026 20:47
@polina-c

Copy link
Copy Markdown
Collaborator

From triage bot:

Triage suggestion:

For humans

Summary:
The PR adds SwiftUI style protocols for Card and Button (A2UICardStyle, A2UIButtonStyle), environment modifiers for catalogs and the theme, and a Surface(viewModel:) initializer that uses the basic catalog by default. It covers part of #3257, in about 780 added lines.

Why flagged for triage:
This external PR has no assignee.

Suggestion:
Assign pinieb, who said on 2026-11-10 that they are reviewing the PR and are still working out how the new APIs fit the requirements of the SwiftUI codebase blueprint. That comment is the team's decision to review, so the PR only needs an owner. On its own, the PR would not meet the review criteria: it adds new public API, and #3257 is not assigned to the author. The author pushed new commits on 2026-11-11, after that comment. #4123, a draft by nan-yu, also changes Surface.swift. The PR is related to #3257 but does not close it: it covers Card and Button, and #3257 also names Text, TextField and Image.

  1. Add labels: type: feature/enhancement
  2. Assign: pinieb
  3. Related issues: [FEATURE]: Let apps customize Basic Catalog styles in the SwiftUI renderer #3257
  4. Related PRs: feat(swiftui): load policy-checked media URLs and add opt-in Markdown links #4123

To adjust and apply this suggestion, run in your a2ui checkout:

tools/internal/triage_new/triage_ui.sh https://github.com/a2ui-project/a2ui/pull/4024
For software
<a2ui>
root = TriageSuggestion(
  summary="""The PR adds SwiftUI style protocols for `Card` and `Button` (`A2UICardStyle`, `A2UIButtonStyle`), environment modifiers for catalogs and the theme, and a `Surface(viewModel:)` initializer that uses the basic catalog by default. It covers part of #3257, in about 780 added lines.""",
  reason="""This external PR has no assignee.""",
  suggestion="""Assign `pinieb`, who [said](https://github.com/a2ui-project/a2ui/pull/4024#issuecomment-6777228439) on 2026-11-10 that they are reviewing the PR and are still working out how the new APIs fit the requirements of the SwiftUI [codebase blueprint](https://github.com/a2ui-project/a2ui/blob/8bf644a37d680a227296b8c0849a7f4b328b8b73/blueprints/codebases/swift/swiftui/codebase.blueprint.md). That comment is the team's decision to review, so the PR only needs an owner. On its own, the PR would not meet the review criteria: it adds new public API, and #3257 is not assigned to the author. The author pushed new commits on 2026-11-11, after that comment. #4123, a draft by `nan-yu`, also changes `Surface.swift`. The PR is related to #3257 but does not close it: it covers `Card` and `Button`, and #3257 also names `Text`, `TextField` and `Image`.""",
  added_labels=["type: feature/enhancement"],
  new_assignees=["pinieb"],
  related_issues=[3257],
  related_prs=[4123]
)
</a2ui>

🤖 Agent-generated

@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 for you bearing with me, I went back and forth for a while about what to do with the Environment pieces of this PR.

The tl;dr: is that I think we should not adopt the Environment changes in this PR, but I do want to take the custom component styles.

The longer story is that I do see value in potentially letting clients populate catalogs through the Environment, but I'm not sure what the right way to reconcile that with the other API goals is at the moment. Because of that, I would rather skip the Environment changes in his PR and spend some time thinking about where the API surface should head.

This PR brought my attention to some pitfalls in the current intended usage (e.g. the Surface initializer defaults its catalog dictionary to [:], and a2uiCatalogs is publicly writable above/outside of the Surface hierarchy`) that I would like to shore up as well.

From the standpoint of allowing clients to customize the appearance of Basic Catalog components, I think styles are the right approach. theme has been replaced on the v1.0 Catalog schema, and outright removed from the Basic Catalog schema. While its replacement could be used to deliver theming data in other catalog implementations, the Basic Catalog starting in v1.0 does not.

@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 12, 2026
@peterfriese
peterfriese force-pushed the feat/swiftui-stratos-progressive-disclosure branch from 37becc3 to 8ea67cf Compare November 12, 2026 20:00
@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
@peterfriese peterfriese changed the title feat(swift): add progressive disclosure modifiers and card/button styles feat(swift): add A2UICardStyle, A2UIButtonStyle, and Surface.makeWithBasicCatalogs Nov 12, 2026
@peterfriese

Copy link
Copy Markdown
Contributor Author

Thanks for taking the time to think this through, @pinieb, and great catch on DynamicProperty for the styles!

Stripping out the Environment catalog/theme bits for now makes sense to me. I've updated the PR to focus just on A2UICardStyle, A2UIButtonStyle, and Surface.makeWithBasicCatalogs(viewModel:locale:), addressed your inline comments, and rebased onto main.

I also left a quick note on #4195 with the use case that prompted the environment modifier idea in the first place so we don't lose track of it.

@peterfriese
peterfriese force-pushed the feat/swiftui-stratos-progressive-disclosure branch from 8ea67cf to 7dd9900 Compare November 13, 2026 10:20

@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.

Thanks again for coming along with me on this one, and thank you for the PR!

@pinieb
pinieb merged commit f2c410a into a2ui-project:main Nov 13, 2026
51 checks passed
pinieb pushed a commit that referenced this pull request Dec 1, 2026
…#4361)

## Problem

#3257 asks for a way to restyle the SwiftUI Basic Catalog components.
#4024 added `A2UIButtonStyle` and `A2UICardStyle`, and the issue stays
open for `Text`, `TextField` and `Image`. `A2UITextField` still
hard-codes its look: 10 pt padding, a gray fill, an 8 pt corner radius,
and a border and label that turn red while a check fails. To change
that, an app has to replace the whole component, copying the data
binding, the variants and the accessibility modifiers.

Part of #3257. This PR covers `TextField` only. `Text` and `Image` are
also changed by the open #4123.

## Changes

- Adds `A2UITextFieldStyle`, `A2UITextFieldStyleConfiguration` and
`View.a2uiTextFieldStyle(_:)` to `BasicCatalogSwiftUI`, built the same
way as `A2UIButtonStyle` in #4024. The protocol refines
`DynamicProperty` and the style is resolved through a generic wrapper
view, so a style's `@Environment` values (such as `\.colorScheme`)
resolve.
- The configuration has:
- `label`: the label text, or `nil` when the `label` property is empty.
It stays hidden from assistive technologies, as before.
- `field`: the input control for the variant (`SecureField` for
`obscured`, a vertical `TextField` for `longText`, the decimal pad on
iOS for `number`), bound to `value` and carrying the accessibility
identifier, label and hint.
- `variant` (`shortText`, `longText`, `number`, `obscured`; unknown
values resolve to `shortText`), `errorMessage` (the first failing
`error` check) with `hasError`, `isDisabled` (from the environment's
`isEnabled`), and `node`.
- `DefaultA2UITextFieldStyle` (`.automatic`) draws what `A2UITextField`
drew before, with the same values.
- `A2UITextField` still renders `A2UICheckMessage` below the style's
view, so errors and warning/info hints look the same under any style.
The accessibility modifiers moved from the padded box onto the input
control, so a custom style can't drop them. They still reach the same
native text field.
- The style is read in a private host view inside `A2UITextField.body`,
so tests that call `.body` outside a hosted view don't read the
environment.
- New tests in `ComponentStyleTests`:
- a custom style gets the label, variant, error message, disabled state
and node;
- an unknown variant resolves to `shortText`, and an ancestor
`.disabled(true)` sets `isDisabled`;
  - a style resolves `\.colorScheme` in Dark Mode inside a `Surface`;
  - the default style renders every variant with a failing check;
- under a custom style, the `a2ui-<id>` element in the platform
accessibility tree has the explicit accessibility label and hint, or the
`label` property when there is no explicit label.

The existing `A2UITextField`, check message, accessibility and adapter
conformance tests pass unchanged.
- A "Styling Basic Catalog components" section in `swift/README.md` with
a text field style example, and a `swift/CHANGELOG.md` entry.

Verified on macOS (Xcode toolchain, Swift 6.1 package):

- `swift build`
- `swift test --enable-swift-testing --no-parallel --filter
A2UISwiftUITests`: 203 tests. All pass except
`A2UIModalTests.keyboardActivationOfButtonTriggerRunsActionOnceAndPresentsSheet`,
which also fails on `main` when run headless.
- `swift test --enable-swift-testing --no-parallel --skip
A2UISwiftUITests`: 809 tests pass.
- `scripts/fix_format.sh --check --only-swift` and `swift-format lint
--strict` on the changed files.

## Pre-launch Checklist

One time:

- [x] I signed the [CLA].
- [x] I read the [Contributors Guide].
- [x] I read the [Style Guide].
- [ ] I have built and run at least one [sample].

For this PR:

- [x] I have updated the relevant CHANGELOG.md file.
- [x] I updated/added relevant documentation.
- [x] My code changes (if any) have tests.
- [ ] If my branch is on a fork, I have verified that
[scripts/e2e_test.sh](https://github.com/a2ui-project/a2ui/blob/main/scripts/e2e_test.sh)
passes.

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

<!-- Links -->

[CLA]: https://cla.developers.google.com/
[Contributors Guide]:
https://github.com/a2ui-project/a2ui/blob/main/CONTRIBUTING.md
[discussion board]: https://github.com/a2ui-project/a2ui/discussions
[Style Guide]:
https://github.com/a2ui-project/a2ui/blob/main/CONTRIBUTING.md#coding-style
[sample]:
https://github.com/a2ui-project/a2ui/blob/main/samples/README.md#samples

---------

Co-authored-by: Polina Cherkasova <polina.c@live.com>
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 review 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.

3 participants