Skip to content

Commit

Permalink
fix(react-positioning): reset overrideTargetRef when target from hook…
Browse files Browse the repository at this point in the history
… option is undefined (#25135)

* reset overrideTargetRef

* chglog

* add screener test

* update fix and vr test

* try to stablize button with icon vr test

* Revert "try to stablize button with icon vr test"

This reverts commit fc46ab2.
  • Loading branch information
YuanboXue-Amber authored Oct 12, 2022
1 parent bcabb97 commit 16c7bca
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
26 changes: 26 additions & 0 deletions apps/vr-tests-react-components/src/stories/Positioning.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,31 @@ const VirtualElement = () => {
);
};

const ResetTarget = () => {
const [virtualElement, setVirtualElement] = React.useState<PositioningVirtualElement | null>(null);
const { containerRef, targetRef } = usePositioning({
position: 'below',
align: 'end',
target: virtualElement,
});

React.useEffect(() => {
if (virtualElement) {
setVirtualElement(null);
}
}, [virtualElement]);

return (
<>
<div style={{ display: 'flex', flexDirection: 'column' }}>
<button ref={setVirtualElement}>Virtual Target</button>
<button ref={targetRef}>Target should be fully visible</button>
</div>
<Box ref={containerRef}>Anchored to virtual element then reset to real target.</Box>
</>
);
};

const TargetProp = () => {
const [target, setTarget] = React.useState<HTMLButtonElement | null>(null);

Expand Down Expand Up @@ -562,6 +587,7 @@ storiesOf('Positioning', module)
.addStory('disable tether', () => <DisableTether />)
.addStory('position fixed', () => <PositionAndAlignProps positionFixed />, { includeRtl: true })
.addStory('virtual element', () => <VirtualElement />)
.addStory('reset target', () => <ResetTarget />)
.addStory('target property', () => <TargetProp />)
.addStory('imperative target', () => <ImperativeTarget />)
.addStory('visibility modifiers', () => (
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "patch",
"comment": "fix `usePositioning` to reset `overrideTargetRef` when target in hook options is undefined",
"packageName": "@fluentui/react-positioning",
"email": "[email protected]",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,7 @@ export function usePositioning(
);

useIsomorphicLayoutEffect(() => {
if (options.target) {
overrideTargetRef.current = options.target;
}
overrideTargetRef.current = options.target ?? null;
}, [options.target, overrideTargetRef, containerRef]);

useIsomorphicLayoutEffect(() => {
Expand Down

0 comments on commit 16c7bca

Please sign in to comment.