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(Coachmark): Remove positioning regression and update bounds on resize #27782

Merged
merged 10 commits into from
May 8, 2023
34 changes: 32 additions & 2 deletions apps/vr-tests/src/stories/Coachmark.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,28 @@ import { Coachmark, DirectionalHint, TeachingBubbleContent, Fabric } from '@flue
import { useId } from '@fluentui/react-hooks';
import { DefaultButton } from '@fluentui/react/lib/Button';

const CoachmarkUsage = ({ isCollapsed = true }: { isCollapsed?: boolean }) => {
const directionalHints = [
DirectionalHint.topLeftEdge,
DirectionalHint.topCenter,
DirectionalHint.topRightEdge,
DirectionalHint.bottomLeftEdge,
DirectionalHint.bottomCenter,
DirectionalHint.bottomRightEdge,
DirectionalHint.leftTopEdge,
DirectionalHint.leftCenter,
DirectionalHint.leftBottomEdge,
DirectionalHint.rightTopEdge,
DirectionalHint.rightCenter,
DirectionalHint.rightBottomEdge,
];

const CoachmarkUsage = ({
isCollapsed = true,
directionalHint = DirectionalHint.rightTopEdge,
}: {
isCollapsed?: boolean;
directionalHint?: DirectionalHint;
}) => {
const targetId = useId();
return (
<>
Expand All @@ -16,7 +37,7 @@ const CoachmarkUsage = ({ isCollapsed = true }: { isCollapsed?: boolean }) => {
<Coachmark
target={`#${targetId}`}
positioningContainerProps={{
directionalHint: DirectionalHint.rightTopEdge,
directionalHint,
doNotLayer: false,
}}
ariaAlertText="A Coachmark has appeared"
Expand Down Expand Up @@ -86,4 +107,13 @@ storiesOf('Coachmark', module)
<Coachmark target={rectangle} positioningContainerProps={positioningContainerProps} />
</>
);
})
.addStory('Positioning', () => {
return (
<div>
{directionalHints.map((directionalHint, index) => (
<CoachmarkUsage isCollapsed={false} directionalHint={directionalHint} key={index} />
))}
</div>
);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "patch",
"comment": "fix(Coachmark): Fix positioning regression and update bounds on resize.",
"packageName": "@fluentui/react",
"email": "esteban.230@hotmail.com",
"dependentChangeType": "patch"
}
22 changes: 11 additions & 11 deletions packages/react/src/components/Coachmark/Coachmark.base.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ function useProximityHandlers(
props: ICoachmarkProps,
translateAnimationContainer: React.RefObject<HTMLDivElement>,
openCoachmark: () => void,
setBounds: (bounds: IRectangle | undefined) => void,
) {
const { setTimeout, clearTimeout } = useSetTimeout();

Expand Down Expand Up @@ -285,6 +286,7 @@ function useProximityHandlers(
timeoutIds.push(
setTimeout((): void => {
setTargetElementRect();
setBounds(getBounds(props.isPositionForced, props.positioningContainerProps));
}, 100),
);
});
Expand Down Expand Up @@ -402,15 +404,6 @@ function useDeprecationWarning(props: ICoachmarkProps) {
}
}

function useGetBounds(props: ICoachmarkProps): IRectangle | undefined {
const async = useAsync();
const [bounds, setBounds] = React.useState<IRectangle | undefined>();
React.useEffect(() => {
async.requestAnimationFrame(() => setBounds(getBounds(props.isPositionForced, props.positioningContainerProps)));
}, [async, props.isPositionForced, props.positioningContainerProps]);
return bounds;
}

const COMPONENT_NAME = 'CoachmarkBase';

export const CoachmarkBase: React.FunctionComponent<ICoachmarkProps> = React.forwardRef<
Expand All @@ -426,14 +419,21 @@ export const CoachmarkBase: React.FunctionComponent<ICoachmarkProps> = React.for
const [isCollapsed, openCoachmark] = useCollapsedState(props, entityInnerHostElementRef);
const [beakPositioningProps, transformOrigin] = useBeakPosition(props, targetAlignment, targetPosition);
const [isMeasuring, entityInnerHostRect] = useEntityHostMeasurements(props, entityInnerHostElementRef);
const [bounds, setBounds] = React.useState<IRectangle | undefined>(
getBounds(props.isPositionForced, props.positioningContainerProps),
);
const alertText = useAriaAlert(props);
const entityHost = useAutoFocus(props);

useListeners(props, translateAnimationContainer, openCoachmark);
useComponentRef(props);
useProximityHandlers(props, translateAnimationContainer, openCoachmark);
useProximityHandlers(props, translateAnimationContainer, openCoachmark, setBounds);
useDeprecationWarning(props);

React.useEffect(() => {
setBounds(getBounds(props.isPositionForced, props.positioningContainerProps));
}, [props.isPositionForced, props.positioningContainerProps]);

const {
beaconColorOne,
beaconColorTwo,
Expand Down Expand Up @@ -484,7 +484,7 @@ export const CoachmarkBase: React.FunctionComponent<ICoachmarkProps> = React.for
finalHeight={finalHeight}
ref={forwardedRef}
onPositioned={onPositioned}
bounds={useGetBounds(props)}
bounds={bounds}
{...positioningContainerProps}
>
<div className={classNames.root}>
Expand Down