Skip to content
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "patch",
"comment": "fix: reset MotionRefForwarder context in renderDialogSurface to prevent child components from corrupting Dialog's motion ref",
"packageName": "@fluentui/react-dialog",
"email": "olfedias@microsoft.com",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "patch",
"comment": "fix: add MotionRefForwarderReset to prevent context leaking to descendants",
"packageName": "@fluentui/react-motion",
"email": "olfedias@microsoft.com",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/** @jsxRuntime automatic */
/** @jsxImportSource @fluentui/react-jsx-runtime */

import { MotionRefForwarderReset } from '@fluentui/react-motion';
import { Portal } from '@fluentui/react-portal';
import { assertSlots } from '@fluentui/react-utilities';
import type { JSXElement } from '@fluentui/react-utilities';
Expand All @@ -27,9 +28,11 @@ export const renderDialogSurface_unstable = (
<state.backdrop />
</state.backdropMotion>
)}
<DialogSurfaceProvider value={contextValues.dialogSurface}>
<state.root />
</DialogSurfaceProvider>
<MotionRefForwarderReset>
<DialogSurfaceProvider value={contextValues.dialogSurface}>
<state.root />
</DialogSurfaceProvider>
</MotionRefForwarderReset>
</Portal>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ export const MotionRefForwarder: React_2.ForwardRefExoticComponent<{
children: React_2.ReactElement;
} & React_2.RefAttributes<HTMLElement>>;

// @internal
export const MotionRefForwarderReset: React_2.FC<{
children: React_2.ReactElement;
}>;

// @public (undocumented)
export const motionTokens: {
curveAccelerateMax: "cubic-bezier(0.9,0.1,1,0.2)";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { render } from '@testing-library/react';
import * as React from 'react';

import { MotionRefForwarder, useMotionForwardedRef } from './MotionRefForwarder';
import { MotionRefForwarder, MotionRefForwarderReset, useMotionForwardedRef } from './MotionRefForwarder';

const TestConsumer: React.FC = () => {
const ref = useMotionForwardedRef();
Expand Down Expand Up @@ -49,3 +49,57 @@ describe('MotionRefForwarder', () => {
expect(callbackRef).toHaveBeenCalledWith(element);
});
});

describe('MotionRefForwarderReset', () => {
it('should reset context to undefined for its children', () => {
let capturedRef: React.Ref<HTMLElement> | undefined = 'not-set' as unknown as React.Ref<HTMLElement>;

const RefCapture: React.FC = () => {
capturedRef = useMotionForwardedRef();
return null;
};

const ref = React.createRef<HTMLElement>();

render(
<MotionRefForwarder ref={ref}>
<MotionRefForwarderReset>
<RefCapture />
</MotionRefForwarderReset>
</MotionRefForwarder>,
);

expect(capturedRef).toBeUndefined();
});

it('should not affect consumers outside the reset boundary', () => {
let innerRef: React.Ref<HTMLElement> | undefined;
let outerRef: React.Ref<HTMLElement> | undefined;

const InnerCapture: React.FC = () => {
innerRef = useMotionForwardedRef();
return null;
};

const OuterCapture: React.FC = () => {
outerRef = useMotionForwardedRef();
return <div data-testid="outer" ref={outerRef as React.Ref<HTMLDivElement>} />;
};

const ref = React.createRef<HTMLElement>();

render(
<MotionRefForwarder ref={ref}>
<>
<OuterCapture />
<MotionRefForwarderReset>
<InnerCapture />
</MotionRefForwarderReset>
</>
</MotionRefForwarder>,
);

expect(innerRef).toBeUndefined();
expect(outerRef).toBe(ref);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,16 @@ export const MotionRefForwarder = React.forwardRef<HTMLElement, { children: Reac
});

MotionRefForwarder.displayName = 'MotionRefForwarder';

/**
* Resets the MotionRefForwarder context to `undefined` for its children.
* Render this in components that consume `useMotionForwardedRef()` and render
* arbitrary user content, to prevent the context from leaking to descendants.
*
* @internal
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why are we marking things with internal when they are public ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Hotell good question. I didn't pay attention for this comment from Claude.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

great 👍

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

*/
export const MotionRefForwarderReset: React.FC<{ children: React.ReactElement }> = props => {
return <MotionRefForwarderContext.Provider value={undefined}>{props.children}</MotionRefForwarderContext.Provider>;
};

MotionRefForwarderReset.displayName = 'MotionRefForwarderReset';
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export {
export { createPresenceComponentVariant } from './factories/createPresenceComponentVariant';

export { PresenceGroup } from './components/PresenceGroup';
export { MotionRefForwarder, useMotionForwardedRef } from './components/MotionRefForwarder';
export { MotionRefForwarder, MotionRefForwarderReset, useMotionForwardedRef } from './components/MotionRefForwarder';

export { presenceMotionSlot, type PresenceMotionSlotProps } from './slots/presenceMotionSlot';

Expand Down
Loading