Co-authored-by: Brendan Allan <14191578+Brendonovich@users.noreply.github.com> Co-authored-by: Kit Langton <kit.langton@gmail.com> Co-authored-by: opencode-agent[bot] <opencode-agent[bot]@users.noreply.github.com> Co-authored-by: Affan Ali <93028901+affanali2k3@users.noreply.github.com> Co-authored-by: affanali2k3 <affanalikhanxx@gmail.com> Co-authored-by: Frank <frank@anoma.ly> Co-authored-by: opencode-agent[bot] <219766164+opencode-agent[bot]@users.noreply.github.com> Co-authored-by: 𝓛𝓲𝓽𝓽𝓵𝓮 𝓕𝓻𝓪𝓷𝓴 <little-frank@opencord.local> Co-authored-by: Aiden Cline <63023139+rekram1-node@users.noreply.github.com> Co-authored-by: Jay V <air@live.ca> Co-authored-by: Dax Raad <d@ironbay.co> Co-authored-by: Aarav Sareen <96787824+arvsrn@users.noreply.github.com> Co-authored-by: OpeOginni <107570612+OpeOginni@users.noreply.github.com> Co-authored-by: Luke Parker <10430890+Hona@users.noreply.github.com> Co-authored-by: Ben Guthrie <benjee.012@gmail.com> Co-authored-by: Dax <mail@thdxr.com> Co-authored-by: Filip <34747899+neriousy@users.noreply.github.com> Co-authored-by: Max Anderson <max.a.anderson95@gmail.com> Co-authored-by: Brendan Allan <git@brendonovich.dev> Co-authored-by: Jack <jack@anoma.ly> Co-authored-by: Shoubhit Dash <shoubhit2005@gmail.com> Co-authored-by: Dustin Deus <deusdustin@gmail.com> Co-authored-by: starptech <starptech@starptechs-MBP.fritz.box> Co-authored-by: Aiden Cline <aidenpcline@gmail.com> Co-authored-by: usrnk1 <7547651+usrnk1@users.noreply.github.com> Co-authored-by: Jay <53023+jayair@users.noreply.github.com> Co-authored-by: runvip <164729189+runvip@users.noreply.github.com> Co-authored-by: opencode <opencode@sst.dev> Co-authored-by: Julian Coy <julian@ex-machina.co> Co-authored-by: Vladimir Glafirov <vglafirov@gitlab.com>
163 lines
4.3 KiB
TypeScript
163 lines
4.3 KiB
TypeScript
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}
|
|
openDelay={400}
|
|
skipDelayDuration={300}
|
|
{...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>
|
|
)
|
|
}
|