Skip to content

Commit 8bedd2f

Browse files
feat(@clayui/popover): add observeRect to align the popover on the scroll
Nothing much different from what was done for `DropDown.Menu`, we just call the alignment method with each change in the `trigger`.
1 parent ef6b7c0 commit 8bedd2f

File tree

2 files changed

+94
-7
lines changed

2 files changed

+94
-7
lines changed

packages/clay-popover/src/index.tsx

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
* SPDX-License-Identifier: BSD-3-Clause
44
*/
55

6-
import {ClayPortal} from '@clayui/shared';
6+
import {ClayPortal, observeRect} from '@clayui/shared';
77
import classNames from 'classnames';
88
import domAlign from 'dom-align';
9-
import React, {useEffect, useRef, useState} from 'react';
9+
import React, {useCallback, useEffect, useRef, useState} from 'react';
1010

1111
export const ALIGN_POSITIONS = [
1212
'top',
@@ -100,12 +100,9 @@ const ClayPopover = React.forwardRef<HTMLDivElement, IProps>(
100100
const show = externalShow ? externalShow : internalShow;
101101
const setShow = onShowChange ? onShowChange : internalSetShow;
102102

103-
useEffect(() => {
103+
const align = useCallback(() => {
104104
if (
105-
trigger &&
106-
ref &&
107105
(ref as React.RefObject<HTMLElement>).current &&
108-
triggerRef &&
109106
triggerRef.current
110107
) {
111108
const points = ALIGNMENTS_MAP[alignPosition] as [
@@ -121,7 +118,19 @@ const ClayPopover = React.forwardRef<HTMLDivElement, IProps>(
121118
}
122119
);
123120
}
124-
}, [show]);
121+
}, [alignPosition, triggerRef, ref]);
122+
123+
useEffect(() => {
124+
if (trigger) {
125+
align();
126+
}
127+
}, [align, show]);
128+
129+
useEffect(() => {
130+
if (trigger && triggerRef.current) {
131+
return observeRect(triggerRef.current, align);
132+
}
133+
}, [align]);
125134

126135
useEffect(() => {
127136
if (!disableScroll && popoverScrollerRef.current && show) {

packages/clay-popover/stories/index.tsx

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import '@clayui/css/lib/css/atlas.css';
77
import ClayButton, {ClayButtonWithIcon} from '@clayui/button';
88
const spritemap = require('@clayui/css/lib/images/icons/icons.svg');
9+
import ClayModal, {useModal} from '@clayui/modal';
910
import {boolean, select} from '@storybook/addon-knobs';
1011
import {storiesOf} from '@storybook/react';
1112
import React from 'react';
@@ -102,4 +103,81 @@ storiesOf('Components|ClayPopover', module)
102103
</ClayPopover>
103104
</>
104105
);
106+
})
107+
.add('popover w/ recalculate position', () => {
108+
const [visible, setVisible] = React.useState(false);
109+
const {observer, onClose} = useModal({
110+
onClose: () => setVisible(false),
111+
});
112+
113+
return (
114+
<>
115+
{visible && (
116+
<ClayModal
117+
observer={observer}
118+
size="lg"
119+
spritemap={spritemap}
120+
status="info"
121+
>
122+
<ClayModal.Header>{'Title'}</ClayModal.Header>
123+
<ClayModal.Body scrollable>
124+
<ClayPopover
125+
header="Popover"
126+
trigger={
127+
<ClayButtonWithIcon
128+
aria-label="Information button"
129+
displayType="secondary"
130+
spritemap={spritemap}
131+
symbol="info-circle-open"
132+
/>
133+
}
134+
>
135+
{`Viennese flavour cup eu, percolator froth ristretto mazagran
136+
caffeine. White roast seasonal, mocha trifecta, dripper caffeine
137+
spoon acerbic to go macchiato strong. Viennese flavour cup eu, percolator froth ristretto mazagran
138+
caffeine. White roast seasonal, mocha trifecta, dripper caffeine
139+
spoon acerbic to go macchiato strong. Viennese flavour cup eu, percolator froth ristretto mazagran
140+
caffeine. White roast seasonal, mocha trifecta, dripper caffeine
141+
spoon acerbic to go macchiato strong. Viennese flavour cup eu, percolator froth ristretto mazagran
142+
caffeine. White roast seasonal, mocha trifecta, dripper caffeine
143+
spoon acerbic to go macchiato strong.`}
144+
</ClayPopover>
145+
<br />
146+
<img
147+
alt="cat"
148+
src="https://cataas.com/cat/says/it will have"
149+
/>
150+
<img
151+
alt="cat"
152+
src="https://cataas.com/cat/says/a scroll"
153+
/>
154+
</ClayModal.Body>
155+
<ClayModal.Footer
156+
first={
157+
<ClayButton.Group spaced>
158+
<ClayButton displayType="secondary">
159+
{'Secondary'}
160+
</ClayButton>
161+
<ClayButton displayType="secondary">
162+
{'Secondary'}
163+
</ClayButton>
164+
</ClayButton.Group>
165+
}
166+
last={
167+
<ClayButton onClick={onClose}>
168+
{'Primary'}
169+
</ClayButton>
170+
}
171+
/>
172+
</ClayModal>
173+
)}
174+
175+
<ClayButton
176+
displayType="primary"
177+
onClick={() => setVisible(true)}
178+
>
179+
{'Open modal'}
180+
</ClayButton>
181+
</>
182+
);
105183
});

0 commit comments

Comments
 (0)