Skip to content

Commit e2ad4ab

Browse files
Conrad Chanmergify[bot]
authored andcommitted
feat(sidebar): Toggle button (#1268)
* feat(sidebar): Toggle button * chore: PR comments and add unit tests * chore: fix classes * chore: add e2e tests * chore: update README.md * chore: decouple resin tags from data testids
1 parent 6ebdcc6 commit e2ad4ab

File tree

12 files changed

+846
-141
lines changed

12 files changed

+846
-141
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
### Examples
2+
**Open**
3+
```js
4+
<SidebarToggleButton isOpen/>
5+
```
6+
**Closed**
7+
```js
8+
<SidebarToggleButton isOpen={false}/>
9+
```
10+
**When sidebar is on the left, change the direction**
11+
```js
12+
<SidebarToggleButton direction="left" isOpen/>
13+
```
14+
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// @flow
2+
import * as React from 'react';
3+
import classNames from 'classnames';
4+
import { injectIntl } from 'react-intl';
5+
6+
import IconHide from '../../icons/general/IconHide';
7+
import IconShow from '../../icons/general/IconShow';
8+
import PlainButton from '../plain-button';
9+
import Tooltip from '../tooltip';
10+
11+
import messages from '../../elements/common/messages';
12+
13+
import './SidebarToggleButton.scss';
14+
15+
const DIRECTION_LEFT = 'left';
16+
const DIRECTION_RIGHT = 'right';
17+
18+
type Props = {
19+
className?: string,
20+
direction?: string,
21+
isOpen: boolean,
22+
onClick?: Function,
23+
} & InjectIntlProvidedProps;
24+
25+
const SidebarToggleButton = ({
26+
className = '',
27+
direction = DIRECTION_RIGHT,
28+
intl,
29+
isOpen,
30+
onClick,
31+
...rest
32+
}: Props) => {
33+
const isCollapsed = !isOpen ? 'collapsed' : '';
34+
const intlMessage = isOpen ? messages.sidebarHide : messages.sidebarShow;
35+
const intlText = intl.formatMessage(intlMessage);
36+
const classes = classNames(className, 'bdl-SidebarToggleButton', {
37+
'bdl-is-collapsed': isCollapsed,
38+
});
39+
const tooltipPosition = direction === DIRECTION_LEFT ? 'middle-right' : 'middle-left';
40+
41+
const renderButton = () => {
42+
if (direction === DIRECTION_LEFT) {
43+
return isOpen ? <IconShow height={16} width={16} /> : <IconHide height={16} width={16} />;
44+
}
45+
return isOpen ? <IconHide height={16} width={16} /> : <IconShow height={16} width={16} />;
46+
};
47+
48+
return (
49+
<Tooltip position={tooltipPosition} text={intlText}>
50+
<PlainButton aria-label={intlText} className={classes} onClick={onClick} type="button" {...rest}>
51+
{renderButton()}
52+
</PlainButton>
53+
</Tooltip>
54+
);
55+
};
56+
57+
export default injectIntl(SidebarToggleButton);
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
@import '../../styles/variables';
2+
3+
.bdl-SidebarToggleButton {
4+
border-radius: $bdl-border-radius-size;
5+
margin: 0 3px;
6+
padding: 4px;
7+
8+
path {
9+
fill: $bdl-gray-50;
10+
}
11+
12+
&:not(.is-disabled):hover {
13+
background-color: $bdl-gray-05;
14+
}
15+
16+
&:not(.is-disabled):focus {
17+
border-color: #96a0a6;
18+
box-shadow: 0 1px 2px rgba(0, 0, 0, .1);
19+
}
20+
21+
&.bdl-is-collapsed,
22+
&.bdl-is-collapsed:hover {
23+
background-color: $bdl-box-blue;
24+
25+
path {
26+
fill: $white;
27+
}
28+
}
29+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import React from 'react';
2+
3+
import SidebarToggleButton from '..';
4+
5+
describe('components/sidebar-toggle-button/SidebarToggleButton', () => {
6+
test('should render correctly as open', () => {
7+
const wrapper = mount(<SidebarToggleButton isOpen />);
8+
9+
expect(wrapper).toMatchSnapshot();
10+
});
11+
12+
test('should render correctly as closed', () => {
13+
const wrapper = mount(<SidebarToggleButton isOpen={false} />);
14+
15+
expect(wrapper).toMatchSnapshot();
16+
});
17+
18+
test('should have the proper class when it is collapsed', () => {
19+
const wrapper = mount(<SidebarToggleButton isOpen={false} />);
20+
21+
expect(wrapper.find('PlainButton').hasClass('bdl-is-collapsed')).toBeTruthy();
22+
});
23+
24+
test('should render correctly as left oriented toggle when open', () => {
25+
const wrapper = mount(<SidebarToggleButton direction="left" isOpen />);
26+
27+
expect(wrapper).toMatchSnapshot();
28+
});
29+
30+
test('should render correctly as left oriented toggle when closed', () => {
31+
const wrapper = mount(<SidebarToggleButton direction="left" isOpen={false} />);
32+
33+
expect(wrapper).toMatchSnapshot();
34+
});
35+
});

0 commit comments

Comments
 (0)