Skip to content

feat: support styles #427

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ ReactDom.render(
|------------|----------------|---------|----------------|
| className | string | null | - |
| classNames | { mask?: string; wrapper?: string; } | - | pass className to target area |
| styles | { mask?: CSSProperties; wrapper?: CSSProperties; } | - | pass style to target area |
| prefixCls | string | 'drawer' | prefix class |
| width | string \| number | null | drawer content wrapper width, drawer level transition width |
| height | string \| number | null | drawer content wrapper height, drawer level transition height |
Expand Down
3 changes: 2 additions & 1 deletion src/Drawer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type { DrawerPanelEvents } from './DrawerPanel';
import type { DrawerPopupProps } from './DrawerPopup';
import DrawerPopup from './DrawerPopup';
import { warnCheck } from './util';
import type { DrawerClassNames } from './inter';
import type { DrawerClassNames, DrawerStyles } from './inter';

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

Expand All @@ -21,6 +21,7 @@ export interface DrawerProps
getContainer?: PortalProps['getContainer'];
panelRef?: React.Ref<HTMLDivElement>;
classNames?: DrawerClassNames;
styles?: DrawerStyles;
}

const Drawer: React.FC<DrawerProps> = props => {
Expand Down
9 changes: 8 additions & 1 deletion src/DrawerPopup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import DrawerContext from './context';
import type { DrawerPanelEvents } from './DrawerPanel';
import DrawerPanel from './DrawerPanel';
import { parseWidthHeight } from './util';
import type { DrawerClassNames } from './inter';
import type { DrawerClassNames, DrawerStyles } from './inter';

const sentinelStyle: React.CSSProperties = {
width: 0,
Expand Down Expand Up @@ -67,6 +67,9 @@ export interface DrawerPopupProps extends DrawerPanelEvents {

// classNames
classNames?: DrawerClassNames;

// styles
styles?: DrawerStyles;
}

function DrawerPopup(props: DrawerPopupProps, ref: React.Ref<HTMLDivElement>) {
Expand Down Expand Up @@ -113,6 +116,8 @@ function DrawerPopup(props: DrawerPopupProps, ref: React.Ref<HTMLDivElement>) {
onClick,
onKeyDown,
onKeyUp,

styles,
} = props;

// ================================ Refs ================================
Expand Down Expand Up @@ -228,6 +233,7 @@ function DrawerPopup(props: DrawerPopupProps, ref: React.Ref<HTMLDivElement>) {
style={{
...motionMaskStyle,
...maskStyle,
...styles?.mask,
}}
onClick={maskClosable && open ? onClose : undefined}
ref={maskRef}
Expand Down Expand Up @@ -299,6 +305,7 @@ function DrawerPopup(props: DrawerPopupProps, ref: React.Ref<HTMLDivElement>) {
...wrapperStyle,
...motionStyle,
...contentWrapperStyle,
...styles?.wrapper,
}}
{...pickAttrs(props, { data: true })}
>
Expand Down
5 changes: 5 additions & 0 deletions src/inter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,8 @@ export interface DrawerClassNames {
mask?: string;
wrapper?: string;
}

export interface DrawerStyles {
mask?: React.CSSProperties;
wrapper?: React.CSSProperties;
}
61 changes: 38 additions & 23 deletions tests/index.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,23 +61,23 @@ describe('rc-drawer-menu', () => {
placement: DrawerProps['placement'];
transform: string;
}[] = [
{
placement: 'left',
transform: 'translateX(903px)',
},
{
placement: 'right',
transform: 'translateX(-903px)',
},
{
placement: 'top',
transform: 'translateY(903px)',
},
{
placement: 'bottom',
transform: 'translateY(-903px)',
},
];
{
placement: 'left',
transform: 'translateX(903px)',
},
{
placement: 'right',
transform: 'translateX(-903px)',
},
{
placement: 'top',
transform: 'translateY(903px)',
},
{
placement: 'bottom',
transform: 'translateY(-903px)',
},
];

placementList.forEach(({ placement, transform }) => {
it(placement, () => {
Expand Down Expand Up @@ -359,15 +359,15 @@ describe('rc-drawer-menu', () => {
);
errSpy.mockRestore();
});


it('pass data props to internal div', () => {
const value = 'bamboo';
const { unmount } = render(<Drawer open data-attr={value} />);
expect(document.querySelector('.rc-drawer-content-wrapper')).toHaveAttribute('data-attr',value);
expect(document.querySelector('.rc-drawer-content-wrapper')).toHaveAttribute('data-attr', value);
unmount();
});

it('support bodyProps', () => {
const enter = jest.fn();
const leave = jest.fn();
Expand All @@ -386,7 +386,7 @@ describe('rc-drawer-menu', () => {
});

it('pass id & className props to Panel', () => {
const { unmount } = render(<Drawer className='customer-className' id="customer-id" open/>);
const { unmount } = render(<Drawer className='customer-className' id="customer-id" open />);
expect(
document.querySelector('.rc-drawer-content')
).toHaveClass('customer-className');
Expand All @@ -401,7 +401,7 @@ describe('rc-drawer-menu', () => {
<Drawer classNames={{
wrapper: 'customer-wrapper',
mask: 'customer-mask',
}} open/>
}} open />
);
expect(
document.querySelector('.rc-drawer-content-wrapper')
Expand All @@ -411,4 +411,19 @@ describe('rc-drawer-menu', () => {
).toHaveClass('customer-mask');
unmount();
});
it('should support styles', () => {
const { unmount } = render(
<Drawer styles={{
wrapper: { background: 'red' },
mask: { background: 'blue' },
}} open />
);
expect(
document.querySelector('.rc-drawer-content-wrapper')
).toHaveStyle('background: red');
expect(
document.querySelector('.rc-drawer-mask')
).toHaveStyle('background: blue');
unmount();
});
});