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

Develop fix scroll shadow dom #2114

Merged
merged 2 commits into from
Jun 25, 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
32 changes: 19 additions & 13 deletions packages/ffe-modals-react/src/Modal.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import React, { useEffect, useImperativeHandle, useRef } from 'react';
import React, { useEffect, useImperativeHandle, useState, useRef } from 'react';
import classnames from 'classnames';
import { CloseButton } from './CloseButton';
import { Locale } from './types';

export interface ModalProps extends React.ComponentPropsWithoutRef<'dialog'> {
/** Id of modal heading */
ariaLabelledby: string;
/** Id of modal heading */
/** Locale used */
locale?: Locale;
/** Called when dialog is closed */
onClose?: () => void;
Expand All @@ -30,10 +30,12 @@ export const Modal = React.forwardRef<ModalHandle, ModalProps>(
},
ref,
) => {
const [isOpen, setIsOpen] = useState(false);
const modalRef = useRef<HTMLDialogElement>(null);

useImperativeHandle(ref, () => ({
open: () => {
setIsOpen(true);
modalRef.current?.showModal();
},
close: () => {
Expand All @@ -42,17 +44,17 @@ export const Modal = React.forwardRef<ModalHandle, ModalProps>(
}));

useEffect(() => {
const handleClose = () => {
onClose?.();
};
const modal = modalRef.current;

modal?.addEventListener('close', handleClose);

return () => {
modal?.removeEventListener('close', handleClose);
};
}, [onClose]);
const inShadow =
modalRef.current?.getRootNode() instanceof ShadowRoot;
if (inShadow) {
const html = document.documentElement;
if (isOpen) {
html.classList.add('ffe-modal__root');
} else {
html.classList.remove('ffe-modal__root');
}
}
}, [isOpen]);

return (
// eslint-disable-next-line jsx-a11y/no-noninteractive-element-interactions
Expand All @@ -68,6 +70,10 @@ export const Modal = React.forwardRef<ModalHandle, ModalProps>(
}
onClick?.(event);
}}
onClose={() => {
onClose?.();
setIsOpen(false);
}}
>
<div className="ffe-modal__body">
<CloseButton
Expand Down
3 changes: 2 additions & 1 deletion packages/ffe-modals/less/modal.less
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
@import 'theme';

:is(:root):has(.ffe-modal[open]) {
.ffe-modal__root,
:root:has(.ffe-modal[open]) {
overflow-y: hidden;
}

Expand Down
Loading