Skip to content

Commit 620ce4c

Browse files
committed
tooltip floating-ui/react
1 parent 424f056 commit 620ce4c

File tree

4 files changed

+91
-21
lines changed

4 files changed

+91
-21
lines changed

frontend/app/components/redesign/components/ToolsWalletAddress.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,9 +159,9 @@ export const ToolsWalletAddress = ({ toolName }: ToolsWalletAddressProps) => {
159159
<Tooltip>
160160
Your wallet is required in order for us to save this components
161161
configuration for you, link it to the original author, and verify
162-
ownership for future updates. It also embeds the wallet address into
163-
your web page automatically, enabling Web Monetization on your
164-
behalf.
162+
ownership for future updates.
163+
<br /> It also embeds the wallet address into your web page
164+
automatically, enabling Web Monetization on your behalf.
165165
</Tooltip>
166166
</div>
167167

Lines changed: 47 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,76 @@
1-
import { useState, useId } from 'react'
2-
import { cx } from 'class-variance-authority'
1+
import { useState } from 'react'
2+
import {
3+
useFloating,
4+
offset,
5+
flip,
6+
shift,
7+
autoUpdate,
8+
size
9+
} from '@floating-ui/react-dom'
310
import { SVGTooltip } from '../../../assets/svg'
411

512
export interface TooltipProps {
613
children: React.ReactNode
7-
className?: string
814
}
915

10-
export function Tooltip({ children, className }: TooltipProps) {
16+
export function Tooltip({ children }: TooltipProps) {
1117
const [open, setOpen] = useState(false)
12-
const tooltipId = useId()
18+
19+
const { x, y, strategy, refs } = useFloating({
20+
open,
21+
placement: 'right',
22+
middleware: [
23+
offset(8),
24+
flip({
25+
fallbackPlacements: ['top', 'bottom'],
26+
padding: 8
27+
}),
28+
shift({ padding: 8 }),
29+
size({
30+
apply({ availableWidth, elements }) {
31+
const maxWidth = Math.min(availableWidth, 450)
32+
Object.assign(elements.floating.style, {
33+
maxWidth: `${maxWidth}px`,
34+
width: 'auto'
35+
})
36+
},
37+
padding: 8
38+
})
39+
],
40+
whileElementsMounted: autoUpdate
41+
})
1342

1443
return (
15-
<div className={cx('w-fit relative inline-flex', className)}>
44+
<>
1645
<button
46+
ref={refs.setReference}
1747
type="button"
1848
aria-label="More information"
19-
aria-describedby={open ? tooltipId : undefined}
20-
aria-expanded={open}
49+
aria-describedby={open ? 'tooltip' : undefined}
2150
onMouseEnter={() => setOpen(true)}
2251
onMouseLeave={() => setOpen(false)}
2352
onFocus={() => setOpen(true)}
2453
onBlur={() => setOpen(false)}
25-
onClick={() => setOpen(!open)}
2654
className="rounded-full hover:bg-gray-100 focus:outline-none focus:ring-1 focus:ring-primary-focus"
2755
>
2856
<SVGTooltip className="w-6 h-6" />
2957
</button>
3058

3159
{open && (
3260
<div
33-
id={tooltipId}
61+
ref={refs.setFloating}
62+
id="tooltip"
3463
role="tooltip"
35-
className="absolute z-50 left-8 top-1/2 -translate-y-1/2 w-[485px] flex items-center justify-center gap-2.5 p-4 bg-interface-tooltip rounded-lg shadow-lg"
64+
style={{
65+
position: strategy,
66+
top: y,
67+
left: x
68+
}}
69+
className="z-50 p-md bg-interface-tooltip rounded-lg shadow-lg text-white text-xs sm:text-sm"
3670
>
37-
<p className="text-white text-sm leading-relaxed">{children}</p>
71+
{children}
3872
</div>
3973
)}
40-
</div>
74+
</>
4175
)
4276
}

frontend/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"typecheck": "tsc --noEmit"
1414
},
1515
"dependencies": {
16+
"@floating-ui/react-dom": "^2.1.6",
1617
"@interledger/open-payments": "^7.1.3",
1718
"@noble/ed25519": "^3.0.0",
1819
"@paralleldrive/cuid2": "^2.2.2",

pnpm-lock.yaml

Lines changed: 40 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)