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-focus-holds-open.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"effect": patch
---

Add `focusHoldsOpen` to `Tooltip`. It is true by default, as before: keyboard focus on the trigger keeps the tooltip open after the pointer leaves. Set to false, the tooltip closes when the pointer leaves whatever opened it, as Radix's tooltip does, which helps when porting a Radix UI.
12 changes: 9 additions & 3 deletions packages/effect/src/ui/kit/Tooltip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,9 @@ export type TooltipAlign = typeof TooltipAlign.Type
* - `closeDelay`: milliseconds after the pointer leaves the trigger, the
* positioner and the triangle between them before the tooltip closes, 0
* by default.
* - `focusHoldsOpen`: whether keyboard focus on the trigger keeps the
* tooltip open after the pointer leaves, true by default. False closes it
* when the pointer leaves whatever opened it, as Radix's tooltip does.
* - `closeOnClick`: whether activating the trigger, by a click or the keys
* the element activates on, closes the tooltip, true by default.
* - `disableHoverablePopup`: the pointer on the popup doesn't keep the
Expand Down Expand Up @@ -280,6 +283,7 @@ export const TooltipProps = Schema.Struct({
disabled: Schema.optional(Schema.Boolean),
delay: Schema.optional(Schema.Number),
closeDelay: Schema.optional(Schema.Number),
focusHoldsOpen: Schema.optional(Schema.Boolean),
closeOnClick: Schema.optional(Schema.Boolean),
disableHoverablePopup: Schema.optional(Schema.Boolean),
describesTrigger: Schema.optional(Schema.Boolean),
Expand Down Expand Up @@ -481,7 +485,8 @@ const dismiss = ($: TooltipUpdate, instant = $.model.instant) =>
: $.model

// The pointer left the trigger or the hover area: a pending open stops, and
// an open tooltip closes after `closeDelay` unless keyboard focus holds it.
// an open tooltip closes after `closeDelay` unless keyboard focus holds it
// (`focusHoldsOpen`).
const left = (
$: TooltipUpdate,
released: Partial<typeof TooltipModel.Type>
Expand All @@ -490,7 +495,7 @@ const left = (
if (!shownBy($.props)) {
return next.run(Command.interrupt(WaitBeforeOpening))
}
if ($.model.focused) return next
if ($.model.focused && $.props.focusHoldsOpen !== false) return next
return next.run(
WaitBeforeClosing.make({ delay: $.props.closeDelay ?? 0 })
)
Expand Down Expand Up @@ -788,7 +793,8 @@ export const Tooltip = Html.component({
? $.evolve({ instant: null }).emit(requestedOpen)
: $.model,
CompletedWaitBeforeClosing: ($) =>
shownBy($.props) && !hoveredBy($.model, $.props) && !$.model.focused
shownBy($.props) && !hoveredBy($.model, $.props) &&
!($.model.focused && $.props.focusHoldsOpen !== false)
? $.evolve({ instant: null }).emit(requestedClose)
: $.model,
// The shown tooltips of the group changed. Its turn follows whether it
Expand Down
29 changes: 29 additions & 0 deletions packages/effect/test/ui/kit/Tooltip.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,35 @@ describe("Tooltip", () => {
.emitted({ _tag: "RequestedClose" })
}))

it.effect("keyboard focus holds it open after the pointer leaves unless focusHoldsOpen is false", () =>
Effect.gen(function*() {
const leave = Effect.fnUntraced(function*(focusHoldsOpen?: boolean) {
const scene = yield* HtmlScene.make(
Tooltip,
{ open: false, focusHoldsOpen },
standIn
)
scene.focus(trigger)
.emitted({ _tag: "RequestedOpen" })
.props({ open: true, focusHoldsOpen })
scene.mounted(tracking(scene), placed())
.fire(trigger, "pointerenter")
.fire(trigger, "pointerleave")
.mounted(tracking(scene), sent("LeftHoverArea"))
return scene
})
const held = yield* leave()
assert.deepStrictEqual(held.current.pending, [])
assert.deepStrictEqual(held.current.emitted, [])

const released = yield* leave(false)
released.resolve(
released.current.pending[0]!.command,
completed("CompletedWaitBeforeClosing")
)
.emitted({ _tag: "RequestedClose" })
}))

it.effect("activation dismisses on click rather than mousedown unless closeOnClick is false", () =>
Effect.gen(function*() {
const scene = yield* opened({ open: true })
Expand Down
Loading