Skip to content
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
5 changes: 5 additions & 0 deletions .changeset/tooltip-click-reaches-ancestor.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"effect": patch
---

Leave a click on a `Tooltip` trigger to the page when `closeOnClick` is false. The trigger always took the click, even when it then did nothing, and since the host sends an event only to the innermost element with a handler for it, a click on a trigger inside a clickable row never reached the row. Without `closeOnClick` the trigger now registers no click handler.
16 changes: 12 additions & 4 deletions packages/effect/src/ui/kit/Tooltip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,9 @@ export type TooltipAlign = typeof TooltipAlign.Type
* positioner and the triangle between them before the tooltip closes, 0
* by default.
* - `closeOnClick`: whether activating the trigger, by a click or the keys
* the element activates on, closes the tooltip, true by default.
* the element activates on, closes the tooltip, true by default. When
* false, the trigger has no click handler, so a click on it reaches a
* clickable ancestor, such as a table row.
* - `disableHoverablePopup`: the pointer on the popup doesn't keep the
* tooltip open, and the positioner takes no pointer events.
* - `describesTrigger`: makes the popup the trigger's accessible description
Expand Down Expand Up @@ -843,8 +845,8 @@ export const Tooltip = Html.component({
placed(builder).attr("data-instant", instantBy ?? undefined)
const trigger = p.part(
["button", "a"],
(b) =>
onHover(
(b) => {
const built = onHover(
b.attrs({
"data-popup-open": flag(shown),
"data-trigger-disabled": flag(p.props.disabled),
Expand All @@ -854,13 +856,19 @@ export const Tooltip = Html.component({
TooltipMessage.LeftTrigger()
)
.on("mousedown", TooltipMessage.PressedTrigger())
.on("click", TooltipMessage.ClickedTrigger())
.on(
"focus",
({ focusVisible }: Html.FocusPayload) => TooltipMessage.FocusedTrigger({ visible: focusVisible })
)
.on("blur", TooltipMessage.BlurredTrigger())
.attr("data-tooltip-trigger", triggerId)
// The host sends an event to the innermost element with a handler for
// it, so without closeOnClick the trigger takes no click: a click on it
// reaches the trigger's own handler or a clickable ancestor.
return p.props.closeOnClick === false
? built
: built.on("click", TooltipMessage.ClickedTrigger())
}
)
const style = Positioning.positionerStyle(tracked.positionMethod) +
(p.props.disableHoverablePopup === true ? ";pointer-events:none" : "")
Expand Down
4 changes: 3 additions & 1 deletion packages/effect/test/ui/kit/Tooltip.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -419,8 +419,10 @@ describe("Tooltip", () => {
.emitted({ _tag: "RequestedClose" })
scene.fire(trigger, "mousedown")
assert.deepStrictEqual(scene.current.emitted, [])
// Without closeOnClick the trigger takes no click at all, so the click
// is left for its own handler or a clickable ancestor.
scene.props({ open: true, closeOnClick: false })
.fire(trigger, "click")
assert.throws(() => scene.fire(trigger, "click"), /nothing handles "click"/)
assert.deepStrictEqual(scene.current.emitted, [])
}))

Expand Down
60 changes: 59 additions & 1 deletion packages/effect/test/ui/kit/TooltipDocument.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Effect, Schema } from "effect"
import { UiSchema } from "effect/ui"
import { BrowserHost, Html } from "effect/ui/html"
import { Tooltip } from "effect/ui/kit"
import { byId, container, mouse, popovers, press, tick } from "./utils/document.ts"
import { byId, click, container, mouse, popovers, press, tick } from "./utils/document.ts"

const rects = new Map<
string,
Expand Down Expand Up @@ -236,6 +236,44 @@ const mountPage = Effect.fnUntraced(

const style = (name: string) => byId("positioner").style.getPropertyValue(name)

const RowMessage = UiSchema.TaggedEnum({
ClickedRow: {},
RequestedOpen: {},
RequestedClose: {}
})

// A clickable row around a tooltip trigger with no click handler of its own.
const Row = Html.component({
name: "Row",
props: Schema.Struct({ closeOnClick: Schema.Boolean }),
model: Schema.Struct({ open: Schema.Boolean, rowClicks: Schema.Number }),
message: RowMessage
}).make({
init: () => ({ open: false, rowClicks: 0 }),
update: {
ClickedRow: ($) => ({ ...$.model, rowClicks: $.model.rowClicks + 1 }),
RequestedOpen: ($) => ({ ...$.model, open: true }),
RequestedClose: ($) => ({ ...$.model, open: false })
},
view: (h, model) =>
h.div.attr("id", "row").on("click", RowMessage.ClickedRow()).children(
Tooltip(h, {
props: { open: model.open, closeOnClick: h.props.closeOnClick },
onEmit: (event) =>
event._tag === "RequestedOpen"
? RowMessage.RequestedOpen()
: RowMessage.RequestedClose(),
view: (tooltip) =>
h.div.children(
tooltip.trigger(h.button.attr("id", "trigger")).children("file.md"),
tooltip.positioner(h.div.attr("id", "positioner")).children(
tooltip.popup(h.div.attr("id", "popup")).children("file.md")
)
)
})
)
})

describe("Tooltip in a document", { concurrent: false }, () => {
it.effect("a diagonal move from a narrow trigger to a wider popup keeps it open, while leaving away closes it", () =>
Effect.gen(function*() {
Expand Down Expand Up @@ -515,4 +553,24 @@ describe("Tooltip in a document", { concurrent: false }, () => {
assert.strictEqual(style("padding-bottom"), "8px")
assert.strictEqual(style("top"), "592px")
}).pipe(Effect.scoped))

it.effect("without closeOnClick, a click on the trigger reaches a clickable ancestor", () =>
Effect.gen(function*() {
yield* popovers
yield* geometry
const clicksWith = Effect.fnUntraced(function*(closeOnClick: boolean) {
const app = yield* container
const root = yield* BrowserHost.mountEffect(Row, { closeOnClick }, app)
yield* tick
click(byId("trigger"))
yield* tick
const clicks = root.model()!.rowClicks
app.remove()
return clicks
})
// With closeOnClick (the default) the trigger handles the click to close
// the tooltip, and the host does not send it on to the row.
assert.strictEqual(yield* clicksWith(true), 0)
assert.strictEqual(yield* clicksWith(false), 1)
}).pipe(Effect.scoped))
})
Loading