diff --git a/README.md b/README.md index a89b561..7b089fd 100644 --- a/README.md +++ b/README.md @@ -58,8 +58,8 @@ TODO | containerStyles | ViewStyle | {} | no | Override container styles | | hideHeader | boolean | false | no | Hide category names | | expandable | boolean | true | no | Show knob and enable expand on swipe up | -| defaultHeight | number | 0.4 | no | Specify collapsed container height (1 is full screen height) | -| expandedHeight | number | 0.8 | no | Specify expanded container height (1 is full screen height) *only if expandable is true* | +| defaultHeight | number \| string | "40%" | no | Specify collapsed container height (number is points, string is a percentage of the screen height) | +| expandedHeight | number \| string | "80%" | no | Specify expanded container height (number is points, string is a percentage of the screen height) _works only if expandable is true_ | | backdropColor | string | "#00000055" | no | Change backdrop color and alpha | | categoryColor | string | "#000000" | no | Change category item color | | activeCategoryColor | string | "#005b96" | no | Change active category item color | diff --git a/example/src/App.tsx b/example/src/App.tsx index 49a759c..ce76d76 100644 --- a/example/src/App.tsx +++ b/example/src/App.tsx @@ -26,6 +26,8 @@ export default function App() { onEmojiSelected={handlePick} open={isModalOpen} onClose={() => setIsModalOpen(false)} + defaultHeight={100} + expandedHeight={200} /> {/* //////////////////////////////////////////// */} diff --git a/src/EmojiPicker.tsx b/src/EmojiPicker.tsx index 04e95c8..afd3759 100644 --- a/src/EmojiPicker.tsx +++ b/src/EmojiPicker.tsx @@ -6,6 +6,7 @@ import { defaultKeyboardContext, KeyboardProvider } from './KeyboardProvider'; import type { KeyboardProps } from './KeyboardContext'; import type { EmojiType } from './types'; import { ModalWithBackdrop } from './components/ModalWithBackdrop'; +import { getHeight } from './utils'; export const EmojiPicker = ({ onEmojiSelected, @@ -18,7 +19,7 @@ export const EmojiPicker = ({ const { height: screenHeight } = useWindowDimensions(); const offsetY = React.useRef(new Animated.Value(0)).current; const height = React.useRef( - new Animated.Value(screenHeight * defaultHeight) + new Animated.Value(getHeight(defaultHeight, screenHeight)) ).current; const translateY = React.useRef(new Animated.Value(0)).current; diff --git a/src/KeyboardContext.tsx b/src/KeyboardContext.tsx index 3e18546..b375178 100644 --- a/src/KeyboardContext.tsx +++ b/src/KeyboardContext.tsx @@ -16,8 +16,8 @@ export type KeyboardProps = { headerStyles?: TextStyle; expandable?: boolean; hideHeader?: boolean; - defaultHeight?: number; - expandedHeight?: number; + defaultHeight?: number | string; + expandedHeight?: number | string; backdropColor?: string; categoryColor?: string; activeCategoryColor?: string; diff --git a/src/KeyboardProvider.tsx b/src/KeyboardProvider.tsx index f93790a..7468817 100644 --- a/src/KeyboardProvider.tsx +++ b/src/KeyboardProvider.tsx @@ -21,8 +21,8 @@ export const defaultKeyboardContext: Required = { headerStyles: {}, expandable: true, hideHeader: false, - defaultHeight: 0.4, - expandedHeight: 0.8, + defaultHeight: '40%', + expandedHeight: '80%', backdropColor: '#00000055', categoryColor: '#000000', activeCategoryColor: '#005b96', diff --git a/src/components/Knob.tsx b/src/components/Knob.tsx index 656761c..236b15c 100644 --- a/src/components/Knob.tsx +++ b/src/components/Knob.tsx @@ -5,6 +5,7 @@ import { PanGestureHandlerGestureEvent, State, } from 'react-native-gesture-handler'; +import { getHeight } from '../utils'; import { KeyboardContext } from '../KeyboardContext'; type KnobProps = { @@ -33,16 +34,16 @@ export const Knob = ({ offsetY, height, onClose }: KnobProps) => { if (translationY < -30) { Animated.spring(height, { useNativeDriver: false, - toValue: screenHeight * ctx.expandedHeight, + toValue: getHeight(ctx.expandedHeight, screenHeight), }).start(); } else if (translationY > 150) { - height.setValue(screenHeight * ctx.defaultHeight); + height.setValue(getHeight(ctx.defaultHeight, screenHeight)); offsetY.setValue(0); onClose(); } else { Animated.spring(height, { useNativeDriver: false, - toValue: screenHeight * ctx.defaultHeight, + toValue: getHeight(ctx.defaultHeight, screenHeight), }).start(); } } diff --git a/src/utils.ts b/src/utils.ts new file mode 100644 index 0000000..1324d7d --- /dev/null +++ b/src/utils.ts @@ -0,0 +1,4 @@ +export const getHeight = (value: string | number, screenHeight: number) => + typeof value === 'number' + ? value + : (screenHeight / 100) * parseInt(value.replace('%', ''), 10);