chore: allow pass height in percentage

This commit is contained in:
Jakub Grzywacz 2021-07-10 12:39:52 +02:00
parent cf77277802
commit dff1271b30
No known key found for this signature in database
GPG Key ID: 5BBB685871FF63C4
7 changed files with 18 additions and 10 deletions

View File

@ -58,8 +58,8 @@ TODO
| containerStyles | ViewStyle | {} | no | Override container styles | | containerStyles | ViewStyle | {} | no | Override container styles |
| hideHeader | boolean | false | no | Hide category names | | hideHeader | boolean | false | no | Hide category names |
| expandable | boolean | true | no | Show knob and enable expand on swipe up | | 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) | | defaultHeight | number \| string | "40%" | no | Specify collapsed container height (number is points, string is a percentage of the screen height) |
| expandedHeight | number | 0.8 | no | Specify expanded container height (1 is full screen height) *only if expandable is true* | | 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 | | backdropColor | string | "#00000055" | no | Change backdrop color and alpha |
| categoryColor | string | "#000000" | no | Change category item color | | categoryColor | string | "#000000" | no | Change category item color |
| activeCategoryColor | string | "#005b96" | no | Change active category item color | | activeCategoryColor | string | "#005b96" | no | Change active category item color |

View File

@ -26,6 +26,8 @@ export default function App() {
onEmojiSelected={handlePick} onEmojiSelected={handlePick}
open={isModalOpen} open={isModalOpen}
onClose={() => setIsModalOpen(false)} onClose={() => setIsModalOpen(false)}
defaultHeight={100}
expandedHeight={200}
/> />
{/* //////////////////////////////////////////// */} {/* //////////////////////////////////////////// */}

View File

@ -6,6 +6,7 @@ import { defaultKeyboardContext, KeyboardProvider } from './KeyboardProvider';
import type { KeyboardProps } from './KeyboardContext'; import type { KeyboardProps } from './KeyboardContext';
import type { EmojiType } from './types'; import type { EmojiType } from './types';
import { ModalWithBackdrop } from './components/ModalWithBackdrop'; import { ModalWithBackdrop } from './components/ModalWithBackdrop';
import { getHeight } from './utils';
export const EmojiPicker = ({ export const EmojiPicker = ({
onEmojiSelected, onEmojiSelected,
@ -18,7 +19,7 @@ export const EmojiPicker = ({
const { height: screenHeight } = useWindowDimensions(); const { height: screenHeight } = useWindowDimensions();
const offsetY = React.useRef(new Animated.Value(0)).current; const offsetY = React.useRef(new Animated.Value(0)).current;
const height = React.useRef( const height = React.useRef(
new Animated.Value(screenHeight * defaultHeight) new Animated.Value(getHeight(defaultHeight, screenHeight))
).current; ).current;
const translateY = React.useRef(new Animated.Value(0)).current; const translateY = React.useRef(new Animated.Value(0)).current;

View File

@ -16,8 +16,8 @@ export type KeyboardProps = {
headerStyles?: TextStyle; headerStyles?: TextStyle;
expandable?: boolean; expandable?: boolean;
hideHeader?: boolean; hideHeader?: boolean;
defaultHeight?: number; defaultHeight?: number | string;
expandedHeight?: number; expandedHeight?: number | string;
backdropColor?: string; backdropColor?: string;
categoryColor?: string; categoryColor?: string;
activeCategoryColor?: string; activeCategoryColor?: string;

View File

@ -21,8 +21,8 @@ export const defaultKeyboardContext: Required<KeyboardProps> = {
headerStyles: {}, headerStyles: {},
expandable: true, expandable: true,
hideHeader: false, hideHeader: false,
defaultHeight: 0.4, defaultHeight: '40%',
expandedHeight: 0.8, expandedHeight: '80%',
backdropColor: '#00000055', backdropColor: '#00000055',
categoryColor: '#000000', categoryColor: '#000000',
activeCategoryColor: '#005b96', activeCategoryColor: '#005b96',

View File

@ -5,6 +5,7 @@ import {
PanGestureHandlerGestureEvent, PanGestureHandlerGestureEvent,
State, State,
} from 'react-native-gesture-handler'; } from 'react-native-gesture-handler';
import { getHeight } from '../utils';
import { KeyboardContext } from '../KeyboardContext'; import { KeyboardContext } from '../KeyboardContext';
type KnobProps = { type KnobProps = {
@ -33,16 +34,16 @@ export const Knob = ({ offsetY, height, onClose }: KnobProps) => {
if (translationY < -30) { if (translationY < -30) {
Animated.spring(height, { Animated.spring(height, {
useNativeDriver: false, useNativeDriver: false,
toValue: screenHeight * ctx.expandedHeight, toValue: getHeight(ctx.expandedHeight, screenHeight),
}).start(); }).start();
} else if (translationY > 150) { } else if (translationY > 150) {
height.setValue(screenHeight * ctx.defaultHeight); height.setValue(getHeight(ctx.defaultHeight, screenHeight));
offsetY.setValue(0); offsetY.setValue(0);
onClose(); onClose();
} else { } else {
Animated.spring(height, { Animated.spring(height, {
useNativeDriver: false, useNativeDriver: false,
toValue: screenHeight * ctx.defaultHeight, toValue: getHeight(ctx.defaultHeight, screenHeight),
}).start(); }).start();
} }
} }

4
src/utils.ts Normal file
View File

@ -0,0 +1,4 @@
export const getHeight = (value: string | number, screenHeight: number) =>
typeof value === 'number'
? value
: (screenHeight / 100) * parseInt(value.replace('%', ''), 10);