Skip to content
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

Add ability to specify custom color names for background. #192

Merged
merged 1 commit into from
Sep 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions Sources/Slipstream/TailwindCSS/Backgrounds/View+background.swift
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,27 @@ extension View {
modifier(TailwindClassModifier(add: "bg-\(color.toTailwindColorClass())", condition: condition))
}

/// Sets the background color to a specific name.
///
/// To use custom colors, add them to your tailwind.config.js `theme.extend.colors` property:
///
/// ```js
/// theme: {
/// extend: {
/// colors: {
/// 'sidecar-gray': '#262625',
/// },
/// }
/// }
/// ```
///
/// - SeeAlso: Tailwind CSS' [background color](https://tailwindcss.com/docs/background-color) documentation.
/// - SeeAlso: Tailwind CSS' [customizing colors](https://tailwindcss.com/docs/customizing-colors) documentation.
@available(iOS 17.0, macOS 14.0, *)
public func background(_ color: String, condition: Condition? = nil) -> some View {
modifier(TailwindClassModifier(add: "bg-" + color, condition: condition))
}

/// Sets the background material of the view.
///
/// - SeeAlso: Tailwind CSS' [backdrop blur](https://tailwindcss.com/docs/backdrop-blur) documentation.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ struct BackgroundImageTests {
try #expect(renderHTML(Div {}.background(.init(.cyan, darkness: 500).opacity(20))) == #"<div class="bg-cyan-500"></div>"#)
}

@Test func customColor() throws {
try #expect(renderHTML(Div {}.background("my-dark")) == #"<div class="bg-my-dark"></div>"#)
}

// MARK: - Image backgrounds

@Test func justURL() throws {
Expand Down