Skip to content

Commit d3087c1

Browse files
authored
feat: support styles (#427)
1 parent 3287d62 commit d3087c1

File tree

5 files changed

+54
-25
lines changed

5 files changed

+54
-25
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ ReactDom.render(
4545
|------------|----------------|---------|----------------|
4646
| className | string | null | - |
4747
| classNames | { mask?: string; wrapper?: string; } | - | pass className to target area |
48+
| styles | { mask?: CSSProperties; wrapper?: CSSProperties; } | - | pass style to target area |
4849
| prefixCls | string | 'drawer' | prefix class |
4950
| width | string \| number | null | drawer content wrapper width, drawer level transition width |
5051
| height | string \| number | null | drawer content wrapper height, drawer level transition height |

src/Drawer.tsx

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import type { DrawerPanelEvents } from './DrawerPanel';
77
import type { DrawerPopupProps } from './DrawerPopup';
88
import DrawerPopup from './DrawerPopup';
99
import { warnCheck } from './util';
10-
import type { DrawerClassNames } from './inter';
10+
import type { DrawerClassNames, DrawerStyles } from './inter';
1111

1212
export type Placement = 'left' | 'top' | 'right' | 'bottom';
1313

@@ -21,6 +21,7 @@ export interface DrawerProps
2121
getContainer?: PortalProps['getContainer'];
2222
panelRef?: React.Ref<HTMLDivElement>;
2323
classNames?: DrawerClassNames;
24+
styles?: DrawerStyles;
2425
}
2526

2627
const Drawer: React.FC<DrawerProps> = props => {

src/DrawerPopup.tsx

+8-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import DrawerContext from './context';
99
import type { DrawerPanelEvents } from './DrawerPanel';
1010
import DrawerPanel from './DrawerPanel';
1111
import { parseWidthHeight } from './util';
12-
import type { DrawerClassNames } from './inter';
12+
import type { DrawerClassNames, DrawerStyles } from './inter';
1313

1414
const sentinelStyle: React.CSSProperties = {
1515
width: 0,
@@ -67,6 +67,9 @@ export interface DrawerPopupProps extends DrawerPanelEvents {
6767

6868
// classNames
6969
classNames?: DrawerClassNames;
70+
71+
// styles
72+
styles?: DrawerStyles;
7073
}
7174

7275
function DrawerPopup(props: DrawerPopupProps, ref: React.Ref<HTMLDivElement>) {
@@ -113,6 +116,8 @@ function DrawerPopup(props: DrawerPopupProps, ref: React.Ref<HTMLDivElement>) {
113116
onClick,
114117
onKeyDown,
115118
onKeyUp,
119+
120+
styles,
116121
} = props;
117122

118123
// ================================ Refs ================================
@@ -228,6 +233,7 @@ function DrawerPopup(props: DrawerPopupProps, ref: React.Ref<HTMLDivElement>) {
228233
style={{
229234
...motionMaskStyle,
230235
...maskStyle,
236+
...styles?.mask,
231237
}}
232238
onClick={maskClosable && open ? onClose : undefined}
233239
ref={maskRef}
@@ -299,6 +305,7 @@ function DrawerPopup(props: DrawerPopupProps, ref: React.Ref<HTMLDivElement>) {
299305
...wrapperStyle,
300306
...motionStyle,
301307
...contentWrapperStyle,
308+
...styles?.wrapper,
302309
}}
303310
{...pickAttrs(props, { data: true })}
304311
>

src/inter.ts

+5
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,8 @@ export interface DrawerClassNames {
22
mask?: string;
33
wrapper?: string;
44
}
5+
6+
export interface DrawerStyles {
7+
mask?: React.CSSProperties;
8+
wrapper?: React.CSSProperties;
9+
}

tests/index.spec.tsx

+38-23
Original file line numberDiff line numberDiff line change
@@ -61,23 +61,23 @@ describe('rc-drawer-menu', () => {
6161
placement: DrawerProps['placement'];
6262
transform: string;
6363
}[] = [
64-
{
65-
placement: 'left',
66-
transform: 'translateX(903px)',
67-
},
68-
{
69-
placement: 'right',
70-
transform: 'translateX(-903px)',
71-
},
72-
{
73-
placement: 'top',
74-
transform: 'translateY(903px)',
75-
},
76-
{
77-
placement: 'bottom',
78-
transform: 'translateY(-903px)',
79-
},
80-
];
64+
{
65+
placement: 'left',
66+
transform: 'translateX(903px)',
67+
},
68+
{
69+
placement: 'right',
70+
transform: 'translateX(-903px)',
71+
},
72+
{
73+
placement: 'top',
74+
transform: 'translateY(903px)',
75+
},
76+
{
77+
placement: 'bottom',
78+
transform: 'translateY(-903px)',
79+
},
80+
];
8181

8282
placementList.forEach(({ placement, transform }) => {
8383
it(placement, () => {
@@ -359,15 +359,15 @@ describe('rc-drawer-menu', () => {
359359
);
360360
errSpy.mockRestore();
361361
});
362-
363-
362+
363+
364364
it('pass data props to internal div', () => {
365365
const value = 'bamboo';
366366
const { unmount } = render(<Drawer open data-attr={value} />);
367-
expect(document.querySelector('.rc-drawer-content-wrapper')).toHaveAttribute('data-attr',value);
367+
expect(document.querySelector('.rc-drawer-content-wrapper')).toHaveAttribute('data-attr', value);
368368
unmount();
369369
});
370-
370+
371371
it('support bodyProps', () => {
372372
const enter = jest.fn();
373373
const leave = jest.fn();
@@ -386,7 +386,7 @@ describe('rc-drawer-menu', () => {
386386
});
387387

388388
it('pass id & className props to Panel', () => {
389-
const { unmount } = render(<Drawer className='customer-className' id="customer-id" open/>);
389+
const { unmount } = render(<Drawer className='customer-className' id="customer-id" open />);
390390
expect(
391391
document.querySelector('.rc-drawer-content')
392392
).toHaveClass('customer-className');
@@ -401,7 +401,7 @@ describe('rc-drawer-menu', () => {
401401
<Drawer classNames={{
402402
wrapper: 'customer-wrapper',
403403
mask: 'customer-mask',
404-
}} open/>
404+
}} open />
405405
);
406406
expect(
407407
document.querySelector('.rc-drawer-content-wrapper')
@@ -411,4 +411,19 @@ describe('rc-drawer-menu', () => {
411411
).toHaveClass('customer-mask');
412412
unmount();
413413
});
414+
it('should support styles', () => {
415+
const { unmount } = render(
416+
<Drawer styles={{
417+
wrapper: { background: 'red' },
418+
mask: { background: 'blue' },
419+
}} open />
420+
);
421+
expect(
422+
document.querySelector('.rc-drawer-content-wrapper')
423+
).toHaveStyle('background: red');
424+
expect(
425+
document.querySelector('.rc-drawer-mask')
426+
).toHaveStyle('background: blue');
427+
unmount();
428+
});
414429
});

0 commit comments

Comments
 (0)