|
| 1 | +# Augmentable variants (Swift) |
| 2 | + |
| 3 | +- Status: draft |
| 4 | +- Relation: applies the native form of the AST-082 FR4 rule for augmentable |
| 5 | + props (`docs/specs/AST-082-native-first-surfaces/spec.md`, FR4 as amended) (a string-backed value with built-in constants, extended by the theme |
| 6 | + generator, that decodes any name and draws an unknown name in the base look) |
| 7 | + to Swift, and extends `packages/native/specs/swift/library/theme-generator.md`. |
| 8 | + Native style data comes from `defineNativeTheme` platform entries (AST-082 |
| 9 | + FR10, FR18; AST-093 FR4). |
| 10 | + |
| 11 | +## Summary |
| 12 | + |
| 13 | +A brand that adds its own button or badge variant gets the same variant name on |
| 14 | +iOS: an iOS developer writes `Button("Save", variant: .primaryMuted)`, it |
| 15 | +compiles because the theme declares it, and the person using the app sees it |
| 16 | +styled as the brand's native theme defines it. A variant name nothing styles |
| 17 | +draws the component's base look, with no value style, instead of failing. |
| 18 | + |
| 19 | +## Requirements |
| 20 | + |
| 21 | +- **V1 — An augmentable prop is an open, string-backed type.** For every prop |
| 22 | + the core contract marks augmentable, the contract generator emits a |
| 23 | + `struct <Name><Prop>: RawRepresentable, Hashable, Codable, Sendable, |
| 24 | +ExpressibleByStringLiteral` holding the web value as its raw string, instead |
| 25 | + of a closed `enum`. Each built-in value is a static constant named by the |
| 26 | + theme generator's name rule (`.primary`, `.ghost`); a name that is a Swift |
| 27 | + keyword is written in backticks, so Card's built-in `default` is |
| 28 | + `` .`default` ``. Decoding accepts any |
| 29 | + string, so a value the app has no constant for still decodes; the wire |
| 30 | + encodes and decodes the raw string unchanged. Code never switches over every |
| 31 | + value. |
| 32 | +- **V2 — Each theme's added values become static members.** For each |
| 33 | + augmentable axis, the theme generator collects the values a theme adds and |
| 34 | + emits each one as a static member in an extension |
| 35 | + (`extension ButtonVariant { public static let primaryMuted: ButtonVariant = "primary-muted" }`). |
| 36 | + A value is declared once per generation run, however many themes add it, in a |
| 37 | + generated `Variants.swift` beside the theme files, and never when it is a |
| 38 | + built-in. A product's run skips values the package already declares. A |
| 39 | + Swift keyword is written in backticks, as in V1. A value whose Swift name |
| 40 | + collides with another fails the run with both values. |
| 41 | +- **V3 — Native style data comes from platform entries.** A value's native |
| 42 | + style comes from the native theme's platform entries for the platform being |
| 43 | + built (`platforms.ios` on iOS, `platforms.macos` on macOS). In an entry's |
| 44 | + `components`, a target holds, beside its own style keys, a key |
| 45 | + `'<axis>:<value>'` whose value is that value's style map |
| 46 | + (`components.button['variant:primary-muted'] = {backgroundColor: …}`); the |
| 47 | + `PlatformEntry.components` type allows that nested map. Each theme file keeps |
| 48 | + every such entry with its platform, `minVersion`, and `maxVersion`, and its |
| 49 | + style per mode as typed data keyed by the raw value: colors (foreground, |
| 50 | + background, border), border width, corner radius, font family, size, and |
| 51 | + weight, and padding. Values are resolved per mode with references to tokens |
| 52 | + replaced by the token's value (theme generator G1, G7). At run time a value's |
| 53 | + style is chosen by AST-082 FR10's entry rule: only entries for the running |
| 54 | + platform whose range holds the running OS version apply, and a later |
| 55 | + matching entry replaces an earlier one. The theme's web component overrides |
| 56 | + are never read. A style key Swift does not draw is reported and left out; a |
| 57 | + value in a drawn key that Swift cannot read fails the run. |
| 58 | +- **V4 — Components look style up by name.** A component reads its style for an |
| 59 | + augmentable prop from the theme in the environment, by raw value. A built-in |
| 60 | + value always has its built-in look, which the theme's platform entry may |
| 61 | + restyle. A value with no built-in look that no applying entry styles draws |
| 62 | + the component's base look, as AST-082 FR4 says and as the web does: the |
| 63 | + component with no value style at all, not the look of another value. A |
| 64 | + platform entry's style for the component's own target, not for a value, |
| 65 | + still applies. A development build logs that fallback once, naming the |
| 66 | + component, prop, and value. |
| 67 | +- **V5 — Accessibility does not depend on the variant.** A custom value changes |
| 68 | + only what V3 carries: paint, shape, font, and padding, never behavior. Name, |
| 69 | + role, and state come from the core contract, as for built-ins. |
| 70 | + |
| 71 | +## Acceptance checks |
| 72 | + |
| 73 | +| Requirement | Check | |
| 74 | +| ----------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | |
| 75 | +| V1 | The generated `ButtonVariant` is a struct with every built-in as a static constant; Card's built-in `default` generates as `` .`default` `` and compiles; a wire node with `"variant": "primary-muted"` decodes and re-encodes unchanged, with or without a constant for it. | |
| 76 | +| V2 | A fixture theme adding `variant:primary-muted` generates `ButtonVariant.primaryMuted`; two themes adding it declare it once; a compile test with `Button("x", variant: .primaryMuted) {}` type-checks; `variant:class` generates as `` .`class` ``; `variant:primary-muted` and `variant:primary_muted` in one run fail with both values. | |
| 77 | +| V3 | A fixture native theme whose `platforms.ios` entry styles `variant:primary-muted` generates its light and dark background; a later `platforms.ios` entry with `minVersion` replaces it only on OS versions in its range; a macOS build reads `platforms.macos` and not `platforms.ios`; a `var(--color-accent)` reference generates the accent's value; a `transition` key is reported and absent; a web `components` override for the same value changes nothing in Swift. | |
| 78 | +| V4 | Under neutral, `.primary` and `.ghost` Buttons draw their built-in looks with no warning; an added variant nothing styles draws the Button with no value style, not the default variant's look, and logs one development warning; when an applying `platforms.ios` entry styles the `button` target itself, that unstyled added variant draws with it. A Heading with an added `type` nothing styles draws with no type style. | |
| 79 | +| V5 | Accessibility assertions for a custom variant equal those for the default variant. | |
| 80 | +| Drift | The theme generator's drift check regenerates `Variants.swift` and each theme's style data and fails on any difference. | |
| 81 | + |
| 82 | +## Current state |
| 83 | + |
| 84 | +- Generated contracts declare every closed set, including visual axes, as a |
| 85 | + closed `enum` (`ButtonVariant`, `BadgeVariant`). |
| 86 | +- `PlatformEntry.components` is flat today: a target maps to one style map, |
| 87 | + with no `'<axis>:<value>'` keys. |
| 88 | +- Published themes style values on augmentable axes in their web component |
| 89 | + overrides (for example butter's `variant:muted`); no published theme has a |
| 90 | + native theme with platform entries, and no Swift theme carries style data |
| 91 | + for any component. |
| 92 | + |
| 93 | +## Open questions |
| 94 | + |
| 95 | +None. |
0 commit comments