Skip to content

Commit cc14751

Browse files
fix(accordions): enable dynamic height for panel (#1600)
1 parent 243e052 commit cc14751

File tree

8 files changed

+66
-145
lines changed

8 files changed

+66
-145
lines changed
Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
{
22
"index.esm.js": {
3-
"bundled": 34407,
4-
"minified": 25374,
5-
"gzipped": 5671,
3+
"bundled": 33588,
4+
"minified": 24988,
5+
"gzipped": 5509,
66
"treeshaked": {
77
"rollup": {
8-
"code": 19686,
9-
"import_statements": 683
8+
"code": 19325,
9+
"import_statements": 585
1010
},
1111
"webpack": {
12-
"code": 21849
12+
"code": 21424
1313
}
1414
}
1515
},
1616
"index.cjs.js": {
17-
"bundled": 37238,
18-
"minified": 27956,
19-
"gzipped": 5905
17+
"bundled": 36245,
18+
"minified": 27444,
19+
"gzipped": 5724
2020
}
2121
}

packages/accordions/package.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,8 @@
2323
"dependencies": {
2424
"@zendeskgarden/container-accordion": "^2.0.0",
2525
"@zendeskgarden/container-utilities": "^1.0.0",
26-
"lodash.debounce": "^4.0.8",
2726
"polished": "^4.0.0",
28-
"prop-types": "^15.5.7",
29-
"react-merge-refs": "^1.1.0"
27+
"prop-types": "^15.5.7"
3028
},
3129
"peerDependencies": {
3230
"@zendeskgarden/react-theming": "^8.67.0",
@@ -35,7 +33,6 @@
3533
"styled-components": "^4.2.0 || ^5.0.0"
3634
},
3735
"devDependencies": {
38-
"@types/lodash.debounce": "4.0.7",
3936
"@zendeskgarden/react-theming": "^8.69.6",
4037
"@zendeskgarden/svg-icons": "6.33.0"
4138
},

packages/accordions/src/elements/accordion/components/Panel.tsx

Lines changed: 4 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -5,59 +5,23 @@
55
* found at http://www.apache.org/licenses/LICENSE-2.0.
66
*/
77

8-
import React, { useEffect, useRef, useContext, forwardRef, HTMLAttributes } from 'react';
9-
import debounce from 'lodash.debounce';
10-
import mergeRefs from 'react-merge-refs';
11-
import { ThemeContext } from 'styled-components';
12-
import { useDocument } from '@zendeskgarden/react-theming';
8+
import React, { forwardRef, HTMLAttributes } from 'react';
139

1410
import { useAccordionContext, useSectionContext, IAccordionContext } from '../../../utils';
1511
import { StyledPanel, StyledInnerPanel } from '../../../styled';
1612

1713
type PanelProps = IAccordionContext | { isExpanded?: boolean };
1814

1915
const PanelComponent = forwardRef<HTMLElement, HTMLAttributes<HTMLElement>>((props, ref) => {
20-
const { isCompact, isBare, isAnimated, getPanelProps, expandedSections } = useAccordionContext();
21-
const panelRef = useRef<HTMLElement>();
16+
const { isCompact, isBare, isAnimated, expandedSections, getPanelProps } = useAccordionContext();
2217
const index = useSectionContext();
2318
const isExpanded = expandedSections.includes(index);
2419

25-
const theme = useContext(ThemeContext);
26-
const environment = useDocument(theme);
27-
28-
useEffect(() => {
29-
if (!isAnimated) {
30-
return undefined;
31-
}
32-
33-
if (environment) {
34-
const updateMaxHeight = debounce(() => {
35-
if (panelRef.current) {
36-
const child = panelRef.current.children[0] as any;
37-
38-
child.style.maxHeight = `${child.scrollHeight}px`;
39-
}
40-
}, 100);
41-
42-
const win = environment.defaultView! || window;
43-
44-
win.addEventListener('resize', updateMaxHeight);
45-
updateMaxHeight();
46-
47-
return () => {
48-
updateMaxHeight.cancel();
49-
win.removeEventListener('resize', updateMaxHeight);
50-
};
51-
}
52-
53-
return undefined;
54-
}, [isAnimated, panelRef, environment, props.children]);
55-
5620
return (
5721
<StyledPanel
5822
{...getPanelProps<PanelProps>({
5923
role: null,
60-
ref: mergeRefs([panelRef, ref]),
24+
ref,
6125
index,
6226
isBare,
6327
isCompact,
@@ -66,9 +30,7 @@ const PanelComponent = forwardRef<HTMLElement, HTMLAttributes<HTMLElement>>((pro
6630
...props
6731
})}
6832
>
69-
<StyledInnerPanel isExpanded={isExpanded} isAnimated={isAnimated}>
70-
{props.children}
71-
</StyledInnerPanel>
33+
<StyledInnerPanel>{props.children}</StyledInnerPanel>
7234
</StyledPanel>
7335
);
7436
});

packages/accordions/src/styled/accordion/StyledInnerPanel.spec.tsx

Lines changed: 0 additions & 31 deletions
This file was deleted.

packages/accordions/src/styled/accordion/StyledInnerPanel.ts

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,37 +6,21 @@
66
*/
77

88
import styled from 'styled-components';
9-
import {
10-
retrieveComponentStyles,
11-
DEFAULT_THEME,
12-
getLineHeight
13-
} from '@zendeskgarden/react-theming';
9+
import { retrieveComponentStyles, DEFAULT_THEME } from '@zendeskgarden/react-theming';
1410

1511
const COMPONENT_ID = 'accordions.step_inner_panel';
1612

17-
interface IStyledInnerPanel {
18-
isExpanded?: boolean;
19-
isAnimated?: boolean;
20-
}
21-
22-
/**
23-
* 1. Override the inline max-height style used for animation.
24-
*/
25-
export const StyledInnerPanel = styled.div.attrs<IStyledInnerPanel>({
13+
export const StyledInnerPanel = styled.div.attrs({
2614
'data-garden-id': COMPONENT_ID,
2715
'data-garden-version': PACKAGE_VERSION
28-
})<IStyledInnerPanel>`
29-
transition: ${props => props.isAnimated && 'max-height 0.25s ease-in-out'};
30-
/* stylelint-disable-next-line declaration-no-important */
31-
max-height: ${props => !props.isExpanded && '0 !important'}; /* [1] */
16+
})`
3217
overflow: hidden;
33-
line-height: ${props => getLineHeight(props.theme.space.base * 5, props.theme.fontSizes.md)};
34-
font-size: ${props => props.theme.fontSizes.md};
18+
line-height: inherit;
19+
font-size: inherit;
3520
3621
${props => retrieveComponentStyles(COMPONENT_ID, props)};
3722
`;
3823

3924
StyledInnerPanel.defaultProps = {
40-
isAnimated: true,
4125
theme: DEFAULT_THEME
4226
};

packages/accordions/src/styled/accordion/StyledPanel.spec.tsx

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,20 @@ describe('StyledPanel', () => {
1414
it('renders default styling correctly', () => {
1515
const { container } = render(<StyledPanel />);
1616

17+
expect(container.firstChild).toHaveStyleRule('grid-template-rows', '0fr');
1718
expect(container.firstChild).toHaveStyleRule('padding', '8px 20px 32px');
1819
expect(container.firstChild).toHaveStyleRule(
19-
'border-bottom',
20-
`${DEFAULT_THEME.borders.sm} ${getColor('neutralHue', 300, DEFAULT_THEME)}`
20+
'border-bottom-width',
21+
`${DEFAULT_THEME.borderWidths.sm}`
22+
);
23+
expect(container.firstChild).toHaveStyleRule(
24+
'border-bottom-color',
25+
`${getColor('neutralHue', 300, DEFAULT_THEME)}`
26+
);
27+
expect(container.firstChild).toHaveStyleRule(
28+
'transition',
29+
'padding 0.25s ease-in-out,grid-template-rows 0.25s ease-in-out'
2130
);
22-
expect(container.firstChild).toHaveStyleRule('transition', 'padding 0.25s ease-in-out');
2331
});
2432

2533
it('renders isCompact styling correctly', () => {
@@ -32,21 +40,24 @@ describe('StyledPanel', () => {
3240
const { container } = render(<StyledPanel isExpanded />);
3341

3442
expect(container.firstChild).toHaveStyleRule('padding', '8px 20px 32px');
43+
expect(container.firstChild).toHaveStyleRule('grid-template-rows', '1fr');
3544
});
3645

3746
it('renders isCompact & isExpanded styling correctly', () => {
3847
const { container } = render(<StyledPanel isCompact isExpanded />);
3948

4049
expect(container.firstChild).toHaveStyleRule('padding', '8px 12px 16px');
50+
expect(container.firstChild).toHaveStyleRule('grid-template-rows', '1fr');
4151
});
4252

4353
it('renders isBare styling correctly', () => {
4454
const { container } = render(<StyledPanel isBare />);
4555

4656
expect(container.firstChild).toHaveStyleRule(
47-
'border-bottom',
48-
`${DEFAULT_THEME.borders.sm} transparent`
57+
'border-bottom-width',
58+
`${DEFAULT_THEME.borderWidths.sm}`
4959
);
60+
expect(container.firstChild).toHaveStyleRule('border-bottom-color', 'transparent');
5061
});
5162

5263
it('renders transition styling correctly', () => {

packages/accordions/src/styled/accordion/StyledPanel.ts

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@
66
*/
77

88
import styled, { css, ThemeProps, DefaultTheme } from 'styled-components';
9-
import { getColor, retrieveComponentStyles, DEFAULT_THEME } from '@zendeskgarden/react-theming';
9+
import {
10+
getColor,
11+
getLineHeight,
12+
retrieveComponentStyles,
13+
DEFAULT_THEME
14+
} from '@zendeskgarden/react-theming';
1015

1116
interface IStyledPanel {
1217
isBare?: boolean;
@@ -17,38 +22,53 @@ interface IStyledPanel {
1722

1823
const COMPONENT_ID = 'accordions.panel';
1924

20-
const paddingStyles = (props: IStyledPanel & ThemeProps<DefaultTheme>) => {
21-
const { base } = props.theme.space;
25+
const colorStyles = (props: IStyledPanel & ThemeProps<DefaultTheme>) => {
26+
const { theme, isBare } = props;
27+
28+
return css`
29+
border-bottom-color: ${isBare ? 'transparent' : getColor('neutralHue', 300, theme)};
30+
`;
31+
};
32+
33+
const sizeStyles = (props: IStyledPanel & ThemeProps<DefaultTheme>) => {
34+
const { theme, isCompact, isExpanded } = props;
35+
const { base } = theme.space;
2236
let paddingTop = base * 2;
2337
let paddingHorizontal = base * 5;
2438
let paddingBottom = base * 8;
2539

26-
if (props.isCompact) {
40+
if (isCompact) {
2741
paddingTop = base * 2;
2842
paddingHorizontal = base * 3;
2943
paddingBottom = base * 4;
3044
}
3145

32-
if (props.isExpanded === false) {
46+
if (isExpanded === false) {
3347
paddingTop = 0;
3448
paddingBottom = 0;
3549
}
3650

3751
return css`
38-
transition: ${props.isAnimated && 'padding 0.25s ease-in-out'};
52+
grid-template-rows: ${isExpanded ? 1 : 0}fr;
53+
border-bottom-width: ${theme.borderWidths.sm};
54+
border-bottom-style: solid;
3955
padding: ${paddingTop}px ${paddingHorizontal}px ${paddingBottom}px;
56+
line-height: ${getLineHeight(base * 5, theme.fontSizes.md)};
57+
font-size: ${theme.fontSizes.md};
4058
`;
4159
};
4260

4361
export const StyledPanel = styled.section.attrs<IStyledPanel>({
4462
'data-garden-id': COMPONENT_ID,
4563
'data-garden-version': PACKAGE_VERSION
4664
})<IStyledPanel>`
47-
${paddingStyles};
48-
border-bottom: ${props =>
49-
`${props.theme.borders.sm} ${
50-
props.isBare ? 'transparent' : getColor('neutralHue', 300, props.theme)
51-
}`};
65+
display: grid;
66+
transition: ${props =>
67+
props.isAnimated && 'padding 0.25s ease-in-out, grid-template-rows 0.25s ease-in-out'};
68+
overflow: hidden;
69+
70+
${sizeStyles}
71+
${colorStyles}
5272
5373
${props => retrieveComponentStyles(COMPONENT_ID, props)};
5474
`;

packages/accordions/yarn.lock

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,6 @@
2121
resolved "https://registry.yarnpkg.com/@reach/utils/-/utils-0.18.0.tgz#4f3cebe093dd436eeaff633809bf0f68f4f9d2ee"
2222
integrity sha512-KdVMdpTgDyK8FzdKO9SCpiibuy/kbv3pwgfXshTI6tEcQT1OOwj7BAksnzGC0rPz0UholwC+AgkqEl3EJX3M1A==
2323

24-
"@types/lodash.debounce@4.0.7":
25-
version "4.0.7"
26-
resolved "https://registry.yarnpkg.com/@types/lodash.debounce/-/lodash.debounce-4.0.7.tgz#0285879defb7cdb156ae633cecd62d5680eded9f"
27-
integrity sha512-X1T4wMZ+gT000M2/91SYj0d/7JfeNZ9PeeOldSNoE/lunLeQXKvkmIumI29IaKMotU/ln/McOIvgzZcQ/3TrSA==
28-
dependencies:
29-
"@types/lodash" "*"
30-
31-
"@types/lodash@*":
32-
version "4.14.176"
33-
resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.176.tgz#641150fc1cda36fbfa329de603bbb175d7ee20c0"
34-
integrity sha512-xZmuPTa3rlZoIbtDUyJKZQimJV3bxCmzMIO2c9Pz9afyDro6kr7R79GwcB6mRhuoPmV2p1Vb66WOJH7F886WKQ==
35-
3624
"@zendeskgarden/container-accordion@^2.0.0":
3725
version "2.0.8"
3826
resolved "https://registry.yarnpkg.com/@zendeskgarden/container-accordion/-/container-accordion-2.0.8.tgz#eb7192dfcc2a5473bc63a3751e0d529285535d3f"
@@ -78,11 +66,6 @@
7866
resolved "https://registry.yarnpkg.com/js-tokens/-/js-tokens-4.0.0.tgz#19203fb59991df98e3a287050d4647cdeaf32499"
7967
integrity sha512-RdJUflcE3cUzKiMqQgsCu06FPu9UdIJO0beYbPhHN4k6apgJtifcoCtT9bcxOpYBtpD2kCM6Sbzg4CausW/PKQ==
8068

81-
lodash.debounce@^4.0.8:
82-
version "4.0.8"
83-
resolved "https://registry.yarnpkg.com/lodash.debounce/-/lodash.debounce-4.0.8.tgz#82d79bff30a67c4005ffd5e2515300ad9ca4d7af"
84-
integrity sha1-gteb/zCmfEAF/9XiUVMArZyk168=
85-
8669
lodash.memoize@^4.1.2:
8770
version "4.1.2"
8871
resolved "https://registry.yarnpkg.com/lodash.memoize/-/lodash.memoize-4.1.2.tgz#bcc6c49a42a2840ed997f323eada5ecd182e0bfe"
@@ -121,11 +104,6 @@ react-is@^16.13.1:
121104
resolved "https://registry.yarnpkg.com/react-is/-/react-is-16.13.1.tgz#789729a4dc36de2999dc156dd6c1d9c18cea56a4"
122105
integrity sha512-24e6ynE2H+OKt4kqsOvNd8kBpV65zoxbA4BVsEOB3ARVWQki/DHzaUoC5KuON/BiccDaCCTZBuOcfZs70kR8bQ==
123106

124-
react-merge-refs@^1.1.0:
125-
version "1.1.0"
126-
resolved "https://registry.yarnpkg.com/react-merge-refs/-/react-merge-refs-1.1.0.tgz#73d88b892c6c68cbb7a66e0800faa374f4c38b06"
127-
integrity sha512-alTKsjEL0dKH/ru1Iyn7vliS2QRcBp9zZPGoWxUOvRGWPUYgjo+V01is7p04It6KhgrzhJGnIj9GgX8W4bZoCQ==
128-
129107
react-uid@^2.2.0:
130108
version "2.3.3"
131109
resolved "https://registry.yarnpkg.com/react-uid/-/react-uid-2.3.3.tgz#6a485ccc868555997f3506c6db97a3e735d97adf"

0 commit comments

Comments
 (0)