#767 resolve issue of keyboard when opening drawer on iphone

This commit is contained in:
lethemanh
2026-04-10 15:52:05 +07:00
parent e5687feaeb
commit 99242af538
@@ -54,14 +54,54 @@ export const SmallTimezoneSelector: React.FC<
const selectedRef = useRef<HTMLDivElement>(null) const selectedRef = useRef<HTMLDivElement>(null)
const inputRef = useRef<HTMLInputElement>(null) const inputRef = useRef<HTMLInputElement>(null)
const paperRef = useRef<HTMLDivElement>(null)
useEffect((): void => { useEffect((): (() => void) | void => {
if (open) { if (!open) return
inputRef.current?.focus()
selectedRef.current?.scrollIntoView({ const viewport = window.visualViewport
behavior: 'auto',
block: 'center' const paper = paperRef.current
})
const adjustForKeyboard = (): void => {
if (!paper || !viewport) return
// viewport.offsetTop > 0 means iOS has scrolled the page to reveal the
// focused element (triggered by programmatic focus). When the user taps
// manually, the keyboard floats over content without scrolling the page,
// so offsetTop stays 0 and we should not resize the drawer.
if (viewport.offsetTop <= 0) {
paper.style.bottom = ''
paper.style.height = ''
return
}
const bottom = Math.max(
0,
window.innerHeight - viewport.offsetTop - viewport.height
)
paper.style.bottom = `${bottom}px`
paper.style.height = `${viewport.height}px`
}
if (viewport) {
// Register before calling focus so the listener fires as the keyboard opens.
viewport.addEventListener('resize', adjustForKeyboard)
viewport.addEventListener('scroll', adjustForKeyboard)
}
inputRef.current?.focus()
selectedRef.current?.scrollIntoView({ behavior: 'auto', block: 'center' })
return (): void => {
if (viewport) {
viewport.removeEventListener('resize', adjustForKeyboard)
viewport.removeEventListener('scroll', adjustForKeyboard)
}
if (paper) {
paper.style.bottom = ''
paper.style.height = ''
}
} }
}, [open]) }, [open])
@@ -74,14 +114,14 @@ export const SmallTimezoneSelector: React.FC<
disableAutoFocus disableAutoFocus
slotProps={{ slotProps={{
paper: { paper: {
sx: { height: '90%' } ref: paperRef,
sx: { height: '90%', maxHeight: '90dvh' }
} }
}} }}
> >
<Box sx={{ px: 2 }}> <Box sx={{ px: 2 }}>
<TextField <TextField
inputRef={inputRef} inputRef={inputRef}
autoFocus
fullWidth fullWidth
variant="standard" variant="standard"
placeholder={t('calendar.searchTimezone')} placeholder={t('calendar.searchTimezone')}
@@ -91,7 +131,10 @@ export const SmallTimezoneSelector: React.FC<
) => setSearchQuery(e.target.value)} ) => setSearchQuery(e.target.value)}
slotProps={{ slotProps={{
htmlInput: { htmlInput: {
'aria-label': t('calendar.searchTimezone') 'aria-label': t('calendar.searchTimezone'),
autoComplete: 'off',
inputMode: 'search',
enterKeyHint: 'search'
}, },
input: { input: {
startAdornment: ( startAdornment: (
@@ -104,7 +147,8 @@ export const SmallTimezoneSelector: React.FC<
}} }}
sx={{ sx={{
'& .MuiInputBase-root': { '& .MuiInputBase-root': {
padding: '8px 0' padding: '8px 0',
fontSize: '16px'
} }
}} }}
/> />