Repository navigation
feat(modal-dialog): add a dialog card with paragraphs and stacked actions #60
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
SnapshotTests/Tests/DashUIKitSnapshotTests/ModalDialogSnapshotTests.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 |
Binary file added
BIN
+163 KB
...__/ModalDialogSnapshotTests/testLongTextScrollsAboveTheButtons.scrolls-dark.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+158 KB
..._/ModalDialogSnapshotTests/testLongTextScrollsAboveTheButtons.scrolls-light.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+78.6 KB
...tTests/__Snapshots__/ModalDialogSnapshotTests/testOneButton.one-button-dark.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+74.6 KB
...Tests/__Snapshots__/ModalDialogSnapshotTests/testOneButton.one-button-light.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+198 KB
...shots__/ModalDialogSnapshotTests/testParagraphsAndBare.paragraphs-bare-dark.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+190 KB
...hots__/ModalDialogSnapshotTests/testParagraphsAndBare.paragraphs-bare-light.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+115 KB
...ests/__Snapshots__/ModalDialogSnapshotTests/testTwoButtons.two-buttons-dark.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+111 KB
...sts/__Snapshots__/ModalDialogSnapshotTests/testTwoButtons.two-buttons-light.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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) { | ||
|
romchornyi marked this conversation as resolved.
Resolved
|
||
| 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 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
...edia.xcassets/Icons & Illustrations/Illustrations/dialogue-warning.imageset/Contents.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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" | ||
| } | ||
| } |
6 changes: 6 additions & 0 deletions
6
...ns & Illustrations/Illustrations/dialogue-warning.imageset/dialogue-warning.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.