|
| 1 | +import React, { memo, forwardRef, ReactNode } from "react"; |
| 2 | +import { symToStr } from "tsafe/symToStr"; |
| 3 | +import { assert } from "tsafe/assert"; |
| 4 | +import type { Equals } from "tsafe"; |
| 5 | + |
| 6 | +import { FrClassName } from "./lib/generatedFromCss/classNames"; |
| 7 | +import { cx } from "./lib/tools/cx"; |
| 8 | +import { fr } from "./lib"; |
| 9 | + |
| 10 | +import "./dsfr/component/quote/quote.css"; |
| 11 | + |
| 12 | +export type QuoteProps = { |
| 13 | + className?: string; |
| 14 | + text: ReactNode; |
| 15 | + author?: ReactNode; |
| 16 | + source?: ReactNode; |
| 17 | + sourceUrl?: string; |
| 18 | + imageUrl?: string; |
| 19 | + size?: "medium" | "large" | "xlarge"; |
| 20 | + accentColor?: QuoteProps.AccentColor; |
| 21 | + classes?: Partial<Record<"root" | "author" | "source" | "image" | "imageTag" | "text", string>>; |
| 22 | +}; |
| 23 | + |
| 24 | +export namespace QuoteProps { |
| 25 | + type ExtractAccentColor<FrClassName> = FrClassName extends `fr-quote--${infer AccentColor}` |
| 26 | + ? AccentColor |
| 27 | + : never; |
| 28 | + |
| 29 | + export type AccentColor = ExtractAccentColor<FrClassName>; |
| 30 | +} |
| 31 | + |
| 32 | +/** @see <https://react-dsfr-components.etalab.studio/?path=/docs/components-quote> */ |
| 33 | +export const Quote = memo( |
| 34 | + forwardRef<HTMLDivElement, QuoteProps>((props, ref) => { |
| 35 | + const { |
| 36 | + className, |
| 37 | + text, |
| 38 | + author, |
| 39 | + source, |
| 40 | + sourceUrl, |
| 41 | + imageUrl, |
| 42 | + size = "xlarge", |
| 43 | + accentColor, |
| 44 | + classes = {}, |
| 45 | + ...rest |
| 46 | + } = props; |
| 47 | + |
| 48 | + assert<Equals<keyof typeof rest, never>>(); |
| 49 | + |
| 50 | + return ( |
| 51 | + <figure |
| 52 | + className={cx( |
| 53 | + fr.cx("fr-quote"), |
| 54 | + imageUrl && fr.cx("fr-quote--column"), |
| 55 | + accentColor && `fr-quote--${accentColor}`, |
| 56 | + classes.root, |
| 57 | + className |
| 58 | + )} |
| 59 | + ref={ref} |
| 60 | + > |
| 61 | + <blockquote cite={sourceUrl}> |
| 62 | + <p |
| 63 | + className={cx( |
| 64 | + size === "large" && fr.cx("fr-text--lg"), |
| 65 | + size === "medium" && fr.cx("fr-text--md"), |
| 66 | + classes.text |
| 67 | + )} |
| 68 | + > |
| 69 | + « {text} » |
| 70 | + </p> |
| 71 | + </blockquote> |
| 72 | + <figcaption> |
| 73 | + {author !== undefined && ( |
| 74 | + <p className={cx(fr.cx("fr-quote__author"), classes.author)}>{author}</p> |
| 75 | + )} |
| 76 | + {source !== undefined && ( |
| 77 | + <ul className={cx(fr.cx("fr-quote__source"), classes.source)}>{source}</ul> |
| 78 | + )} |
| 79 | + {imageUrl !== undefined && ( |
| 80 | + <div className={cx("fr-quote__image", classes.image)}> |
| 81 | + <img |
| 82 | + src={imageUrl} |
| 83 | + className={cx(fr.cx("fr-responsive-img"), classes.imageTag)} |
| 84 | + alt="" |
| 85 | + /> |
| 86 | + </div> |
| 87 | + )} |
| 88 | + </figcaption> |
| 89 | + </figure> |
| 90 | + ); |
| 91 | + }) |
| 92 | +); |
| 93 | + |
| 94 | +Quote.displayName = symToStr({ Quote }); |
| 95 | + |
| 96 | +export default Quote; |
0 commit comments