Skip to content

Commit

Permalink
fix(Modal): Change solution for setting width and fixed clicking on t…
Browse files Browse the repository at this point in the history
…op or bottom closing the Modal (#1171)

Co-authored-by: Michael Marszalek <[email protected]>
  • Loading branch information
AlbyIsCoding and mimarz authored Dec 1, 2023
1 parent 21dcf2a commit 22fbc6b
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 31 deletions.
8 changes: 8 additions & 0 deletions packages/react/src/components/Modal/Modal.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ Vi bruker native `autoFocus` på `Textfield` for å fokusere inputen i skjemaet.

<Canvas of={ModalStories.ModalWithForm} />

## Med egendefinert bredde

Bruk max-width for å sette egendefinert maks bredde på modalen. Default er 650px.

<Canvas of={ModalStories.ModalWithMaxWidth} />

### `Modal.Header`

<ArgTypes of={Modal.Header} />


6 changes: 3 additions & 3 deletions packages/react/src/components/Modal/Modal.module.css
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
.modal {
min-width: 650px;
max-width: min(650px, calc(100% - 6px - 2em));
padding: var(--fds-spacing-6) 0;
padding: 0;
width: 100%;
max-width: 650px;
border: none;
border-radius: var(--fds-border_radius-medium);
box-shadow: var(--fds-shadow-xlarge);
Expand Down
43 changes: 43 additions & 0 deletions packages/react/src/components/Modal/Modal.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,46 @@ export const ModalWithForm: StoryFn<typeof Modal> = () => {
</>
);
};

export const ModalWithMaxWidth: StoryFn<typeof Modal> = () => {
const modalRef = useRef<HTMLDialogElement>(null);
const [input, setInput] = useState('');

return (
<>
<Button onClick={() => modalRef.current?.showModal()}>Open Modal</Button>
<Modal
ref={modalRef}
onClose={() => setInput('')}
style={{ maxWidth: '1200px' }}
>
<Modal.Header>Modal med skjema</Modal.Header>
<Modal.Content>
<Textfield
label='Navn'
placeholder='Ola Nordmann'
value={input}
autoFocus
onChange={(e) => setInput(e.target.value)}
/>
</Modal.Content>
<Modal.Footer>
<Button
onClick={() => {
window.alert(`Du har sendt inn skjema med navn: ${input}`);
modalRef.current?.close();
}}
>
Send inn skjema
</Button>
<Button
variant='secondary'
onClick={() => modalRef.current?.close()}
>
Avbryt
</Button>
</Modal.Footer>
</Modal>
</>
);
};
26 changes: 1 addition & 25 deletions packages/react/src/components/Modal/Modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,6 @@ export type ModalProps = {
* @default undefined
*/
onClose?: () => void;
/**
* The width of the modal.
* Will go to full width below the specified width.
* @default '650px'
*/
/* width?: string; */
/**
* Called before the modal is closed when using the close button, `closeOnBackdropClick` or `ESCAPE`.
* If the function returns `false` the modal will not close.
Expand All @@ -42,23 +36,12 @@ export type ModalProps = {
export const ModalContext = createContext<ModalContextProps | null>(null);

export const Modal = forwardRef<HTMLDialogElement, ModalProps>(
(
{
onInteractOutside,
onClose,
/* width = '650px', */
onBeforeClose,
children,
...props
},
ref,
) => {
({ onInteractOutside, onClose, onBeforeClose, children, ...props }, ref) => {
const modalRef = useRef<HTMLDialogElement>(null);
const mergedRefs = useMergeRefs([modalRef, ref]);
const { context } = useFloating();
useScrollLock(modalRef, classes.lockScroll);
const open = useModalState(modalRef);
/* const belowWidth = useMediaQuery(`(max-width: ${width})`); */

useEffect(() => {
const currentModalRef = modalRef.current;
Expand Down Expand Up @@ -122,13 +105,6 @@ export const Modal = forwardRef<HTMLDialogElement, ModalProps>(
ref={mergedRefs}
{...props}
className={cn(classes.modal, props.className)}
style={{
/* minWidth: belowWidth ? '100%' : width,
maxWidth: belowWidth
? '100%'
: `min(${width}, calc(100% - 6px - 2em))`, */
...props.style,
}}
onCancel={onCancel}
>
{open && (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
display: flex;
align-items: center;
gap: var(--fds-spacing-4);
padding: var(--fds-spacing-3) var(--fds-spacing-6);
padding-bottom: 0;
padding-top: var(--fds-spacing-3);
padding-bottom: var(--fds-spacing-6);
padding-left: var(--fds-spacing-6);
padding-right: var(--fds-spacing-6);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
display: flex;
justify-content: space-between;
flex-direction: column;
padding-top: 0;
padding-top: var(--fds-spacing-6);
padding-bottom: var(--fds-spacing-2);
padding-left: var(--fds-spacing-6);
padding-right: var(--fds-spacing-18);
Expand Down

0 comments on commit 22fbc6b

Please sign in to comment.