Skip to content

Commit

Permalink
fix(HvDialog): blinking background. #3314 (#3350)
Browse files Browse the repository at this point in the history
  • Loading branch information
stephaniegaspar committed May 19, 2023
1 parent 03b04f3 commit 16747f4
Showing 1 changed file with 44 additions and 30 deletions.
74 changes: 44 additions & 30 deletions packages/core/src/components/Dialog/Dialog.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
import React, { useCallback, useRef } from "react";
import React, { useCallback, useMemo, useRef } from "react";
import { ClassNames } from "@emotion/react";
import MuiDialog, { DialogProps as MuiDialogProps } from "@mui/material/Dialog";
import { BackdropProps } from "@mui/material";
import isNil from "lodash/isNil";

import { Close } from "@hitachivantara/uikit-react-icons";
import { theme } from "@hitachivantara/uikit-styles";
import isNil from "lodash/isNil";
import { HvBaseProps } from "@core/types/generic";
import { StyledBackdrop, StyledClose, styles } from "./Dialog.styles";
import {
isKeypress,
keyboardCodes,
setId,
getFocusableList,
} from "@core/utils";
import { withTooltip } from "@core/hocs";
import dialogClasses, { HvDialogClasses } from "./dialogClasses";
import { useTheme } from "@core/hooks";
import { ClassNames } from "@emotion/react";
import dialogClasses, { HvDialogClasses } from "./dialogClasses";
import { StyledBackdrop, StyledClose, styles } from "./Dialog.styles";

export interface HvDialogProps
extends Omit<MuiDialogProps, "fullScreen" | "classes" | "open">,
Expand All @@ -40,6 +42,18 @@ export interface HvDialogProps
classes?: HvDialogClasses;
}

const DialogBackdrop = (backdropProps: BackdropProps) => {
const { activeTheme, selectedMode } = useTheme();
return (
<StyledBackdrop
$backColor={
activeTheme?.colors?.modes[selectedMode].atmo4 || theme.colors.atmo4
}
{...backdropProps}
/>
);
};

export const HvDialog = ({
classes,
className,
Expand All @@ -55,7 +69,7 @@ export const HvDialog = ({
}: HvDialogProps) => {
delete (others as any).fullScreen;

const { activeTheme, selectedMode, rootId } = useTheme();
const { rootId } = useTheme();

const focusableQueue = useRef<{
first?: HTMLElement;
Expand All @@ -65,17 +79,20 @@ export const HvDialog = ({
// Because the `disableBackdropClick` property was deprecated in MUI5
// and we want to maintain that functionality to the user we're wrapping
// the onClose call here to make that check.
const wrappedClose = (
event,
bypassValidation: boolean = false,
reason?: "escapeKeyDown" | "backdropClick"
) => {
if (bypassValidation) {
onClose?.(event, reason);
} else if (!disableBackdropClick) {
onClose?.(event, reason);
}
};
const wrappedClose = useCallback(
(
event,
bypassValidation: boolean = false,
reason?: "escapeKeyDown" | "backdropClick"
) => {
if (bypassValidation) {
onClose?.(event, reason);
} else if (!disableBackdropClick) {
onClose?.(event, reason);
}
},
[onClose]
);

const measuredRef = useCallback(
(node) => {
Expand Down Expand Up @@ -140,6 +157,15 @@ export const HvDialog = ({
? withTooltip(closeButtonDisplay, buttonTitle, "top")
: closeButtonDisplay;

const slots = useMemo<MuiDialogProps["slots"]>(
() => ({
backdrop: (backdropProps) => (
<DialogBackdrop open={open} onClick={wrappedClose} {...backdropProps} />
),
}),
[open, wrappedClose]
);

return (
<ClassNames>
{({ css, cx }) => (
Expand All @@ -154,19 +180,7 @@ export const HvDialog = ({
onKeyDown={keyDownHandler}
fullWidth
maxWidth={false}
slots={{
backdrop: (backdropProps) => (
<StyledBackdrop
open={open}
onClick={(event) => wrappedClose(event)}
$backColor={
activeTheme?.colors?.modes[selectedMode].atmo4 ||
theme.colors.atmo4
}
{...backdropProps}
/>
),
}}
slots={slots}
classes={{ container: css({ position: "relative" }) }}
BackdropProps={{
classes: {
Expand Down

0 comments on commit 16747f4

Please sign in to comment.