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

Fix isPressed for ButtonConfiguration #537

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions Sources/TokamakCore/Views/Controls/Button.swift
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public struct _Button<Label>: View where Label: View {
public let action: () -> ()

@State
public var isPressed = false
public var isPressed: (down: Bool, inside: Bool) = (false, false)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure about the public api here. It's a breaking change in theory, but AFAIK _Button should only be used internally.


let anyStyle: AnyButtonStyle
public var style: Any.Type { anyStyle.type }
Expand All @@ -112,7 +112,7 @@ public struct _Button<Label>: View where Label: View {
configuration: .init(
role: role,
label: .init(body: AnyView(label)),
isPressed: isPressed
isPressed: isPressed.down && isPressed.inside
)
)
}
Expand Down
12 changes: 10 additions & 2 deletions Sources/TokamakDOM/Views/Buttons/Button.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,22 @@

import TokamakCore
import TokamakStaticHTML
import JavaScriptKit

extension _Button: DOMPrimitive {
@_spi(TokamakCore)
public var renderedBody: AnyView {
let listeners: [String: Listener] = [
"pointerdown": { _ in isPressed = true },
// Only fires on down *inside*. Set both to true.
"pointerdown": { _ in isPressed = (down: true, inside: true) },
"pointerenter": {
// See https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/buttons#value
let buttons: UInt32? = $0.buttons.fromJSValue()
isPressed = (down: buttons != 0, inside: true)
},
"pointerleave": { _ in isPressed.inside = false },
"pointerup": { _ in
isPressed = false
isPressed.down = false
action()
},
]
Expand Down