Skip to content

Commit

Permalink
Open modal only on ref callback
Browse files Browse the repository at this point in the history
  • Loading branch information
tu55eladd committed Oct 9, 2023
1 parent 2553393 commit 6caab1d
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions src/felles-komponenter/modal/Modal.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Heading, Link, Modal as AkselModal } from '@navikt/ds-react';
import React, { MouseEventHandler, ReactNode, useEffect, useRef } from 'react';
import React, { MouseEventHandler, ReactNode, RefCallback, useEffect, useRef } from 'react';
import { useNavigate } from 'react-router-dom';

import { SerializedError } from '../../api/utils';
Expand All @@ -23,7 +23,7 @@ interface Props {
}

const Modal = (props: Props) => {
const modalRef = useRef<HTMLDialogElement | null>(null);
// const modalRef = useRef<HTMLDialogElement | null>(null);
const {
heading,
subHeading,
Expand All @@ -49,17 +49,21 @@ const Modal = (props: Props) => {
navigate(hovedsideRoute());
};

useEffect(() => {
if (modalRef.current) {
if (!modalRef.current.open) {
modalRef.current.showModal();
}
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [modalRef.current]);
const onRefSet: RefCallback<HTMLDialogElement> = (ref) => {
if (ref?.open) return;
ref?.showModal();
};
// useEffect(() => {
// if (modalRef.current) {
// if (!modalRef.current.open) {
// modalRef.current.showModal();
// }
// }
// // eslint-disable-next-line react-hooks/exhaustive-deps
// }, [modalRef.current]);

return (
<AkselModal ref={modalRef} portal onClose={closeFuncOrDefault} className="lg:w-120">
<AkselModal ref={onRefSet} portal onClose={closeFuncOrDefault} className="lg:w-120">
<AkselModal.Header closeButton={true} aria-labelledby="modal-heading">
<div className="space-y-2">
<Heading id="modal-heading" size="large">
Expand Down

0 comments on commit 6caab1d

Please sign in to comment.