Skip to content

Commit 35e725f

Browse files
authored
fix(Modal): remove id on modal and select input scrolll (#5391)
1 parent 95ded82 commit 35e725f

File tree

6 files changed

+29
-11
lines changed

6 files changed

+29
-11
lines changed

.changeset/tasty-tables-type.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@ultraviolet/ui": patch
3+
---
4+
5+
Fix `<Modal />` and `<SelectInput />` together scroll down not using id but context

packages/ui/src/components/Drawer/__tests__/__snapshots__/index.test.tsx.snap

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -912,7 +912,6 @@ exports[`Drawer > renders with disclosure and onClose 1`] = `
912912
data-open="true"
913913
data-testid="test-backdrop"
914914
data-visible="true"
915-
id="backdrop-modal"
916915
>
917916
<dialog
918917
aria-label="drawer-test"

packages/ui/src/components/Modal/ModalProvider.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use client'
22

3-
import type { ReactNode, Ref } from 'react'
3+
import type { ReactNode, RefObject } from 'react'
44
import {
55
createContext,
66
useCallback,
@@ -11,7 +11,7 @@ import {
1111

1212
type ModalObject = {
1313
id: string
14-
ref: Ref<HTMLDialogElement>
14+
ref: RefObject<HTMLDialogElement | null>
1515
}
1616

1717
type ModalContextValues = {

packages/ui/src/components/Modal/__tests__/__snapshots__/index.test.tsx.snap

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,6 @@ exports[`Modal > renders with disclosure and onClose 1`] = `
526526
data-open="true"
527527
data-testid="test-backdrop"
528528
data-visible="true"
529-
id="backdrop-modal"
530529
>
531530
<dialog
532531
aria-label="modal-test"

packages/ui/src/components/Modal/components/Dialog.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ export const StyledDialog = styled('dialog', {
8181
width: ${MODAL_WIDTH.medium}rem;
8282
box-shadow: ${({ theme }) =>
8383
`${theme.shadows.overlay[0]}, ${theme.shadows.overlay[1]}`};
84-
84+
8585
8686
8787
${Object.entries(MODAL_WIDTH).map(
@@ -310,7 +310,6 @@ export const Dialog = ({
310310
data-open
311311
data-testid={dataTestId ? `${dataTestId}-backdrop` : undefined}
312312
data-visible={isVisible}
313-
id="backdrop-modal"
314313
onClick={handleClose}
315314
onFocus={stopFocus}
316315
>

packages/ui/src/components/SelectInput/Dropdown.tsx

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,16 @@ import type {
1010
RefObject,
1111
SetStateAction,
1212
} from 'react'
13-
import { useCallback, useEffect, useMemo, useRef, useState } from 'react'
13+
import {
14+
useCallback,
15+
useContext,
16+
useEffect,
17+
useMemo,
18+
useRef,
19+
useState,
20+
} from 'react'
1421
import { Checkbox } from '../Checkbox'
22+
import { ModalContext } from '../Modal/ModalProvider'
1523
import { Popup } from '../Popup'
1624
import { Skeleton } from '../Skeleton'
1725
import { Stack } from '../Stack'
@@ -708,6 +716,7 @@ export const Dropdown = ({
708716
const ref = useRef<HTMLDivElement>(null)
709717
const [search, setSearch] = useState<string>('')
710718
const [maxWidth, setWidth] = useState<string | number>()
719+
const modalContext = useContext(ModalContext)
711720

712721
useEffect(() => {
713722
if (refSelect.current && isDropdownVisible) {
@@ -716,12 +725,19 @@ export const Dropdown = ({
716725
DROPDOWN_MAX_HEIGHT +
717726
Number(theme.sizing[INPUT_SIZE_HEIGHT[size]].replace('rem', '')) * 16 +
718727
Number.parseInt(theme.space['5'], 10)
719-
const overflow = position - window.innerHeight
720-
if (overflow > 0) {
721-
const modalElement = document.getElementById('backdrop-modal')
728+
const overflow = position - window.innerHeight + 32
729+
if (overflow > 0 && modalContext) {
730+
const currentModal = modalContext.openedModals[0]
731+
const modalElement = currentModal?.ref.current
722732

723733
if (modalElement) {
724-
modalElement.scrollBy({ behavior: 'smooth', top: overflow })
734+
const parentElement = modalElement.parentNode as HTMLElement
735+
if (parentElement) {
736+
parentElement.scrollBy({
737+
behavior: 'smooth',
738+
top: overflow,
739+
})
740+
}
725741
} else window.scrollBy({ behavior: 'smooth', top: overflow })
726742
}
727743
}

0 commit comments

Comments
 (0)