Skip to content

Commit

Permalink
Fix Button on Safari
Browse files Browse the repository at this point in the history
  • Loading branch information
carson-katri committed Aug 10, 2020
1 parent 365e0c7 commit 94510a0
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 39 deletions.
43 changes: 26 additions & 17 deletions Sources/TokamakDOM/Views/Buttons/Button.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,34 @@ import TokamakCore

extension _Button: ViewDeferredToRenderer where Label == Text {
public var deferredBody: AnyView {
let attributes: [String: String]
let listeners: [String: Listener] = [
"pointerdown": { _ in isPressed = true },
"pointerup": { _ in
isPressed = false
action()
},
]
if buttonStyle.type == DefaultButtonStyle.self {
attributes = ["class": "_tokamak-buttonstyle-default"]
return AnyView(DynamicHTML(
"button",
["class": "_tokamak-buttonstyle-default"],
listeners: listeners,
content: label.innerHTML ?? ""
))
} else {
attributes = ["class": "_tokamak-buttonstyle-reset"]
return AnyView(DynamicHTML(
"button",
["class": "_tokamak-buttonstyle-reset"],
listeners: listeners
) {
buttonStyle.makeBody(
configuration: _ButtonStyleConfigurationProxy(
label: AnyView(label),
isPressed: isPressed
).subject
)
.colorScheme(.light)
})
}

return AnyView(DynamicHTML("button", attributes, listeners: [
"click": { _ in action() },
"pointerdown": { _ in isPressed = true },
"pointerup": { _ in isPressed = false },
]) {
buttonStyle.makeBody(
configuration: _ButtonStyleConfigurationProxy(
label: AnyView(label),
isPressed: isPressed
).subject
)
.colorScheme(.light)
})
}
}
38 changes: 27 additions & 11 deletions Sources/TokamakDOM/Views/DynamicHTML.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,35 @@ protocol AnyDynamicHTML: AnyHTML {
var listeners: [String: Listener] { get }
}

public struct DynamicHTML<Content>: View, AnyDynamicHTML where Content: View {
public struct DynamicHTML<Content>: View, AnyDynamicHTML {
public let tag: String
public let attributes: [String: String]
public let listeners: [String: Listener]
let content: Content

public var innerHTML: String?

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

extension DynamicHTML where Content: StringProtocol {
public init(
_ tag: String,
_ attributes: [String: String] = [:],
listeners: [String: Listener] = [:],
content: Content
) {
self.tag = tag
self.attributes = attributes
self.listeners = listeners
self.content = content
innerHTML = String(content)
}
}

extension DynamicHTML: ParentView where Content: View {
public init(
_ tag: String,
_ attributes: [String: String] = [:],
Expand All @@ -41,12 +64,11 @@ public struct DynamicHTML<Content>: View, AnyDynamicHTML where Content: View {
self.attributes = attributes
self.listeners = listeners
self.content = content()
innerHTML = nil
}

public var innerHTML: String? { nil }

public var body: Never {
neverBody("HTML")
public var children: [AnyView] {
[AnyView(content)]
}
}

Expand All @@ -59,9 +81,3 @@ extension DynamicHTML where Content == EmptyView {
self = DynamicHTML(tag, attributes, listeners: listeners) { EmptyView() }
}
}

extension DynamicHTML: ParentView {
public var children: [AnyView] {
[AnyView(content)]
}
}
36 changes: 25 additions & 11 deletions Sources/TokamakStaticHTML/Views/HTML.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,32 @@ extension AnyHTML {
}
}

public struct HTML<Content>: View, AnyHTML where Content: View {
public struct HTML<Content>: View, AnyHTML {
public let tag: String
public let attributes: [String: String]
let content: Content

public let innerHTML: String?

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

extension HTML where Content: StringProtocol {
public init(
_ tag: String,
_ attributes: [String: String] = [:],
content: Content
) {
self.tag = tag
self.attributes = attributes
self.content = content
innerHTML = String(content)
}
}

extension HTML: ParentView where Content: View {
public init(
_ tag: String,
_ attributes: [String: String] = [:],
Expand All @@ -47,12 +68,11 @@ public struct HTML<Content>: View, AnyHTML where Content: View {
self.tag = tag
self.attributes = attributes
self.content = content()
innerHTML = nil
}

public var innerHTML: String? { nil }

public var body: Never {
neverBody("HTML")
public var children: [AnyView] {
[AnyView(content)]
}
}

Expand All @@ -65,12 +85,6 @@ extension HTML where Content == EmptyView {
}
}

extension HTML: ParentView {
public var children: [AnyView] {
[AnyView(content)]
}
}

public protocol StylesConvertible {
var styles: [String: String] { get }
}
Expand Down

0 comments on commit 94510a0

Please sign in to comment.