Skip to content

Commit

Permalink
Use layout effect to set ids
Browse files Browse the repository at this point in the history
  • Loading branch information
michaldudak committed Jun 13, 2024
1 parent 6c9f081 commit 0a0eb8e
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,23 @@ import type {
} from './AlertDialogDescription.types';
import { useAlertDialogRootContext } from '../Root/AlertDialogRootContext';
import { useComponentRenderer } from '../../utils/useComponentRenderer';
import { useEnhancedEffect } from '../../utils/useEnhancedEffect';
import { useId } from '../../utils/useId';

const AlertDialogDescription = React.forwardRef(function AlertDialogDescription(
props: AlertDialogDescriptionProps,
forwardedRef: React.ForwardedRef<HTMLParagraphElement>,
) {
const { render, className, id, ...other } = props;
const { render, className, id: idProp, ...other } = props;
const { setDescriptionElementId, open } = useAlertDialogRootContext();

const ownerState: AlertDialogDescriptionOwnerState = {
open,
};

React.useEffect(() => {
const id = useId(idProp);

useEnhancedEffect(() => {
setDescriptionElementId(id);
return () => {
setDescriptionElementId(undefined);
Expand Down
4 changes: 2 additions & 2 deletions packages/mui-base/src/AlertDialog/Popup/AlertDialogPopup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const AlertDialogPopup = React.forwardRef(function AlertDialogPopup(
animated = true,
className,
container,
id: idProp,
id,
keepMounted = false,
render,
...other
Expand All @@ -25,7 +25,7 @@ const AlertDialogPopup = React.forwardRef(function AlertDialogPopup(
const { open, nestedOpenDialogCount } = rootContext;

const { getRootProps, floatingContext, mounted, transitionStatus } = useDialogPopup({
id: idProp,
id,
animated,
ref: forwardedRef,
dismissible: false,
Expand Down
8 changes: 6 additions & 2 deletions packages/mui-base/src/AlertDialog/Title/AlertDialogTitle.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,23 @@ import PropTypes from 'prop-types';
import type { AlertDialogTitleOwnerState, AlertDialogTitleProps } from './AlertDialogTitle.types';
import { useAlertDialogRootContext } from '../Root/AlertDialogRootContext';
import { useComponentRenderer } from '../../utils/useComponentRenderer';
import { useEnhancedEffect } from '../../utils/useEnhancedEffect';
import { useId } from '../../utils/useId';

const AlertDialogTitle = React.forwardRef(function AlertDialogTitle(
props: AlertDialogTitleProps,
forwardedRef: React.ForwardedRef<HTMLParagraphElement>,
) {
const { render, className, id, ...other } = props;
const { render, className, id: idProp, ...other } = props;
const { setTitleElementId, open } = useAlertDialogRootContext();

const ownerState: AlertDialogTitleOwnerState = {
open,
};

React.useEffect(() => {
const id = useId(idProp);

useEnhancedEffect(() => {
setTitleElementId(id);
return () => {
setTitleElementId(undefined);
Expand Down
3 changes: 2 additions & 1 deletion packages/mui-base/src/Dialog/Backdrop/useDialogBackdrop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { mergeReactProps } from '../../utils/mergeReactProps';
import { useAnimatedElement } from '../../utils/useAnimatedElement';
import { useForkRef } from '../../utils/useForkRef';
import { useEventCallback } from '../../utils/useEventCallback';
import { useEnhancedEffect } from '../../utils/useEnhancedEffect';

/**
*
Expand All @@ -26,7 +27,7 @@ export function useDialogBackdrop(params: UseDialogBackdropParams): UseDialogBac
const onMount = useEventCallback(onMountParam);
const onUnmount = useEventCallback(onUnmountParam);

React.useEffect(() => {
useEnhancedEffect(() => {
onMount();

return onUnmount;
Expand Down
11 changes: 8 additions & 3 deletions packages/mui-base/src/Dialog/Description/DialogDescription.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
import * as React from 'react';
import PropTypes from 'prop-types';
import type { DialogDescriptionProps } from './DialogDescription.types';
import { useComponentRenderer } from '../../utils/useComponentRenderer';
import { useDialogRootContext } from '../Root/DialogRootContext';
import { useComponentRenderer } from '../../utils/useComponentRenderer';
import { useEnhancedEffect } from '../../utils/useEnhancedEffect';
import { useId } from '../../utils/useId';

const DialogDescription = React.forwardRef(function DialogDescription(
props: DialogDescriptionProps,
forwardedRef: React.ForwardedRef<HTMLParagraphElement>,
) {
const { render, className, id, ...other } = props;
const { render, className, id: idProp, ...other } = props;
const { setDescriptionElementId, open, modal } = useDialogRootContext();

const ownerState = {
open,
modal,
};

React.useEffect(() => {
const id = useId(idProp);

useEnhancedEffect(() => {
setDescriptionElementId(id);
return () => {
setDescriptionElementId(undefined);
Expand Down
4 changes: 2 additions & 2 deletions packages/mui-base/src/Dialog/Popup/DialogPopup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const DialogPopup = React.forwardRef(function DialogPopup(
animated = true,
className,
container,
id: idProp,
id,
keepMounted = false,
render,
...other
Expand All @@ -24,7 +24,7 @@ const DialogPopup = React.forwardRef(function DialogPopup(
const { open, modal, nestedOpenDialogCount, dismissible } = rootContext;

const { getRootProps, floatingContext, mounted, transitionStatus } = useDialogPopup({
id: idProp,
id,
animated,
ref: forwardedRef,
isTopmost: nestedOpenDialogCount === 0,
Expand Down
3 changes: 2 additions & 1 deletion packages/mui-base/src/Dialog/Popup/useDialogPopup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { useForkRef } from '../../utils/useForkRef';
import { mergeReactProps } from '../../utils/mergeReactProps';
import { useAnimatedElement } from '../../utils/useAnimatedElement';
import { useScrollLock } from '../../utils/useScrollLock';
import { useEnhancedEffect } from '../../utils/useEnhancedEffect';

/**
*
Expand Down Expand Up @@ -57,7 +58,7 @@ export function useDialogPopup(parameters: UseDialogPopupParameters): UseDialogP

useScrollLock(modal && mounted);

React.useEffect(() => {
useEnhancedEffect(() => {
setPopupElementId(id);
return () => {
setPopupElementId(undefined);
Expand Down
10 changes: 7 additions & 3 deletions packages/mui-base/src/Dialog/Title/DialogTitle.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
import * as React from 'react';
import PropTypes from 'prop-types';
import type { DialogTitleProps } from './DialogTitle.types';
import { useComponentRenderer } from '../../utils/useComponentRenderer';
import { useDialogRootContext } from '../Root/DialogRootContext';
import { useComponentRenderer } from '../../utils/useComponentRenderer';
import { useEnhancedEffect } from '../../utils/useEnhancedEffect';
import { useId } from '../../utils/useId';

const DialogTitle = React.forwardRef(function DialogTitle(
props: DialogTitleProps,
forwardedRef: React.ForwardedRef<HTMLParagraphElement>,
) {
const { render, className, id, ...other } = props;
const { render, className, id: idProp, ...other } = props;
const { setTitleElementId, open, modal } = useDialogRootContext();

const ownerState = {
open,
modal,
};

React.useEffect(() => {
const id = useId(idProp);

useEnhancedEffect(() => {
setTitleElementId(id);
return () => {
setTitleElementId(undefined);
Expand Down

0 comments on commit 0a0eb8e

Please sign in to comment.