diff --git a/README.md b/README.md index c7f870a..ab87c27 100644 --- a/README.md +++ b/README.md @@ -92,6 +92,7 @@ a component already exists before building your own. | `FilterBar` | Bottom "Filtered by" bar with a round filter button; a toast can take the caption's place | [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/noInternet; optional VoiceOver label | [Feedback](docs/feedback.md) | +| `ModalDialog` | Dialog card — warning icon, heading, paragraphs, one or two stacked buttons (one destructive) | [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) | diff --git a/SnapshotTests/Tests/DashUIKitSnapshotTests/ModalDialogSnapshotTests.swift b/SnapshotTests/Tests/DashUIKitSnapshotTests/ModalDialogSnapshotTests.swift new file mode 100644 index 0000000..5e1d8b7 --- /dev/null +++ b/SnapshotTests/Tests/DashUIKitSnapshotTests/ModalDialogSnapshotTests.swift @@ -0,0 +1,72 @@ +// +// Copyright © 2026 Dash Core Group. All rights reserved. +// +// Licensed under the MIT License (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +#if canImport(UIKit) +import DashUIKit +import SwiftUI +import XCTest + +@MainActor +final class ModalDialogSnapshotTests: XCTestCase { + + /// One full-width button under a paragraph. + func testOneButton() throws { + let view = ModalDialog( + icon: .warning, heading: "Success", + paragraphs: ["Disconnected. This wallet could not tell yet which app holds some keys on your identity (1). Open Connections again later to see them."], + primaryAction: .init(title: "OK") {}) + try assertComponentSnapshots(of: view, named: "one-button") + } + + /// The secondary action above a destructive primary one. + func testTwoButtons() throws { + let view = ModalDialog( + icon: .warning, heading: "Keys left on your identity", + paragraphs: ["Disconnected. Keys this wallet did not record giving are still on your identity (2): keys of this app from another device or from before a restore, or DashPay profile keys no connection holds. Turn them off too?"], + primaryAction: .init(title: "Turn them off", role: .destructive) {}, + secondaryAction: .init(title: "Leave them") {}) + try assertComponentSnapshots(of: view, named: "two-buttons") + } + + /// Several paragraphs, a blank line between them; and a heading with no + /// icon and no paragraph. + func testParagraphsAndBare() throws { + let view = VStack(spacing: 16) { + ModalDialog( + icon: .warning, heading: "Success", + paragraphs: [ + "Disconnected. This wallet could not tell yet which app holds some keys on your identity (1). Open Connections again later to see them.", + "Some keys on your identity (1) may belong to another app of the same developer, so this wallet left them on. Each one shows on its own row in Connections; disconnect it there if you no longer need it.", + "Disconnected. Some keys of this app are still on your identity (1). Disconnect it again to turn them off.", + ], + primaryAction: .init(title: "OK") {}) + ModalDialog(heading: "Disconnected", paragraphs: [], primaryAction: .init(title: "OK") {}) + } + try assertComponentSnapshots(of: view, named: "paragraphs-bare") + } + + /// Text taller than the height the host gives scrolls inside the card, + /// and the button stays visible under it. + func testLongTextScrollsAboveTheButtons() throws { + let paragraph = "Some keys on your identity (1) may belong to another app of the same developer, so this wallet left them on. Each one shows on its own row in Connections; disconnect it there if you no longer need it." + let view = ModalDialog( + icon: .warning, heading: "Success", + paragraphs: Array(repeating: paragraph, count: 5), + primaryAction: .init(title: "OK") {}) + .frame(height: 500) + try assertComponentSnapshots(of: view, named: "scrolls") + } +} +#endif diff --git a/SnapshotTests/Tests/DashUIKitSnapshotTests/__Snapshots__/ModalDialogSnapshotTests/testLongTextScrollsAboveTheButtons.scrolls-dark.png b/SnapshotTests/Tests/DashUIKitSnapshotTests/__Snapshots__/ModalDialogSnapshotTests/testLongTextScrollsAboveTheButtons.scrolls-dark.png new file mode 100644 index 0000000..d89755a Binary files /dev/null and b/SnapshotTests/Tests/DashUIKitSnapshotTests/__Snapshots__/ModalDialogSnapshotTests/testLongTextScrollsAboveTheButtons.scrolls-dark.png differ diff --git a/SnapshotTests/Tests/DashUIKitSnapshotTests/__Snapshots__/ModalDialogSnapshotTests/testLongTextScrollsAboveTheButtons.scrolls-light.png b/SnapshotTests/Tests/DashUIKitSnapshotTests/__Snapshots__/ModalDialogSnapshotTests/testLongTextScrollsAboveTheButtons.scrolls-light.png new file mode 100644 index 0000000..4809c43 Binary files /dev/null and b/SnapshotTests/Tests/DashUIKitSnapshotTests/__Snapshots__/ModalDialogSnapshotTests/testLongTextScrollsAboveTheButtons.scrolls-light.png differ diff --git a/SnapshotTests/Tests/DashUIKitSnapshotTests/__Snapshots__/ModalDialogSnapshotTests/testOneButton.one-button-dark.png b/SnapshotTests/Tests/DashUIKitSnapshotTests/__Snapshots__/ModalDialogSnapshotTests/testOneButton.one-button-dark.png new file mode 100644 index 0000000..e771826 Binary files /dev/null and b/SnapshotTests/Tests/DashUIKitSnapshotTests/__Snapshots__/ModalDialogSnapshotTests/testOneButton.one-button-dark.png differ diff --git a/SnapshotTests/Tests/DashUIKitSnapshotTests/__Snapshots__/ModalDialogSnapshotTests/testOneButton.one-button-light.png b/SnapshotTests/Tests/DashUIKitSnapshotTests/__Snapshots__/ModalDialogSnapshotTests/testOneButton.one-button-light.png new file mode 100644 index 0000000..5767ada Binary files /dev/null and b/SnapshotTests/Tests/DashUIKitSnapshotTests/__Snapshots__/ModalDialogSnapshotTests/testOneButton.one-button-light.png differ diff --git a/SnapshotTests/Tests/DashUIKitSnapshotTests/__Snapshots__/ModalDialogSnapshotTests/testParagraphsAndBare.paragraphs-bare-dark.png b/SnapshotTests/Tests/DashUIKitSnapshotTests/__Snapshots__/ModalDialogSnapshotTests/testParagraphsAndBare.paragraphs-bare-dark.png new file mode 100644 index 0000000..9c4c3a3 Binary files /dev/null and b/SnapshotTests/Tests/DashUIKitSnapshotTests/__Snapshots__/ModalDialogSnapshotTests/testParagraphsAndBare.paragraphs-bare-dark.png differ diff --git a/SnapshotTests/Tests/DashUIKitSnapshotTests/__Snapshots__/ModalDialogSnapshotTests/testParagraphsAndBare.paragraphs-bare-light.png b/SnapshotTests/Tests/DashUIKitSnapshotTests/__Snapshots__/ModalDialogSnapshotTests/testParagraphsAndBare.paragraphs-bare-light.png new file mode 100644 index 0000000..6257541 Binary files /dev/null and b/SnapshotTests/Tests/DashUIKitSnapshotTests/__Snapshots__/ModalDialogSnapshotTests/testParagraphsAndBare.paragraphs-bare-light.png differ diff --git a/SnapshotTests/Tests/DashUIKitSnapshotTests/__Snapshots__/ModalDialogSnapshotTests/testTwoButtons.two-buttons-dark.png b/SnapshotTests/Tests/DashUIKitSnapshotTests/__Snapshots__/ModalDialogSnapshotTests/testTwoButtons.two-buttons-dark.png new file mode 100644 index 0000000..69a9ba6 Binary files /dev/null and b/SnapshotTests/Tests/DashUIKitSnapshotTests/__Snapshots__/ModalDialogSnapshotTests/testTwoButtons.two-buttons-dark.png differ diff --git a/SnapshotTests/Tests/DashUIKitSnapshotTests/__Snapshots__/ModalDialogSnapshotTests/testTwoButtons.two-buttons-light.png b/SnapshotTests/Tests/DashUIKitSnapshotTests/__Snapshots__/ModalDialogSnapshotTests/testTwoButtons.two-buttons-light.png new file mode 100644 index 0000000..8a39629 Binary files /dev/null and b/SnapshotTests/Tests/DashUIKitSnapshotTests/__Snapshots__/ModalDialogSnapshotTests/testTwoButtons.two-buttons-light.png differ diff --git a/Sources/DashUIKit/Components/ModalDialog.swift b/Sources/DashUIKit/Components/ModalDialog.swift new file mode 100644 index 0000000..35c5420 --- /dev/null +++ b/Sources/DashUIKit/Components/ModalDialog.swift @@ -0,0 +1,195 @@ +// +// Copyright © 2026 Dash Core Group. All rights reserved. +// +// Licensed under the MIT License (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +import SwiftUI + +/// A dialog card: an optional icon, a heading, any number of paragraphs and +/// one or two actions stacked full width. The host presents it (over a dim, +/// centred); the card only lays itself out. Text longer than the screen +/// scrolls inside the card, and the buttons stay visible. +public struct ModalDialog: View { + + public enum Icon: Sendable { + /// The yellow warning tile. + case warning + } + + /// One button of the dialog. + public struct Action { + public enum Role: Sendable { + case regular + /// Ends something the user cannot simply get back: drawn in red. + case destructive + } + + public let title: String + public let role: Role + public let handler: () -> Void + + public init(title: String, role: Role = .regular, handler: @escaping () -> Void) { + self.title = title + self.role = role + self.handler = handler + } + } + + /// A button as drawn, top to bottom. + struct DrawnButton { + let title: String + let style: DashButtonStyle + let handler: () -> Void + } + + public let icon: Icon? + public let heading: String + public let paragraphs: [String] + public let primaryAction: Action + public let secondaryAction: Action? + + public init( + icon: Icon? = nil, + heading: String, + paragraphs: [String], + primaryAction: Action, + secondaryAction: Action? = nil + ) { + self.icon = icon + self.heading = heading + self.paragraphs = paragraphs + self.primaryAction = primaryAction + self.secondaryAction = secondaryAction + } + + /// The secondary action above the primary one; a destructive one is red, + /// whichever button it is. + var buttons: [DrawnButton] { + [secondaryAction, primaryAction].compactMap { action in + action.map { action in + DrawnButton( + title: action.title, + style: action.role == .destructive ? .tintedRed : .tintedGray, + handler: action.handler) + } + } + } + + /// The paragraphs as one text, a blank line between them; nil without any. + var bodyText: String? { + paragraphs.isEmpty ? nil : paragraphs.joined(separator: "\n\n") + } + + public var body: some View { + VStack(spacing: 30) { + ViewThatFits(in: .vertical) { + topContent + ScrollView { + topContent + } + .scrollBounceBehavior(.basedOnSize) + } + VStack(spacing: 20) { + ForEach(Array(buttons.enumerated()), id: \.offset) { _, button in + DashButton(text: button.title, fillsWidth: true, size: .medium, style: button.style, action: button.handler) + } + } + } + .padding(.top, 30) + .padding([.horizontal, .bottom], 20) + .frame(maxWidth: 330) + .background(Color.dash.secondaryBackground) + .clipShape(RoundedRectangle(cornerRadius: 34, style: .continuous)) + // VoiceOver stays in the card while it is open, whatever the host + // presents it in. + .accessibilityAddTraits(.isModal) + } + + private var topContent: some View { + VStack(alignment: .leading, spacing: 20) { + if let icon { + iconImage(icon) + .accessibilityHidden(true) + } + VStack(alignment: .leading, spacing: 2) { + Text(heading) + .dashFont(.headline) + .accessibilityAddTraits(.isHeader) + if let bodyText { + Text(bodyText) + .dashFont(.subhead) + } + } + .foregroundStyle(Color.dash.primaryText) + .frame(maxWidth: .infinity, alignment: .leading) + .fixedSize(horizontal: false, vertical: true) + } + .padding(.horizontal, 10) + } + + private func iconImage(_ icon: Icon) -> some View { + switch icon { + case .warning: + DashIcon.Illustrations.dialogueWarning.image + .resizable() + .frame(width: 46, height: 46) + } + } +} + +#if DEBUG + +private let questionText = "Disconnected. Keys this wallet did not record giving are still on your identity (2): keys of this app from another device or from before a restore, or DashPay profile keys no connection holds. Turn them off too?" +private let notCheckedText = "Disconnected. This wallet could not tell yet which app holds some keys on your identity (1). Open Connections again later to see them." +private let notAttributableText = "Some keys on your identity (1) may belong to another app of the same developer, so this wallet left them on. Each one shows on its own row in Connections; disconnect it there if you no longer need it." +private let leftText = "Disconnected. Some keys of this app are still on your identity (1). Disconnect it again to turn them off." + +private func dimmed(_ dialog: ModalDialog) -> some View { + ZStack { + Color.dash.backgroundOverlay.ignoresSafeArea() + dialog.padding(.horizontal, 20) + } +} + +#Preview("One button") { + dimmed(ModalDialog(icon: .warning, heading: "Success", paragraphs: [notCheckedText], primaryAction: .init(title: "OK") {})) +} + +#Preview("Two buttons") { + dimmed(ModalDialog( + icon: .warning, heading: "Keys left on your identity", paragraphs: [questionText], + primaryAction: .init(title: "Turn them off", role: .destructive) {}, + secondaryAction: .init(title: "Leave them") {})) +} + +#Preview("Long text") { + dimmed(ModalDialog( + icon: .warning, heading: "Success", + paragraphs: [notCheckedText, notAttributableText, leftText, questionText, notAttributableText], + primaryAction: .init(title: "OK") {})) +} + +#Preview("No icon, no paragraphs") { + dimmed(ModalDialog(heading: "Disconnected", paragraphs: [], primaryAction: .init(title: "OK") {})) +} + +#Preview("Dark") { + dimmed(ModalDialog( + icon: .warning, heading: "Keys left on your identity", paragraphs: [questionText, notAttributableText], + primaryAction: .init(title: "Turn them off", role: .destructive) {}, + secondaryAction: .init(title: "Leave them") {})) + .preferredColorScheme(.dark) +} + +#endif diff --git a/Sources/DashUIKit/Foundation/Icon_DashUI.swift b/Sources/DashUIKit/Foundation/Icon_DashUI.swift index 5a4ea07..4464c5a 100644 --- a/Sources/DashUIKit/Foundation/Icon_DashUI.swift +++ b/Sources/DashUIKit/Foundation/Icon_DashUI.swift @@ -69,6 +69,7 @@ public enum DashIcon { public enum Illustrations: String, CaseIterable, DashIconAsset { case crowdnodeWarning = "crowdnode.warning" case dashDex = "illustration-dash-dex" + case dialogueWarning = "dialogue-warning" } // MARK: - Features diff --git a/Sources/DashUIKit/Resources/Media.xcassets/Icons & Illustrations/Illustrations/dialogue-warning.imageset/Contents.json b/Sources/DashUIKit/Resources/Media.xcassets/Icons & Illustrations/Illustrations/dialogue-warning.imageset/Contents.json new file mode 100644 index 0000000..61a7f3e --- /dev/null +++ b/Sources/DashUIKit/Resources/Media.xcassets/Icons & Illustrations/Illustrations/dialogue-warning.imageset/Contents.json @@ -0,0 +1,16 @@ +{ + "images" : [ + { + "filename" : "dialogue-warning.svg", + "idiom" : "universal" + } + ], + "info" : { + "author" : "xcode", + "version" : 1 + }, + "properties" : { + "preserves-vector-representation" : true, + "template-rendering-intent" : "original" + } +} diff --git a/Sources/DashUIKit/Resources/Media.xcassets/Icons & Illustrations/Illustrations/dialogue-warning.imageset/dialogue-warning.svg b/Sources/DashUIKit/Resources/Media.xcassets/Icons & Illustrations/Illustrations/dialogue-warning.imageset/dialogue-warning.svg new file mode 100644 index 0000000..4da6a85 --- /dev/null +++ b/Sources/DashUIKit/Resources/Media.xcassets/Icons & Illustrations/Illustrations/dialogue-warning.imageset/dialogue-warning.svg @@ -0,0 +1,6 @@ + + + + + + diff --git a/Tests/DashUIKitTests/ModalDialogTests.swift b/Tests/DashUIKitTests/ModalDialogTests.swift new file mode 100644 index 0000000..ac6c2b9 --- /dev/null +++ b/Tests/DashUIKitTests/ModalDialogTests.swift @@ -0,0 +1,73 @@ +// +// Copyright © 2026 Dash Core Group. All rights reserved. +// +// Licensed under the MIT License (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +import XCTest +@testable import DashUIKit + +@MainActor +final class ModalDialogTests: XCTestCase { + + /// The secondary action sits above the primary one, as in the design, and + /// each button runs its own action. + func testTheSecondaryButtonComesFirstAndEachRunsItsOwnAction() { + var ran: [String] = [] + let dialog = ModalDialog( + heading: "Keys left on your identity", + paragraphs: ["One.", "Two."], + primaryAction: .init(title: "Turn them off", role: .destructive) { ran.append("primary") }, + secondaryAction: .init(title: "Leave them") { ran.append("secondary") }) + + XCTAssertEqual(dialog.buttons.map(\.title), ["Leave them", "Turn them off"]) + XCTAssertEqual(dialog.buttons.map(\.style), [.tintedGray, .tintedRed]) + dialog.buttons.forEach { $0.handler() } + XCTAssertEqual(ran, ["secondary", "primary"]) + } + + func testOneButtonIsThePrimaryAction() { + var ran = false + let dialog = ModalDialog(heading: "Success", paragraphs: [], primaryAction: .init(title: "OK") { ran = true }) + + XCTAssertEqual(dialog.buttons.map(\.title), ["OK"]) + XCTAssertEqual(dialog.buttons.map(\.style), [.tintedGray]) + dialog.buttons[0].handler() + XCTAssertTrue(ran) + } + + /// A destructive secondary action is red as well: the colour follows the + /// role, whichever button it is. + func testADestructiveSecondaryActionIsRedToo() { + let dialog = ModalDialog( + heading: "Remove", paragraphs: [], + primaryAction: .init(title: "Cancel") {}, + secondaryAction: .init(title: "Remove", role: .destructive) {}) + + XCTAssertEqual(dialog.buttons.map(\.style), [.tintedRed, .tintedGray]) + } + + /// The paragraphs are drawn as one text, in the order given, a blank line + /// between them. + func testParagraphsAreDrawnInOrderWithABlankLineBetween() { + let dialog = ModalDialog(heading: "Success", paragraphs: ["First.", "Second.", "Third."], primaryAction: .init(title: "OK") {}) + + XCTAssertEqual(dialog.bodyText, "First.\n\nSecond.\n\nThird.") + } + + func testNoParagraphsDrawNoText() { + let dialog = ModalDialog(heading: "Success", paragraphs: [], primaryAction: .init(title: "OK") {}) + + XCTAssertNil(dialog.bodyText) + } +} diff --git a/docs/README.md b/docs/README.md index 0a0d499..8b3b2cc 100644 --- a/docs/README.md +++ b/docs/README.md @@ -23,7 +23,7 @@ and callbacks; they render and report intent. They require `import DashUIKit` an - **[Navigation & containers](navigation-and-containers.md)** — `NavigationBar`, `NavigationBarElement`, `NavigationBarCircleButton`, `NavigationBarCircleMenu`, `NavigationBarCircleIcon`, `TopIntroView`, `BottomSheet`, `FilterBar`, `MenuViewModifier`. -- **[Feedback](feedback.md)** — `Toast`, `SystemMessageView`, +- **[Feedback](feedback.md)** — `Toast`, `SystemMessageView`, `ModalDialog`, `LoadingIllustration` / `LoadingSpinner`, `SuccessIllustration`, `ErrorIllustration`, `XmarkIcon`, `CheckmarkIcon`, `ChevronIcon`, `InfoRoundIcon`. - **[Utilities](utilities.md)** — geometry reader (`readingFrame`), @@ -36,6 +36,7 @@ and callbacks; they render and report intent. They require `import DashUIKit` an | Symbol | Page | |---|---| | `AddressFieldView` | [Buttons & inputs](buttons-and-inputs.md#addressfieldview) | +| `ModalDialog` | [Feedback](feedback.md#modaldialog) | | `SheetFeature` | [Navigation & containers](navigation-and-containers.md#sheetfeature) | | `BottomSheet` | [Navigation & containers](navigation-and-containers.md#bottomsheet) | | `Bundle.dashUIKit` | [Foundation](foundation.md#bundle) | diff --git a/docs/feedback.md b/docs/feedback.md index 5334a35..a5fd2cf 100644 --- a/docs/feedback.md +++ b/docs/feedback.md @@ -66,6 +66,34 @@ one or two of them. --- +## ModalDialog + +File `Components/ModalDialog.swift` + +A dialog card: an optional icon (`.warning`, the yellow tile), a heading, any number of +paragraphs drawn as one text with a blank line between them, and one or two full-width +buttons. The secondary action sits above the primary one; a `.destructive` action is drawn +`tintedRed` whichever button it is, every other one `tintedGray`. The card is at most 330 pt wide and only +lays itself out: the host presents it, over a dim, centred. Text taller than the space +the host gives scrolls inside the card, and the buttons stay visible. + +```swift +ModalDialog( + icon: .warning, + heading: "Keys left on your identity", + paragraphs: [question, warning], + primaryAction: .init(title: "Turn them off", role: .destructive) { /* … */ }, + secondaryAction: .init(title: "Leave them") { /* … */ } +) +``` + +VoiceOver reads the heading as a header, then the paragraphs in order; the icon is +hidden. The card carries the `isModal` trait, so VoiceOver does not reach the screen +behind it while it is shown, even in a host's `ZStack`. The heading uses the `headline` token, which is bold where the Figma `Dialogue` +component draws it semibold. + +--- + ## LoadingIllustration / LoadingSpinner File `Components/Illustrations/LoadingIllustration.swift` diff --git a/docs/snapshot-tests.md b/docs/snapshot-tests.md index 1e1c0da..92c737c 100644 --- a/docs/snapshot-tests.md +++ b/docs/snapshot-tests.md @@ -45,7 +45,7 @@ right to left, a taller container, a narrow width, a selection that matches no o (including a top row taller than the height it assumes before measuring), `Criteria`, `DashAmount`, `DashBalanceView`, `DashSwitch` (the track; the system thumb is a visual effect and is not drawn), `EnterAmountView` (both styles, at rest), `SwapAmountView`, `DashPickerView`, -`RadioButtonRow` (styles, content, highlighted title, disabled), `ReceiveEstimateView`, `SimpleSelect`, `SystemMessageView`, `TopIntroView` (both description +`RadioButtonRow` (styles, content, highlighted title, disabled), `ReceiveEstimateView`, `SimpleSelect`, `SystemMessageView`, `ModalDialog` (one and two buttons, several paragraphs, no icon), `TopIntroView` (both description colours, title only, one description), `List1View`, `List8View`, `MenuViewModifier`, `NavigationBarCircleIcon`, `NavigationBarCircleButton`, `NavigationBarCircleMenu` (its label; the open menu is system UI),