Skip to content

Commit 8fc2b86

Browse files
authored
refactor(preview-sidebar): add modernized version of sidebar toggle button (#4390)
refactor(sidebar-toggle-button): update sidebar toggle button icon
1 parent 83917df commit 8fc2b86

File tree

6 files changed

+79
-38
lines changed

6 files changed

+79
-38
lines changed

flow-typed/npm/@box/blueprint-web-assets_vx.x.x.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ declare module '@box/blueprint-web-assets/icons/Medium' {
99
declare export var XMark: React$ComponentType<any>;
1010
declare export var Comment: React$ComponentType<any>;
1111
declare export var Metadata: React$ComponentType<any>;
12+
declare export var ChevronDown: React$ComponentType<any>;
13+
declare export var ChevronUp: React$ComponentType<any>;
14+
declare export var RightSidebarChevronClose: React$ComponentType<any>;
15+
declare export var RightSidebarChevronOpen: React$ComponentType<any>;
1216
}
1317

1418
declare module '@box/blueprint-web-assets/icons/MediumFilled' {

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@
125125
"@babel/template": "^7.24.7",
126126
"@babel/types": "^7.24.7",
127127
"@box/blueprint-web": "^12.98.0",
128-
"@box/blueprint-web-assets": "^4.80.4",
128+
"@box/blueprint-web-assets": "4.88.2",
129129
"@box/box-ai-agent-selector": "^0.53.0",
130130
"@box/box-ai-content-answers": "^0.139.0",
131131
"@box/box-item-type-selector": "^0.73.1",
@@ -297,7 +297,7 @@
297297
},
298298
"peerDependencies": {
299299
"@box/blueprint-web": "^12.98.0",
300-
"@box/blueprint-web-assets": "^4.80.4",
300+
"@box/blueprint-web-assets": "4.88.2",
301301
"@box/box-ai-agent-selector": "^0.53.0",
302302
"@box/box-ai-content-answers": "^0.139.0",
303303
"@box/box-item-type-selector": "^0.73.1",

src/components/sidebar-toggle-button/SidebarToggleButton.js

Lines changed: 48 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,15 @@ import * as React from 'react';
33
import classNames from 'classnames';
44
import { injectIntl } from 'react-intl';
55
import type { IntlShape } from 'react-intl';
6-
import { Tooltip as BPTooltip } from '@box/blueprint-web';
6+
7+
import { IconButton, Tooltip as BPTooltip, useBreakpoint, Breakpoint } from '@box/blueprint-web';
8+
import {
9+
ChevronDown,
10+
ChevronUp,
11+
RightSidebarChevronClose,
12+
RightSidebarChevronOpen,
13+
} from '@box/blueprint-web-assets/icons/Medium';
14+
715
import IconHide from '../../icons/general/IconHide';
816
import IconShow from '../../icons/general/IconShow';
917
import PlainButton from '../plain-button';
@@ -32,21 +40,20 @@ const SidebarToggleButton = ({
3240
onClick,
3341
...rest
3442
}: Props) => {
43+
const breakpoint = useBreakpoint();
3544
const { enabled: isPreviewModernizationEnabled } = useFeatureConfig('previewModernization');
45+
3646
const isCollapsed = !isOpen ? 'collapsed' : '';
3747
const intlMessage = isOpen ? messages.sidebarHide : messages.sidebarShow;
3848
const intlText = intl.formatMessage(intlMessage);
39-
const classes = classNames(className, 'bdl-SidebarToggleButton', {
49+
const classes = classNames(className, {
4050
'bdl-is-collapsed': isCollapsed,
51+
'bdl-SidebarToggleButton': !isPreviewModernizationEnabled,
4152
'bdl-SidebarToggleButton--modernized': isPreviewModernizationEnabled,
4253
});
43-
const tooltipPosition = direction === DIRECTION_LEFT ? 'middle-right' : 'middle-left';
44-
const renderButton = () => {
45-
if (direction === DIRECTION_LEFT) {
46-
return isOpen ? <IconShow height={16} width={16} /> : <IconHide height={16} width={16} />;
47-
}
48-
return isOpen ? <IconHide height={16} width={16} /> : <IconShow height={16} width={16} />;
49-
};
54+
55+
const isDirectionLeft = direction === DIRECTION_LEFT;
56+
const tooltipPosition = isDirectionLeft ? 'middle-right' : 'middle-left';
5057

5158
// Adding this to stop the mousedown event from being propagated up to box-annotations as
5259
// that will cause the active annotation to no longer be active which means that it will not be displayed.
@@ -56,26 +63,43 @@ const SidebarToggleButton = ({
5663
};
5764

5865
if (isPreviewModernizationEnabled) {
59-
const tooltipPositionModernized = direction === DIRECTION_LEFT ? DIRECTION_RIGHT : DIRECTION_LEFT;
66+
const isBelowMediumView = breakpoint <= Breakpoint.Medium;
67+
const tooltipPositionModernized = isDirectionLeft ? DIRECTION_RIGHT : DIRECTION_LEFT;
68+
69+
const renderModernizedIcon = () => {
70+
if (isBelowMediumView) {
71+
return isOpen ? ChevronDown : ChevronUp;
72+
}
73+
74+
if (isDirectionLeft) {
75+
return isOpen ? RightSidebarChevronOpen : RightSidebarChevronClose;
76+
}
77+
78+
return isOpen ? RightSidebarChevronClose : RightSidebarChevronOpen;
79+
};
6080

6181
return (
6282
<BPTooltip content={intlText} side={tooltipPositionModernized}>
63-
{/* Workaround to attach BP tooltip to legacy button, remove span when buttons are migrated to BP */}
64-
<span onMouseDown={mouseDownHandler} role="presentation">
65-
<PlainButton
66-
aria-label={intlText}
67-
className={classes}
68-
onClick={onClick}
69-
onMouseDown={mouseDownHandler}
70-
type="button"
71-
{...rest}
72-
>
73-
{renderButton()}
74-
</PlainButton>
75-
</span>
83+
<IconButton
84+
aria-label={intlText}
85+
className={classes}
86+
icon={renderModernizedIcon()}
87+
onClick={onClick}
88+
onMouseDown={mouseDownHandler}
89+
size={isBelowMediumView ? 'small' : 'large'}
90+
{...rest}
91+
/>
7692
</BPTooltip>
7793
);
7894
}
95+
96+
const renderIcon = () => {
97+
if (isDirectionLeft) {
98+
return isOpen ? <IconShow height={16} width={16} /> : <IconHide height={16} width={16} />;
99+
}
100+
return isOpen ? <IconHide height={16} width={16} /> : <IconShow height={16} width={16} />;
101+
};
102+
79103
return (
80104
<Tooltip position={tooltipPosition} text={intlText}>
81105
<PlainButton
@@ -86,7 +110,7 @@ const SidebarToggleButton = ({
86110
type="button"
87111
{...rest}
88112
>
89-
{renderButton()}
113+
{renderIcon()}
90114
</PlainButton>
91115
</Tooltip>
92116
);

src/components/sidebar-toggle-button/SidebarToggleButton.scss

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,16 @@
3030
}
3131
}
3232

33+
.bdl-SidebarToggleButton--modernized {
34+
// See: src/elements/content-sidebar/ContentSidebar.scss
35+
// Establish a stacking context so the button appears above other positioned elements
36+
// within ContentSidebar's z-index: 5 stacking context, preventing it from being blocked.
37+
position: relative;
38+
}
39+
3340
.bcs-SidebarNav--modernized {
34-
.bdl-SidebarToggleButton {
35-
&:not(.bdl-is-disabled):hover svg,
36-
&:not(.is-disabled):hover svg,
37-
&:not(.bdl-is-disabled):focus svg,
38-
&:not(.is-disabled):focus svg,
39-
&.bdl-is-collapsed svg,
40-
&.bdl-is-collapsed:hover svg {
41+
.bdl-SidebarToggleButton--modernized {
42+
&:hover svg {
4143
transform: scale(1.09);
4244
transition: transform 150ms;
4345
}

src/components/sidebar-toggle-button/__tests__/SidebarToggleButton.test.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,15 @@ describe('components/sidebar-toggle-button/SidebarToggleButton', () => {
6565
wrapperProps: { features: { previewModernization: { enabled: isPreviewModernizationEnabled } } },
6666
});
6767
const button = screen.getByRole('button');
68-
expect(button).toHaveClass('bdl-SidebarToggleButton');
69-
const isModernized = button.classList.contains('bdl-SidebarToggleButton--modernized');
70-
expect(isModernized).toBe(isPreviewModernizationEnabled);
68+
69+
if (isPreviewModernizationEnabled) {
70+
expect(button).toHaveClass('bdl-SidebarToggleButton--modernized');
71+
expect(button).not.toHaveClass('bdl-SidebarToggleButton');
72+
} else {
73+
expect(button).toHaveClass('bdl-SidebarToggleButton');
74+
expect(button).not.toHaveClass('bdl-SidebarToggleButton--modernized');
75+
}
76+
7177
fireEvent.click(button);
7278
expect(onClick).toHaveBeenCalled();
7379
},

yarn.lock

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1419,7 +1419,12 @@
14191419
resolved "https://registry.yarnpkg.com/@bcoe/v8-coverage/-/v8-coverage-0.2.3.tgz#75a2e8b51cb758a7553d6804a5932d7aace75c39"
14201420
integrity sha512-0hYQ8SB4Db5zvZB4axdMHGwEaQjkZzFjQiN9LVYvIFB2nSUHW9tYpxWriPrWDASIxiaXax83REcLxuSdnGPZtw==
14211421

1422-
"@box/blueprint-web-assets@^4.80.0", "@box/blueprint-web-assets@^4.80.4":
1422+
1423+
version "4.88.2"
1424+
resolved "https://registry.yarnpkg.com/@box/blueprint-web-assets/-/blueprint-web-assets-4.88.2.tgz#cb98f14dcd5a072d7c97be31030485cf9219478e"
1425+
integrity sha512-TAtiYWvudxj3i5GAa4NsiHzlF9WcIARBbp1dCxgwsFP652NEamo/3SH2wyhcc/5u1bJJLftxe5xsTv6a7dpj0w==
1426+
1427+
"@box/blueprint-web-assets@^4.80.0":
14231428
version "4.80.4"
14241429
resolved "https://registry.yarnpkg.com/@box/blueprint-web-assets/-/blueprint-web-assets-4.80.4.tgz#3db74e9c1006d2eb86ddc7e4caa23a4f0376cf92"
14251430
integrity sha512-zXIUfIC+RgzpQkA6KtLi0M5gtbXjNO3orWPNsVaJgEbYjrKahC4AejL2j4/XNaxt7AO0Xvz6vRr1Zjn1Byu4YA==
@@ -14400,7 +14405,7 @@ node-fetch@^2.6.12, node-fetch@^2.6.7:
1440014405
dependencies:
1440114406
whatwg-url "^5.0.0"
1440214407

14403-
node-forge@1.3.2, node-forge@^1:
14408+
node-forge@^1:
1440414409
version "1.3.2"
1440514410
resolved "https://registry.yarnpkg.com/node-forge/-/node-forge-1.3.2.tgz#d0d2659a26eef778bf84d73e7f55c08144ee7750"
1440614411
integrity sha512-6xKiQ+cph9KImrRh0VsjH2d8/GXA4FIMlgU4B757iI1ApvcyA9VlouP0yZJha01V+huImO+kKMU7ih+2+E14fw==

0 commit comments

Comments
 (0)