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 Link view, update JavaScriptKit to 0.8.0 #276

Merged
merged 8 commits into from
Nov 5, 2020
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
2 changes: 1 addition & 1 deletion .swift-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
wasm-5.3-SNAPSHOT-2020-09-25-a
wasm-5.3-SNAPSHOT-2020-10-29-c
22 changes: 19 additions & 3 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ let package = Package(
// .package(url: /* package url */, from: "1.0.0"),
.package(
url: "https://github.com/swiftwasm/JavaScriptKit.git",
.upToNextMinor(from: "0.7.2")
.upToNextMinor(from: "0.8.0")
),
.package(url: "https://github.com/MaxDesiatov/Runtime.git", from: "2.1.2"),
.package(url: "https://github.com/MaxDesiatov/OpenCombine.git", from: "0.0.1"),
Expand Down Expand Up @@ -69,15 +69,31 @@ let package = Package(
),
.target(
name: "TokamakDOM",
dependencies: ["CombineShim", "JavaScriptKit", "TokamakCore", "TokamakStaticHTML"]
dependencies: [
"CombineShim",
"TokamakCore",
"TokamakStaticHTML",
.product(
name: "JavaScriptKit",
package: "JavaScriptKit",
condition: .when(platforms: [.wasi])
)
]
),
.target(
name: "TokamakShim",
dependencies: [.target(name: "TokamakDOM", condition: .when(platforms: [.wasi]))]
),
.target(
name: "TokamakDemo",
dependencies: ["JavaScriptKit", "TokamakShim"]
dependencies: [
"TokamakShim",
.product(
name: "JavaScriptKit",
package: "JavaScriptKit",
condition: .when(platforms: [.wasi])
)
]
),
.target(
name: "TokamakStaticDemo",
Expand Down
48 changes: 48 additions & 0 deletions Sources/TokamakCore/Views/Buttons/Link.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright 2018-2020 Tokamak contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Created by Carson Katri on 9/9/20.
//

import struct Foundation.URL

public struct Link<Label>: View where Label: View {
let destination: URL
let label: Label

public init(destination: URL, @ViewBuilder label: () -> Label) {
(self.destination, self.label) = (destination, label())
}

public var body: Never {
neverBody("Link")
}
}

extension Link where Label == Text {
public init<S: StringProtocol>(_ titleKey: S, destination: URL) {
self.init(destination: destination) { Text(titleKey) }
}
}

public struct _LinkProxy<Label> where Label: View {
public let subject: Link<Label>

public init(_ subject: Link<Label>) { self.subject = subject }

public var label: Label { subject.label }
public var destination: URL {
subject.destination
}
}
3 changes: 3 additions & 0 deletions Sources/TokamakStaticHTML/Core.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ public typealias Spacer = TokamakCore.Spacer
public typealias Text = TokamakCore.Text
public typealias VStack = TokamakCore.VStack
public typealias ZStack = TokamakCore.ZStack
public typealias Link = TokamakCore.Link

// MARK: Special Views

Expand All @@ -87,6 +88,8 @@ public typealias SceneStorage = TokamakCore.SceneStorage

// MARK: Misc

public typealias ViewBuilder = TokamakCore.ViewBuilder

// FIXME: I would put this inside TokamakCore, but for
// some reason it doesn't get exported with the typealias
extension Text {
Expand Down
2 changes: 1 addition & 1 deletion Sources/TokamakStaticHTML/Modifiers/LayoutModifiers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ extension _PaddingLayout: DOMViewModifier {
}
}
return ["style": padding
.map { "padding-\($0.0): \($0.1);" }
.map { "padding-\($0.0): \($0.1)px;" }
.joined(separator: " ")]
}
}
4 changes: 4 additions & 0 deletions Sources/TokamakStaticHTML/Resources/TokamakStyles.swift
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ public let tokamakStyles = """
color-scheme: light dark;
}

._tokamak-link {
text-decoration: none;
}

@media (prefers-color-scheme:dark) {
._tokamak-text-redacted::after {
background-color: rgb(100, 100, 100);
Expand Down
27 changes: 27 additions & 0 deletions Sources/TokamakStaticHTML/Views/Buttons/Link.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright 2018-2020 Tokamak contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Created by Carson Katri on 9/9/20.
//

import TokamakCore

extension Link: ViewDeferredToRenderer {
public var deferredBody: AnyView {
let proxy = _LinkProxy(self)
return AnyView(HTML("a", ["href": proxy.destination.absoluteString, "class": "_tokamak-link"]) {
proxy.label
})
}
}