Skip to content

Commit 1e56311

Browse files
authored
Coachmark reposition on page resize (#26725)
* re-render and re-calculate bounds when page resizes * change
1 parent dc2adec commit 1e56311

File tree

3 files changed

+35
-22
lines changed

3 files changed

+35
-22
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"type": "patch",
3+
"comment": "Coachmark: re-render and re-calculate bounds when page resizes",
4+
"packageName": "@fluentui/react",
5+
"email": "mgodbolt@microsoft.com",
6+
"dependentChangeType": "patch"
7+
}

packages/react/src/components/Coachmark/Coachmark.base.tsx

+11-1
Original file line numberDiff line numberDiff line change
@@ -467,14 +467,24 @@ export const CoachmarkBase: React.FunctionComponent<ICoachmarkProps> = React.for
467467

468468
const finalHeight: number | undefined = isCollapsed ? COACHMARK_HEIGHT : entityInnerHostRect.height;
469469

470+
function useGetBounds(): IRectangle | undefined {
471+
const async = useAsync();
472+
const [bounds, setBounds] = React.useState<IRectangle | undefined>();
473+
const updateAsyncPosition = (): void => {
474+
async.requestAnimationFrame(() => setBounds(getBounds(props)));
475+
};
476+
React.useEffect(updateAsyncPosition);
477+
return bounds;
478+
}
479+
470480
return (
471481
<PositioningContainer
472482
target={target}
473483
offsetFromTarget={BEAK_HEIGHT}
474484
finalHeight={finalHeight}
475485
ref={forwardedRef}
476486
onPositioned={onPositioned}
477-
bounds={getBounds(props)}
487+
bounds={useGetBounds()}
478488
{...positioningContainerProps}
479489
>
480490
<div className={classNames.root}>

packages/react/src/components/Coachmark/PositioningContainer/PositioningContainer.tsx

+17-21
Original file line numberDiff line numberDiff line change
@@ -34,30 +34,26 @@ const DEFAULT_PROPS = {
3434
directionalHint: DirectionalHint.bottomAutoEdge,
3535
};
3636

37-
function useCachedBounds(props: IPositioningContainerProps, targetWindow: Window | undefined) {
37+
function useBounds(props: IPositioningContainerProps, targetWindow: Window | undefined) {
3838
/** The bounds used when determining if and where the PositioningContainer should be placed. */
39-
const positioningBounds = React.useRef<IRectangle>();
40-
41-
const getCachedBounds = (): IRectangle => {
42-
if (!positioningBounds.current) {
43-
let currentBounds = props.bounds;
44-
45-
if (!currentBounds) {
46-
currentBounds = {
47-
top: 0 + props.minPagePadding!,
48-
left: 0 + props.minPagePadding!,
49-
right: targetWindow!.innerWidth - props.minPagePadding!,
50-
bottom: targetWindow!.innerHeight - props.minPagePadding!,
51-
width: targetWindow!.innerWidth - props.minPagePadding! * 2,
52-
height: targetWindow!.innerHeight - props.minPagePadding! * 2,
53-
};
54-
}
55-
positioningBounds.current = currentBounds;
39+
40+
const getBounds = (): IRectangle => {
41+
let currentBounds = props.bounds;
42+
43+
if (!currentBounds) {
44+
currentBounds = {
45+
top: 0 + props.minPagePadding!,
46+
left: 0 + props.minPagePadding!,
47+
right: targetWindow!.innerWidth - props.minPagePadding!,
48+
bottom: targetWindow!.innerHeight - props.minPagePadding!,
49+
width: targetWindow!.innerWidth - props.minPagePadding! * 2,
50+
height: targetWindow!.innerHeight - props.minPagePadding! * 2,
51+
};
5652
}
57-
return positioningBounds.current;
53+
return currentBounds;
5854
};
5955

60-
return getCachedBounds;
56+
return getBounds;
6157
}
6258

6359
function usePositionState(
@@ -312,7 +308,7 @@ export const PositioningContainer: React.FunctionComponent<IPositioningContainer
312308
const rootRef = useMergedRefs(forwardedRef, positionedHost);
313309

314310
const [targetRef, targetWindow] = useTarget(props.target, positionedHost);
315-
const getCachedBounds = useCachedBounds(props, targetWindow);
311+
const getCachedBounds = useBounds(props, targetWindow);
316312
const [positions, updateAsyncPosition] = usePositionState(
317313
props,
318314
positionedHost,

0 commit comments

Comments
 (0)