-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4cc6080
commit 3f86d0b
Showing
6 changed files
with
131 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,14 @@ | ||
import { Button } from '../../packages/react-components/src/Button.js'; | ||
import { Popover } from '../../packages/react-components/src/Popover.js'; | ||
|
||
export default function () { | ||
return ( | ||
<> | ||
<Popover for="button"> | ||
<Button>Click me</Button> | ||
</Popover> | ||
|
||
<Button id="button">Toggle popover</Button> | ||
</> | ||
); | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,45 @@ | ||
import { | ||
type ComponentType, | ||
type ForwardedRef, | ||
type HTMLAttributes, | ||
forwardRef, | ||
type ReactElement, | ||
type ReactNode, | ||
} from 'react'; | ||
import { Popover as _Popover, type PopoverElement, type PopoverProps as _PopoverProps } from './generated/Popover.js'; | ||
import { useSimpleOrChildrenRenderer } from './renderers/useSimpleOrChildrenRenderer.js'; | ||
import type { ReactSimpleRendererProps } from './renderers/useSimpleRenderer.js'; | ||
|
||
export * from './generated/Popover.js'; | ||
|
||
export type PopoverReactRendererProps = ReactSimpleRendererProps<PopoverElement>; | ||
|
||
type OmittedPopoverHTMLAttributes = Omit< | ||
HTMLAttributes<PopoverElement>, | ||
'id' | 'className' | 'dangerouslySetInnerHTML' | 'slot' | ||
>; | ||
|
||
export type PopoverProps = Partial< | ||
Omit<_PopoverProps, 'children' | 'renderer' | keyof OmittedPopoverHTMLAttributes> | ||
> & | ||
Readonly<{ | ||
children?: ReactNode | ComponentType<PopoverReactRendererProps>; | ||
renderer?: ComponentType<PopoverReactRendererProps> | null; | ||
}>; | ||
|
||
function Popover( | ||
{ children, ...props }: PopoverProps, | ||
ref: ForwardedRef<PopoverElement>, | ||
): ReactElement | null { | ||
const [portals, renderer] = useSimpleOrChildrenRenderer(props.renderer, children); | ||
|
||
return ( | ||
<_Popover {...props} ref={ref} renderer={renderer}> | ||
{portals} | ||
</_Popover> | ||
); | ||
} | ||
|
||
const ForwardedPopover = forwardRef(Popover); | ||
|
||
export { ForwardedPopover as 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { expect, use as useChaiPlugin } from '@esm-bundle/chai'; | ||
import { cleanup, render } from '@testing-library/react/pure.js'; | ||
import chaiDom from 'chai-dom'; | ||
import { Popover, type PopoverElement } from '../packages/react-components/src/Popover.js'; | ||
import createOverlayCloseCatcher from './utils/createOverlayCloseCatcher.js'; | ||
|
||
useChaiPlugin(chaiDom); | ||
|
||
describe('Popover', () => { | ||
const overlayTag = 'vaadin-popover-overlay'; | ||
|
||
const [ref, catcher] = createOverlayCloseCatcher<PopoverElement>(overlayTag, (ref) => { | ||
ref.opened = false; | ||
}); | ||
|
||
async function assert() { | ||
await new Promise((r) => requestAnimationFrame(r)); | ||
|
||
const overlay = document.querySelector(overlayTag); | ||
expect(overlay).to.exist; | ||
|
||
const childNodes = Array.from(overlay!.childNodes); | ||
|
||
const body = childNodes.find((node) => node.nodeType === Node.TEXT_NODE); | ||
expect(body).to.have.text('FooBar'); | ||
} | ||
|
||
afterEach(cleanup); | ||
afterEach(catcher); | ||
|
||
it('should use children if no renderer property set', async () => { | ||
render( | ||
<Popover ref={ref} opened> | ||
FooBar | ||
</Popover>, | ||
); | ||
|
||
await assert(); | ||
}); | ||
|
||
it('should use renderer prop if it is set', async () => { | ||
render(<Popover ref={ref} opened renderer={() => <>FooBar</>}></Popover>); | ||
|
||
await assert(); | ||
}); | ||
|
||
it('should use children as renderer prop', async () => { | ||
render( | ||
<Popover ref={ref} opened> | ||
{() => <>FooBar</>} | ||
</Popover>, | ||
); | ||
|
||
await assert(); | ||
}); | ||
}); |
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