diff --git a/Sources/DashUIKit/Components/MenuItem.swift b/Sources/DashUIKit/Components/MenuItem.swift index 0cf081a..cbec176 100644 --- a/Sources/DashUIKit/Components/MenuItem.swift +++ b/Sources/DashUIKit/Components/MenuItem.swift @@ -55,6 +55,13 @@ public struct MenuItem: View { public var helpText: String? public var info: MenuItemInfo? public var accessory: MenuItemAccessory + /// What tapping the row's icon and text does, apart from its accessory. + /// + /// With an action the icon and text are one button and the accessory stays + /// its own element, so a row that opens an explainer can still carry a + /// switch that assistive technologies reach on its own. Without one the + /// host decides what the row does, e.g. by wrapping it in a `Button`. + public var action: (() -> Void)? public init( leadingIcon: DashIconSource? = nil, @@ -74,11 +81,48 @@ public struct MenuItem: View { self.accessory = accessory } + /// A row whose icon and text perform `action`, with the accessory kept as + /// a separate element. See ``action``. + public init( + leadingIcon: DashIconSource? = nil, + isEnabled: Bool = true, + disabledLeadingIcon: DashIconSource? = nil, + title: String, + helpText: String? = nil, + info: MenuItemInfo? = nil, + accessory: MenuItemAccessory = .none, + action: @escaping () -> Void + ) { + self.init( + leadingIcon: leadingIcon, + isEnabled: isEnabled, + disabledLeadingIcon: disabledLeadingIcon, + title: title, + helpText: helpText, + info: info, + accessory: accessory + ) + self.action = action + } + public var body: some View { HStack(spacing: 10) { - leading - central - Spacer() + if let action { + Button(action: action) { + HStack(spacing: 10) { + leading + central + Spacer() + } + .contentShape(Rectangle()) + } + .buttonStyle(.plain) + .disabled(!isEnabled) + } else { + leading + central + Spacer() + } trailing } .padding(10) @@ -133,8 +177,11 @@ public struct MenuItem: View { // by UIKit, so a menu row rendered here did not match the switch // the same design system hands out everywhere else. It reads // `isEnabled` from the environment, which `.disabled` sets. + // Named after the row, so the switch reads as its setting rather + // than an unlabelled control next to the title. SwitchView(isOn: isOn) .disabled(!isEnabled) + .accessibilityLabel(Text(title)) case .text(let value): Text(value) .dashFont(.subhead) @@ -194,6 +241,18 @@ public struct MenuItem: View { .padding(.horizontal) } +#Preview("Accessory: toggle + row action") { + @Previewable @State var isOn = false + MenuItem( + title: "Advanced mode", + helpText: "Tap to learn more", + info: .round(color: Color.dash.gray300Alpha70), + accessory: .toggle(isOn: $isOn), + action: {} + ) + .padding(.horizontal) +} + #Preview("Accessory: text") { MenuItem( title: "Balance", diff --git a/docs/lists-and-rows.md b/docs/lists-and-rows.md index ce040a8..dd22b77 100644 --- a/docs/lists-and-rows.md +++ b/docs/lists-and-rows.md @@ -61,7 +61,8 @@ per-call fonts/colors, to keep rows consistent): - `.none` - `.toggle(isOn: Binding)` — a `SwitchView` (the design system's switch, not the - system green `Toggle`); it takes the row's enabled state from the environment + system green `Toggle`); it takes the row's enabled state from the environment, and + assistive technologies read it under the row's title - `.text(String)` - `.button(DashButton)` — embed a `DashButton` - `.balance(dash: Int64, sign: DashAmountSign = .negativeOnly, fiat: String? = nil)` — @@ -72,6 +73,20 @@ per-call fonts/colors, to keep rows consistent): Unselected rows keep the mark's slot (drawn at zero opacity), so nothing shifts horizontally as the selection moves. +**`action`** — optional, set through `MenuItem(…, action:)`. The icon and text become one +button that runs it, and the accessory stays a separate element, so a row that opens an +explainer and carries a switch exposes both: the explainer under the title, the switch +under the title too, as a switch. Without `action` the row has no tap behaviour of its own. + +```swift +MenuItem( + title: "Advanced mode", + info: .round(color: Color.dash.gray300Alpha70), + accessory: .toggle(isOn: $isAdvanced), + action: { showExplainer = true } +) +``` + --- ## ConverterCard