generated from StanfordBDHG/SwiftPackageTemplate
-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Dismiss Button and Reverse Label Style (#37)
# Add Dismiss Button and Reverse Label Style ## ♻️ Current situation & Problem This PR adds a `DismissButton` and a `reverse` label style as generalized components to SpeziViews. ## ⚙️ Release Notes * Add new `DismissButton` that displays as a small, circular "X" button. * Add new `reverse` label style that reverses the label orientation compared to the `titleAndIcon` style. ## 📚 Documentation Documentation catalog was updated and adjusted. ## ✅ Testing These are primarily visual components. Testing was done through snapshot testing. ## 📝 Code of Conduct & Contributing Guidelines By submitting creating this pull request, you agree to follow our [Code of Conduct](https://github.com/StanfordSpezi/.github/blob/main/CODE_OF_CONDUCT.md) and [Contributing Guidelines](https://github.com/StanfordSpezi/.github/blob/main/CONTRIBUTING.md): - [x] I agree to follow the [Code of Conduct](https://github.com/StanfordSpezi/.github/blob/main/CODE_OF_CONDUCT.md) and [Contributing Guidelines](https://github.com/StanfordSpezi/.github/blob/main/CONTRIBUTING.md).
- Loading branch information
Showing
10 changed files
with
158 additions
and
9 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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,48 @@ | ||
// | ||
// This source file is part of the Stanford Spezi open-source project | ||
// | ||
// SPDX-FileCopyrightText: 2024 Stanford University and the project authors (see CONTRIBUTORS.md) | ||
// | ||
// SPDX-License-Identifier: MIT | ||
// | ||
|
||
import SwiftUI | ||
|
||
|
||
/// A label style that shows the title and icon in reverse layout compared to the standard `titleAndIcon` label style. | ||
public struct ReverseLabelStyle: LabelStyle { | ||
public func makeBody(configuration: Configuration) -> some View { | ||
HStack { | ||
configuration.title | ||
configuration.icon | ||
} | ||
.accessibilityElement(children: .combine) | ||
} | ||
} | ||
|
||
|
||
extension LabelStyle where Self == ReverseLabelStyle { | ||
/// A label style that shows the title and icon in reverse layout compared to the standard `titleAndIcon` label style. | ||
public static var reverse: ReverseLabelStyle { | ||
ReverseLabelStyle() | ||
} | ||
} | ||
|
||
|
||
#if DEBUG | ||
#Preview { | ||
VStack { | ||
SwiftUI.Label { | ||
Text(verbatim: "75 %") | ||
} icon: { | ||
Image(systemName: "battery.100") | ||
} | ||
SwiftUI.Label { | ||
Text(verbatim: "75 %") | ||
} icon: { | ||
Image(systemName: "battery.100") | ||
} | ||
.labelStyle(.reverse) | ||
} | ||
} | ||
#endif |
This file contains 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,75 @@ | ||
// | ||
// This source file is part of the Stanford Spezi open-source project | ||
// | ||
// SPDX-FileCopyrightText: 2024 Stanford University and the project authors (see CONTRIBUTORS.md) | ||
// | ||
// SPDX-License-Identifier: MIT | ||
// | ||
|
||
import SwiftUI | ||
|
||
|
||
/// Circular Dismiss button. | ||
public struct DismissButton: View { | ||
@Environment(\.dismiss) private var dismiss | ||
|
||
public var body: some View { | ||
#if os(visionOS) || os(tvOS) || os(macOS) | ||
Button("Dismiss", systemImage: "xmark") { | ||
dismiss() | ||
} | ||
#else | ||
Button { | ||
dismiss() | ||
} label: { | ||
Image(systemName: "xmark") | ||
.font(.system(size: 10, weight: .bold, design: .rounded)) | ||
#if !os(watchOS) | ||
.foregroundStyle(.secondary) | ||
#endif | ||
.background { | ||
Circle() | ||
#if os(iOS) | ||
.fill(Color(uiColor: .secondarySystemBackground)) | ||
#elseif os(watchOS) | ||
.fill(Color(uiColor: .darkGray)) | ||
#endif | ||
.frame(width: 25, height: 25) | ||
} | ||
.frame(width: 27, height: 27) // make the tap-able button region slightly larger | ||
} | ||
.accessibilityLabel("Dismiss") | ||
.buttonStyle(.plain) | ||
#endif | ||
} | ||
|
||
public init() {} | ||
} | ||
|
||
|
||
#if DEBUG | ||
#Preview { | ||
#if os(macOS) // cannot preview sheets in macOS | ||
NavigationStack { | ||
Text(verbatim: "Hello World") | ||
.toolbar { | ||
DismissButton() | ||
} | ||
} | ||
.frame(width: 500, height: 350) | ||
#else | ||
Text(verbatim: "") | ||
.sheet(isPresented: .constant(true)) { | ||
NavigationStack { | ||
Text(verbatim: "Hello World") | ||
.toolbar { | ||
DismissButton() | ||
} | ||
} | ||
.frame(width: 200, height: 200) | ||
.presentationDetents([.medium]) | ||
.presentationCornerRadius(25) | ||
} | ||
#endif | ||
} | ||
#endif |
This file contains 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
Binary file added
BIN
+162 KB
.../SpeziViewsTests/__Snapshots__/SnapshotTests/testDismissButton.ipad-regular.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
+59.5 KB
...peziViewsTests/__Snapshots__/SnapshotTests/testDismissButton.iphone-regular.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
+164 KB
...ziViewsTests/__Snapshots__/SnapshotTests/testReverseLabelStyle.ipad-regular.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
+61.4 KB
...ViewsTests/__Snapshots__/SnapshotTests/testReverseLabelStyle.iphone-regular.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 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