Files
swift-hvac-toolbox/Sources/Styleguide/WarningBox.swift

64 lines
1.3 KiB
Swift

import Elementary
/// A container for displaying warnings.
public struct WarningBox<Header: HTML>: HTML, Sendable {
let warnings: [String]
let header: @Sendable ([String]) -> Header
public init(
warnings: [String],
@HTMLBuilder header: @Sendable @escaping ([String]) -> Header
) {
self.warnings = warnings
self.header = header
}
public init(
_ warnings: String...,
@HTMLBuilder header: @Sendable @escaping ([String]) -> Header
) {
self.warnings = warnings
self.header = header
}
public var content: some HTML<HTMLTag.div> {
div(.id("warnings")) {
header(warnings)
ul(.class("list-disc mx-10")) {
for warning in warnings {
li { warning }
}
}
}
.attributes(
.class("""
mt-6 p-4 rounded-lg shadow-lg
text-amber-500
bg-amber-100 dark:bg-amber-200
border border-amber-500
"""),
when: warnings.count > 0
)
}
}
public extension WarningBox where Header == HTMLElement<HTMLTag.span, HTMLText> {
init(
warnings: [String]
) {
self.init(
warnings: warnings,
header: { warnings in
span(.class("font-semibold mb-4 border-b")) { "Warning\(warnings.count > 1 ? "s:" : ":")" }
}
)
}
init(
_ warnings: String...
) {
self.init(warnings: warnings)
}
}