|
| 1 | +import React, { memo, ForwardedRef } from "react"; |
| 2 | +import { fr } from "./fr"; |
| 3 | +import { cx } from "./tools/cx"; |
| 4 | +import type { ReactNode } from "react"; |
| 5 | +import { Equals, assert } from "tsafe"; |
| 6 | +import { createComponentI18nApi } from "./i18n"; |
| 7 | +import type { FrIconClassName, RiIconClassName } from "./fr/generatedFromCss/classNames"; |
| 8 | +import { symToStr } from "tsafe/symToStr"; |
| 9 | +import Button, { ButtonProps } from "./Button"; |
| 10 | + |
| 11 | +export function createOpenModalButtonProps(id: string): ButtonProps { |
| 12 | + return { |
| 13 | + "onClick": () => { |
| 14 | + /* nothing */ |
| 15 | + }, |
| 16 | + "nativeButtonProps": { |
| 17 | + "aria-controls": id, |
| 18 | + "data-fr-opened": "false" |
| 19 | + } |
| 20 | + }; |
| 21 | +} |
| 22 | + |
| 23 | +export type ModalProps = ModalProps.Common & |
| 24 | + (ModalProps.WithTitleIcon | ModalProps.WithoutTitleIcon) & |
| 25 | + (ModalProps.WithAction | ModalProps.WithoutAction); |
| 26 | +export namespace ModalProps { |
| 27 | + export type Common = { |
| 28 | + className?: string; |
| 29 | + size?: "small" | "medium" | "large"; |
| 30 | + id: string; |
| 31 | + title: ReactNode; |
| 32 | + children: ReactNode; |
| 33 | + concealingBackdrop?: boolean; |
| 34 | + topAnchor?: boolean; |
| 35 | + ref?: ForwardedRef<HTMLDialogElement>; |
| 36 | + }; |
| 37 | + |
| 38 | + export type WithTitleIcon = { |
| 39 | + iconId: FrIconClassName | RiIconClassName; |
| 40 | + }; |
| 41 | + |
| 42 | + export type WithoutTitleIcon = { |
| 43 | + iconId?: never; |
| 44 | + }; |
| 45 | + |
| 46 | + export type WithAction = { |
| 47 | + actionArea: [ButtonProps, ...ButtonProps[]]; |
| 48 | + }; |
| 49 | + |
| 50 | + export type WithoutAction = { |
| 51 | + actionArea?: never; |
| 52 | + }; |
| 53 | +} |
| 54 | + |
| 55 | +/** @see <> add doc url */ |
| 56 | +export const Modal = memo((props: ModalProps) => { |
| 57 | + const { |
| 58 | + className, |
| 59 | + id, |
| 60 | + title, |
| 61 | + children, |
| 62 | + concealingBackdrop = true, |
| 63 | + topAnchor = false, |
| 64 | + ref, |
| 65 | + iconId, |
| 66 | + actionArea, |
| 67 | + size = "medium", |
| 68 | + ...rest |
| 69 | + } = props; |
| 70 | + |
| 71 | + assert<Equals<keyof typeof rest, never>>(); |
| 72 | + |
| 73 | + const { t } = useTranslation(); |
| 74 | + |
| 75 | + return ( |
| 76 | + <dialog |
| 77 | + aria-labelledby="fr-modal-title-modal-1" |
| 78 | + role="dialog" |
| 79 | + id={id} |
| 80 | + className={cx(fr.cx("fr-modal", topAnchor && "fr-modal--top"), className)} |
| 81 | + ref={ref} |
| 82 | + data-fr-concealing-backdrop={concealingBackdrop} |
| 83 | + > |
| 84 | + <div className={fr.cx("fr-container", "fr-container--fluid", "fr-container-md")}> |
| 85 | + <div className={fr.cx("fr-grid-row", "fr-grid-row--center")}> |
| 86 | + <div |
| 87 | + className={(() => { |
| 88 | + switch (size) { |
| 89 | + case "large": |
| 90 | + return fr.cx("fr-col-12", "fr-col-md-10", "fr-col-lg-8"); |
| 91 | + case "small": |
| 92 | + return fr.cx("fr-col-12", "fr-col-md-6", "fr-col-lg-4"); |
| 93 | + case "medium": |
| 94 | + return fr.cx("fr-col-12", "fr-col-md-8", "fr-col-lg-6"); |
| 95 | + } |
| 96 | + })()} |
| 97 | + > |
| 98 | + <div className={fr.cx("fr-modal__body")}> |
| 99 | + <div className={fr.cx("fr-modal__header")}> |
| 100 | + <button |
| 101 | + className={fr.cx("fr-link--close", "fr-link")} |
| 102 | + title={t("close")} |
| 103 | + aria-controls={id} |
| 104 | + > |
| 105 | + {t("close")} |
| 106 | + </button> |
| 107 | + </div> |
| 108 | + <div className={fr.cx("fr-modal__content")}> |
| 109 | + <h1 |
| 110 | + id="fr-modal-title-modal-1" |
| 111 | + className={fr.cx("fr-modal__title")} |
| 112 | + > |
| 113 | + {iconId !== undefined && ( |
| 114 | + <span className={fr.cx(iconId, "fr-fi--lg")} /> |
| 115 | + )} |
| 116 | + {title} |
| 117 | + </h1> |
| 118 | + {children} |
| 119 | + </div> |
| 120 | + {actionArea !== undefined && ( |
| 121 | + <div className="fr-modal__footer"> |
| 122 | + <ul |
| 123 | + className={fr.cx( |
| 124 | + "fr-btns-group", |
| 125 | + "fr-btns-group--right", |
| 126 | + "fr-btns-group--inline-reverse", |
| 127 | + "fr-btns-group--inline-lg", |
| 128 | + "fr-btns-group--icon-left" |
| 129 | + )} |
| 130 | + > |
| 131 | + {actionArea.map((buttonProps, i) => { |
| 132 | + const { priority: priorityProps, ...rest } = |
| 133 | + buttonProps; |
| 134 | + const priority = i === 0 ? "primary" : priorityProps; |
| 135 | + return ( |
| 136 | + <li key={i}> |
| 137 | + <Button priority={priority} {...rest} /> |
| 138 | + </li> |
| 139 | + ); |
| 140 | + })} |
| 141 | + </ul> |
| 142 | + </div> |
| 143 | + )} |
| 144 | + </div> |
| 145 | + </div> |
| 146 | + </div> |
| 147 | + </div> |
| 148 | + </dialog> |
| 149 | + ); |
| 150 | +}); |
| 151 | + |
| 152 | +Modal.displayName = symToStr({ Modal }); |
| 153 | + |
| 154 | +const { useTranslation, addModalTranslations } = createComponentI18nApi({ |
| 155 | + "componentName": symToStr({ Modal }), |
| 156 | + "frMessages": { |
| 157 | + "close": "Fermer" |
| 158 | + } |
| 159 | +}); |
| 160 | + |
| 161 | +addModalTranslations({ |
| 162 | + "lang": "en", |
| 163 | + "messages": { |
| 164 | + "close": "Close" |
| 165 | + } |
| 166 | +}); |
| 167 | + |
| 168 | +addModalTranslations({ |
| 169 | + "lang": "es", |
| 170 | + "messages": { |
| 171 | + "close": "Cerrar" |
| 172 | + } |
| 173 | +}); |
| 174 | + |
| 175 | +export { addModalTranslations }; |
0 commit comments