Skip to content

Commit 237c452

Browse files
committed
Updated slide to for picture aspect ratio
1 parent 64fb3ff commit 237c452

1 file changed

Lines changed: 76 additions & 13 deletions

File tree

Sources/Ignite/Elements/Slide.swift

Lines changed: 76 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,30 @@
11
//
2-
// Slide.swift
2+
// Slide.swift - Enhanced Version with Configurable Attributes
33
// Ignite
44
// https://www.github.com/twostraws/Ignite
55
// See LICENSE for license information.
66
//
77

88
/// One slide in a `Carousel`.
99
public struct Slide: HTML {
10+
/// Defines how an image should fit within its container.
11+
public enum ImageFit: String {
12+
/// Scale image to completely fill container, may crop edges.
13+
case cover
14+
15+
/// Scale image to fit entirely within container, preserving aspect ratio (default).
16+
case contain
17+
18+
/// Stretch image to fill container, may distort aspect ratio.
19+
case fill
20+
21+
/// Display image at original size or scaled down if larger than container.
22+
case scaleDown = "scale-down"
23+
24+
/// Display image at original size.
25+
case none
26+
}
27+
1028
/// The content and behavior of this HTML.
1129
public var body: some HTML { self }
1230

@@ -26,27 +44,53 @@ public struct Slide: HTML {
2644
/// How opaque the background image should be. Use values lower than 1.0
2745
/// to progressively dim the background image.
2846
var backgroundOpacity = 1.0
47+
48+
/// How the background image should fit within the slide container.
49+
/// Default is `.contain` which preserves aspect ratio and shows the full image.
50+
var imageFit: ImageFit = .contain
51+
52+
/// The background color for the slide container. Used for letterboxing
53+
/// when image doesn't fill the entire space.
54+
/// Default is "transparent" to allow page background to show through.
55+
var containerBackgroundColor: String = "transparent"
2956

3057
/// Creates a new `Slide` object using a background image.
31-
/// - Parameter background: An optional background image to use for
32-
/// this slide. This should be specified relative to the root of your
33-
/// site, e.g. /images/dog.jpg.
34-
public init(background: String) {
58+
/// - Parameters:
59+
/// - background: An optional background image to use for this slide.
60+
/// This should be specified relative to the root of your site, e.g. /images/dog.jpg.
61+
/// - imageFit: How the image should fit within the container. Default is `.contain`.
62+
/// - backgroundColor: Background color for letterboxing areas. Default is "transparent".
63+
public init(
64+
background: String,
65+
imageFit: ImageFit = .contain,
66+
backgroundColor: String = "transparent"
67+
) {
3568
self.background = background
3669
self.items = HTMLCollection([])
70+
self.imageFit = imageFit
71+
self.containerBackgroundColor = backgroundColor
3772
}
3873

3974
/// Creates a new `Slide` object using a background image and a page
4075
/// element builder that returns an array of `HTML` objects to use
4176
/// inside the slide.
42-
/// - Parameter background: An optional background image to use for
43-
/// this slide. This should be specified relative to the root of your
44-
/// site, e.g. /images/dog.jpg.
45-
/// - Parameter items: Other items to place inside this slide, which will
46-
/// be placed on top of the background image.
47-
public init(background: String? = nil, @HTMLBuilder items: () -> some HTML) {
77+
/// - Parameters:
78+
/// - background: An optional background image to use for this slide.
79+
/// This should be specified relative to the root of your site, e.g. /images/dog.jpg.
80+
/// - imageFit: How the image should fit within the container. Default is `.contain`.
81+
/// - backgroundColor: Background color for letterboxing areas. Default is "transparent".
82+
/// - items: Other items to place inside this slide, which will
83+
/// be placed on top of the background image.
84+
public init(
85+
background: String? = nil,
86+
imageFit: ImageFit = .contain,
87+
backgroundColor: String = "transparent",
88+
@HTMLBuilder items: () -> some HTML
89+
) {
4890
self.background = background
4991
self.items = HTMLCollection(items)
92+
self.imageFit = imageFit
93+
self.containerBackgroundColor = backgroundColor
5094
}
5195

5296
/// Adjusts the opacity of the background image for this slide. Use values
@@ -58,6 +102,25 @@ public struct Slide: HTML {
58102
copy.backgroundOpacity = opacity
59103
return copy
60104
}
105+
106+
/// Sets how the background image should fit within the slide container.
107+
/// - Parameter fit: The image fit mode.
108+
/// - Returns: A new `Slide` instance with the updated image fit.
109+
public func imageFit(_ fit: ImageFit) -> Slide {
110+
var copy = self
111+
copy.imageFit = fit
112+
return copy
113+
}
114+
115+
/// Sets the background color for the slide container, used for letterboxing
116+
/// when the image doesn't fill the entire space.
117+
/// - Parameter color: The CSS color value (e.g., "black", "#000", "rgba(0,0,0,0.5)").
118+
/// - Returns: A new `Slide` instance with the updated background color.
119+
public func backgroundColor(_ color: String) -> Slide {
120+
var copy = self
121+
copy.containerBackgroundColor = color
122+
return copy
123+
}
61124

62125
/// Used during rendering to assign this carousel slide to a particular parent,
63126
/// so our open paging behavior works correctly.
@@ -68,7 +131,7 @@ public struct Slide: HTML {
68131
.class("d-block", "w-100")
69132
.style(
70133
.init(.height, value: "100%"),
71-
.init(.objectFit, value: "cover"),
134+
.init(.objectFit, value: imageFit.rawValue),
72135
.init(.opacity, value: backgroundOpacity.formatted(.nonLocalizedDecimal))
73136
)
74137
}
@@ -82,7 +145,7 @@ public struct Slide: HTML {
82145
.attributes(attributes)
83146
.class("carousel-item")
84147
.class(index == 0 ? "active" : nil)
85-
.style(.backgroundColor, "black")
148+
.style(.backgroundColor, containerBackgroundColor)
86149
}
87150

88151
/// Renders this element using publishing context passed in.

0 commit comments

Comments
 (0)