Skip to content
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

Click on modal scrollbar no longer closes modal #785

Merged
merged 2 commits into from
Sep 17, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useRef } from "react";
import { useMultiOnClickOutside } from "../UseMultiOnClickOutside";
import { vi } from "vitest";

describe("useOnClickOutside", () => {
describe("useMultiOnClickOutside", () => {
const options = {
altKey: true,
bubbles: true,
Expand Down
85 changes: 53 additions & 32 deletions packages/modal/src/dialog/UseDialog.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import cx from "classnames";
import React, {
CSSProperties,
MouseEventHandler,
ReactNode,
RefObject,
useCallback,
Expand Down Expand Up @@ -75,6 +76,7 @@ export function useDialog<TProps, TPromiseResolve = void>(

const resolve = useCallback<ResolveCommand<TPromiseResolve>>(
(value) => {
// Trigger closing animation.
setClosing(true);
currentRef.current?.addEventListener(
"animationend",
Expand All @@ -92,8 +94,18 @@ export function useDialog<TProps, TPromiseResolve = void>(
[currentRef, options]
);

const onClose = useCallback(() => {
// Remove content immediately, since it cannot be animated when closed by browser.
setClosing(false);
setContentVisible(false);
rejectRef.current?.();
options.onReject?.();
modalComponentProps.current = undefined;
}, [options]);

const reject = useCallback<RejectCommand>(
(error) => {
// Trigger closing animation.
setClosing(true);
currentRef.current?.addEventListener(
"animationend",
Expand All @@ -111,53 +123,62 @@ export function useDialog<TProps, TPromiseResolve = void>(
[currentRef, options]
);

const onClick = useCallback<MouseEventHandler<HTMLDialogElement>>(
(e) => {
if (e.target !== currentRef.current) {
return;
}

const rect = currentRef.current.getBoundingClientRect();

const clickedInsideDialog =
rect.top <= e.clientY &&
e.clientY <= rect.top + rect.height &&
rect.left <= e.clientX &&
e.clientX <= rect.left + rect.width;

if (!clickedInsideDialog) {
reject();
}
},
[currentRef, reject]
);

const dialogElement = useMemo<ReactNode>(
() => (
<DialogContext.Provider value={{ resolve, reject }}>
<dialog
onMouseDown={
options.disableCloseOnClickOutside ? undefined : () => reject()
}
ref={currentRef}
className={cx(options.className, closing && options.closingClassName)}
style={options.dialogStyle}
<dialog
onClick={options.disableCloseOnClickOutside ? undefined : onClick}
onClose={onClose}
ref={currentRef}
className={cx(options.className, closing && options.closingClassName)}
style={options.dialogStyle}
>
<div
style={options.contentWrapperStyle}
className={options.contentWrapperClassName}
>
{options.disableCloseOnClickOutside ? (
<>
{contentVisible && (
<Comp {...(modalComponentProps.current as TProps)} key={key} />
)}
</>
) : (
<div
style={options.contentWrapperStyle}
className={options.contentWrapperClassName}
onMouseDown={
options.disableCloseOnClickOutside
? undefined
: (ev) => ev.stopPropagation()
}
>
{contentVisible && (
<Comp {...(modalComponentProps.current as TProps)} key={key} />
)}
</div>
{contentVisible && (
<DialogContext.Provider value={{ resolve, reject }}>
<Comp {...(modalComponentProps.current as TProps)} key={key} />
</DialogContext.Provider>
)}
</dialog>
</DialogContext.Provider>
</div>
</dialog>
),
[
resolve,
reject,
options.disableCloseOnClickOutside,
options.className,
options.closingClassName,
options.dialogStyle,
options.contentWrapperStyle,
options.contentWrapperClassName,
onClick,
onClose,
currentRef,
closing,
contentVisible,
resolve,
reject,
Comp,
key,
]
Expand Down
11 changes: 11 additions & 0 deletions packages/modal/src/dialog/modal/ModalDialog.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,17 @@

@media (min-width: 769px) {
border-radius: var(--swui-border-radius-large);

&::-webkit-scrollbar-thumb {
background-color: var(--lhds-color-ui-400);
border: 4px solid transparent;
border-radius: 8px;
background-clip: padding-box;
}

&::-webkit-scrollbar {
width: 16px;
}
}

&:not([open]) {
Expand Down
Loading