forked from aeharding/voyager
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix create comment, edit comment, create post text area being cut off (…
…aeharding#332) * Fix create comment, edit comment, create post text area being cut off This is really fragile logic, but I've tried to abstract it to IonModalAutosizedForOnScreenKeyboard to keep it modular at least... * Abstract TextareaAutosizedForOnScreenKeyboard
- Loading branch information
Showing
6 changed files
with
164 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
src/features/shared/IonModalAutosizedForOnScreenKeyboard.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import { IonModal } from "@ionic/react"; | ||
import React, { useCallback, useEffect, useRef, useState } from "react"; | ||
import usePageVisibility from "../../helpers/usePageVisibility"; | ||
import styled from "@emotion/styled"; | ||
|
||
// TODO it's a bit buggy trying to compute this | ||
// in realtime with the new post dialog + comment dialogs | ||
// So hardcode for now | ||
const FIXED_HEADER_HEIGHT = 56; | ||
|
||
const StyledIonModal = styled(IonModal)<{ viewportHeight: number }>` | ||
ion-content::part(scroll) { | ||
max-height: ${({ viewportHeight }) => viewportHeight}px; | ||
} | ||
`; | ||
|
||
export default function IonModalAutosizedForOnScreenKeyboard( | ||
props: React.ComponentProps<typeof IonModal> | ||
) { | ||
const [viewportHeight, setViewportHeight] = useState( | ||
document.documentElement.clientHeight | ||
); | ||
const isVisible = usePageVisibility(); | ||
// eslint-disable-next-line no-undef | ||
const modalRef = useRef<HTMLIonModalElement>(null); | ||
|
||
const updateViewport = useCallback(() => { | ||
if (!props.isOpen) return; | ||
|
||
// For the rare legacy browsers that don't support it | ||
if (!window.visualViewport) { | ||
return; | ||
} | ||
|
||
const page = modalRef.current?.querySelector( | ||
".ion-page:not(.ion-page-hidden)" | ||
); | ||
|
||
setViewportHeight( | ||
window.visualViewport.height - | ||
(page instanceof HTMLElement ? cumulativeOffset(page).top : 0) - | ||
FIXED_HEADER_HEIGHT | ||
); | ||
}, [props.isOpen]); | ||
|
||
const onScroll = useCallback(() => { | ||
setTimeout(() => { | ||
window.scrollTo(0, 0); | ||
}, 100); | ||
}, []); | ||
|
||
// Turning iPhone on/off can mess up the scrolling to top again | ||
useEffect(() => { | ||
if (!props.isOpen) return; | ||
|
||
updateViewport(); | ||
}, [isVisible, updateViewport, props.isOpen]); | ||
|
||
useEffect(() => { | ||
if (!props.isOpen) return; | ||
|
||
document.addEventListener("scroll", onScroll); | ||
|
||
return () => { | ||
document.removeEventListener("scroll", onScroll); | ||
}; | ||
}, [onScroll, props.isOpen]); | ||
|
||
useEffect(() => { | ||
if (!props.isOpen) return; | ||
|
||
const onResize = () => { | ||
updateViewport(); | ||
}; | ||
|
||
window.visualViewport?.addEventListener("resize", onResize); | ||
|
||
return () => { | ||
window.visualViewport?.removeEventListener("resize", onResize); | ||
}; | ||
}, [updateViewport, props.isOpen]); | ||
|
||
return ( | ||
<StyledIonModal | ||
ref={modalRef} | ||
viewportHeight={viewportHeight} | ||
onDidPresent={() => { | ||
window.scrollTo(0, 0); | ||
}} | ||
{...props} | ||
/> | ||
); | ||
} | ||
|
||
function cumulativeOffset(element: HTMLElement) { | ||
let top = 0, | ||
left = 0; | ||
do { | ||
top += element.offsetTop || 0; | ||
left += element.offsetLeft || 0; | ||
element = element.offsetParent as HTMLElement; | ||
} while (element instanceof HTMLElement); | ||
|
||
return { | ||
top: top, | ||
left: left, | ||
}; | ||
} |
29 changes: 29 additions & 0 deletions
29
src/features/shared/TextareaAutosizedForOnScreenKeyboard.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import React from "react"; | ||
import { isAppleDeviceInstalledToHomescreen } from "../../helpers/device"; | ||
import { fixSafariAutoscroll } from "../../helpers/safari"; | ||
import TextareaAutosize, { | ||
TextareaAutosizeProps, | ||
} from "react-textarea-autosize"; | ||
|
||
export default function TextareaAutosizedForOnScreenKeyboard( | ||
props: Omit< | ||
TextareaAutosizeProps & React.RefAttributes<HTMLTextAreaElement>, | ||
"onFocus" | ||
> | ||
) { | ||
return ( | ||
<TextareaAutosize | ||
onFocus={(e) => { | ||
if (!isAppleDeviceInstalledToHomescreen()) return; | ||
|
||
// https://stackoverflow.com/a/74902393/1319878 | ||
const target = e.currentTarget; | ||
target.style.opacity = "0"; | ||
setTimeout(() => (target.style.opacity = "1")); | ||
|
||
fixSafariAutoscroll(); | ||
}} | ||
{...props} | ||
/> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
export function fixSafariAutoscroll() { | ||
let checkAttempts = 0; | ||
|
||
const interval = setInterval(() => { | ||
window.scrollTo(0, 0); | ||
|
||
if (window.scrollY === 0) { | ||
checkAttempts++; | ||
|
||
if (checkAttempts > 10) clearInterval(interval); | ||
} | ||
}, 100); | ||
} |