Skip to content

Commit

Permalink
[@mantine/core] Change Modal and Drawer to use native scrollbars by d…
Browse files Browse the repository at this point in the history
…efault, remove excessive markup (mantinedev#3854)
  • Loading branch information
rtivital committed Mar 28, 2023
1 parent 93687fa commit f585c32
Show file tree
Hide file tree
Showing 11 changed files with 24 additions and 36 deletions.
7 changes: 2 additions & 5 deletions docs/src/docs/core/Drawer.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,9 @@ By default, [ScrollArea.Autosize](/core/scroll-area/) component is used as scrol

<Demo data={DrawerDemos.overflow} />

## Native scrollbars
## Usage with ScrollArea

To use native scrollbars instead of [ScrollArea](/core/scroll-area/) component,
set `scrollAreaComponent={Drawer.NativeScrollArea}`:

<Demo data={DrawerDemos.nativeScroll} />
<Demo data={DrawerDemos.scrollarea} />

## Change transition

Expand Down
7 changes: 2 additions & 5 deletions docs/src/docs/core/Modal.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,9 @@ it will not scroll with the rest of the content.

<Demo data={ModalDemos.overflow} />

## Native scrollbars
## Usage with ScrollArea

To use native scrollbars instead of [ScrollArea](/core/scroll-area/) component,
set `scrollAreaComponent={Modal.NativeScrollArea}`:

<Demo data={ModalDemos.nativeScroll} />
<Demo data={ModalDemos.scrollarea} />

## Change offsets

Expand Down
5 changes: 1 addition & 4 deletions src/mantine-core/src/Drawer/Drawer.context.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import { createSafeContext } from '@mantine/utils';

export type ScrollAreaComponent = React.FC<{
mah: React.CSSProperties['maxHeight'];
children: React.ReactNode;
}>;
export type ScrollAreaComponent = React.FC<any>;

interface DrawerContext {
scrollAreaComponent: ScrollAreaComponent;
Expand Down
6 changes: 3 additions & 3 deletions src/mantine-core/src/Drawer/DrawerContent/DrawerContent.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import React, { forwardRef } from 'react';
import { useComponentDefaultProps } from '@mantine/styles';
import { ScrollArea } from '../../ScrollArea';
import { ModalBase, ModalBaseContentProps } from '../../ModalBase';
import { useDrawerContext, ScrollAreaComponent } from '../Drawer.context';

Expand All @@ -22,11 +21,12 @@ export const DrawerContent = forwardRef<HTMLElement, DrawerContentProps>((props,

const ctx = useDrawerContext();

const Scroll = scrollAreaComponent || ctx.scrollAreaComponent || ScrollArea.Autosize;
const Scroll: React.FC<any> =
scrollAreaComponent || ctx.scrollAreaComponent || ModalBase.NativeScrollArea;

return (
<ModalBase.Content ref={ref} radius={0} {...others}>
<Scroll mah="100vh">{children}</Scroll>
<Scroll style={{ height: '100vh' }}>{children}</Scroll>
</ModalBase.Content>
);
});
5 changes: 1 addition & 4 deletions src/mantine-core/src/Modal/Modal.context.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import { MantineNumberSize } from '@mantine/styles';
import { createSafeContext } from '@mantine/utils';

export type ScrollAreaComponent = React.FC<{
mah: React.CSSProperties['maxHeight'];
children: React.ReactNode;
}>;
export type ScrollAreaComponent = React.FC<any>;

interface ModalContext {
yOffset: string | number;
Expand Down
6 changes: 3 additions & 3 deletions src/mantine-core/src/Modal/ModalContent/ModalContent.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import React, { forwardRef } from 'react';
import { useComponentDefaultProps, rem } from '@mantine/styles';
import { ScrollArea } from '../../ScrollArea';
import { ModalBase, ModalBaseContentProps } from '../../ModalBase';
import { useModalContext, ScrollAreaComponent } from '../Modal.context';

Expand All @@ -21,11 +20,12 @@ export const ModalContent = forwardRef<HTMLElement, ModalContentProps>((props, r
);

const ctx = useModalContext();
const Scroll = scrollAreaComponent || ctx.scrollAreaComponent || ScrollArea.Autosize;
const Scroll: React.FC<any> =
scrollAreaComponent || ctx.scrollAreaComponent || ModalBase.NativeScrollArea;

return (
<ModalBase.Content ref={ref} radius={ctx.radius} {...others}>
<Scroll mah={`calc(100vh - (${rem(ctx.yOffset)} * 2))`}>{children}</Scroll>
<Scroll style={{ maxHeight: `calc(100vh - (${rem(ctx.yOffset)} * 2))` }}>{children}</Scroll>
</ModalBase.Content>
);
});
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';

export function NativeScrollArea({ children }) {
return <div>{children}</div>;
return <>{children}</>;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';
import { useDisclosure } from '@mantine/hooks';
import { Drawer, Group, Button } from '@mantine/core';
import { Drawer, Group, Button, ScrollArea } from '@mantine/core';
import { MantineDemo } from '@mantine/ds';

const code = `
Expand All @@ -20,7 +20,7 @@ function Demo() {
opened={opened}
onClose={close}
title="Header is sticky"
scrollAreaComponent={Drawer.NativeScrollArea}
scrollAreaComponent={ScrollArea.Autosize}
>
{content}
</Drawer>
Expand All @@ -46,7 +46,7 @@ function Demo() {
opened={opened}
onClose={close}
title="Header is sticky"
scrollAreaComponent={Drawer.NativeScrollArea}
scrollAreaComponent={ScrollArea.Autosize}
>
{content}
</Drawer>
Expand All @@ -58,7 +58,7 @@ function Demo() {
);
}

export const nativeScroll: MantineDemo = {
export const scrollarea: MantineDemo = {
type: 'demo',
code,
component: Demo,
Expand Down
2 changes: 1 addition & 1 deletion src/mantine-demos/src/demos/core/Drawer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ export { overlay } from './Drawer.demo.overlay';
export { composition } from './Drawer.demo.composition';
export { header } from './Drawer.demo.header';
export { initialFocus } from './Drawer.demo.initialFocus';
export { nativeScroll } from './Drawer.demo.nativeScroll';
export { scrollarea } from './Drawer.demo.scrollarea';
export { overflow } from './Drawer.demo.overflow';
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import React from 'react';
import { useDisclosure } from '@mantine/hooks';
import { Modal, Group, Button } from '@mantine/core';
import { Modal, Group, Button, ScrollArea } from '@mantine/core';
import { MantineDemo } from '@mantine/ds';

const code = `
import { useDisclosure } from '@mantine/hooks';
import { Modal, Group, Button } from '@mantine/core';
import { Modal, Group, Button, ScrollArea } from '@mantine/core';
function Demo() {
const [opened, { open, close }] = useDisclosure(false);
Expand All @@ -20,7 +20,7 @@ function Demo() {
opened={opened}
onClose={close}
title="Header is sticky"
scrollAreaComponent={Modal.NativeScrollArea}
scrollAreaComponent={ScrollArea.Autosize}
>
{content}
</Modal>
Expand All @@ -46,7 +46,7 @@ function Demo() {
opened={opened}
onClose={close}
title="Header is sticky"
scrollAreaComponent={Modal.NativeScrollArea}
scrollAreaComponent={ScrollArea.Autosize}
>
{content}
</Modal>
Expand All @@ -58,7 +58,7 @@ function Demo() {
);
}

export const nativeScroll: MantineDemo = {
export const scrollarea: MantineDemo = {
type: 'demo',
code,
component: Demo,
Expand Down
2 changes: 1 addition & 1 deletion src/mantine-demos/src/demos/core/Modal/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export { transitions } from './Modal.demo.transitions';
export { centered } from './Modal.demo.centered';
export { fullScreen } from './Modal.demo.fullScreen';
export { sizeAuto } from './Modal.demo.sizeAuto';
export { nativeScroll } from './Modal.demo.nativeScroll';
export { scrollarea } from './Modal.demo.scrollarea';
export { composition } from './Modal.demo.composition';
export { offset } from './Modal.demo.offset';
export { initialFocus } from './Modal.demo.initialFocus';
Expand Down

0 comments on commit f585c32

Please sign in to comment.