Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add trigger property, make opened property public #7430

Merged
merged 16 commits into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions dev/popover.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
<script type="module">
const popover = document.querySelector('vaadin-popover');

// popover.trigger = ['hover', 'focus'];

popover.renderer = (root) => {
if (root.firstChild) {
return;
Expand Down
21 changes: 21 additions & 0 deletions packages/popover/src/vaadin-popover-overlay.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,27 @@ class PopoverOverlay extends PopoverOverlayMixin(DirMixin(ThemableMixin(PolylitM
margin-top: var(--vaadin-popover-offset-top, 0);
}

[part='overlay'] {
position: relative;
overflow: visible;
}

[part='content'] {
overflow: auto;
}

/* Increase the area of the popover so the pointer can go from the target directly to it. */
[part='overlay']::before {
position: absolute;
content: '';
top: calc(var(--vaadin-popover-offset-top, 0) * -1);
bottom: calc(var(--vaadin-popover-offset-bottom, 0) * -1);
inset-inline-start: calc(var(--vaadin-popover-offset-start, 0) * -1);
inset-inline-end: calc(var(--vaadin-popover-offset-end, 0) * -1);
z-index: -1;
pointer-events: auto;
}

:host([position^='top'][bottom-aligned]) [part='overlay'],
:host([position^='bottom'][bottom-aligned]) [part='overlay'] {
margin-bottom: var(--vaadin-popover-offset-bottom, 0);
Expand Down
52 changes: 52 additions & 0 deletions packages/popover/src/vaadin-popover.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,36 @@ export type { PopoverPosition } from './vaadin-popover-position-mixin.js';

export type PopoverRenderer = (root: HTMLElement, popover: Popover) => void;

export type PopoverTrigger = 'click' | 'focus' | 'hover';

/**
* Fired when the `opened` property changes.
*/
export type PopoverOpenedChangedEvent = CustomEvent<{ value: boolean }>;

export interface PopoverCustomEventMap {
'opened-changed': PopoverOpenedChangedEvent;
}

export type PopoverEventMap = HTMLElementEventMap & PopoverCustomEventMap;

/**
* `<vaadin-popover>` is a Web Component for creating overlays
* that are positioned next to specified DOM element (target).
*
* Unlike `<vaadin-tooltip>`, the popover supports rich content
* that can be provided by using `renderer` function.
*
* @fires {CustomEvent} opened-changed - Fired when the `opened` property changes.
*/
declare class Popover extends PopoverPositionMixin(
PopoverTargetMixin(OverlayClassMixin(ThemePropertyMixin(ElementMixin(HTMLElement)))),
) {
/**
* True if the popover overlay is opened, false otherwise.
*/
opened: boolean;

/**
* Custom function for rendering the content of the overlay.
* Receives two arguments:
Expand Down Expand Up @@ -56,6 +76,26 @@ declare class Popover extends PopoverPositionMixin(
*/
noCloseOnEsc: boolean;

/**
* Popover trigger mode, used to configure how the overlay is opened or closed.
* Could be set to multiple by providing an array, e.g. `trigger = ['hover', 'focus']`.
*
* Supported values:
* - `click` (default) - opens and closes on target click.
* - `hover` - opens on target mouseenter, closes on target mouseleave. Moving mouse
* to the popover overlay content keeps the overlay opened.
* - `focus` - opens on target focus, closes on target blur. Moving focus to the
* popover overlay content keeps the overlay opened.
*
* In addition to the behavior specified by `trigger`, the popover can be closed by:
* - pressing Escape key (unless `noCloseOnEsc` property is true)
* - outside click (unless `noCloseOnOutsideClick` property is true)
*
* When setting `trigger` property to `null`, `undefined` or empty array, the popover
* can be only opened or closed programmatically by changing `opened` property.
*/
trigger: PopoverTrigger[] | null | undefined;

/**
* When true, the overlay has a backdrop (modality curtain) on top of the
* underlying page content, covering the whole viewport.
Expand All @@ -71,6 +111,18 @@ declare class Popover extends PopoverPositionMixin(
* It is not guaranteed that the update happens immediately (synchronously) after it is requested.
*/
requestContentUpdate(): void;

addEventListener<K extends keyof PopoverEventMap>(
type: K,
listener: (this: Popover, ev: PopoverEventMap[K]) => void,
options?: AddEventListenerOptions | boolean,
): void;

removeEventListener<K extends keyof PopoverEventMap>(
type: K,
listener: (this: Popover, ev: PopoverEventMap[K]) => void,
options?: EventListenerOptions | boolean,
): void;
}

declare global {
Expand Down
Loading
Loading