mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-19 00:03:13 +03:00
fix(ui): mount tooltips on first hover and render buttons natively (#60755)
This commit is contained in:
committed by
GitHub
Unverified
parent
c626de0bee
commit
30a97bc351
@@ -0,0 +1,12 @@
|
||||
import { expect, story } from "../../stories/playwright/story"
|
||||
|
||||
story("opens a neighbouring tooltip without the open delay", async ({ mount, page }) => {
|
||||
const root = await mount("ui-tooltip--appearances")
|
||||
await root.getByText("Standard", { exact: true }).hover()
|
||||
await expect(page.getByRole("tooltip", { name: "Standard tooltip" })).toBeVisible()
|
||||
// With timers frozen, the next tooltip can only open through the skip-delay window that closing this one starts.
|
||||
await page.clock.install({ time: new Date("2026-01-01T00:00:00Z") })
|
||||
await page.clock.pauseAt(new Date("2026-01-01T00:00:01Z"))
|
||||
await root.getByText("Compact", { exact: true }).hover()
|
||||
await expect(page.getByRole("tooltip", { name: "Compact tooltip" })).toBeVisible()
|
||||
})
|
||||
@@ -1,11 +1,11 @@
|
||||
import { Root } from "@kobalte/core/button"
|
||||
import type { Root } from "@kobalte/core/button"
|
||||
import { type ComponentProps, Show, splitProps } from "solid-js"
|
||||
import { Icon, type IconProps } from "@opencode/ui/icon"
|
||||
import "./button.css"
|
||||
|
||||
export interface ButtonProps
|
||||
extends ComponentProps<typeof Root>,
|
||||
Pick<ComponentProps<"button">, "class" | "classList" | "children"> {
|
||||
extends Omit<ComponentProps<typeof Root>, "type">,
|
||||
Pick<ComponentProps<"button">, "class" | "classList" | "children" | "type"> {
|
||||
size?: "small" | "normal" | "large"
|
||||
variant?:
|
||||
| "neutral"
|
||||
@@ -22,11 +22,16 @@ export interface ButtonProps
|
||||
icon?: IconProps["name"]
|
||||
}
|
||||
|
||||
// A native button with the attributes Kobalte's Button sets on one, without its per-instance computations.
|
||||
export function Button(props: ButtonProps) {
|
||||
const [split, rest] = splitProps(props, ["variant", "size", "icon", "class", "classList"])
|
||||
const [split, rest] = splitProps(props, ["variant", "size", "icon", "class", "classList", "disabled"])
|
||||
|
||||
return (
|
||||
<Root
|
||||
<button
|
||||
// A type the caller passes in `rest` replaces this default.
|
||||
type="button"
|
||||
disabled={split.disabled}
|
||||
data-disabled={split.disabled ? "" : undefined}
|
||||
{...rest}
|
||||
data-component="button-v2"
|
||||
data-size={split.size || "normal"}
|
||||
@@ -39,6 +44,6 @@ export function Button(props: ButtonProps) {
|
||||
>
|
||||
<Show when={split.icon}>{(icon) => <Icon name={icon()} />}</Show>
|
||||
{props.children}
|
||||
</Root>
|
||||
</button>
|
||||
)
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { Root } from "@kobalte/core/button"
|
||||
import type { Root } from "@kobalte/core/button"
|
||||
import { type ComponentProps, splitProps } from "solid-js"
|
||||
import { JSX } from "solid-js"
|
||||
import "./icon-button.css"
|
||||
@@ -12,11 +12,24 @@ export interface IconButtonProps
|
||||
state?: "rest" | "hover" | "pressed"
|
||||
}
|
||||
|
||||
// A native button with the attributes Kobalte's Button sets on one, without its per-instance computations.
|
||||
export function IconButton(props: ComponentProps<"button"> & IconButtonProps) {
|
||||
const [local, rest] = splitProps(props, ["variant", "size", "icon", "class", "classList", "state"])
|
||||
const [local, rest] = splitProps(props, [
|
||||
"variant",
|
||||
"size",
|
||||
"icon",
|
||||
"class",
|
||||
"classList",
|
||||
"state",
|
||||
"type",
|
||||
"disabled",
|
||||
])
|
||||
|
||||
return (
|
||||
<Root
|
||||
<button
|
||||
type={local.type ?? "button"}
|
||||
disabled={local.disabled}
|
||||
data-disabled={local.disabled ? "" : undefined}
|
||||
{...rest}
|
||||
data-component="icon-button-v2"
|
||||
data-size={local.size || "normal"}
|
||||
@@ -28,6 +41,6 @@ export function IconButton(props: ComponentProps<"button"> & IconButtonProps) {
|
||||
}}
|
||||
>
|
||||
{local.icon}
|
||||
</Root>
|
||||
</button>
|
||||
)
|
||||
}
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
import { Content, Portal, Root, Trigger } from "@kobalte/core/tooltip"
|
||||
import { createEffect, Match, onCleanup, splitProps, Switch, untrack, type JSX } from "solid-js"
|
||||
import { createComputed, createEffect, Match, onCleanup, Show, splitProps, Switch, untrack, type JSX } from "solid-js"
|
||||
import type { ComponentProps } from "solid-js"
|
||||
import { createStore } from "solid-js/store"
|
||||
import { spread } from "solid-js/web"
|
||||
import "./tooltip.css"
|
||||
|
||||
export interface TooltipProps extends ComponentProps<typeof Root> {
|
||||
@@ -15,13 +16,23 @@ export interface TooltipProps extends ComponentProps<typeof Root> {
|
||||
triggerTabIndex?: number
|
||||
}
|
||||
|
||||
type TriggerHandlers = {
|
||||
onPointerEnter?: (event: PointerEvent) => void
|
||||
onFocus?: (event: FocusEvent) => void
|
||||
}
|
||||
|
||||
export function Tooltip(props: TooltipProps) {
|
||||
let ref: HTMLDivElement | undefined
|
||||
let handlers: TriggerHandlers | undefined
|
||||
|
||||
const [state, setState] = createStore({
|
||||
open: false,
|
||||
block: false,
|
||||
expand: false,
|
||||
// Kobalte's tooltip costs dozens of computations and listeners per trigger, and most triggers (an action on every
|
||||
// timeline row, a copy button on every code block) are never hovered. It mounts on the first interaction that
|
||||
// could open it, or as soon as a caller controls it with forceOpen.
|
||||
mounted: false,
|
||||
})
|
||||
|
||||
const [local, others] = splitProps(props, [
|
||||
@@ -75,11 +86,6 @@ export function Tooltip(props: TooltipProps) {
|
||||
drop(expand)
|
||||
}
|
||||
|
||||
const arm = () => {
|
||||
setState("block", true)
|
||||
close()
|
||||
}
|
||||
|
||||
const leave = () => {
|
||||
// Kobalte's own pointerleave handler runs after this one and only starts the
|
||||
// skip-delay window for neighbouring tooltips if it still sees this one open.
|
||||
@@ -89,8 +95,21 @@ export function Tooltip(props: TooltipProps) {
|
||||
})
|
||||
}
|
||||
|
||||
const mount = (event: PointerEvent | FocusEvent) => {
|
||||
if (state.mounted) return
|
||||
setState("mounted", true)
|
||||
|
||||
// Kobalte attached its listeners while this event was being dispatched, too late to receive it.
|
||||
if (event instanceof PointerEvent) handlers?.onPointerEnter?.(event)
|
||||
else handlers?.onFocus?.(event)
|
||||
}
|
||||
|
||||
createComputed(() => {
|
||||
if (controlled()) setState("mounted", true)
|
||||
})
|
||||
|
||||
createEffect(() => {
|
||||
if (!ref) return
|
||||
if (!ref || !state.mounted) return
|
||||
untrack(sync)
|
||||
const obs = new MutationObserver(sync)
|
||||
obs.observe(ref, {
|
||||
@@ -104,73 +123,92 @@ export function Tooltip(props: TooltipProps) {
|
||||
|
||||
let justClickedTrigger = false
|
||||
|
||||
// Kobalte's trigger takes over the element rendered below instead of rendering one, so mounting the tooltip leaves
|
||||
// the trigger and its children in place, with their focus and hover.
|
||||
const adopt = (triggerProps: TriggerHandlers) => {
|
||||
handlers = triggerProps
|
||||
|
||||
if (!ref) return null
|
||||
// Kobalte owns the open state attributes from here, and spread never removes an attribute it did not set, so a
|
||||
// tooltip that mounts open would keep the closed marker.
|
||||
ref.removeAttribute("data-closed")
|
||||
spread(ref, triggerProps, false, true)
|
||||
|
||||
return null
|
||||
}
|
||||
|
||||
return (
|
||||
<Switch>
|
||||
<Match when={local.inactive}>{local.children}</Match>
|
||||
<Match when={true}>
|
||||
<Root
|
||||
gutter={4}
|
||||
openDelay={400}
|
||||
skipDelayDuration={300}
|
||||
{...others}
|
||||
closeDelay={0}
|
||||
ignoreSafeArea={local.ignoreSafeArea ?? true}
|
||||
open={controlled() ? local.forceOpen : state.open}
|
||||
onOpenChange={(open) => {
|
||||
if (controlled()) return
|
||||
|
||||
if (state.block && open) return
|
||||
|
||||
if (justClickedTrigger) {
|
||||
justClickedTrigger = false
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
setState("open", open)
|
||||
<div
|
||||
ref={(el) => {
|
||||
ref = el
|
||||
// Kobalte's trigger sets this on the element it renders; it sets it again here once it mounts.
|
||||
el.setAttribute("type", "button")
|
||||
}}
|
||||
data-closed=""
|
||||
tabIndex={local.triggerTabIndex}
|
||||
data-component="tooltip-v2-trigger"
|
||||
class={local.class}
|
||||
onPointerEnter={mount}
|
||||
onFocus={mount}
|
||||
onFocusOut={() => requestAnimationFrame(() => drop())}
|
||||
>
|
||||
<Trigger
|
||||
ref={ref}
|
||||
as="div"
|
||||
tabIndex={local.triggerTabIndex}
|
||||
data-component="tooltip-v2-trigger"
|
||||
class={local.class}
|
||||
onPointerDownCapture={arm}
|
||||
onKeyDownCapture={(event: KeyboardEvent) => {
|
||||
if (event.key !== "Enter" && event.key !== " ") return
|
||||
arm()
|
||||
{local.children}
|
||||
</div>
|
||||
<Show when={state.mounted}>
|
||||
<Root
|
||||
gutter={4}
|
||||
openDelay={400}
|
||||
skipDelayDuration={300}
|
||||
{...others}
|
||||
closeDelay={0}
|
||||
ignoreSafeArea={local.ignoreSafeArea ?? true}
|
||||
open={controlled() ? local.forceOpen : state.open}
|
||||
onOpenChange={(open) => {
|
||||
if (controlled()) return
|
||||
|
||||
if (state.block && open) return
|
||||
|
||||
if (justClickedTrigger) {
|
||||
justClickedTrigger = false
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
setState("open", open)
|
||||
}}
|
||||
onPointerLeave={leave}
|
||||
onFocusOut={() => requestAnimationFrame(() => drop())}
|
||||
>
|
||||
{local.children}
|
||||
</Trigger>
|
||||
<Portal>
|
||||
<Content
|
||||
ref={(el) => {
|
||||
const theme = ref?.closest("[data-theme]")?.getAttribute("data-theme")
|
||||
{/* Kobalte calls leave inside its own pointerleave listener. As a separate listener, its microtask would
|
||||
close the tooltip before Kobalte's listener ran, which then skipped the skip-delay window. */}
|
||||
<Trigger as={adopt} onPointerLeave={leave} />
|
||||
<Portal>
|
||||
<Content
|
||||
ref={(el) => {
|
||||
const theme = ref?.closest("[data-theme]")?.getAttribute("data-theme")
|
||||
|
||||
if (theme) el.setAttribute("data-theme", theme)
|
||||
}}
|
||||
data-component="tooltip-v2"
|
||||
data-appearance={local.appearance ?? "compact"}
|
||||
data-placement={props.placement}
|
||||
data-force-open={local.forceOpen}
|
||||
class={local.contentClass}
|
||||
style={local.contentStyle}
|
||||
onPointerDownOutside={(e) => {
|
||||
if (ref === e.target || (e.target instanceof Node && ref?.contains(e.target))) {
|
||||
justClickedTrigger = true
|
||||
}
|
||||
if (theme) el.setAttribute("data-theme", theme)
|
||||
}}
|
||||
data-component="tooltip-v2"
|
||||
data-appearance={local.appearance ?? "compact"}
|
||||
data-placement={props.placement}
|
||||
data-force-open={local.forceOpen}
|
||||
class={local.contentClass}
|
||||
style={local.contentStyle}
|
||||
onPointerDownOutside={(e) => {
|
||||
if (ref === e.target || (e.target instanceof Node && ref?.contains(e.target))) {
|
||||
justClickedTrigger = true
|
||||
}
|
||||
|
||||
e.preventDefault()
|
||||
}}
|
||||
>
|
||||
{local.value}
|
||||
</Content>
|
||||
</Portal>
|
||||
</Root>
|
||||
e.preventDefault()
|
||||
}}
|
||||
>
|
||||
{local.value}
|
||||
</Content>
|
||||
</Portal>
|
||||
</Root>
|
||||
</Show>
|
||||
</Match>
|
||||
</Switch>
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user