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(popover): auto update on anchor and popover resize and move #1261

Closed
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Fixed

- `Popover`: fix improper first placement on React 18
- `Popover`: update placement on both anchor and popover resize

## [3.11.0][] - 2025-02-05

### Added
Expand Down
297 changes: 146 additions & 151 deletions packages/lumx-react/src/components/popover/Popover.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,17 @@ import {
Popover,
Size,
Elevation,
Message,
FlexBox,
FlexBoxProps,
IconButtonProps,
} from '@lumx/react';
import range from 'lodash/range';
import { withCombinations } from '@lumx/react/stories/decorators/withCombinations';
import { getSelectArgType } from '@lumx/react/stories/controls/selectArgType';
import { withChromaticForceScreenSize } from '@lumx/react/stories/decorators/withChromaticForceScreenSize';
import { FitAnchorWidth } from '@lumx/react/components/popover/constants';
import { withUndefined } from '@lumx/react/stories/controls/withUndefined';

export default {
title: 'LumX components/popover/Popover',
Expand Down Expand Up @@ -110,164 +116,153 @@ export const Placements = {
],
};

export const WithUpdatingChildren = () => {
const anchorRef = useRef(null);
const [isOpen, setIsOpen] = useState(false);

const toggleOpen = () => setIsOpen(!isOpen);

const [text, setText] = useState('Long loading text with useless words');
useEffect(() => {
if (isOpen) {
const timer = setTimeout(() => {
setText('Text');
}, 1000);
return () => clearTimeout(timer);
}
setText('Long loading text with useless words');
return undefined;
}, [isOpen]);

return (
<div style={{ float: 'right' }} className="lumx-spacing-margin-right-huge">
<IconButton
label="Notifications"
className="lumx-spacing-margin-right-huge"
ref={anchorRef}
emphasis={Emphasis.low}
icon={mdiBell}
size={Size.m}
onClick={toggleOpen}
/>
<Popover
closeOnClickAway
closeOnEscape
isOpen={isOpen}
anchorRef={anchorRef}
placement={Placement.BOTTOM_END}
onClose={toggleOpen}
fitWithinViewportHeight
>
<List>
<ListItem before={<Icon icon={mdiAccount} />} className="lumx-spacing-margin-right-huge">
<span>{text}</span>
</ListItem>
</List>
</Popover>
</div>
);
/**
* Demo all fitAnchorWidth configurations
*/
export const FitToAnchorWidth = {
render({ anchorText, fitAnchorWidth }: any) {
const anchorRef = useRef(null);
return (
<>
<Chip className="lumx-spacing-margin-huge" ref={anchorRef} size="s">
{anchorText}
</Chip>
<Popover
isOpen
className="lumx-spacing-padding"
placement="top"
anchorRef={anchorRef}
fitToAnchorWidth={fitAnchorWidth}
>
Popover {fitAnchorWidth}
</Popover>
</>
);
},
decorators: [
withCombinations({
combinations: {
cols: {
'Small Anchor': { anchorText: 'Small' },
'Large Anchor': { anchorText: 'Very very very very large anchor' },
},
rows: { key: 'fitAnchorWidth', options: withUndefined(Object.values(FitAnchorWidth)) },
},
cellStyle: { padding: 16 },
}),
],
};

export const WithScrollingPopover = () => {
const anchorRef = useRef(null);
const [isOpen, setIsOpen] = useState(false);
/**
* Testing update of the popover on anchor and popover resize and move
*/
export const TestUpdatingChildrenAndMovingAnchor = {
render() {
const anchorRef = useRef(null);
const [isOpen, setIsOpen] = useState(false);

const toggleOpen = () => setIsOpen(!isOpen);

const toggleOpen = () => setIsOpen(!isOpen);
const [text, setText] = useState('Initial large span of text');
const [anchorSize, setAnchorSize] = useState<IconButtonProps['size']>('m');
const [anchorPosition, setAnchorPosition] = useState<FlexBoxProps['vAlign']>('center');
useEffect(() => {
if (isOpen) {
const timers = [
setTimeout(() => setText('Text'), 1000),
setTimeout(() => setAnchorSize('s'), 1500),
setTimeout(() => setAnchorPosition('left'), 2000),
];
return () => timers.forEach(clearTimeout);
}
setText('Initial large span of text');
setAnchorSize('m');
setAnchorPosition('center');
return undefined;
}, [isOpen]);

return (
<div style={{ float: 'right' }} className="lumx-spacing-margin-right-huge">
<IconButton
label="Notifications"
className="lumx-spacing-margin-right-huge"
ref={anchorRef}
emphasis={Emphasis.low}
icon={mdiBell}
size={Size.m}
onClick={toggleOpen}
/>
<Popover
closeOnClickAway
closeOnEscape
isOpen={isOpen}
anchorRef={anchorRef}
placement={Placement.BOTTOM}
onClose={toggleOpen}
fitWithinViewportHeight
>
<List style={{ overflowY: 'auto' }}>
{range(100).map((n: number) => {
return (
<ListItem
key={`key-${n}`}
before={<Icon icon={mdiAccount} />}
className="lumx-spacing-margin-right-huge"
>
<span>{`List item ${n} and some text`}</span>
</ListItem>
);
})}
</List>
</Popover>
</div>
);
return (
<FlexBox orientation="vertical" gap="huge">
<Message kind="info">
Test popover text resize (after 1sec), anchor resize (after 1.5sec) and anchor move (after 2sec)
</Message>
<FlexBox orientation="horizontal" vAlign={anchorPosition}>
<IconButton
label="Notifications"
className="lumx-spacing-margin-right-huge"
ref={anchorRef}
emphasis={Emphasis.low}
icon={mdiBell}
size={anchorSize}
onClick={toggleOpen}
/>
<Popover
closeOnClickAway
closeOnEscape
isOpen={isOpen}
anchorRef={anchorRef}
placement={Placement.BOTTOM_END}
onClose={toggleOpen}
fitWithinViewportHeight
hasArrow
>
<Text as="p" className="lumx-spacing-padding-huge">
{text}
</Text>
</Popover>
</FlexBox>
</FlexBox>
);
},
parameters: { chromatic: { disable: true } },
};

export const FitToAnchorWidth = () => {
const demoPopperStyle = {
alignItems: 'center',
display: 'flex',
height: 100,
justifyContent: 'center',
width: 200,
};

const container = {
alignItems: 'center',
display: 'flex',
justifyContent: 'center',
flexDirection: 'column',
gap: 150,
marginTop: 150,
} as const;
/**
* Testing popover with scroll inside
*/
export const TestScrollingPopover = {
render() {
const anchorRef = useRef(null);
const [isOpen, setIsOpen] = useState(false);

const maxWidthAnchorRef = useRef(null);
const widthSmallAnchorRef = useRef(null);
const widthLargeAnchorRef = useRef(null);
const minWidthAnchorRef = useRef(null);
const defaultWidthAnchorRef = useRef(null);
const toggleOpen = () => setIsOpen(!isOpen);

return (
<div style={container}>
<div>
<Chip ref={maxWidthAnchorRef} size={Size.s}>
Anchor
</Chip>
</div>
<Popover anchorRef={maxWidthAnchorRef} fitToAnchorWidth="maxWidth" isOpen placement="top">
<div style={demoPopperStyle}>Popover maxWidth</div>
</Popover>
<div>
<Chip ref={widthSmallAnchorRef} size={Size.s}>
Anchor
</Chip>
</div>
<Popover anchorRef={widthSmallAnchorRef} fitToAnchorWidth="width" isOpen placement="top">
<div style={demoPopperStyle}>Popover width small anchor</div>
</Popover>
<div>
<Chip ref={widthLargeAnchorRef} size={Size.s}>
VeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLargeAnchor
</Chip>
</div>
<Popover anchorRef={widthLargeAnchorRef} fitToAnchorWidth="width" isOpen placement="top">
<div style={demoPopperStyle}>Popover width large anchor</div>
</Popover>
<div>
<Chip ref={minWidthAnchorRef} size={Size.s}>
VeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLargeAnchor
</Chip>
</div>
<Popover anchorRef={minWidthAnchorRef} fitToAnchorWidth="minWidth" isOpen placement="top">
<div style={demoPopperStyle}>Popover minWidth</div>
</Popover>
<div>
<Chip ref={defaultWidthAnchorRef} size={Size.s}>
VeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLargeAnchor
</Chip>
return (
<div style={{ float: 'right' }} className="lumx-spacing-margin-right-huge">
<IconButton
label="Notifications"
className="lumx-spacing-margin-right-huge"
ref={anchorRef}
emphasis={Emphasis.low}
icon={mdiBell}
size={Size.m}
onClick={toggleOpen}
/>
<Popover
closeOnClickAway
closeOnEscape
isOpen={isOpen}
anchorRef={anchorRef}
placement={Placement.BOTTOM_END}
onClose={toggleOpen}
fitWithinViewportHeight
>
<List style={{ overflowY: 'auto' }}>
{range(100).map((n: number) => {
return (
<ListItem
key={`key-${n}`}
before={<Icon icon={mdiAccount} />}
className="lumx-spacing-margin-right-huge"
>
<span>{`List item ${n} and some text`}</span>
</ListItem>
);
})}
</List>
</Popover>
</div>
<Popover anchorRef={defaultWidthAnchorRef} isOpen placement="top">
<div style={demoPopperStyle}>Popover default</div>
</Popover>
</div>
);
);
},
parameters: { chromatic: { disable: true } },
};
5 changes: 3 additions & 2 deletions packages/lumx-react/src/components/popover/Popover.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import classNames from 'classnames';
import { useCallbackOnEscape } from '@lumx/react/hooks/useCallbackOnEscape';
import { useFocus } from '@lumx/react/hooks/useFocus';
import { ClickAwayProvider } from '@lumx/react/utils/ClickAwayProvider';
import { DOCUMENT } from '@lumx/react/constants';
import { DOCUMENT, VISUALLY_HIDDEN } from '@lumx/react/constants';
import { Comp, GenericProps, HasTheme } from '@lumx/react/utils/type';
import { getRootClassName, handleBasicClasses } from '@lumx/react/utils/className';
import { useMergeRefs } from '@lumx/react/utils/react/mergeRefs';
Expand Down Expand Up @@ -137,7 +137,6 @@ const _InnerPopover = forwardRef<PopoverProps, HTMLDivElement>((props, ref) => {
fitWithinViewportHeight,
boundaryRef,
anchorRef,
children,
placement,
style,
zIndex,
Expand Down Expand Up @@ -168,6 +167,8 @@ const _InnerPopover = forwardRef<PopoverProps, HTMLDivElement>((props, ref) => {
elevation: Math.min(elevation || 0, 5),
position,
}),
// Do not show the popover while it's not properly placed
!styles.popover?.transform ? VISUALLY_HIDDEN : undefined,
)}
style={styles.popover}
{...attributes.popper}
Expand Down
Loading