Skip to content

Commit fcb14f7

Browse files
committed
feat: add bodyScrolling to Drawer
1 parent 4ece538 commit fcb14f7

File tree

3 files changed

+38
-2
lines changed

3 files changed

+38
-2
lines changed

src/lib/drawer/Drawer.svelte

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import { sineIn } from "svelte/easing";
55
import { drawer } from ".";
66
7-
let { children, hidden = $bindable(), closeDrawer = () => (hidden = true), activateClickOutside = true, position, width, backdrop = true, backdropClass, placement = "left", class: className, transitionParams, transitionType = fly, ...restProps }: DrawerProps = $props();
7+
let { children, hidden = $bindable(), closeDrawer = () => (hidden = true), activateClickOutside = true, position, width, backdrop = true, backdropClass, placement = "left", class: className, transitionParams, transitionType = fly, bodyScrolling = false, ...restProps }: DrawerProps = $props();
88
99
const { base, backdrop_: backdropCls } = $derived(
1010
drawer({
@@ -27,7 +27,7 @@
2727
<svelte:window bind:innerWidth bind:innerHeight />
2828

2929
{#if !hidden}
30-
<div role="presentation" class={cn(backdropCls(), backdropClass)} onclick={activateClickOutside ? closeDrawer : undefined}></div>
30+
<div role="presentation" class={cn(backdropCls(), backdropClass)} onclick={activateClickOutside ? closeDrawer : undefined} style={bodyScrolling ? "pointer-events: none;" : ""}></div>
3131
<div use:trapFocus={{ onEscape: closeDrawer }} {...restProps} class={cn(base(), className)} transition:transitionType={transitionParams ? transitionParams : (transition_params as ParamsType)} tabindex="-1">
3232
{@render children?.()}
3333
</div>

src/lib/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,7 @@ export interface DrawerProps extends DrawerVariants, HTMLAttributes<HTMLDivEleme
583583
backdropClass?: ClassValue;
584584
transitionParams?: ParamsType;
585585
transitionType?: TransitionFunc;
586+
bodyScrolling?: boolean;
586587
}
587588

588589
export interface DrawerheadProps extends HTMLButtonAttributes {

src/routes/docs/components/drawer.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,41 @@ As the default, the drawer closes when you click the outside of the drawer. Howe
566566
</Drawer>
567567
```
568568

569+
## Body scrolling
570+
571+
By default, body scrolling is disabled when the drawer is visible, however, you can choose to enable it using the `bodyScrolling={true|false}` prop.
572+
573+
**Limitation:** When bodyScrolling={true}, clicking the backdrop to close the drawer is disabled. Use the Escape key or provide a close button inside the drawer content instead.
574+
575+
```svelte example hideResponsiveButtons class="h-48"
576+
<script>
577+
import { Drawer, Button, CloseButton } from "flowbite-svelte";
578+
import { InfoCircleSolid, ArrowRightOutline } from "flowbite-svelte-icons";
579+
let drawerHidden = $state(true);
580+
</script>
581+
582+
<div class="text-center">
583+
<Button onclick={() => (drawerHidden = false)}>Show drawer</Button>
584+
</div>
585+
586+
<Drawer bind:hidden={drawerHidden} bodyScrolling={true}>
587+
<div class="flex items-center justify-between">
588+
<h5 id="drawer-label" class="mb-4 inline-flex items-center text-base font-semibold text-gray-500 dark:text-gray-400">
589+
<InfoCircleSolid class="me-2.5 h-5 w-5" />Info
590+
</h5>
591+
<CloseButton onclick={() => (drawerHidden = true)} class="mb-4 dark:text-white" />
592+
</div>
593+
<p class="mb-6 text-sm text-gray-500 dark:text-gray-400">
594+
Supercharge your hiring by taking advantage of our <a href="/" class="text-primary-600 dark:text-primary-500 underline hover:no-underline">limited-time sale</a>
595+
for Flowbite Docs + Job Board. Unlimited access to over 190K top-ranked candidates and the #1 design job board.
596+
</p>
597+
<div class="grid grid-cols-2 gap-4">
598+
<Button color="light" href="/">Learn more</Button>
599+
<Button href="/" class="px-4">Get access <ArrowRightOutline class="ms-2 h-5 w-5" /></Button>
600+
</div>
601+
</Drawer>
602+
```
603+
569604
## Component data
570605

571606
The component has the following props, type, and default values. See [types page](/docs/pages/typescript) for type information.

0 commit comments

Comments
 (0)