-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support children as fn and remove disclosure
- Loading branch information
1 parent
560eb2e
commit 7094afa
Showing
7 changed files
with
111 additions
and
83 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
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
78 changes: 37 additions & 41 deletions
78
packages/design-system/src/components/Popover/Popover.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,69 +1,65 @@ | ||
import { useRef } from 'react'; | ||
import type { ReactNode } from 'react'; | ||
import { useRef, Fragment } from 'react'; | ||
import type { ReactNode, MouseEvent } from 'react'; | ||
import tokens from '@talend/design-tokens'; | ||
|
||
import { Placement, FloatingArrow, FloatingPortal } from '@floating-ui/react'; | ||
import { usePopover } from './usePopover'; | ||
import { Disclosure } from '../Disclosure/Disclosure'; | ||
import theme from './Popover.module.scss'; | ||
import { renderOrClone, ChildrenOrFn } from '../../renderOrClone'; | ||
|
||
interface PopoverOptions { | ||
disclosure?: ReactNode; | ||
type PopoverOptions = { | ||
popup: ReactNode | ((props: any) => ReactNode); | ||
Check warning on line 11 in packages/design-system/src/components/Popover/Popover.tsx GitHub Actions / ESLint Report Analysispackages/design-system/src/components/Popover/Popover.tsx#L11
|
||
initialOpen?: boolean; | ||
placement?: Placement; | ||
modal?: boolean; | ||
open?: boolean; | ||
isFixed?: boolean; | ||
onOpenChange?: (open: boolean) => void; | ||
} | ||
}; | ||
|
||
export type PopoverProps = { | ||
children: ChildrenOrFn; | ||
} & PopoverOptions; | ||
|
||
export function Popover({ | ||
children, | ||
disclosure, | ||
modal = true, | ||
isFixed = false, | ||
popup, | ||
...restOptions | ||
}: { | ||
children: ReactNode; | ||
} & PopoverOptions) { | ||
}: PopoverProps) { | ||
// This can accept any props as options, e.g. `placement`, | ||
// or other positioning options. | ||
const arrowRef = useRef<SVGSVGElement>(null); | ||
const popover = usePopover({ modal, arrowRef, ...restOptions }); | ||
|
||
let childrenRendered = children; | ||
if (typeof children === 'function') { | ||
childrenRendered = children(popover); | ||
} | ||
let content = ( | ||
<div | ||
ref={popover.refs.setFloating} | ||
className={theme.popover} | ||
style={{ ...popover.floatingStyles, display: popover.open ? 'block' : 'none' }} | ||
aria-labelledby={popover.labelId} | ||
aria-describedby={popover.descriptionId} | ||
{...popover.getFloatingProps(restOptions)} | ||
> | ||
<FloatingArrow | ||
ref={arrowRef} | ||
context={popover.context} | ||
strokeWidth={1} | ||
stroke={tokens.coralColorIllustrationShadow} | ||
fill={tokens.coralColorNeutralBackground} | ||
/> | ||
{childrenRendered} | ||
</div> | ||
); | ||
if (isFixed) { | ||
content = <FloatingPortal>{content}</FloatingPortal>; | ||
} | ||
const disclosureProps = popover.getReferenceProps({ onClick: e => e.stopPropagation() }); | ||
const Wrapper = isFixed ? FloatingPortal : Fragment; | ||
const onClick = (e: MouseEvent<HTMLElement>) => { | ||
e.preventDefault(); | ||
}; | ||
const childrenProps = popover.getReferenceProps({ onClick }); | ||
return ( | ||
<> | ||
<Disclosure popref={popover.refs.setReference} {...disclosureProps}> | ||
{disclosure} | ||
</Disclosure> | ||
{content} | ||
{renderOrClone(children, { ...childrenProps, ref: popover.refs.setReference })} | ||
<Wrapper> | ||
<div | ||
ref={popover.refs.setFloating} | ||
className={theme.popover} | ||
style={{ ...popover.floatingStyles, display: popover.open ? 'block' : 'none' }} | ||
aria-labelledby={popover.labelId} | ||
aria-describedby={popover.descriptionId} | ||
{...popover.getFloatingProps(restOptions)} | ||
> | ||
<FloatingArrow | ||
ref={arrowRef} | ||
context={popover.context} | ||
strokeWidth={1} | ||
stroke={tokens.coralColorIllustrationShadow} | ||
fill={tokens.coralColorNeutralBackground} | ||
/> | ||
{typeof popup === 'function' ? popup(popover.getFloatingProps()) : popup} | ||
</div> | ||
</Wrapper> | ||
</> | ||
); | ||
} |
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,3 +1,4 @@ | ||
import { Popover } from './Popover'; | ||
|
||
export type { PopoverProps } from './Popover'; | ||
export type { PopoverTriggerProps } from './usePopover'; | ||
export default Popover; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import type { ReactNode } from 'react'; | ||
|
||
export type ChildrenOrFn = ReactNode | ((props: any) => ReactNode); | ||
|
||
export function renderOrClone(children: ChildrenOrFn, props: any = {}): ReactNode { | ||
if (typeof children === 'function') { | ||
return children(props); | ||
} | ||
return children && props ? React.cloneElement(children, props) : children; | ||
} |