diff --git a/README.md b/README.md index 6660d92..9b58ac0 100644 --- a/README.md +++ b/README.md @@ -67,6 +67,7 @@ a component already exists before building your own. |---|---|---| | `DashButton` | Themed button — 4 sizes × 11 styles, icons, loading, full-width | [Buttons & inputs](docs/buttons-and-inputs.md) | | `DashSwitch` | DS-tinted toggle | [Buttons & inputs](docs/buttons-and-inputs.md) | +| `SwitchView` | DS-drawn switch; a native `Toggle` to VoiceOver | [Buttons & inputs](docs/buttons-and-inputs.md) | | `SearchBar` | Search field with an animated cancel button | [Buttons & inputs](docs/buttons-and-inputs.md) | | `AddressFieldView` | Labeled text field (label / field / help text) with optional QR, clear and error state | [Buttons & inputs](docs/buttons-and-inputs.md) | | `Criteria` | Requirement check-list — `Criterion` rows in six states | [Buttons & inputs](docs/buttons-and-inputs.md) | diff --git a/Sources/DashUIKit/Components/Switch/SwitchView.swift b/Sources/DashUIKit/Components/Switch/SwitchView.swift index 3e260bc..08a9391 100644 --- a/Sources/DashUIKit/Components/Switch/SwitchView.swift +++ b/Sources/DashUIKit/Components/Switch/SwitchView.swift @@ -7,6 +7,14 @@ import SwiftUI +/// The design system's switch: a blue track with a white thumb, drawn by hand +/// rather than tinting the system `Toggle`. +/// +/// To assistive technologies it is a native `Toggle` with a hidden label: the +/// same traits, the same state value, which VoiceOver speaks in the user's +/// language, the same disabled state, and activation flips `isOn`. Give it a +/// name with `.accessibilityLabel(_:)`, or let a row that combines its children +/// supply one. public struct SwitchView: View { private struct Constants { @@ -18,8 +26,6 @@ public struct SwitchView: View { static let switchTrackPadding: CGFloat = 2 static let switchTrackRadius: CGFloat = 1000 static let animationDuration: Double = 0.2 - static let onValue = NSLocalizedString("On", bundle: .module, comment: "DashUIKit") - static let offValue = NSLocalizedString("Off", bundle: .module, comment: "DashUIKit") } @Environment(\.isEnabled) private var isEnabled @@ -42,9 +48,15 @@ public struct SwitchView: View { .animation(.easeInOut(duration: Constants.animationDuration), value: isOn) .contentShape(Rectangle()) .onTapGesture { isOn.toggle() } - .accessibilityAddTraits(.isToggle) - .accessibilityValue(Text(isOn ? Constants.onValue : Constants.offValue)) - .accessibilityAction { isOn.toggle() } + // The system control's semantics, not hand-made ones: VoiceOver + // localizes a toggle's state only when it comes from a real `Toggle`. + // The style is pinned so the role stays a switch on macOS, where the + // default is a checkbox, and under a host's `toggleStyle`. + .accessibilityRepresentation { + Toggle(isOn: $isOn) { EmptyView() } + .labelsHidden() + .toggleStyle(.switch) + } } private var trackFill: Color { diff --git a/docs/README.md b/docs/README.md index 626a7e1..ae29a38 100644 --- a/docs/README.md +++ b/docs/README.md @@ -12,8 +12,8 @@ and callbacks; they render and report intent. They require `import DashUIKit` an - **[Foundation](foundation.md)** — design tokens: colors (`Color.dash.*`), typography (`Font.dash.*`, `.dashFont`, `DashTextStyle`), icons (`DashIconSource`, `Image(dash:)`), bundle access. -- **[Buttons & inputs](buttons-and-inputs.md)** — `DashButton`, `DashSwitch`, `SearchBar`, - `AddressFieldView`, `Criteria`, `SimpleSelect`, `NumericKeyboardView`. +- **[Buttons & inputs](buttons-and-inputs.md)** — `DashButton`, `DashSwitch`, `SwitchView`, + `SearchBar`, `AddressFieldView`, `Criteria`, `SimpleSelect`, `NumericKeyboardView`. - **[Amount & currency](amount-and-currency.md)** — `EnterAmountView`, `SwapAmountView`, `ReceiveEstimateView`, `DashAmount`, `DashBalanceView`, `DashPickerView`, `CurrencyOption`. @@ -72,6 +72,7 @@ and callbacks; they render and report intent. They require `import DashUIKit` an | `SimpleSelect` | [Buttons & inputs](buttons-and-inputs.md#simpleselect) | | `SuccessIllustration` | [Feedback](feedback.md#successillustration--errorillustration) | | `SwapAmountView` | [Amount & currency](amount-and-currency.md#swapamountview) | +| `SwitchView` | [Buttons & inputs](buttons-and-inputs.md#switchview) | | `SystemMessageView` | [Feedback](feedback.md#systemmessageview) | | `Toast` (`ToastStyle`) | [Feedback](feedback.md#toast) | | `TopIntroView` | [Navigation & containers](navigation-and-containers.md#topintroview) | diff --git a/docs/buttons-and-inputs.md b/docs/buttons-and-inputs.md index 0e2cb7b..84edb1a 100644 --- a/docs/buttons-and-inputs.md +++ b/docs/buttons-and-inputs.md @@ -59,6 +59,31 @@ DashSwitch(isOn: $isOn) --- +## SwitchView + +File `Components/Switch/SwitchView.swift` + +The design system's switch: the DS track and thumb drawn in SwiftUI, on iOS and macOS. +`MenuItem`'s `.toggle` accessory uses it. It takes its enabled state from the environment, +so `.disabled(true)` dims it and stops the tap. Use it where the DS switch is drawn; +`DashSwitch` is the system switch with the DS tint, UIKit only. + +```swift +@State private var isOn = false +SwitchView(isOn: $isOn) + .accessibilityLabel("Advanced mode") // optional, see below +``` + +To assistive technologies and UI tests it is a native switch-style `Toggle` with a hidden +label: the same traits, the same state value, which VoiceOver speaks in the user's +language, dimmed when disabled, and a double-tap flips `isOn`. Its own label is empty: +name it with `.accessibilityLabel(_:)`, or put it in a row that combines its children +(`.accessibilityElement(children: .combine)`), which then reads the row's text as the +label and the switch's state as the value. On iOS, UI tests read the state as the value +`"1"` / `"0"`, as for any `Toggle`. + +--- + ## SearchBar File `Components/SearchBar.swift` · **UIKit only**