-
-
Notifications
You must be signed in to change notification settings - Fork 112
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support meta tags in StaticHTMLRenderer (#483)
This PR adds the ability to control `<head>` tags using a new `HTMLTitle` view and `HTMLMeta` view. Taking inspiration from Next.js `<NextHead>`, you can use these views anywhere in your view hierarchy and they will be hoisted to the top `<head>` section of the html. Use as a view: ```swift var body: some View { VStack { ... HTMLTitle("Hello, Tokamak") HTMLMeta(charset: "utf-8") ... } } ``` Use as a view modifier: ```swift var body: some View { VStack { ... } .htmlTitle("Hello, Tokamak") .htmlMeta(charset: "utf-8") } ``` And the resulting html (no matter where these are used in your view hierarchy): ```html <html> <head> <title>Hello, Tokamak</title> <meta charset="utf-8"> </head> <body> ... </body> </html> ```
- Loading branch information
1 parent
8177fc8
commit 2ba5488
Showing
19 changed files
with
2,425 additions
and
28 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
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
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,37 @@ | ||
// Copyright 2022 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 Andrew Barba on 5/20/22. | ||
// | ||
|
||
import TokamakCore | ||
|
||
public struct HTMLTitlePreferenceKey: PreferenceKey { | ||
public static var defaultValue: String = "" | ||
|
||
public static func reduce(value: inout String, nextValue: () -> String) { | ||
value = nextValue() | ||
} | ||
} | ||
|
||
public struct HTMLMetaPreferenceKey: PreferenceKey { | ||
public static var defaultValue: [HTMLMeta.MetaTag] = [] | ||
|
||
public static func reduce( | ||
value: inout [HTMLMeta.MetaTag], | ||
nextValue: () -> [HTMLMeta.MetaTag] | ||
) { | ||
value += nextValue() | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// Copyright 2022 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 Andrew Barba on 5/20/22. | ||
// | ||
|
||
import TokamakCore | ||
|
||
public struct HTMLMeta: View { | ||
public enum MetaTag: Equatable, Hashable { | ||
case charset(_ charset: String) | ||
case name(_ name: String, content: String) | ||
case property(_ property: String, content: String) | ||
case httpEquiv(_ httpEquiv: String, content: String) | ||
} | ||
|
||
var meta: MetaTag | ||
|
||
public init(_ value: MetaTag) { | ||
meta = value | ||
} | ||
|
||
public init(charset: String) { | ||
meta = .charset(charset) | ||
} | ||
|
||
public init(name: String, content: String) { | ||
meta = .name(name, content: content) | ||
} | ||
|
||
public init(property: String, content: String) { | ||
meta = .property(property, content: content) | ||
} | ||
|
||
public init(httpEquiv: String, content: String) { | ||
meta = .httpEquiv(httpEquiv, content: content) | ||
} | ||
|
||
public var body: some View { | ||
EmptyView() | ||
.preference(key: HTMLMetaPreferenceKey.self, value: [meta]) | ||
} | ||
} | ||
|
||
public extension View { | ||
func htmlMeta(_ value: HTMLMeta.MetaTag) -> some View { | ||
htmlMeta(.init(value)) | ||
} | ||
|
||
func htmlMeta(charset: String) -> some View { | ||
htmlMeta(.init(charset: charset)) | ||
} | ||
|
||
func htmlMeta(name: String, content: String) -> some View { | ||
htmlMeta(.init(name: name, content: content)) | ||
} | ||
|
||
func htmlMeta(property: String, content: String) -> some View { | ||
htmlMeta(.init(property: property, content: content)) | ||
} | ||
|
||
func htmlMeta(httpEquiv: String, content: String) -> some View { | ||
htmlMeta(.init(httpEquiv: httpEquiv, content: content)) | ||
} | ||
|
||
func htmlMeta(_ meta: HTMLMeta) -> some View { | ||
Group { | ||
self | ||
meta | ||
} | ||
} | ||
} |
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,44 @@ | ||
// Copyright 2022 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 Andrew Barba on 5/20/22. | ||
// | ||
|
||
import TokamakCore | ||
|
||
public struct HTMLTitle: View { | ||
var title: String | ||
|
||
public init(_ title: String) { | ||
self.title = title | ||
} | ||
|
||
public var body: some View { | ||
EmptyView() | ||
.preference(key: HTMLTitlePreferenceKey.self, value: title) | ||
} | ||
} | ||
|
||
public extension View { | ||
func htmlTitle(_ title: String) -> some View { | ||
htmlTitle(.init(title)) | ||
} | ||
|
||
func htmlTitle(_ title: HTMLTitle) -> some View { | ||
Group { | ||
self | ||
title | ||
} | ||
} | ||
} |
Oops, something went wrong.