Skip to content

Commit dba459a

Browse files
committed
feat: Stops icons from throwing when passed empty icons in favour of warning
1 parent fa1a366 commit dba459a

File tree

6 files changed

+40
-17
lines changed

6 files changed

+40
-17
lines changed

.changeset/fluffy-singers-sneeze.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@autoguru/overdrive': patch
3+
---
4+
5+
Icon: Not throwing when passed null icons

packages/overdrive/lib/components/Icon/Icon.tsx

+23-17
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { FunctionComponent, ReactElement, SVGAttributes } from 'react';
33
import * as React from 'react';
44
import { cloneElement } from 'react';
55

6+
import { useNullCheck } from '../../hooks';
67
import { resolveResponsiveStyle } from '../../utils/resolveResponsiveProps';
78
import { ResponsiveProp } from '../../utils/responsiveProps.css';
89
import type { BoxStyleProps } from '../Box';
@@ -22,22 +23,27 @@ export const Icon: FunctionComponent<Props> = ({
2223
icon,
2324
size = 'small',
2425
display = 'block',
25-
}) => (
26-
<Box
27-
is="i"
28-
display={display}
29-
className={[resolveResponsiveStyle(size, styles.size), className]}
30-
role="presentation"
31-
>
32-
{cloneElement(icon, {
33-
className: useBoxStyles({
34-
is: 'svg',
35-
display: 'block',
36-
width: 'full',
37-
height: 'full',
38-
}),
39-
})}
40-
</Box>
41-
);
26+
}) => {
27+
useNullCheck(icon, 'Icon component received an empty icon prop.');
28+
return (
29+
<Box
30+
is="i"
31+
display={display}
32+
className={[resolveResponsiveStyle(size, styles.size), className]}
33+
role="presentation"
34+
>
35+
{icon
36+
? cloneElement(icon, {
37+
className: useBoxStyles({
38+
is: 'svg',
39+
display: 'block',
40+
width: 'full',
41+
height: 'full',
42+
}),
43+
})
44+
: '⬤'}
45+
</Box>
46+
);
47+
};
4248

4349
export default Icon;

packages/overdrive/lib/components/Icon/stories.tsx

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ const iconOptions = {
2727
PlusIcon,
2828
StarIcon,
2929
CheckIcon,
30+
empty: null
3031
};
3132

3233
export default {

packages/overdrive/lib/hooks/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ export * from './useResponsiveValue';
66
export * from './useWindowHeightFill';
77
export * from './useWindowScrollLock';
88
export * from './useMedia';
9+
export * from './useNullCheck';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { useNullCheck } from './useNullCheck';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { useEffect } from 'react';
2+
3+
export const useNullCheck = <T>(value: T, message: string) => {
4+
useEffect(() => {
5+
if (!value) {
6+
console.warn(`%c${message}`, 'color: orange');
7+
}
8+
}, [value, message]);
9+
};

0 commit comments

Comments
 (0)