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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ a component already exists before building your own.
| `NavigationBarCircleIcon` | The nav bar's 34pt token circle around any icon | [Navigation & containers](docs/navigation-and-containers.md) |
| `BottomSheet` | Sheet chrome (grabber + nav bar) with self-sizing support | [Navigation & containers](docs/navigation-and-containers.md) |
| `MenuViewModifier` | Card chrome — rounded background + shadow | [Navigation & containers](docs/navigation-and-containers.md) |
| `Toast` | Blurred toast — warning/info/error/success/copied/loading | [Feedback](docs/feedback.md) |
| `Toast` | Blurred toast — warning/info/error/success/copied/loading/noInternet; optional VoiceOver label | [Feedback](docs/feedback.md) |
| `LoadingIllustration` / `LoadingSpinner` | iOS-style activity spinner | [Feedback](docs/feedback.md) |
| `SuccessIllustration` / `ErrorIllustration` | 90×90 status badges | [Feedback](docs/feedback.md) |
| `XmarkIcon` | Code-drawn close (✕) icon | [Feedback](docs/feedback.md) |
Expand Down
22 changes: 21 additions & 1 deletion Sources/DashUIKit/Components/Toast.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,29 +54,48 @@ public struct Toast: View {

private let style: ToastStyle
private let message: String
private let accessibilityLabel: Text
private let onDismiss: (() -> Void)?

public init(
style: ToastStyle,
message: String,
onDismiss: (() -> Void)? = nil
) {
self.init(style: style, message: message, accessibilityLabel: Text(message), onDismiss: onDismiss)
}

/// `accessibilityLabel` is what VoiceOver reads instead of `message`, for a
/// host that wants the style spoken too ("Error, payment failed"). It is a
/// `Text`, so the host localizes it as it would a `Button` title. It replaces
/// the message's label, so pass a new one with every new message. The icon
/// itself is never read.
public init(
style: ToastStyle,
message: String,
accessibilityLabel: Text,
onDismiss: (() -> Void)? = nil
) {
self.style = style
self.message = message
self.accessibilityLabel = accessibilityLabel
self.onDismiss = onDismiss
}

public var body: some View {
HStack(alignment: .top, spacing: 8) {
HStack(alignment: .top, spacing: 0) {
// Otherwise VoiceOver reads the asset name ("toast-warning, image").
leadingIcon
.frame(width: 24, height: 24)
.accessibilityHidden(true)

Text(message)
.dashFont(.footnoteMedium)
.foregroundColor(Color.dash.toastText)
.padding(.vertical, 3)
.padding(.leading, 8)
.accessibilityLabel(accessibilityLabel)
}
.padding(.trailing, 8)

Expand Down Expand Up @@ -126,9 +145,10 @@ public struct Toast: View {

#Preview {
VStack(spacing: 12) {
Toast(style: .warning, message: "Sgome coins are not available", onDismiss: {})
Toast(style: .warning, message: "Some coins are not available", onDismiss: {})
Toast(style: .info, message: "Heads up", onDismiss: {})
Toast(style: .error, message: "Something went wrong", onDismiss: {})
Toast(style: .error, message: "Payment failed", accessibilityLabel: Text("Error, payment failed"), onDismiss: {})
Toast(style: .success, message: "Done", onDismiss: {})
Toast(style: .copied, message: "Copied to clipboard")
Toast(style: .loading, message: "Loading…")
Expand Down
14 changes: 14 additions & 0 deletions docs/feedback.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,20 @@ Toast(
· `.noInternet`. Each draws a `DashIcon.Toast` case (`.warning`, `.info`, …, `.noWifi`
for `.noInternet`). `.loading` instead renders the DS `LoadingSpinner`.

VoiceOver reads only the message; the icon is hidden from it. When the style matters to a
listener and the message does not say it, pass an `accessibilityLabel:` to read instead of
the message. It is a `Text`: a literal `Text("…")` is looked up in the app's main bundle,
`Text("…", bundle: .module)` in a package's, and a `String` variable is read as is. VoiceOver
reads it when focus lands on the message (the dismiss button is a separate stop); announcing
the toast as it appears is the host's job, like its presentation. The label replaces the
message's, for UI tests too, so build it from the same localized message, and add the new
key to the host's strings like any other:

```swift
Toast(style: .error, message: paymentFailed,
accessibilityLabel: Text("Error, \(paymentFailed)")) // key "Error, %@"
```

This is just the toast *view* — presentation/animation/auto-dismiss timing is the host's
responsibility (e.g. overlay it and drive visibility yourself).

Expand Down
Loading