Skip to content

Commit 2a62e9b

Browse files
authored
Tooltip improvements (#1874)
Allow the tooltip position (and arrow) to be configured, and change the placement of the tooltip on the Device show page.
1 parent 76633a2 commit 2a62e9b

File tree

3 files changed

+56
-19
lines changed

3 files changed

+56
-19
lines changed

assets/ui-rework/hooks/toolTip.js

Lines changed: 50 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,64 @@
1-
import { computePosition, offset, arrow } from "@floating-ui/dom"
1+
import { autoUpdate, computePosition, offset, arrow } from "@floating-ui/dom"
22

33
export default {
44
mounted() {
55
this.updated()
66
},
77

88
updateTooltip() {
9-
computePosition(this.el, this.content, {
10-
placement: this.placement,
11-
middleware: [offset(15), arrow({ element: this.arrow })]
12-
}).then(({ x, y, middlewareData }) => {
13-
Object.assign(this.content.style, {
14-
top: `${y}px`,
15-
left: `${x}px`
16-
})
9+
const arrowLen = this.arrow.offsetWidth
10+
11+
let placement = this.placement
1712

18-
if (middlewareData.arrow) {
19-
const { x } = middlewareData.arrow
13+
autoUpdate(this.el, this.content, () => {
14+
const sideOffset = {
15+
top: 15,
16+
right: 10,
17+
bottom: 15,
18+
left: 10
19+
}[this.placement]
2020

21-
Object.assign(this.arrow.style, {
21+
computePosition(this.el, this.content, {
22+
placement,
23+
middleware: [offset(sideOffset), arrow({ element: this.arrow })]
24+
}).then(({ x, y, middlewareData, placement }) => {
25+
Object.assign(this.content.style, {
2226
left: `${x}px`,
23-
top: `${-this.arrow.offsetHeight / 2}px`
27+
top: `${y}px`
2428
})
25-
}
29+
30+
const side = placement.split("-")[0]
31+
32+
const staticSide = {
33+
top: "bottom",
34+
right: "left",
35+
bottom: "top",
36+
left: "right"
37+
}[side]
38+
39+
if (middlewareData.arrow) {
40+
const { x, y } = middlewareData.arrow
41+
42+
const border = {
43+
top: "0 1px 1px 0",
44+
right: "0 0 1px 1px",
45+
bottom: "1px 0 0 1px",
46+
left: "1px 1px 0 0"
47+
}[this.placement]
48+
49+
Object.assign(this.arrow.style, {
50+
left: x != null ? `${x}px` : "",
51+
top: y != null ? `${y}px` : "",
52+
// Ensure the static side gets unset when
53+
// flipping to other placements' axes.
54+
right: "",
55+
bottom: "",
56+
[staticSide]: `${(-arrowLen - 1) / 2}px`,
57+
transform: "rotate(45deg)",
58+
"border-width": border
59+
})
60+
}
61+
})
2662
})
2763
},
2864

lib/nerves_hub_web/components/device_health/health_status.ex

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
11
defmodule NervesHubWeb.Components.HealthStatus do
2-
use NervesHubWeb, :live_component
2+
use NervesHubWeb, :component
33

44
attr(:device_id, :integer)
55
attr(:health, :map, default: %{status: :unknown, status_reasons: nil})
6+
attr(:tooltip_position, :string, default: "bottom")
67

78
def render(assigns) do
89
~H"""
9-
<div id={"health-tooltip-#{@device_id}"} phx-hook="ToolTip">
10+
<div id={"health-tooltip-#{@device_id}"} phx-hook="ToolTip" data-placement={@tooltip_position}>
1011
<.icon name={icon_name(@health)} />
1112
12-
<div class="tooltip-content hidden w-max absolute top-0 left-0 text-xs px-2 py-1.5 rounded border border-[#3F3F46] bg-base-900 flex">
13+
<div class="tooltip-content hidden w-max absolute top-0 left-0 z-40 text-xs px-2 py-1.5 rounded border border-[#3F3F46] bg-base-900 flex">
1314
<%= if @health && @health.status_reasons do %>
1415
<div :for={{status, reasons} <- @health.status_reasons}>
1516
{format_health_status_reason(status, reasons)}
1617
</div>
1718
<% else %>
1819
<div>{no_reasons(@health)}</div>
1920
<% end %>
20-
<div class="tooltip-arrow absolute w-2 h-2 border-l border-t border-[#3F3F46] bg-base-900 origin-center rotate-45"></div>
21+
<div class="tooltip-arrow absolute w-2 h-2 border-[#3F3F46] bg-base-900 origin-center rotate-45"></div>
2122
</div>
2223
</div>
2324
"""

lib/nerves_hub_web/components/device_page/details.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ defmodule NervesHubWeb.Components.DevicePage.Details do
9494
<div class="h-14 pl-4 pr-3 flex items-center justify-between">
9595
<div class="flex items-center gap-2">
9696
<div class="text-neutral-50 font-medium leading-6">Health</div>
97-
<HealthStatus.render device_id={@device.id} health={@device.latest_health} />
97+
<HealthStatus.render device_id={@device.id} health={@device.latest_health} tooltip_position="right" />
9898
</div>
9999
<div class="flex items-center gap-2">
100100
<div class="text-xs text-nerves-gray-500 tracking-wide">

0 commit comments

Comments
 (0)