Skip to content

Commit 15ffbaa

Browse files
committed
chore: enable correct sizing with asymmetric layouts
1 parent 50cbda3 commit 15ffbaa

File tree

7 files changed

+106
-43
lines changed

7 files changed

+106
-43
lines changed

polaris-react/src/components/BulkActions/BulkActions.scss

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ $bulk-actions-button-stacking-order: (
1111
align-items: center;
1212
flex-wrap: wrap;
1313
opacity: 0;
14-
width: 90vw;
14+
// width: 90vw;
15+
width: 100%;
1516
justify-content: center;
1617
transition: var(--p-duration-250) var(--p-ease-in);
1718
transform: translateY(120px);

polaris-react/src/components/BulkActions/BulkActions.tsx

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React, {PureComponent, createRef} from 'react';
2-
import {TransitionGroup, CSSTransition} from 'react-transition-group';
2+
import {Transition} from 'react-transition-group';
33

44
import {debounce} from '../../utilities/debounce';
55
import {classNames} from '../../utilities/css';
@@ -43,6 +43,8 @@ export interface BulkActionsProps {
4343
onMoreActionPopoverToggle?(isOpen: boolean): void;
4444
/** If the BulkActions is currently sticky in view */
4545
isSticky?: boolean;
46+
/** The width of the BulkActions */
47+
width: number;
4648
}
4749

4850
type CombinedProps = BulkActionsProps & {
@@ -190,7 +192,8 @@ class BulkActionsInner extends PureComponent<CombinedProps, State> {
190192

191193
// eslint-disable-next-line @typescript-eslint/member-ordering
192194
render() {
193-
const {selectMode, disabled, promotedActions, i18n, isSticky} = this.props;
195+
const {selectMode, disabled, promotedActions, i18n, isSticky, width} =
196+
this.props;
194197

195198
const actionSections = this.actionSections();
196199

@@ -296,34 +299,36 @@ class BulkActionsInner extends PureComponent<CombinedProps, State> {
296299
}
297300

298301
const group = (
299-
<TransitionGroup>
300-
<CSSTransition
301-
timeout={250}
302-
in={selectMode}
303-
key="group"
304-
nodeRef={this.groupNode}
305-
>
306-
{(status: TransitionStatus) => {
307-
const groupClassName = classNames(
308-
styles.Group,
309-
!isSticky && styles['Group-not-sticky'],
310-
!measuring && isSticky && styles[`Group-${status}`],
311-
measuring && styles['Group-measuring'],
312-
);
313-
return (
314-
<div className={groupClassName} ref={this.groupNode}>
315-
<EventListener event="resize" handler={this.handleResize} />
316-
<div
317-
className={styles.ButtonGroupWrapper}
318-
ref={this.setButtonsNode}
319-
>
320-
{groupContent}
321-
</div>
302+
<Transition
303+
timeout={250}
304+
in={selectMode}
305+
key="group"
306+
nodeRef={this.groupNode}
307+
>
308+
{(status: TransitionStatus) => {
309+
const groupClassName = classNames(
310+
styles.Group,
311+
!isSticky && styles['Group-not-sticky'],
312+
!measuring && isSticky && styles[`Group-${status}`],
313+
measuring && styles['Group-measuring'],
314+
);
315+
return (
316+
<div
317+
className={groupClassName}
318+
ref={this.groupNode}
319+
style={{width}}
320+
>
321+
<EventListener event="resize" handler={this.handleResize} />
322+
<div
323+
className={styles.ButtonGroupWrapper}
324+
ref={this.setButtonsNode}
325+
>
326+
{groupContent}
322327
</div>
323-
);
324-
}}
325-
</CSSTransition>
326-
</TransitionGroup>
328+
</div>
329+
);
330+
}}
331+
</Transition>
327332
);
328333

329334
return <div ref={this.setContainerNode}>{group}</div>;

polaris-react/src/components/BulkActions/hooks/tests/use-is-bulk-actions-sticky.test.tsx

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,16 @@ function Component({selectMode}: ComponentProps) {
1414
tableMeasurerRef,
1515
isBulkActionsSticky,
1616
bulkActionsAbsoluteOffset,
17+
bulkActionsMaxWidth,
18+
bulkActionsOffsetLeft,
1719
} = useIsBulkActionsSticky(selectMode);
1820

1921
return (
2022
<div className="table" ref={tableMeasurerRef}>
2123
<p className="sticky">{isBulkActionsSticky ? 'true' : 'false'}</p>
2224
<span className="offset">{bulkActionsAbsoluteOffset}</span>
25+
<span className="width">{bulkActionsMaxWidth}</span>
26+
<span className="left">{bulkActionsOffsetLeft}</span>
2327
<em style={{height: 400}} />
2428
<i className="intersection" ref={bulkActionsIntersectionRef} />
2529
</div>
@@ -34,7 +38,11 @@ describe('useIsBulkActionsSticky', () => {
3438
Element.prototype,
3539
'getBoundingClientRect',
3640
);
37-
setGetBoundingClientRect(400);
41+
setGetBoundingClientRect({
42+
width: 600,
43+
height: 400,
44+
left: 20,
45+
});
3846

3947
intersectionObserver.mock();
4048
});
@@ -47,15 +55,27 @@ describe('useIsBulkActionsSticky', () => {
4755
describe('when measuring', () => {
4856
it('returns the offset correctly when select mode is false', () => {
4957
const component = mountWithApp(<Component selectMode={false} />);
50-
const result = component.find('span')?.text();
58+
const result = component.findAll('span')[0]?.text();
5159
expect(result).toBe('400');
5260
});
5361

5462
it('returns the offset correctly when select mode is true', () => {
5563
const component = mountWithApp(<Component selectMode />);
56-
const result = component.find('span')?.text();
64+
const result = component.findAll('span')[0]?.text();
5765
expect(result).toBe('300');
5866
});
67+
68+
it('returns the width value correctly', () => {
69+
const component = mountWithApp(<Component selectMode />);
70+
const result = component.findAll('span')[1]?.text();
71+
expect(result).toBe('600');
72+
});
73+
74+
it('returns the left value correctly', () => {
75+
const component = mountWithApp(<Component selectMode />);
76+
const result = component.findAll('span')[2]?.text();
77+
expect(result).toBe('20');
78+
});
5979
});
6080

6181
describe('when isIntersecting', () => {
@@ -94,13 +114,21 @@ describe('useIsBulkActionsSticky', () => {
94114
});
95115
});
96116

97-
function setGetBoundingClientRect(height: number) {
117+
function setGetBoundingClientRect({
118+
width,
119+
height,
120+
left,
121+
}: {
122+
width: number;
123+
height: number;
124+
left: number;
125+
}) {
98126
getBoundingClientRectSpy.mockImplementation(() => {
99127
return {
100128
height,
101-
width: 0,
129+
width,
102130
top: 0,
103-
left: 0,
131+
left,
104132
bottom: 0,
105133
right: 0,
106134
x: 0,

polaris-react/src/components/BulkActions/hooks/use-is-bulk-actions-sticky.ts

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,29 +9,46 @@ const PADDING_IN_SELECT_MODE = 100;
99
export function useIsBulkActionsSticky(selectMode: boolean) {
1010
const [isBulkActionsSticky, setIsSticky] = useState(false);
1111
const [bulkActionsAbsoluteOffset, setBulkActionsAbsoluteOffset] = useState(0);
12+
const [bulkActionsMaxWidth, setBulkActionsMaxWidth] = useState(0);
13+
const [bulkActionsOffsetLeft, setBulkActionsOffsetLeft] = useState(0);
1214
const bulkActionsIntersectionRef = useRef<HTMLDivElement>(null);
1315
const tableMeasurerRef = useRef<HTMLDivElement>(null);
1416

1517
useEffect(() => {
16-
function computeTableHeight() {
18+
function computeTableDimensions() {
1719
const node = tableMeasurerRef.current;
1820
if (!node) {
19-
return 0;
21+
return {
22+
maxWidth: 0,
23+
offsetHeight: 0,
24+
offsetLeft: 0,
25+
};
2026
}
27+
const box = node.getBoundingClientRect();
2128
const paddingHeight = selectMode ? PADDING_IN_SELECT_MODE : 0;
22-
return node.getBoundingClientRect().height - paddingHeight;
29+
const offsetHeight = box.height - paddingHeight;
30+
const maxWidth = box.width;
31+
const offsetLeft = box.left;
32+
33+
return {
34+
offsetHeight,
35+
offsetLeft,
36+
maxWidth,
37+
};
2338
}
24-
const tableHeight = computeTableHeight();
39+
const {offsetHeight, offsetLeft, maxWidth} = computeTableDimensions();
2540

2641
const debouncedComputeTableHeight = debounce(
27-
computeTableHeight,
42+
computeTableDimensions,
2843
DEBOUNCE_PERIOD,
2944
{
3045
trailing: true,
3146
},
3247
);
3348

34-
setBulkActionsAbsoluteOffset(tableHeight);
49+
setBulkActionsAbsoluteOffset(offsetHeight);
50+
setBulkActionsMaxWidth(maxWidth);
51+
setBulkActionsOffsetLeft(offsetLeft);
3552

3653
window.addEventListener('resize', debouncedComputeTableHeight);
3754

@@ -73,5 +90,7 @@ export function useIsBulkActionsSticky(selectMode: boolean) {
7390
tableMeasurerRef,
7491
isBulkActionsSticky,
7592
bulkActionsAbsoluteOffset,
93+
bulkActionsMaxWidth,
94+
bulkActionsOffsetLeft,
7695
};
7796
}

polaris-react/src/components/IndexTable/IndexTable.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,8 @@ function IndexTableBase({
169169
tableMeasurerRef,
170170
isBulkActionsSticky,
171171
bulkActionsAbsoluteOffset,
172+
bulkActionsMaxWidth,
173+
bulkActionsOffsetLeft,
172174
} = useIsBulkActionsSticky(selectMode);
173175

174176
const tableBodyRef = useCallback(
@@ -553,6 +555,8 @@ function IndexTableBase({
553555
data-condensed={condensed}
554556
style={{
555557
top: isBulkActionsSticky ? undefined : bulkActionsAbsoluteOffset,
558+
width: bulkActionsMaxWidth,
559+
left: isBulkActionsSticky ? bulkActionsOffsetLeft : undefined,
556560
}}
557561
>
558562
{loadingMarkup}
@@ -563,6 +567,7 @@ function IndexTableBase({
563567
actions={actions}
564568
onSelectModeToggle={condensed ? handleSelectModeToggle : undefined}
565569
isSticky={isBulkActionsSticky}
570+
width={bulkActionsMaxWidth}
566571
/>
567572
</div>
568573
) : null;

polaris-react/src/components/ResourceList/ResourceList.scss

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ $breakpoints-empty-search-results-height-up: '(min-height: #{breakpoint(600px)})
3232
position: relative;
3333
background-color: var(--p-surface);
3434
z-index: resource-list(header-outer-wrapper-stacking-order);
35-
overflow: hidden;
3635
border-top-left-radius: var(--p-border-radius-2);
3736
border-top-right-radius: var(--p-border-radius-2);
3837

polaris-react/src/components/ResourceList/ResourceList.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,8 @@ export const ResourceList: ResourceListType = function ResourceList<TItemType>({
171171
tableMeasurerRef,
172172
isBulkActionsSticky,
173173
bulkActionsAbsoluteOffset,
174+
bulkActionsMaxWidth,
175+
bulkActionsOffsetLeft,
174176
} = useIsBulkActionsSticky(selectMode);
175177

176178
const defaultResourceName = useLazyRef(() => ({
@@ -572,6 +574,8 @@ export const ResourceList: ResourceListType = function ResourceList<TItemType>({
572574
className={bulkActionClassNames}
573575
style={{
574576
top: isBulkActionsSticky ? undefined : bulkActionsAbsoluteOffset,
577+
width: bulkActionsMaxWidth,
578+
left: isBulkActionsSticky ? bulkActionsOffsetLeft : undefined,
575579
}}
576580
>
577581
<BulkActions
@@ -580,6 +584,8 @@ export const ResourceList: ResourceListType = function ResourceList<TItemType>({
580584
promotedActions={promotedBulkActions}
581585
actions={bulkActions}
582586
disabled={loading}
587+
isSticky={isBulkActionsSticky}
588+
width={bulkActionsMaxWidth}
583589
/>
584590
</div>
585591
) : null;

0 commit comments

Comments
 (0)