x
1
2
3
4
<div data-controller="popover" data-popover-placement-value="top"> <button name="button" type="submit" class="btn" data-popover-target="button" data-action="mouseenter->popover#showLater focus->popover#show mouseleave->popover#hide focusout->popover#hide">Hover</button> <div popover class="popover text-xs p-2" data-popover-target="menu" role="tooltip">Add to library</div></div>
1
2
3
4
<div data-controller="popover" data-popover-placement-value="top"> <%= button_tag "Hover", class: "btn", data: { popover_target: "button", action: "mouseenter->popover#showLater focus->popover#show mouseleave->popover#hide focusout->popover#hide" } %> <div popover class="popover text-xs p-2" data-popover-target="menu" role="tooltip">Add to library</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})] } }}