Skip to content

Commit 08a6045

Browse files
authored
feat(secondary-icon-button): refactor hover and add theming (#770)
* feat(secondary-icon-button): refactor hover and add theming
1 parent 508027c commit 08a6045

File tree

2 files changed

+64
-41
lines changed

2 files changed

+64
-41
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,69 @@
11
import React from 'react';
22
import PropTypes from 'prop-types';
33
import { css } from '@emotion/core';
4-
import withMouseOverState from '../../../hocs/with-mouse-over-state';
4+
import styled from '@emotion/styled';
55
import filterAriaAttributes from '../../../utils/filter-aria-attributes';
66
import filterDataAttributes from '../../../utils/filter-data-attributes';
7+
import vars from '../../../../materials/custom-properties';
78
import AccessibleButton from '../accessible-button';
89

10+
const hoverStyle = props => {
11+
const overwrittenVars = {
12+
...vars,
13+
...props.theme,
14+
};
15+
16+
return css`
17+
&:hover {
18+
svg * {
19+
fill: ${overwrittenVars.colorPrimary};
20+
}
21+
}
22+
`;
23+
};
24+
25+
const fillStyle = props => {
26+
const overwrittenVars = {
27+
...vars,
28+
...props.theme,
29+
};
30+
return css`
31+
svg * {
32+
fill: ${props.isDisabled
33+
? overwrittenVars.colorNeutral60
34+
: overwrittenVars.colorSolid};
35+
}
36+
`;
37+
};
38+
39+
const IconContainer = styled.div`
40+
display: flex;
41+
align-items: center;
42+
justify-content: center;
43+
44+
${fillStyle}
45+
46+
${props => !props.isDisabled && hoverStyle}
47+
`;
48+
949
export const SecondaryIconButton = props => {
1050
const buttonAttributes = {
1151
'data-track-component': 'SecondaryIconButton',
1252
...filterAriaAttributes(props),
1353
...filterDataAttributes(props),
1454
};
15-
let iconTheme = 'black';
16-
if (props.isDisabled) iconTheme = 'grey';
17-
else if (props.isMouseOver) iconTheme = 'green';
1855
return (
19-
<div
20-
onMouseOver={props.handleMouseOver}
21-
onMouseOut={props.handleMouseOut}
22-
css={css`
23-
display: inline-flex;
24-
align-items: center;
25-
border: none;
26-
background: none;
27-
padding: 0;
28-
min-height: initial;
29-
cursor: pointer;
30-
`}
56+
<AccessibleButton
57+
type={props.type}
58+
buttonAttributes={buttonAttributes}
59+
label={props.label}
60+
onClick={props.onClick}
61+
isDisabled={props.isDisabled}
3162
>
32-
<AccessibleButton
33-
type={props.type}
34-
buttonAttributes={buttonAttributes}
35-
label={props.label}
36-
onClick={props.onClick}
37-
isDisabled={props.isDisabled}
38-
>
39-
<div
40-
css={css`
41-
display: flex;
42-
align-items: center;
43-
justify-content: center;
44-
`}
45-
>
46-
{React.cloneElement(props.icon, { theme: iconTheme })}
47-
</div>
48-
</AccessibleButton>
49-
</div>
63+
<IconContainer isDisabled={props.isDisabled}>
64+
{React.cloneElement(props.icon)}
65+
</IconContainer>
66+
</AccessibleButton>
5067
);
5168
};
5269

@@ -57,15 +74,11 @@ SecondaryIconButton.propTypes = {
5774
icon: PropTypes.element.isRequired,
5875
isDisabled: PropTypes.bool,
5976
onClick: PropTypes.func.isRequired,
60-
61-
// HoC
62-
isMouseOver: PropTypes.bool.isRequired,
63-
handleMouseOver: PropTypes.func.isRequired,
64-
handleMouseOut: PropTypes.func.isRequired,
6577
};
78+
6679
SecondaryIconButton.defaultProps = {
6780
type: 'button',
6881
isDisabled: false,
6982
};
7083

71-
export default withMouseOverState(SecondaryIconButton);
84+
export default SecondaryIconButton;

src/components/buttons/secondary-icon-button/secondary-icon-button.visualroute.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import React from 'react';
2+
import { ThemeProvider } from 'emotion-theming';
23
import { InformationIcon, SecondaryIconButton } from 'ui-kit';
34
import { Suite, Spec } from '../../../../test/percy';
45

56
export const routePath = '/secondary-icon-button';
67

7-
export const component = () => (
8+
export const component = ({ themes }) => (
89
<Suite>
910
<Spec label="regular">
1011
<SecondaryIconButton
@@ -21,5 +22,14 @@ export const component = () => (
2122
isDisabled={true}
2223
/>
2324
</Spec>
25+
<ThemeProvider theme={themes.darkTheme}>
26+
<Spec label="with custom (dark) theme">
27+
<SecondaryIconButton
28+
icon={<InformationIcon />}
29+
label="A label text"
30+
onClick={() => {}}
31+
/>
32+
</Spec>
33+
</ThemeProvider>
2434
</Suite>
2535
);

0 commit comments

Comments
 (0)