From de357f652e0d5033b585974c520e804f181a5ac8 Mon Sep 17 00:00:00 2001 From: Cory O'Brien Date: Mon, 11 Nov 2024 13:21:48 -0500 Subject: [PATCH] better types for hovercard, remove unused context trigger --- src/ui/HoverCard.tsx | 54 ++++++++++++++++++++++++-------------------- 1 file changed, 29 insertions(+), 25 deletions(-) diff --git a/src/ui/HoverCard.tsx b/src/ui/HoverCard.tsx index 5e6f7a6..59b38f6 100644 --- a/src/ui/HoverCard.tsx +++ b/src/ui/HoverCard.tsx @@ -7,13 +7,11 @@ import { useMemo, useState, } from 'react'; -import { styled } from 'styled-system/jsx'; +import { styled, type HTMLStyledProps } from 'styled-system/jsx'; type HoverCardContextValue = { isOpen: boolean; - isHovered: boolean; toggleOpen: (updates: boolean) => void; - toggleHovered: (updates: boolean) => void; }; const HoverCardContext = createContext( @@ -39,11 +37,12 @@ export const InnerContent = styled(animated(RxHoverCard.Content), { }, }); -export interface ContentProps extends RxHoverCard.HoverCardContentProps {} +export type ContentProps = RxHoverCard.HoverCardContentProps & + HTMLStyledProps<'div'>; export const Content = forwardRef( function Content(props, innerRef) { - const { children } = props; + const { children, ...contentProps } = props; const { isOpen } = useContext(HoverCardContext); const transitions = useTransition(isOpen, { from: { opacity: 0.84, y: 4 }, @@ -62,7 +61,12 @@ export const Content = forwardRef( {transitions((styles, shouldRender) => shouldRender ? ( - + {children} ) : null, @@ -72,22 +76,13 @@ export const Content = forwardRef( }, ); -export interface HoverCardTriggerProps - extends RxHoverCard.HoverCardTriggerProps {} +export type HoverCardTriggerProps = RxHoverCard.HoverCardTriggerProps; export const Trigger = (props: HoverCardTriggerProps) => { - const { children } = props; - const { toggleHovered } = useContext(HoverCardContext); - const { enter, leave } = useMemo( - () => ({ - enter: () => toggleHovered(true), - leave: () => toggleHovered(false), - }), - [toggleHovered], - ); + const { children, asChild = true, ...triggerProps } = props; return ( - + {children} ); @@ -98,28 +93,37 @@ const rootDefaults: RxHoverCard.HoverCardProps = { openDelay: 0, }; -export interface HoverCardProps extends RxHoverCard.HoverCardProps {} +export type HoverCardProps = Omit< + RxHoverCard.HoverCardProps, + 'open' | 'onOpenChange' +>; export const Root = (props: HoverCardProps) => { - const { children } = props; - const [isOpen, toggleOpen] = useState(false); - const [isHovered, toggleHovered] = useState(false); + const { + children, + closeDelay = 0, + defaultOpen = false, + openDelay = 0, + ...rootProps + } = props; + const [isOpen, toggleOpen] = useState(() => defaultOpen); const context = useMemo( () => ({ - isHovered, isOpen, - toggleHovered, toggleOpen, }), - [isHovered, isOpen], + [isOpen], ); return ( {children}