Skip to content

Commit

Permalink
diableDismiss and disableScrim props + examples
Browse files Browse the repository at this point in the history
  • Loading branch information
lukeac123 committed Feb 13, 2024
1 parent 40f0ff5 commit 024c3e7
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 23 deletions.
48 changes: 35 additions & 13 deletions packages/lab/src/drawer/Drawer.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
import {
ComponentPropsWithoutRef,
ForwardedRef,
forwardRef,
useEffect,
useState,
} from "react";
import { clsx } from "clsx";
import {
useClick,
useDismiss,
useInteractions,
useRole,
} from "@floating-ui/react";
import { useClick, useDismiss, useInteractions } from "@floating-ui/react";
import {
makePrefixer,
Scrim,
Expand All @@ -24,6 +18,17 @@ import { useWindow } from "@salt-ds/window";
import { useComponentCssInjection } from "@salt-ds/styles";
import drawerCss from "./Drawer.css";

interface ConditionalScrimWrapperProps extends React.PropsWithChildren {
condition: boolean;
}

export const ConditionalScrimWrapper = ({
condition,
children,
}: ConditionalScrimWrapperProps) => {
return condition ? <Scrim fixed> {children} </Scrim> : <>{children} </>;
};

export interface DrawerProps extends ComponentPropsWithoutRef<"div"> {
/**
* Defines the drawer position within the screen. Defaults to `left`.
Expand All @@ -41,6 +46,14 @@ export interface DrawerProps extends ComponentPropsWithoutRef<"div"> {
* Change background color palette
*/
variant?: "primary" | "secondary";
/**
* Prevent the dialog closing on click away
* */
disableDismiss?: boolean;
/**
* Prevent Scrim from rendering
* */
disableScrim?: boolean;
}

const withBaseName = makePrefixer("saltDrawer");
Expand All @@ -57,6 +70,9 @@ export const Drawer = forwardRef<HTMLDivElement, DrawerProps>(function Drawer(
onOpenChange,
variant = "primary",
id: idProp,
role: roleProp,
disableDismiss,
disableScrim,
...rest
} = props;

Expand All @@ -72,15 +88,16 @@ export const Drawer = forwardRef<HTMLDivElement, DrawerProps>(function Drawer(

const id = useId(idProp);

const { context, floating } = useFloatingUI({
const { context, floating, x, y, elements } = useFloatingUI({
open,
onOpenChange,
});

const role = roleProp ?? "dialog";

const { getFloatingProps } = useInteractions([
useRole(context, { role: "dialog" }),
useClick(context),
useDismiss(context),
useDismiss(context, { enabled: !disableDismiss }),
]);

const handleRef = useForkRef<HTMLDivElement>(floating, ref);
Expand All @@ -101,10 +118,15 @@ export const Drawer = forwardRef<HTMLDivElement, DrawerProps>(function Drawer(
return (
<>
{showComponent && (
<Scrim fixed>
<ConditionalScrimWrapper condition={!disableScrim}>
<FloatingComponent
open={open}
open={showComponent}
ref={handleRef}
role={role}
left={x ?? 0}
top={y ?? 0}
width={elements.floating?.offsetWidth}
height={elements.floating?.offsetHeight}
aria-modal="true"
aria-labelledby={`${id}-header`}
focusManagerProps={{
Expand All @@ -125,7 +147,7 @@ export const Drawer = forwardRef<HTMLDivElement, DrawerProps>(function Drawer(
>
{children}
</FloatingComponent>
</Scrim>
</ConditionalScrimWrapper>
)}
</>
);
Expand Down
18 changes: 13 additions & 5 deletions site/docs/components/drawer/examples.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,22 @@ Anchor a drawer to the bottom edge of the screen when using a master-detail layo

</LivePreview>

<LivePreview componentName="drawer" exampleName="OptionalCloseAction" >
<LivePreview componentName="drawer" exampleName="MandatoryAction" >

### Optional close action
### MandatoryAction

This example closes when the user clicks the **Submit** button instead of closing with the drawer's optional close button.
When present, the close button always appears on the top right corner of the drawer, it's sticky and doesn't scroll.
This example uses the `disableDismiss` prop to prevent the drawer closing on click away therefore ensuring the user takes an action within the drawer for it to close.
This example closes when the user clicks the **Submit** button.

In certain use cases, i.e., when user needs to complete form fields, an alternative action such as a 'submit and close' button, would be more appropriate than a close button.
When the optional close button is not shown the `diableDismiss` prop should be used.

</LivePreview>

<LivePreview componentName="drawer" exampleName="DisableScrim" >

### DisableScrim

Use the `disableScrim` prop to prevent the scrim from rendering

</LivePreview>
</LivePreviewControls>
23 changes: 23 additions & 0 deletions site/src/examples/drawer/DisableScrim.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { ReactElement, useState } from "react";

import { Drawer, DrawerCloseButton } from "@salt-ds/lab";
import { Button, StackLayout } from "@salt-ds/core";

export const DisableScrim = (): ReactElement => {
const [open, setOpen] = useState(false);

return (
<StackLayout>
<Button onClick={() => setOpen(true)}>Open Primary Drawer</Button>
<Drawer
open={open}
onOpenChange={(newOpen) => setOpen(newOpen)}
id="primary-drawer"
style={{ width: 200 }}
disableScrim
>
<DrawerCloseButton onClick={() => setOpen(false)} />
</Drawer>
</StackLayout>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
H2,
} from "@salt-ds/core";

export const OptionalCloseAction = (): ReactElement => {
export const MandatoryAction = (): ReactElement => {
const [open, setOpen] = useState(false);
const [value, setValue] = useState("");

Expand Down Expand Up @@ -45,15 +45,14 @@ export const OptionalCloseAction = (): ReactElement => {

return (
<>
<Button onClick={handleRequestOpen}>
Open Optional Close Action Drawer
</Button>
<Button onClick={handleRequestOpen}>Open Mandatory Action Drawer</Button>
<Drawer
open={open}
onOpenChange={onOpenChange}
position="right"
style={{ width: 500 }}
id={id}
disableDismiss
>
<StackLayout>
<H2 id={`${id}-header`}>Add your delivery details</H2>
Expand Down
3 changes: 2 additions & 1 deletion site/src/examples/drawer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ export * from "./TopDrawer";
export * from "./RightDrawer";
export * from "./BottomDrawer";
export * from "./Default";
export * from "./OptionalCloseAction";
export * from "./MandatoryAction";
export * from "./DisableScrim"

0 comments on commit 024c3e7

Please sign in to comment.