1- import React , { forwardRef , HTMLAttributes } from "react" ;
1+ import React , { forwardRef , HTMLAttributes , ImgHTMLAttributes } from "react" ;
22import cn from "classnames" ;
3- import { bem } from "@react-md/utils" ;
3+ import { bem , PropsWithRef } from "@react-md/utils" ;
4+
5+ type ImgAttributes = ImgHTMLAttributes < HTMLImageElement > ;
46
57export interface AvatarProps extends HTMLAttributes < HTMLSpanElement > {
68 /**
@@ -21,6 +23,29 @@ export interface AvatarProps extends HTMLAttributes<HTMLSpanElement> {
2123 */
2224 alt ?: string ;
2325
26+ /**
27+ * An optional `referrerPolicy` to provide to the `<img>` element if the `src`
28+ * or `imgProps` props are provided.
29+ *
30+ * @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/img#attr-referrerpolicy
31+ *
32+ * @since 2.2.0
33+ */
34+ referrerPolicy ?: ImgAttributes [ "referrerPolicy" ] ;
35+
36+ /**
37+ * An optional object of image props and ref that can be used to create an
38+ * image within the `Avatar`. This can be useful to add a custom `style`
39+ * or`className` to the `<img>` element if that additional customization is
40+ * needed.
41+ *
42+ * Note: The values in this object will override the `src`, `alt`, and
43+ * `referrerPolicy` root level avatar props if they exist on this object.
44+ *
45+ * @since 2.2.0
46+ */
47+ imgProps ?: PropsWithRef < ImgAttributes , HTMLImageElement > ;
48+
2449 /**
2550 * An optional color to apply to the avatar. This will apply a className of
2651 * `rmd-avatar--${color}`, so only the keys from the `$rmd-avatar-colors` Map
@@ -40,12 +65,29 @@ const block = bem("rmd-avatar");
4065 * avatar more unique.
4166 */
4267const Avatar = forwardRef < HTMLSpanElement , AvatarProps > ( function Avatar (
43- { className, children, src, alt = "" , color = "" , ...props } ,
68+ {
69+ className,
70+ children,
71+ src,
72+ alt = "" ,
73+ color = "" ,
74+ imgProps,
75+ referrerPolicy,
76+ ...props
77+ } ,
4478 ref
4579) {
4680 let img ;
47- if ( src ) {
48- img = < img src = { src } alt = { alt } className = { block ( "image" ) } /> ;
81+ if ( src || imgProps ) {
82+ img = (
83+ < img
84+ src = { src }
85+ alt = { alt }
86+ referrerPolicy = { referrerPolicy }
87+ { ...imgProps }
88+ className = { cn ( block ( "image" ) , imgProps ?. className ) }
89+ />
90+ ) ;
4991 }
5092
5193 return (
@@ -70,6 +112,10 @@ if (process.env.NODE_ENV !== "production") {
70112 color : PropTypes . string ,
71113 className : PropTypes . string ,
72114 children : PropTypes . node ,
115+ // Note: The MDN website has a lot more values, but this is what Typescript
116+ // says is valid at the time of writing this
117+ referrerPolicy : PropTypes . oneOf ( [ "no-referrer" , "origin" , "unsafe-url" ] ) ,
118+ imgProps : PropTypes . object ,
73119 } ;
74120 } catch ( e ) { }
75121}
0 commit comments