|
| 1 | +import React, { forwardRef, useMemo } from "react"; |
| 2 | +import { Platform, StyleSheet, View as RNView } from "react-native"; |
| 3 | + |
| 4 | +import KeyboardStickyView from "../KeyboardStickyView"; |
| 5 | + |
| 6 | +import type { KeyboardStickyViewProps } from "../KeyboardStickyView"; |
| 7 | +import type { View } from "react-native"; |
| 8 | + |
| 9 | +const KEYBOARD_HAS_ROUNDED_CORNERS = |
| 10 | + Platform.OS === "ios" && parseInt(Platform.Version, 10) >= 26; |
| 11 | + |
| 12 | +export type KeyboardEffectsProps = KeyboardStickyViewProps; |
| 13 | + |
| 14 | +/** |
| 15 | + * A component that renders content behind the keyboard. Since the keyboard |
| 16 | + * is translucent, the content (colors, gradients, animations) will blend |
| 17 | + * through and create visual effects on the keyboard. |
| 18 | + * |
| 19 | + * On iOS 26+ the keyboard has rounded corners, and the effect view |
| 20 | + * automatically matches that shape. |
| 21 | + * |
| 22 | + * @returns A view component positioned behind the keyboard. |
| 23 | + * @example |
| 24 | + * ```tsx |
| 25 | + * <KeyboardEffects> |
| 26 | + * <View style={{ flex: 1, backgroundColor: "purple" }} /> |
| 27 | + * </KeyboardEffects> |
| 28 | + * ``` |
| 29 | + */ |
| 30 | +const KeyboardEffects = forwardRef< |
| 31 | + View, |
| 32 | + React.PropsWithChildren<KeyboardEffectsProps> |
| 33 | +>(({ children, ...props }, ref) => { |
| 34 | + const containerStyle = useMemo( |
| 35 | + () => [styles.container, KEYBOARD_HAS_ROUNDED_CORNERS && styles.rounded], |
| 36 | + [], |
| 37 | + ); |
| 38 | + |
| 39 | + return ( |
| 40 | + <KeyboardStickyView ref={ref} {...props}> |
| 41 | + <RNView style={containerStyle}>{children}</RNView> |
| 42 | + </KeyboardStickyView> |
| 43 | + ); |
| 44 | +}); |
| 45 | + |
| 46 | +const styles = StyleSheet.create({ |
| 47 | + container: { |
| 48 | + position: "absolute", |
| 49 | + bottom: 0, |
| 50 | + top: 0, |
| 51 | + left: 0, |
| 52 | + right: 0, |
| 53 | + height: 999, |
| 54 | + }, |
| 55 | + rounded: { |
| 56 | + borderTopLeftRadius: 30, |
| 57 | + borderTopRightRadius: 30, |
| 58 | + overflow: "hidden", |
| 59 | + }, |
| 60 | +}); |
| 61 | + |
| 62 | +export default KeyboardEffects; |
0 commit comments