Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) |
Expand Down
22 changes: 17 additions & 5 deletions Sources/DashUIKit/Components/Switch/SwitchView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand All @@ -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 {
Expand Down
5 changes: 3 additions & 2 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down Expand Up @@ -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) |
Expand Down
25 changes: 25 additions & 0 deletions docs/buttons-and-inputs.md
Original file line number Diff line number Diff line change
Expand Up @@ -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**
Expand Down
Loading