-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(docs): enable native popover when supported (#10581)
* fix: enable native popover when supported * fix: ts-expect-error where necessary
- Loading branch information
Showing
5 changed files
with
124 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,75 @@ | ||
import React from "react" | ||
"use client" | ||
|
||
import React, { useRef } from "react" | ||
import polyfill from "@oddbird/css-anchor-positioning/fn" | ||
import { Tooltip as ArkTooltip } from "@ark-ui/react/tooltip" | ||
|
||
interface Props { | ||
label: string | ||
framework: string | ||
children: React.ReactNode | ||
} | ||
|
||
export function Tooltip({ label, children }: Props) { | ||
return ( | ||
<ArkTooltip.Root | ||
positioning={{ placement: "bottom" }} | ||
openDelay={0} | ||
lazyMount | ||
unmountOnExit | ||
> | ||
<ArkTooltip.Trigger asChild={true}>{children}</ArkTooltip.Trigger> | ||
<ArkTooltip.Positioner> | ||
<ArkTooltip.Content | ||
export function Tooltip({ label, framework, children }: Props) { | ||
if (typeof window === "undefined") return null | ||
|
||
const supportsPopover = | ||
HTMLElement?.prototype.hasOwnProperty("popover") ?? false | ||
|
||
if (supportsPopover) { | ||
// Web Native Popover | ||
const popoverTargetRef = useRef() | ||
const slug = label.replace(/[^a-zA-Z0-9]/g, "-").toLowerCase() | ||
|
||
// Enable CSS Anchor Positioning Polyfill | ||
// https://github.com/oddbird/css-anchor-positioning | ||
polyfill() | ||
|
||
return ( | ||
<div className="relative w-full"> | ||
<button | ||
id={`anchor-${framework}-${slug}`} | ||
// @ts-expect-error | ||
popovertarget={`popover-${framework}-${slug}`} | ||
className="w-full tooltip-anchor" | ||
// @ts-expect-error | ||
onMouseEnter={() => popoverTargetRef.current?.showPopover()} | ||
// @ts-expect-error | ||
onMouseLeave={() => popoverTargetRef.current?.hidePopover()} | ||
> | ||
{children} | ||
</button> | ||
<div | ||
// @ts-expect-error | ||
popover="auto" | ||
ref={popoverTargetRef} | ||
className="py-2 px-4 max-w-xs text-sm text-center text-fuchsia-900 bg-purple-100 rounded-lg border shadow-md" | ||
style={{ animation: " animation: fadeIn .2s linear" }} | ||
anchor={`anchor-${framework}-${slug}`} | ||
id={`popover-${framework}-${slug}`} | ||
> | ||
{label} | ||
</ArkTooltip.Content> | ||
</ArkTooltip.Positioner> | ||
</ArkTooltip.Root> | ||
) | ||
</div> | ||
</div> | ||
) | ||
} else { | ||
// Ark Tooltip | ||
return ( | ||
<ArkTooltip.Root | ||
positioning={{ placement: "bottom" }} | ||
openDelay={0} | ||
lazyMount | ||
unmountOnExit | ||
> | ||
<ArkTooltip.Trigger asChild={true}>{children}</ArkTooltip.Trigger> | ||
<ArkTooltip.Positioner> | ||
<ArkTooltip.Content | ||
className="py-2 px-4 max-w-xs text-sm text-center text-fuchsia-900 bg-purple-100 rounded-lg border shadow-md" | ||
style={{ animation: " animation: fadeIn .2s linear" }} | ||
> | ||
{label} | ||
</ArkTooltip.Content> | ||
</ArkTooltip.Positioner> | ||
</ArkTooltip.Root> | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.