Files
swift-estimated-pressures-core/Sources/FlaggedViews/FlaggedMessageLabel.swift

51 lines
1.3 KiB
Swift

import SharedModels
import Styleguide
import SwiftUI
/// Represents the message of a flagged value result, which is stylable using the
/// `.flaggedMessageLabelStyle` modifier on a view.
///
/// By default the status label is colored using the default status color.
public struct FlaggedMessageLabel: View {
@Environment(\.flaggedMessageLabelStyle) private var style
let message: String?
public init(message: String?) {
self.message = message
}
public var body: some View {
if let message {
TextLabel(message)
.textLabelStyle(style)
}
}
}
private struct FlaggedMessageLabelStyleKey: EnvironmentKey {
static var defaultValue = AnyTextLabelStyle(style: .font(.caption))
}
extension EnvironmentValues {
var flaggedMessageLabelStyle: AnyTextLabelStyle {
get { self[FlaggedMessageLabelStyleKey.self] }
set { self[FlaggedMessageLabelStyleKey.self] = newValue }
}
}
extension View {
public func flaggedMessageLabelStyle(_ style: AnyTextLabelStyle) -> some View {
environment(\.flaggedMessageLabelStyle, style)
}
public func flaggedMessageLabelStyle<S: TextLabelStyle>(_ style: S) -> some View {
flaggedMessageLabelStyle(AnyTextLabelStyle(style: style))
}
}
#Preview {
FlaggedStatusLabel(status: .warning)
.flaggedStatusLabelStyle(.textLabel(.heavyTitle2))
}