Skip to content

Commit

Permalink
Merge pull request #147 from onmotion/fix/measurement-inside-modal
Browse files Browse the repository at this point in the history
fix: measurement inside ios modal
  • Loading branch information
onmotion authored Nov 22, 2024
2 parents 46c1f51 + b68539f commit 7df36a4
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 8 deletions.
2 changes: 1 addition & 1 deletion example/components/RemoteDataSetExample.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export const RemoteDataSetExample = memo((props: Omit<IAutocompleteDropdownProps
useFilter={false}
clearOnFocus={false}
textInputProps={{
placeholder: 'Start typing "est"...',
placeholder: 'Start typing "lorem"...',
}}
onSelectItem={setSelectedItem}
loading={loading}
Expand Down
16 changes: 11 additions & 5 deletions src/AutocompleteDropdownContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export const AutocompleteDropdownContextProvider: FC<IAutocompleteDropdownContex
undefined,
)
const activeInputContainerRef = useRef<View>(null)
const wrapperRef = useRef<View>(null)
const activeControllerRef = useRef<IAutocompleteDropdownRef | null>(null)
const controllerRefs = useRef<IAutocompleteDropdownRef[]>([])
const positionTrackingIntervalRef = useRef<NodeJS.Timeout>()
Expand Down Expand Up @@ -83,8 +84,10 @@ export const AutocompleteDropdownContextProvider: FC<IAutocompleteDropdownContex
useEffect(() => {
if (content) {
activeInputContainerRef?.current?.measure((x, y, width, height, pageX, pageY) => {
setInputMeasurements({ x: pageX, y: pageY, width, height })
setShow(true)
wrapperRef.current?.measure((wrapperX, wrapperY, wrapperW, wrapperH, wrapperPageX, wrapperPageY) => {
setInputMeasurements({ x: pageX, y: pageY - wrapperPageY, width, height })
setShow(true)
})
})
} else {
setInputMeasurements(undefined)
Expand All @@ -99,12 +102,14 @@ export const AutocompleteDropdownContextProvider: FC<IAutocompleteDropdownContex
if (show && !!opacity) {
positionTrackingIntervalRef.current = setInterval(() => {
requestAnimationFrame(() => {
activeInputContainerRef?.current &&
activeInputContainerRef?.current?.measure((_x, _y, width, height, x, y) => {
activeInputContainerRef?.current?.measure((_x, _y, width, height, inputPageX, inputPageY) => {
wrapperRef.current?.measure((wrapperX, wrapperY, wrapperW, wrapperH, wrapperPageX, wrapperPageY) => {
const currentMeasurement = { x: inputPageX, y: inputPageY - wrapperPageY, width, height }
setInputMeasurements(prev =>
JSON.stringify(prev) === JSON.stringify({ x, y, width, height }) ? prev : { x, y, width, height },
JSON.stringify(prev) === JSON.stringify(currentMeasurement) ? prev : currentMeasurement,
)
})
})
})
}, 16)
} else {
Expand Down Expand Up @@ -132,6 +137,7 @@ export const AutocompleteDropdownContextProvider: FC<IAutocompleteDropdownContex
controllerRefs,
}}>
<View
ref={wrapperRef}
style={styles.clickOutsideHandlerArea}
onTouchEnd={() => {
activeControllerRef.current?.close()
Expand Down
2 changes: 0 additions & 2 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,7 @@ export const AutocompleteDropdown = memo<
return new Promise<void>(resolve => {
setTimeout(
() => {
Keyboard.isVisible()
const kbHeight = Keyboard.metrics()?.height || 0
console.log({ kbHeight })
const screenHeight = Dimensions.get('window').height
setDirection((screenHeight - kbHeight) / 2 > positionY ? 'down' : 'up')
resolve()
Expand Down

0 comments on commit 7df36a4

Please sign in to comment.