-
-
Notifications
You must be signed in to change notification settings - Fork 168
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add hover card component example
- Loading branch information
1 parent
b501d8b
commit 5c78652
Showing
1 changed file
with
54 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* eslint-disable react/jsx-no-target-blank */ | ||
import * as hoverCard from "@zag-js/hover-card" | ||
import { normalizeProps, Portal, useMachine } from "@zag-js/react" | ||
import { useId } from "react" | ||
|
||
interface Props extends Omit<hoverCard.Context, "__controlled" | "id"> { | ||
defaultOpen?: boolean | ||
} | ||
|
||
export function HoverCard(props: Props) { | ||
const { defaultOpen, open, ...context } = props | ||
|
||
const [state, send] = useMachine( | ||
hoverCard.machine({ | ||
id: useId(), | ||
open: open ?? defaultOpen, | ||
}), | ||
{ | ||
context: { | ||
...context, | ||
__controlled: open !== undefined, | ||
open: open ?? defaultOpen, | ||
}, | ||
}, | ||
) | ||
|
||
const api = hoverCard.connect(state, send, normalizeProps) | ||
|
||
return ( | ||
<main className="hover-card"> | ||
<div style={{ display: "flex", gap: "50px" }}> | ||
<a href="https://twitter.com/zag_js" target="_blank" {...api.triggerProps}> | ||
</a> | ||
|
||
{api.isOpen && ( | ||
<Portal> | ||
<div {...api.positionerProps}> | ||
<div {...api.contentProps}> | ||
<div {...api.arrowProps}> | ||
<div {...api.arrowTipProps} /> | ||
</div> | ||
Twitter Preview | ||
<a href="https://twitter.com/zag_js" target="_blank"> | ||
</a> | ||
</div> | ||
</div> | ||
</Portal> | ||
)} | ||
</div> | ||
</main> | ||
) | ||
} |