-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'upstream/main'
- Loading branch information
Showing
4 changed files
with
88 additions
and
95 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
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,59 @@ | ||
import { useCallback, useState, useMemo } from 'react'; | ||
import { | ||
Keyboard, | ||
LayoutChangeEvent, | ||
LayoutRectangle, | ||
useWindowDimensions, | ||
ViewStyle, | ||
} from 'react-native'; | ||
|
||
function useDropdown(maxMenuHeight?: number, maxHeightFraction: number = 2.5) { | ||
const [enable, setEnable] = useState(false); | ||
const { height } = useWindowDimensions(); | ||
const finalMenuHeight = maxMenuHeight ?? height / maxHeightFraction; | ||
|
||
const [dropdownLayout, setDropdownLayout] = useState<LayoutRectangle>({ | ||
x: 0, | ||
y: 0, | ||
height: 0, | ||
width: 0, | ||
}); | ||
|
||
const toggleMenu = useCallback(() => { | ||
Keyboard.dismiss(); | ||
setEnable((prev) => !prev); | ||
}, []); | ||
|
||
const onLayout = useCallback( | ||
({ nativeEvent: { layout } }: LayoutChangeEvent) => { | ||
setDropdownLayout(layout); | ||
}, | ||
[] | ||
); | ||
|
||
const menuStyle: ViewStyle = useMemo( | ||
() => ({ | ||
width: dropdownLayout.width, | ||
}), | ||
[dropdownLayout.width] | ||
); | ||
|
||
const scrollViewStyle: ViewStyle = useMemo( | ||
() => ({ | ||
maxHeight: finalMenuHeight, | ||
}), | ||
[finalMenuHeight] | ||
); | ||
|
||
return { | ||
enable, | ||
setEnable, | ||
toggleMenu, | ||
onLayout, | ||
menuStyle, | ||
scrollViewStyle, | ||
dropdownLayout, | ||
}; | ||
} | ||
|
||
export default useDropdown; |