Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[@mantine/core] HoverCard: Allow HoverCard.Target usage with Tooltip (#…
- Loading branch information
Showing
4 changed files
with
55 additions
and
21 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
30 changes: 16 additions & 14 deletions
30
src/mantine-core/src/HoverCard/HoverCardTarget/HoverCardTarget.tsx
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,25 +1,27 @@ | ||
import React, { cloneElement } from 'react'; | ||
import React, { cloneElement, forwardRef } from 'react'; | ||
import { isElement, createEventHandler } from '@mantine/utils'; | ||
import { Popover, PopoverTargetProps } from '../../Popover'; | ||
import { useHoverCardContext } from '../HoverCard.context'; | ||
import { HOVER_CARD_ERRORS } from '../HoverCard.errors'; | ||
|
||
export interface HoverCardTargetProps extends PopoverTargetProps {} | ||
|
||
export function HoverCardTarget({ children, refProp }: HoverCardTargetProps) { | ||
if (!isElement(children)) { | ||
throw new Error(HOVER_CARD_ERRORS.children); | ||
} | ||
export const HoverCardTarget = forwardRef<HTMLElement, HoverCardTargetProps>( | ||
({ children, refProp, ...others }, ref) => { | ||
if (!isElement(children)) { | ||
throw new Error(HOVER_CARD_ERRORS.children); | ||
} | ||
|
||
const ctx = useHoverCardContext(); | ||
const onMouseEnter = createEventHandler(children.props.onMouseEnter, ctx.openDropdown); | ||
const onMouseLeave = createEventHandler(children.props.onMouseLeave, ctx.closeDropdown); | ||
const ctx = useHoverCardContext(); | ||
const onMouseEnter = createEventHandler(children.props.onMouseEnter, ctx.openDropdown); | ||
const onMouseLeave = createEventHandler(children.props.onMouseLeave, ctx.closeDropdown); | ||
|
||
return ( | ||
<Popover.Target refProp={refProp}> | ||
{cloneElement(children as React.ReactElement, { onMouseEnter, onMouseLeave })} | ||
</Popover.Target> | ||
); | ||
} | ||
return ( | ||
<Popover.Target refProp={refProp} ref={ref} {...others}> | ||
{cloneElement(children as React.ReactElement, { onMouseEnter, onMouseLeave })} | ||
</Popover.Target> | ||
); | ||
} | ||
); | ||
|
||
HoverCardTarget.displayName = '@mantine/core/HoverCardTarget'; |
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