Skip to content

Commit

Permalink
fix(Dialog): do not require ref forwarding
Browse files Browse the repository at this point in the history
  • Loading branch information
layershifter committed Aug 5, 2024
1 parent 804c367 commit 7d41f77
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "patch",
"comment": "fix: do not require ref forwarding",
"packageName": "@fluentui/react-dialog",
"email": "[email protected]",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import { assertSlots } from '@fluentui/react-utilities';
import * as React from 'react';

import { MotionRefForwarder } from '../MotionRefForwarder';
import { DialogProvider, DialogSurfaceProvider } from '../../contexts';
import type { DialogState, DialogContextValues, DialogSlots } from './Dialog.types';

Expand All @@ -19,9 +20,11 @@ export const renderDialog_unstable = (state: DialogState, contextValues: DialogC
{state.trigger}
{state.content && (
<state.surfaceMotion>
{/* Casting here as content should be equivalent to <DialogSurface/> */}
{/* FIXME: content should not be ReactNode it should be ReactElement instead. */}
{state.content as React.ReactElement}
<MotionRefForwarder>
{/* Casting here as content should be equivalent to <DialogSurface/> */}
{/* FIXME: content should not be ReactNode it should be ReactElement instead. */}
{state.content as React.ReactElement}
</MotionRefForwarder>
</state.surfaceMotion>
)}
</DialogSurfaceProvider>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import * as React from 'react';
import { useDialogContext_unstable } from '../../contexts';
import { useDisableBodyScroll } from '../../utils/useDisableBodyScroll';
import { DialogBackdropMotion } from '../DialogBackdropMotion';
import { useMotionForwardedRef } from '../MotionRefForwarder';
import type { DialogSurfaceElement, DialogSurfaceProps, DialogSurfaceState } from './DialogSurface.types';

/**
Expand All @@ -28,6 +29,8 @@ export const useDialogSurface_unstable = (
props: DialogSurfaceProps,
ref: React.Ref<DialogSurfaceElement>,
): DialogSurfaceState => {
const contextRef = useMotionForwardedRef();

const modalType = useDialogContext_unstable(ctx => ctx.modalType);
const isNestedDialog = useDialogContext_unstable(ctx => ctx.isNestedDialog);

Expand Down Expand Up @@ -116,7 +119,7 @@ export const useDialogSurface_unstable = (
// FIXME:
// `DialogSurfaceElement` is wrongly assigned to be `HTMLElement` instead of `HTMLDivElement`
// but since it would be a breaking change to fix it, we are casting ref to it's proper type
ref: useMergedRefs(ref, dialogRef) as React.Ref<HTMLDivElement>,
ref: useMergedRefs(ref, contextRef, dialogRef) as React.Ref<HTMLDivElement>,
}),
{ elementType: 'div' },
),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import * as React from 'react';

const MotionRefForwarderContext = React.createContext<React.Ref<HTMLElement> | undefined>(undefined);

/**
* @internal
*/
export function useMotionForwardedRef() {
return React.useContext(MotionRefForwarderContext);
}

/**
* A component that forwards a ref to its children via a React context.
*
* @internal
*/
export const MotionRefForwarder = React.forwardRef<HTMLElement, { children: React.ReactElement }>((props, ref) => {
return <MotionRefForwarderContext.Provider value={ref}>{props.children}</MotionRefForwarderContext.Provider>;
});

0 comments on commit 7d41f77

Please sign in to comment.