Skip to content

Commit 48b3705

Browse files
committed
client/common/icons: add types & update snapshot test
1 parent 79d584d commit 48b3705

File tree

2 files changed

+30
-18
lines changed

2 files changed

+30
-18
lines changed

client/common/icons.tsx

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,25 @@ import Filter from '../images/filter.svg';
2626
import Cross from '../images/cross.svg';
2727
import Copy from '../images/copy.svg';
2828

29+
export interface IconColors {
30+
default?: string;
31+
hover?: string;
32+
}
33+
34+
export interface IconProps extends React.SVGProps<SVGSVGElement> {
35+
'aria-label'?: string;
36+
Icon?: IconColors;
37+
}
38+
2939
// HOC that adds the right web accessibility props
3040
// https://www.scottohara.me/blog/2019/05/22/contextual-images-svgs-and-a11y.html
3141

3242
// could also give these a default size, color, etc. based on the theme
3343
// Need to add size to these - like small icon, medium icon, large icon. etc.
34-
function withLabel(SvgComponent) {
35-
const StyledIcon = styled(SvgComponent)`
44+
function withLabel(
45+
SvgComponent: React.ComponentType<React.SVGProps<SVGSVGElement>>
46+
) {
47+
const StyledIcon = styled(SvgComponent)<IconProps>`
3648
&&& {
3749
color: ${(props) => props.Icon?.default};
3850
& g,
@@ -53,27 +65,27 @@ function withLabel(SvgComponent) {
5365
}
5466
`;
5567

56-
const Icon = (props) => {
57-
const { 'aria-label': ariaLabel } = props;
68+
// Necessary because styled components inject a different type for the ref prop
69+
type StyledIconProps = Omit<
70+
React.ComponentProps<typeof StyledIcon>,
71+
'ref'
72+
> & {
73+
ref?: React.Ref<SVGSVGElement>;
74+
};
75+
76+
const Icon = (props: StyledIconProps) => {
77+
const { 'aria-label': ariaLabel, ...rest } = props;
5878
if (ariaLabel) {
5979
return (
6080
<StyledIcon
61-
{...props}
81+
{...rest}
6282
aria-label={ariaLabel}
6383
role="img"
6484
focusable="false"
6585
/>
6686
);
6787
}
68-
return <StyledIcon {...props} aria-hidden focusable="false" />;
69-
};
70-
71-
Icon.propTypes = {
72-
'aria-label': PropTypes.string
73-
};
74-
75-
Icon.defaultProps = {
76-
'aria-label': null
88+
return <StyledIcon {...rest} aria-hidden focusable="false" />;
7789
};
7890

7991
return Icon;

client/modules/IDE/components/Header/__snapshots__/Nav.unit.test.jsx.snap

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ exports[`Nav renders dashboard version for mobile 1`] = `
351351
>
352352
<test-file-stub
353353
aria-hidden="true"
354-
classname="icons__StyledIcon-sc-xmer15-0 kjSZIe"
354+
classname="icons__StyledIcon-sc-1rmv3zl-0 bKwKVB"
355355
focusable="false"
356356
/>
357357
</button>
@@ -367,7 +367,7 @@ exports[`Nav renders dashboard version for mobile 1`] = `
367367
>
368368
<test-file-stub
369369
aria-hidden="true"
370-
classname="icons__StyledIcon-sc-xmer15-0 kjSZIe"
370+
classname="icons__StyledIcon-sc-1rmv3zl-0 bKwKVB"
371371
focusable="false"
372372
/>
373373
</button>
@@ -971,7 +971,7 @@ exports[`Nav renders editor version for mobile 1`] = `
971971
>
972972
<test-file-stub
973973
aria-hidden="true"
974-
classname="icons__StyledIcon-sc-xmer15-0 kjSZIe"
974+
classname="icons__StyledIcon-sc-1rmv3zl-0 bKwKVB"
975975
focusable="false"
976976
/>
977977
</button>
@@ -987,7 +987,7 @@ exports[`Nav renders editor version for mobile 1`] = `
987987
>
988988
<test-file-stub
989989
aria-hidden="true"
990-
classname="icons__StyledIcon-sc-xmer15-0 kjSZIe"
990+
classname="icons__StyledIcon-sc-1rmv3zl-0 bKwKVB"
991991
focusable="false"
992992
/>
993993
</button>

0 commit comments

Comments
 (0)