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
65 changes: 62 additions & 3 deletions Sources/DashUIKit/Components/MenuItem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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",
Expand Down
17 changes: 16 additions & 1 deletion docs/lists-and-rows.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ per-call fonts/colors, to keep rows consistent):

- `.none`
- `.toggle(isOn: Binding<Bool>)` — 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)` —
Expand All @@ -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
Expand Down
Loading