Skip to content

Commit 0a6f174

Browse files
[IRN-5328] [BpkDrawer] supports non-padded content (#3646)
* [IRN-5328] BpkDrawer supports non-padded content * Add logic to optionally disable padding * Convert content to TS and add component test scenario * update snap * Convert test to TS * Fix type
1 parent 6d53c2e commit 0a6f174

File tree

9 files changed

+155
-78
lines changed

9 files changed

+155
-78
lines changed

examples/bpk-component-drawer/examples.js

+18
Original file line numberDiff line numberDiff line change
@@ -262,10 +262,28 @@ const WithFullHeightContentExample = () => (
262262
</DrawerContainer>
263263
);
264264

265+
const WithNonPaddedContentExample = () => (
266+
<DrawerContainer
267+
title="Drawer title"
268+
closeLabel="Close drawer"
269+
buttonText="Open drawer"
270+
getApplicationElement={() => document.getElementById('pagewrap')}
271+
padded={false}
272+
>
273+
<div className={getClassName('bpk-drawer-content-full-width')}>
274+
<p>
275+
This has default padding disabled to give full control of the layout
276+
e.g. full width background color.
277+
</p>
278+
</div>
279+
</DrawerContainer>
280+
);
281+
265282
export {
266283
DefaultExample,
267284
OverflowingExamples,
268285
CloseButtonTextExample,
269286
WithVisuallyHiddenTitleExample,
270287
WithFullHeightContentExample,
288+
WithNonPaddedContentExample,
271289
};

examples/bpk-component-drawer/examples.module.scss

+12
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,15 @@
3030
.bpk-drawer-button {
3131
margin: auto auto 0;
3232
}
33+
34+
.bpk-drawer-content-full-width {
35+
height: 100%;
36+
padding: tokens.bpk-spacing-base();
37+
background-color: tokens.$bpk-canvas-contrast-day;
38+
39+
p {
40+
padding: tokens.bpk-spacing-base();
41+
border-radius: tokens.$bpk-border-radius-md;
42+
background-color: tokens.$bpk-surface-default-day;
43+
}
44+
}

examples/bpk-component-drawer/stories.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
* limitations under the License.
1717
*/
1818

19-
2019
import BkpDrawer from '../../packages/bpk-component-drawer/src/BpkDrawer';
2120

2221
import {
@@ -25,6 +24,7 @@ import {
2524
CloseButtonTextExample,
2625
WithVisuallyHiddenTitleExample,
2726
WithFullHeightContentExample,
27+
WithNonPaddedContentExample,
2828
} from './examples';
2929

3030
export default {
@@ -39,3 +39,5 @@ export const CloseButtonText = CloseButtonTextExample;
3939
export const WithVisuallyHiddenTitle = WithVisuallyHiddenTitleExample;
4040

4141
export const WithFullHeightContent = WithFullHeightContentExample;
42+
43+
export const WithNonPaddedContent = WithNonPaddedContentExample;

packages/bpk-component-drawer/src/BpkDrawer.js

+2
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ type Props = {
4242
closeLabel: ?string,
4343
closeText: ?string,
4444
hideTitle: ?boolean,
45+
padded?: boolean,
4546
};
4647

4748
type State = {
@@ -118,6 +119,7 @@ BpkDrawer.defaultProps = {
118119
closeLabel: null,
119120
closeText: null,
120121
hideTitle: false,
122+
padded: true,
121123
};
122124

123125
export default BpkDrawer;

packages/bpk-component-drawer/src/BpkDrawerContent-test.js packages/bpk-component-drawer/src/BpkDrawerContent-test.tsx

+18-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
* limitations under the License.
1717
*/
1818

19-
/* @flow strict */
19+
import type { ReactElement } from 'react';
2020

2121
import { render } from '@testing-library/react';
2222

@@ -25,7 +25,7 @@ import BpkDrawerContent from './BpkDrawerContent';
2525
jest.mock(
2626
'react-transition-group/Transition',
2727
() =>
28-
({ children }) =>
28+
({ children }: { children: (state: string) => ReactElement }) =>
2929
children('entered'),
3030
);
3131

@@ -63,6 +63,22 @@ describe('BpkDrawerContent', () => {
6363
expect(asFragment()).toMatchSnapshot();
6464
});
6565

66+
it('should render correctly when it has padded=true', () => {
67+
const { asFragment } = render(
68+
<BpkDrawerContent
69+
id="my-drawer"
70+
title="Drawer title"
71+
onClose={jest.fn()}
72+
onCloseAnimationComplete={jest.fn()}
73+
dialogRef={jest.fn()}
74+
padded
75+
>
76+
Drawer content
77+
</BpkDrawerContent>,
78+
);
79+
expect(asFragment()).toMatchSnapshot();
80+
});
81+
6682
it('should render correctly when it has a contentClassName', () => {
6783
const { asFragment } = render(
6884
<BpkDrawerContent

packages/bpk-component-drawer/src/BpkDrawerContent.module.scss

+4-1
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,11 @@
103103

104104
&__content {
105105
height: 100%;
106-
padding: tokens.bpk-spacing-base();
107106
flex: 1 1 100%;
108107
overflow-y: auto;
109108
}
109+
110+
&__content--padded {
111+
padding: tokens.bpk-spacing-base();
112+
}
110113
}

packages/bpk-component-drawer/src/BpkDrawerContent.js packages/bpk-component-drawer/src/BpkDrawerContent.tsx

+44-73
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,16 @@
1616
* limitations under the License.
1717
*/
1818

19-
/* @flow strict */
20-
21-
import PropTypes from 'prop-types';
22-
import type { Node } from 'react';
19+
import type { ReactNode } from 'react';
2320

21+
// @ts-expect-error Untyped import. See `decisions/imports-ts-suppressions.md`.
2422
import Transition from 'react-transition-group/Transition';
2523

2624
import { animations } from '@skyscanner/bpk-foundations-web/tokens/base.es6';
2725

26+
// @ts-expect-error Untyped import. See `decisions/imports-ts-suppressions.md`.
2827
import BpkCloseButton from '../../bpk-component-close-button';
28+
// @ts-expect-error Untyped import. See `decisions/imports-ts-suppressions.md`.
2929
import { BpkButtonLink } from '../../bpk-component-link';
3030
import { cssModules } from '../../bpk-react-utils';
3131

@@ -34,42 +34,43 @@ import STYLES from './BpkDrawerContent.module.scss';
3434
const getClassName = cssModules(STYLES);
3535

3636
type Props = {
37-
children: Node,
38-
dialogRef: () => mixed,
39-
onCloseAnimationComplete: () => mixed,
40-
onClose: () => mixed,
37+
children: ReactNode,
38+
dialogRef: () => React.RefObject<HTMLElement>,
39+
onCloseAnimationComplete: () => void,
40+
onClose: () => void
4141
id: string,
4242
title: string,
43-
className: ?string,
44-
contentClassName: ?string,
45-
closeLabel: string,
46-
closeText: ?string,
47-
isDrawerShown: boolean,
48-
hideTitle: boolean,
49-
closeOnScrimClick: boolean,
50-
isIphone: boolean,
51-
isIpad: boolean,
43+
className?: string,
44+
contentClassName?: string,
45+
closeLabel?: string,
46+
closeText?: string,
47+
isDrawerShown?: boolean,
48+
hideTitle?: boolean,
49+
closeOnScrimClick?: boolean,
50+
isIphone?: boolean,
51+
isIpad?: boolean,
52+
padded?: boolean,
5253
};
5354

54-
const BpkDrawerContent = (props: Props) => {
55-
const {
56-
children,
57-
className,
58-
closeLabel,
59-
closeOnScrimClick, // Unused from withScrim scrim HOC
60-
closeText,
61-
contentClassName,
62-
dialogRef,
63-
hideTitle,
64-
id,
65-
isDrawerShown,
66-
isIpad, // Unused from withScrim scrim HOC
67-
isIphone, // Unused from withScrim scrim HOC
68-
onClose,
69-
onCloseAnimationComplete,
70-
title,
71-
...rest
72-
} = props;
55+
const BpkDrawerContent = ({
56+
children,
57+
className,
58+
closeLabel,
59+
closeOnScrimClick = true, // Unused from withScrim scrim HOC
60+
closeText,
61+
contentClassName,
62+
dialogRef,
63+
hideTitle = false,
64+
id,
65+
isDrawerShown = true,
66+
isIpad = false, // Unused from withScrim scrim HOC
67+
isIphone = false, // Unused from withScrim scrim HOC
68+
onClose,
69+
onCloseAnimationComplete,
70+
padded,
71+
title,
72+
...rest
73+
}: Props) => {
7374

7475
const drawerClassNames = [getClassName('bpk-drawer')];
7576
const headerClassNames = [getClassName('bpk-drawer__heading')];
@@ -83,6 +84,10 @@ const BpkDrawerContent = (props: Props) => {
8384
headerClassNames.push(getClassName('bpk-drawer__heading--visually-hidden'));
8485
}
8586

87+
if (padded) {
88+
contentClassNames.push(getClassName('bpk-drawer__content--padded'));
89+
}
90+
8691
if (contentClassName) {
8792
contentClassNames.push(contentClassName);
8893
}
@@ -101,11 +106,10 @@ const BpkDrawerContent = (props: Props) => {
101106
in={isDrawerShown}
102107
onExited={onCloseAnimationComplete}
103108
>
104-
{(status) => (
105-
// $FlowFixMe[cannot-spread-inexact] - inexact rest. See decisions/flowfixme.md
109+
{(status: string) => (
106110
<section
107111
id={id}
108-
tabIndex="-1"
112+
tabIndex={-1}
109113
role="dialog"
110114
key="dialog"
111115
aria-labelledby={headingId}
@@ -125,10 +129,7 @@ const BpkDrawerContent = (props: Props) => {
125129
<BpkButtonLink onClick={onClose}>{closeText}</BpkButtonLink>
126130
) : (
127131
<div className={getClassName('bpk-drawer__close-button')}>
128-
<BpkCloseButton
129-
label={closeLabel}
130-
onClick={onClose}
131-
/>
132+
<BpkCloseButton label={closeLabel} onClick={onClose} />
132133
</div>
133134
)}
134135
</header>
@@ -139,34 +140,4 @@ const BpkDrawerContent = (props: Props) => {
139140
);
140141
};
141142

142-
BpkDrawerContent.propTypes = {
143-
children: PropTypes.node.isRequired,
144-
dialogRef: PropTypes.func.isRequired,
145-
onCloseAnimationComplete: PropTypes.func.isRequired,
146-
onClose: PropTypes.func.isRequired,
147-
id: PropTypes.string.isRequired,
148-
title: PropTypes.string.isRequired,
149-
className: PropTypes.string,
150-
contentClassName: PropTypes.string,
151-
closeLabel: PropTypes.string,
152-
closeText: PropTypes.string,
153-
isDrawerShown: PropTypes.bool,
154-
hideTitle: PropTypes.bool,
155-
closeOnScrimClick: PropTypes.bool,
156-
isIphone: PropTypes.bool,
157-
isIpad: PropTypes.bool,
158-
};
159-
160-
BpkDrawerContent.defaultProps = {
161-
className: null,
162-
contentClassName: null,
163-
closeLabel: null,
164-
closeText: null,
165-
isDrawerShown: true,
166-
hideTitle: false,
167-
closeOnScrimClick: true,
168-
isIphone: false,
169-
isIpad: false,
170-
};
171-
172143
export default BpkDrawerContent;

packages/bpk-component-drawer/src/__snapshots__/BpkDrawer-test.js.snap

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ exports[`BpkDrawer should render correctly in the given target if renderTarget i
6161
</div>
6262
</header>
6363
<div
64-
class="bpk-drawer__content"
64+
class="bpk-drawer__content bpk-drawer__content--padded"
6565
>
6666
Drawer content
6767
</div>

packages/bpk-component-drawer/src/__snapshots__/BpkDrawerContent-test.js.snap packages/bpk-component-drawer/src/__snapshots__/BpkDrawerContent-test.tsx.snap

+53
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,59 @@ exports[`BpkDrawerContent should render correctly when it has a contentClassName
165165
</DocumentFragment>
166166
`;
167167

168+
exports[`BpkDrawerContent should render correctly when it has padded=true 1`] = `
169+
<DocumentFragment>
170+
<section
171+
aria-labelledby="bpk-drawer-heading-my-drawer"
172+
class="bpk-drawer bpk-drawer--entered"
173+
id="my-drawer"
174+
role="dialog"
175+
tabindex="-1"
176+
>
177+
<header
178+
class="bpk-drawer__header"
179+
>
180+
<h2
181+
class="bpk-drawer__heading"
182+
id="bpk-drawer-heading-my-drawer"
183+
>
184+
Drawer title
185+
</h2>
186+
 
187+
<div
188+
class="bpk-drawer__close-button"
189+
>
190+
<button
191+
class="bpk-close-button__default"
192+
type="button"
193+
>
194+
<span
195+
class="bpk-close-button-icon"
196+
>
197+
<svg
198+
aria-hidden="true"
199+
height="1rem"
200+
viewBox="0 0 24 24"
201+
width="1rem"
202+
xmlns="http://www.w3.org/2000/svg"
203+
>
204+
<path
205+
d="M5.587 3.467a1.5 1.5 0 1 0-2.194 2.046q.036.038.074.074l6.438 6.44-6.44 6.44a1.5 1.5 0 0 0 2.122 2.12l6.44-6.438 6.44 6.44a1.5 1.5 0 0 0 2.12-2.122l-6.438-6.44 6.44-6.44a1.5 1.5 0 0 0-2.122-2.12l-6.44 6.438-6.44-6.44z"
206+
/>
207+
</svg>
208+
</span>
209+
</button>
210+
</div>
211+
</header>
212+
<div
213+
class="bpk-drawer__content bpk-drawer__content--padded"
214+
>
215+
Drawer content
216+
</div>
217+
</section>
218+
</DocumentFragment>
219+
`;
220+
168221
exports[`BpkDrawerContent should render correctly with arbitrary attributes 1`] = `
169222
<DocumentFragment>
170223
<section

0 commit comments

Comments
 (0)