|
| 1 | +import React, { memo, forwardRef, useState, useEffect, useRef, ReactNode } from "react"; |
| 2 | +import { symToStr } from "tsafe/symToStr"; |
| 3 | +import { fr } from "./lib"; |
| 4 | +import { cx } from "./lib/tools/cx"; |
| 5 | +import { assert } from "tsafe/assert"; |
| 6 | +import type { Equals } from "tsafe"; |
| 7 | +import { useConstCallback } from "./lib/tools/powerhooks/useConstCallback"; |
| 8 | +import { createComponentI18nApi } from "./lib/i18n"; |
| 9 | +// We make users import dsfr.css so we don't need to import the scoped CSS |
| 10 | +// but in the future if we have a complete component coverage it |
| 11 | +// we could stop requiring users to import the hole CSS and only import on a |
| 12 | +// per component basis. |
| 13 | +import "./dsfr/component/notice/notice.css"; |
| 14 | + |
| 15 | +export type NoticeProps = { |
| 16 | + className?: string; |
| 17 | + classes?: Partial<Record<"root" | "title" | "close", string>>; |
| 18 | + title: NonNullable<ReactNode>; |
| 19 | +} & (NoticeProps.NonClosable | NoticeProps.Closable); |
| 20 | + |
| 21 | +export namespace NoticeProps { |
| 22 | + export type NonClosable = { |
| 23 | + isClosable?: false; |
| 24 | + isClosed?: undefined; |
| 25 | + onClose?: undefined; |
| 26 | + }; |
| 27 | + |
| 28 | + export type Closable = { |
| 29 | + isClosable: true; |
| 30 | + } & (Closable.Controlled | Closable.Uncontrolled); |
| 31 | + |
| 32 | + export namespace Closable { |
| 33 | + export type Controlled = { |
| 34 | + isClosed: boolean; |
| 35 | + onClose: () => void; |
| 36 | + }; |
| 37 | + |
| 38 | + export type Uncontrolled = { |
| 39 | + isClosed?: undefined; |
| 40 | + onClose?: () => void; |
| 41 | + }; |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +/** @see <https://react-dsfr-components.etalab.studio/?path=/docs/components-notice> */ |
| 46 | +export const Notice = memo( |
| 47 | + forwardRef<HTMLDivElement, NoticeProps>((props, ref) => { |
| 48 | + const { |
| 49 | + className, |
| 50 | + classes = {}, |
| 51 | + title, |
| 52 | + isClosable = false, |
| 53 | + isClosed: props_isClosed, |
| 54 | + onClose, |
| 55 | + ...rest |
| 56 | + } = props; |
| 57 | + |
| 58 | + assert<Equals<keyof typeof rest, never>>(); |
| 59 | + |
| 60 | + const [isClosed, setIsClosed] = useState(props_isClosed ?? false); |
| 61 | + |
| 62 | + const [buttonElement, setButtonElement] = useState<HTMLButtonElement | null>(null); |
| 63 | + |
| 64 | + const refShouldButtonGetFocus = useRef(false); |
| 65 | + const refShouldSetRole = useRef(false); |
| 66 | + |
| 67 | + useEffect(() => { |
| 68 | + if (props_isClosed === undefined) { |
| 69 | + return; |
| 70 | + } |
| 71 | + setIsClosed((isClosed: boolean) => { |
| 72 | + if (isClosed && !props_isClosed) { |
| 73 | + refShouldButtonGetFocus.current = true; |
| 74 | + refShouldSetRole.current = true; |
| 75 | + } |
| 76 | + |
| 77 | + return props_isClosed; |
| 78 | + }); |
| 79 | + }, [props_isClosed]); |
| 80 | + |
| 81 | + useEffect(() => { |
| 82 | + if (!refShouldButtonGetFocus.current) { |
| 83 | + return; |
| 84 | + } |
| 85 | + |
| 86 | + if (buttonElement === null) { |
| 87 | + //NOTE: This should not be reachable |
| 88 | + return; |
| 89 | + } |
| 90 | + |
| 91 | + refShouldButtonGetFocus.current = false; |
| 92 | + buttonElement.focus(); |
| 93 | + }, [buttonElement]); |
| 94 | + |
| 95 | + const onCloseButtonClick = useConstCallback(() => { |
| 96 | + if (props_isClosed === undefined) { |
| 97 | + //Uncontrolled |
| 98 | + setIsClosed(true); |
| 99 | + onClose?.(); |
| 100 | + } else { |
| 101 | + //Controlled |
| 102 | + onClose(); |
| 103 | + } |
| 104 | + }); |
| 105 | + |
| 106 | + const { t } = useTranslation(); |
| 107 | + |
| 108 | + if (isClosed) { |
| 109 | + return null; |
| 110 | + } |
| 111 | + |
| 112 | + return ( |
| 113 | + <div |
| 114 | + className={cx(fr.cx("fr-notice", `fr-notice--info`), classes.root, className)} |
| 115 | + {...(refShouldSetRole.current && { "role": "notice" })} |
| 116 | + ref={ref} |
| 117 | + {...rest} |
| 118 | + > |
| 119 | + <div className="fr-container"> |
| 120 | + <div className="fr-notice__body"> |
| 121 | + <p className={classes.title}>{title}</p> |
| 122 | + {/* TODO: Use our button once we have one */} |
| 123 | + {isClosable && ( |
| 124 | + <button |
| 125 | + ref={setButtonElement} |
| 126 | + className={cx(fr.cx("fr-btn--close", "fr-btn"), classes.close)} |
| 127 | + onClick={onCloseButtonClick} |
| 128 | + > |
| 129 | + {t("hide message")} |
| 130 | + </button> |
| 131 | + )} |
| 132 | + </div> |
| 133 | + </div> |
| 134 | + </div> |
| 135 | + ); |
| 136 | + }) |
| 137 | +); |
| 138 | + |
| 139 | +Notice.displayName = symToStr({ Notice }); |
| 140 | + |
| 141 | +export default Notice; |
| 142 | + |
| 143 | +const { useTranslation, addNoticeTranslations } = createComponentI18nApi({ |
| 144 | + "componentName": symToStr({ Notice }), |
| 145 | + "frMessages": { |
| 146 | + /* spell-checker: disable */ |
| 147 | + "hide message": "Masquer le message" |
| 148 | + /* spell-checker: enable */ |
| 149 | + } |
| 150 | +}); |
| 151 | + |
| 152 | +addNoticeTranslations({ |
| 153 | + "lang": "en", |
| 154 | + "messages": { |
| 155 | + "hide message": "Hide the message" |
| 156 | + } |
| 157 | +}); |
| 158 | + |
| 159 | +addNoticeTranslations({ |
| 160 | + "lang": "es", |
| 161 | + "messages": { |
| 162 | + /* spell-checker: disable */ |
| 163 | + "hide message": "Occultar el mesage" |
| 164 | + /* spell-checker: enable */ |
| 165 | + } |
| 166 | +}); |
| 167 | + |
| 168 | +export { addNoticeTranslations }; |
0 commit comments