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

Fix remove class in body #85

Merged
merged 8 commits into from
Jul 5, 2023
Merged

Conversation

VadymBezsmertnyi
Copy link
Contributor

Added dependencies for class removing during 'isOpen' prop changes

@@ -33,10 +33,17 @@ function PureModal(props: Props) {
const [mouseOffsetY, setMouseOffsetY] = useState(0);

const { isOpen, onClose } = props;
const isClassBody = useMemo(() => document.body.classList.contains('body-modal-fix'), [isOpen]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bad idea, you will have expensive layout on each re-render

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, I've fixed it.


useEffect(() => {
if (isOpen) {
open();
} else if (isClassBody) {
removeClassBody();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, you already have it in function unsetModalContext so just check why that function doesn't run when you close the modal

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct, but it is not triggered in the code when isOpen changes, so we will invoke it through the effect.

@@ -71,7 +66,7 @@ function PureModal(props: Props) {

function unsetModalContext() {
document.removeEventListener('keydown', handleEsc);
removeClassBody();
document.body.classList.remove('body-modal-fix');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

тобто було гарно одною функцією, а стало тепер інлайново в двох місцях?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Згоден, не вірно зрозумів спочатку.


useEffect(() => {
if (isOpen) {
open();
} else if (isClassBody) {
removeClassBody();
} else if (document.body.classList.contains('body-modal-fix')) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Тут проблема, що isClassBody має неправильне значення?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Зрозумів, що useMemo може зробити не вірно при перерендерах кожного разу тай лишні перестворення функції.

@VadymBezsmertnyi
Copy link
Contributor Author

Скоротив до простого і зрозумілого функціоналу.

useEffect(() => {
if (isOpen) {
open();
} else {
unsetModalContext();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

check if you can call close function

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, the code does look more logical.

@max-mykhailenko max-mykhailenko merged commit 51f88e5 into Memcrab:master Jul 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants