-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
110 additions
and
5 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
7 changes: 7 additions & 0 deletions
7
Sources/Slipstream/Documentation.docc/W3C/EmbeddedContent/Source.md
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,7 @@ | ||
# ``Source`` | ||
|
||
## Topics | ||
|
||
### Supporting types | ||
|
||
- ``ColorScheme`` |
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
24 changes: 24 additions & 0 deletions
24
Sources/Slipstream/W3C/Elements/EmbeddedContent/Picture.swift
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,24 @@ | ||
import Foundation | ||
|
||
import SwiftSoup | ||
|
||
/// A container that provides multiple sources to its contained ``Image`` view. | ||
/// | ||
/// Picture allow authors to declaratively control or give hints to the user agent about which | ||
/// image resource to use, based on the screen pixel density, viewport size, image format, | ||
/// and other factors | ||
/// | ||
/// - SeeAlso: W3C [picture](https://html.spec.whatwg.org/multipage/embedded-content.html#the-picture-element) specification. | ||
@available(iOS 17.0, macOS 14.0, *) | ||
public struct Picture<Content>: W3CElement where Content: View { | ||
@_documentation(visibility: private) | ||
public let tagName: String = "picture" | ||
|
||
@_documentation(visibility: private) | ||
@ViewBuilder public let content: () -> Content | ||
|
||
/// Creates a Picture view. | ||
public init(@ViewBuilder content: @escaping () -> Content) { | ||
self.content = content | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
Sources/Slipstream/W3C/Elements/EmbeddedContent/Source.swift
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,47 @@ | ||
import Foundation | ||
|
||
import SwiftSoup | ||
|
||
/// A media query representation of the user's selected color scheme. | ||
@available(iOS 17.0, macOS 14.0, *) | ||
public enum ColorScheme { | ||
case light | ||
case dark | ||
} | ||
|
||
/// A view that allows authors to specify multiple alternative source sets for ``Image`` | ||
/// views or multiple alternative media resources for media elements. | ||
/// | ||
/// ``Source`` does not represent anything on its own. | ||
/// | ||
/// - SeeAlso: W3C [source](https://html.spec.whatwg.org/multipage/embedded-content.html#the-source-element) specification. | ||
@available(iOS 17.0, macOS 14.0, *) | ||
public struct Source: View { | ||
/// Creates a Source view. | ||
/// | ||
/// - Parameters: | ||
/// - url: The image will be loaded from this URL when the browser is using the given color scheme. | ||
/// - colorScheme: The color scheme for which this image should be loaded. | ||
public init(_ url: URL?, colorScheme: ColorScheme) { | ||
self.url = url | ||
self.colorScheme = colorScheme | ||
} | ||
|
||
@_documentation(visibility: private) | ||
public func render(_ container: Element, environment: EnvironmentValues) throws { | ||
guard let url else { | ||
return | ||
} | ||
let element = try container.appendElement("source") | ||
try element.attr("srcset", url.absoluteString) | ||
switch colorScheme { | ||
case .light: | ||
try element.attr("media", "(prefers-color-scheme: light)") | ||
case .dark: | ||
try element.attr("media", "(prefers-color-scheme: dark)") | ||
} | ||
} | ||
|
||
private let url: URL? | ||
private let colorScheme: ColorScheme | ||
} |
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,10 @@ | ||
import Foundation | ||
import Testing | ||
|
||
import Slipstream | ||
|
||
struct PictureTests { | ||
@Test func emptyContent() throws { | ||
try #expect(renderHTML(Picture { }) == "<picture></picture>") | ||
} | ||
} |
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,15 @@ | ||
import Foundation | ||
import Testing | ||
|
||
import Slipstream | ||
|
||
struct SourceTests { | ||
@Test func nilURL() throws { | ||
try #expect(renderHTML(Source(nil, colorScheme: .light)) == "") | ||
} | ||
|
||
@Test func colorSchemes() throws { | ||
try #expect(renderHTML(Source(URL(string: "/logo.png"), colorScheme: .light)) == #"<source srcset="/logo.png" media="(prefers-color-scheme: light)" />"#) | ||
try #expect(renderHTML(Source(URL(string: "/logo.png"), colorScheme: .dark)) == #"<source srcset="/logo.png" media="(prefers-color-scheme: dark)" />"#) | ||
} | ||
} |