💄(react) make modal backdrop color customisable

As we cannot user CSS vars into backdrop pseudo element, we decide to
create a dom element to simulate the modal backdrop.

Resolve #257
This commit is contained in:
jbpenrath
2024-02-15 11:30:39 +01:00
committed by Jean-Baptiste PENRATH
parent fac203314f
commit be1e2e2614
9 changed files with 60 additions and 30 deletions

View File

@@ -253,6 +253,7 @@ Here a the custom design tokens defined by the Toast.
| Token | Description |
|--------------- |----------------------------- |
| background-color | Default background color |
| backdrop-color | Default backdrop color |
| border-radius | Border radius of the modal |
| border-color | Border color of the modal |
| box-shadow | Box shadow of the modal |

View File

@@ -11,10 +11,26 @@
&::backdrop {
// ::backdrop does not inherit from its element so CSS vars does not work.
// ( https://stackoverflow.com/a/77393321 ) So we need to use the color directly.
// In the future we will maybe be able to use the following:
// background: var(--c--components--modal--backdrop-color);
background: #0C1A2B99;
// ( https://stackoverflow.com/a/77393321 )
// So for now, we hide the backdrop element and use a dom element to create the backdrop.
visibility: hidden;
}
&__backdrop {
position: fixed;
inset: 0;
background-color: var(--c--components--modal--backdrop-color);
opacity: 0;
animation: backdrop-fade-in 0.2s ease-out forwards;
}
@keyframes backdrop-fade-in {
from {
opacity: 0;
}
to {
opacity: 1;
}
}
&__title {

View File

@@ -104,31 +104,36 @@ export const Modal = (props: ModalProps) => {
return (
<>
{createPortal(
<dialog
ref={ref}
className={classNames("c__modal", "c__modal--" + props.size)}
>
{!props.hideCloseButton && !props.preventClose && (
<div className="c__modal__close">
<Button
icon={<span className="material-icons">close</span>}
color="tertiary-text"
size="nano"
onClick={() => props.onClose()}
/>
</div>
)}
{props.titleIcon && (
<div className="c__modal__title-icon">{props.titleIcon}</div>
)}
{props.title && <div className="c__modal__title">{props.title}</div>}
<>
<div aria-hidden={true} className="c__modal__backdrop" />
<dialog
ref={ref}
className={classNames("c__modal", "c__modal--" + props.size)}
>
{!props.hideCloseButton && !props.preventClose && (
<div className="c__modal__close">
<Button
icon={<span className="material-icons">close</span>}
color="tertiary-text"
size="nano"
onClick={() => props.onClose()}
/>
</div>
)}
{props.titleIcon && (
<div className="c__modal__title-icon">{props.titleIcon}</div>
)}
{props.title && (
<div className="c__modal__title">{props.title}</div>
)}
{/* eslint-disable-next-line jsx-a11y/no-noninteractive-tabindex */}
<div className="c__modal__content" tabIndex={0}>
{props.children}
</div>
<ModalFooter {...props} />
</dialog>,
{/* eslint-disable-next-line jsx-a11y/no-noninteractive-tabindex */}
<div className="c__modal__content" tabIndex={0}>
{props.children}
</div>
<ModalFooter {...props} />
</dialog>
</>,
document.getElementById("c__modals-portal")!,
)}
</>

View File

@@ -3,6 +3,7 @@ import { DefaultTokens } from "@openfun/cunningham-tokens";
export const tokens = (defaults: DefaultTokens) => {
return {
"background-color": defaults.theme.colors["greyscale-000"],
"backdrop-color": "#0C1A2B99",
"border-radius": "4px",
"border-color": defaults.theme.colors["greyscale-300"],
"box-shadow": "0px 1px 2px 0px #0C1A2B4D",

View File

@@ -120,6 +120,7 @@
--c--components--toast--icon-size: 19px;
--c--components--toast--progress-bar-height: 3px;
--c--components--modal--background-color: var(--c--theme--colors--greyscale-000);
--c--components--modal--backdrop-color: #0C1A2B99;
--c--components--modal--border-radius: 4px;
--c--components--modal--border-color: var(--c--theme--colors--greyscale-300);
--c--components--modal--box-shadow: 0px 1px 2px 0px #0C1A2B4D;

File diff suppressed because one or more lines are too long

View File

@@ -145,6 +145,7 @@ $themes: (
),
'modal': (
'background-color': #FFFFFF,
'backdrop-color': #0C1A2B99,
'border-radius': 4px,
'border-color': #E7E8EA,
'box-shadow': 0px 1px 2px 0px #0C1A2B4D,

File diff suppressed because one or more lines are too long