1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
|
import { Tooltip as KobalteTooltip } from "@kobalte/core/tooltip"
import { createEffect, Match, onCleanup, splitProps, Switch, type JSX } from "solid-js"
import type { ComponentProps } from "solid-js"
import { createStore } from "solid-js/store"
export interface TooltipProps extends ComponentProps<typeof KobalteTooltip> {
value: JSX.Element
class?: string
contentClass?: string
contentStyle?: JSX.CSSProperties
inactive?: boolean
forceOpen?: boolean
}
export interface TooltipKeybindProps extends Omit<TooltipProps, "value"> {
title: string
keybind: string
}
export function TooltipKeybind(props: TooltipKeybindProps) {
const [local, others] = splitProps(props, ["title", "keybind"])
return (
<Tooltip
{...others}
value={
<div data-slot="tooltip-keybind">
<span>{local.title}</span>
<span data-slot="tooltip-keybind-key">{local.keybind}</span>
</div>
}
/>
)
}
export function Tooltip(props: TooltipProps) {
let ref: HTMLDivElement | undefined
const [state, setState] = createStore({
open: false,
block: false,
expand: false,
})
const [local, others] = splitProps(props, [
"children",
"class",
"contentClass",
"contentStyle",
"inactive",
"forceOpen",
"ignoreSafeArea",
"value",
])
const close = () => setState("open", false)
const inside = () => {
const active = document.activeElement
if (!ref || !active) return false
return ref.contains(active)
}
const drop = (expand = state.expand) => {
if (expand) return
if (ref?.matches(":hover")) return
if (inside()) return
setState("block", false)
}
const sync = () => {
const expand = !!ref?.querySelector('[aria-expanded="true"], [data-expanded]')
setState("expand", expand)
if (expand) {
setState("block", true)
close()
return
}
drop(expand)
}
const arm = () => {
setState("block", true)
close()
}
const leave = () => {
if (!inside()) close()
drop()
}
createEffect(() => {
if (!ref) return
sync()
const obs = new MutationObserver(sync)
obs.observe(ref, {
subtree: true,
childList: true,
attributes: true,
attributeFilter: ["aria-expanded", "data-expanded"],
})
onCleanup(() => obs.disconnect())
})
let justClickedTrigger = false
return (
<Switch>
<Match when={local.inactive}>{local.children}</Match>
<Match when={true}>
<KobalteTooltip
gutter={4}
{...others}
closeDelay={0}
ignoreSafeArea={local.ignoreSafeArea ?? true}
open={local.forceOpen || state.open}
onOpenChange={(open) => {
if (local.forceOpen) return
if (state.block && open) return
if (justClickedTrigger) {
justClickedTrigger = false
return
}
setState("open", open)
}}
>
<KobalteTooltip.Trigger
ref={ref}
as={"div"}
data-component="tooltip-trigger"
class={local.class}
onPointerDownCapture={arm}
onKeyDownCapture={(event: KeyboardEvent) => {
if (event.key !== "Enter" && event.key !== " ") return
arm()
}}
onPointerLeave={leave}
onFocusOut={() => requestAnimationFrame(() => drop())}
>
{local.children}
</KobalteTooltip.Trigger>
<KobalteTooltip.Portal>
<KobalteTooltip.Content
data-component="tooltip"
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}
{/* <KobalteTooltip.Arrow data-slot="tooltip-arrow" /> */}
</KobalteTooltip.Content>
</KobalteTooltip.Portal>
</KobalteTooltip>
</Match>
</Switch>
)
}
|