x
1
2
3
4
5
6
7
8
9
10
11
<div data-controller="popover">
<a href="" class="font-medium underline" data-popover-target="button" data-action="mouseenter->popover#showLater mouseleave->popover#hide">
@rails
</a>
<div popover class="popover" data-popover-target="menu">
<div class="flex flex-col gap-half p-4">
<h1 class="text-lg font-semibold leading-none">Ruby on Rails</h1>
<p class="text-sm text-subtle">The webframework that changed the world.</p>
</div>
</div>
</div>
1
2
3
4
5
6
7
8
9
10
11
12
<div data-controller="popover">
<a href="" class="font-medium underline" data-popover-target="button" data-action="mouseenter->popover#showLater mouseleave->popover#hide">
@rails
</a>
<div popover class="popover" data-popover-target="menu">
<div class="flex flex-col gap-half p-4">
<h1 class="text-lg font-semibold leading-none">Ruby on Rails</h1>
<p class="text-sm text-subtle">The webframework that changed the world.</p>
</div>
</div>
</div>
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
.popover {
background-color: var(--color-bg);
border-radius: var(--rounded-md);
border-width: var(--border);
box-shadow: var(--shadow-md);
color: var(--color-text);
inline-size: var(--popover-size, max-content);
/* Setup animation */
opacity: 0;
transform: var(--scale-95);
transition-behavior: allow-discrete;
transition-duration: var(--time-150);
transition-property: display, overlay, opacity, transform;
/* Animation end */
&:popover-open {
opacity: 1; transform: var(--scale-100);
}
/* Animation start */
@starting-style {
&:popover-open {
opacity: 0; transform: var(--scale-95);
}
}
}
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
import { Controller } from "@hotwired/stimulus"
import { computePosition, flip, shift, offset, autoUpdate } from "https://esm.sh/@floating-ui/dom@1.6.13?standalone"
export default class extends Controller {
static targets = [ "button", "menu" ]
static values = { placement: { type: String, default: "bottom" } }
#showTimer = null
initialize() {
this.orient = this.orient.bind(this)
}
connect() {
this.cleanup = autoUpdate(this.buttonTarget, this.menuTarget, this.orient)
}
disconnect() {
this.cleanup()
}
show() {
this.menuTarget.showPopover()
}
hide() {
clearTimeout(this.#showTimer); this.menuTarget.hidePopover()
}
showLater() {
this.#showTimer = setTimeout(() => this.show(), 700)
}
orient() {
computePosition(this.buttonTarget, this.menuTarget, this.#options).then(({x, y}) => {
this.menuTarget.style.insetInlineStart = `${x}px`
this.menuTarget.style.insetBlockStart = `${y}px`
})
}
get #options() {
return { placement: this.placementValue, middleware: [offset(4), flip(), shift({padding: 4})] }
}
}