@@ -76,9 +76,11 @@ export const Icon: ChakraComponent<
7676 variant = "default" ,
7777 ...rest
7878 } = props ;
79+ const hasChildSVG = children && ( children as JSX . Element ) . type === "svg" ;
7980 const styles = useStyleConfig ( "ReservoirIcon" , {
8081 align,
8182 color,
83+ hasChildSVG,
8284 iconRotation,
8385 size,
8486 variant,
@@ -90,16 +92,21 @@ export const Icon: ChakraComponent<
9092 title,
9193 ...rest ,
9294 } ;
93- let childSVG : any = null ;
9495
9596 // Component prop validation
96- if ( name && children ) {
97+ if ( name && hasChildSVG ) {
9798 console . warn (
9899 "NYPL Reservoir Icon: Pass in either a `name` prop or an `svg` element " +
99100 "child. Do not pass both."
100101 ) ;
101102 return null ;
102- } else if ( ! name && ! children ) {
103+ } else if ( children && ! hasChildSVG ) {
104+ console . warn (
105+ "NYPL Reservoir Icon: Only an `svg` element can be passed to the `Icon` " +
106+ "component as its child."
107+ ) ;
108+ return null ;
109+ } else if ( ! name && ! hasChildSVG ) {
103110 console . warn (
104111 "NYPL Reservoir Icon: Pass an icon `name` prop or an SVG child to " +
105112 "ensure an icon appears."
@@ -117,22 +124,13 @@ export const Icon: ChakraComponent<
117124 ) ;
118125 }
119126
120- // If no `name` prop was passed, we expect a child SVG element to be passed.
127+ // If all prop validation passed and no `name` prop was passed, we expect a
128+ // child SVG element was passed and render it accordingly.
121129 // Apply icon props to the SVG child.
122- if (
123- ( children as JSX . Element ) . type === "svg" ||
124- ( children as JSX . Element ) . props ?. type === "svg"
125- ) {
126- childSVG = React . cloneElement ( children as JSX . Element , {
127- ...iconProps ,
128- ref,
129- } ) ;
130- } else {
131- console . warn (
132- "NYPL Reservoir Icon: An `svg` element must be passed to the `Icon` " +
133- "component as its child."
134- ) ;
135- }
130+ const childSVG = React . cloneElement ( children as JSX . Element , {
131+ ...iconProps ,
132+ ref,
133+ } ) ;
136134
137135 return (
138136 < Box ref = { ref } __css = { styles } >
0 commit comments