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

Fix #4819: Overlay handling better handling of alignment with browser… #4821

Merged
merged 1 commit into from
Aug 24, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
11 changes: 11 additions & 0 deletions components/lib/hooks/useOverlayListener.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ export const useOverlayListener = ({ target, overlay, listener, when = true }) =
* @param {boolean} options.valid It is controlled by PrimeReact. It is determined whether it is valid or not according to some custom validation.
*/
const [bindDocumentClickListener, unbindDocumentClickListener] = useEventListener({
target: 'window',
type: 'click',
listener: (event) => {
listener && listener(event, { type: 'outside', valid: event.which !== 3 && isOutsideClicked(event) });
}
});
const [bindWindowResizeListener, unbindWindowResizeListener] = useResizeListener({
target: 'window',
listener: (event) => {
listener && listener(event, { type: 'resize', valid: !DomHandler.isTouchDevice() });
}
Expand All @@ -34,6 +36,13 @@ export const useOverlayListener = ({ target, overlay, listener, when = true }) =
listener && listener(event, { type: 'orientationchange', valid: true });
}
});
const [bindWindowScrollChangeListener, unbindWindowScrollChangeListener] = useEventListener({
target: 'window',
type: 'scroll',
listener: (event) => {
listener && listener(event, { type: 'scroll', valid: true });
}
});
const [bindOverlayScrollListener, unbindOverlayScrollListener] = useOverlayScrollListener({
target,
listener: (event) => {
Expand All @@ -49,13 +58,15 @@ export const useOverlayListener = ({ target, overlay, listener, when = true }) =
bindDocumentClickListener();
bindWindowResizeListener();
bindWindowOrientationChangeListener();
bindWindowScrollChangeListener();
bindOverlayScrollListener();
};

const unbind = () => {
unbindDocumentClickListener();
unbindWindowResizeListener();
unbindWindowOrientationChangeListener();
unbindWindowScrollChangeListener();
unbindOverlayScrollListener();
};

Expand Down
19 changes: 16 additions & 3 deletions components/lib/overlaypanel/OverlayPanel.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as React from 'react';
import { useOnEscapeKey } from '../../lib/hooks/Hooks';
import PrimeReact, { PrimeReactContext, localeOption } from '../api/Api';
import { useHandleStyle } from '../componentbase/ComponentBase';
import { CSSTransition } from '../csstransition/CSSTransition';
Expand All @@ -9,7 +10,6 @@ import { Portal } from '../portal/Portal';
import { Ripple } from '../ripple/Ripple';
import { DomHandler, IconUtils, UniqueComponentId, ZIndexUtils, mergeProps } from '../utils/Utils';
import { OverlayPanelBase } from './OverlayPanelBase';
import { useOnEscapeKey } from '../../lib/hooks/Hooks';

export const OverlayPanel = React.forwardRef((inProps, ref) => {
const context = React.useContext(PrimeReactContext);
Expand Down Expand Up @@ -37,7 +37,16 @@ export const OverlayPanel = React.forwardRef((inProps, ref) => {
overlay: overlayRef,
listener: (event, { type, valid }) => {
if (valid) {
type === 'outside' ? props.dismissable && !isPanelClicked.current && hide() : hide();
switch (type) {
case 'outside':
props.dismissable ? !isPanelClicked.current && hide() : hide();
break;
case 'resize':
case 'scroll':
case 'orientationchange':
align();
break;
}
}

isPanelClicked.current = false;
Expand Down Expand Up @@ -154,7 +163,10 @@ export const OverlayPanel = React.forwardRef((inProps, ref) => {

if (containerOffset.top < targetOffset.top) {
overlayRef.current.setAttribute('data-p-overlaypanel-flipped', 'true');
!isUnstyled && DomHandler.addClass(overlayRef.current, 'p-overlaypanel-flipped');
isUnstyled && DomHandler.addClass(overlayRef.current, 'p-overlaypanel-flipped');
} else {
overlayRef.current.setAttribute('data-p-overlaypanel-flipped', 'false');
isUnstyled && DomHandler.removeClass(overlayRef.current, 'p-overlaypanel-flipped');
}
}
};
Expand Down Expand Up @@ -203,6 +215,7 @@ export const OverlayPanel = React.forwardRef((inProps, ref) => {
toggle,
show,
hide,
align,
getElement: () => overlayRef.current
}));

Expand Down
4 changes: 4 additions & 0 deletions components/lib/overlaypanel/overlaypanel.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,10 @@ export declare class OverlayPanel extends React.Component<OverlayPanelProps, any
* Hides the overlay.
*/
public hide(): void;
/**
* Align the overlay panel to its surroundings.
*/
public align(): void;
/**
* Used to get container element.
* @return {HTMLDivElement} Container element
Expand Down