UNPKG

@sendbird/uikit-utils

Version:

A collection of utility functions and constants for building chat UI components with Sendbird UIKit.

68 lines (67 loc) 2.24 kB
import { useEffect, useRef, useState } from 'react'; import { AppState, I18nManager } from 'react-native'; import { useSafeAreaInsets } from 'react-native-safe-area-context'; const edgePaddingMap = { ltr: { left: 'paddingStart', right: 'paddingEnd', top: 'paddingTop', bottom: 'paddingBottom' }, rtl: { left: 'paddingEnd', right: 'paddingStart', top: 'paddingTop', bottom: 'paddingBottom' } }; export const useSafeAreaPadding = (edges, direction = I18nManager.isRTL ? 'rtl' : 'ltr') => { const safeAreaInsets = useSafeAreaInsets(); return edges.reduce((map, edge) => { const paddingKey = edgePaddingMap[direction][edge]; map[paddingKey] = safeAreaInsets[edge]; if (edge === 'left') { // @ts-expect-error backward compatibility map['paddingLeft'] = safeAreaInsets[edge]; } if (edge === 'right') { // @ts-expect-error backward compatibility map['paddingRight'] = safeAreaInsets[edge]; } return map; }, {}); }; export const useAppState = (type, listener) => { const callbackRef = useRef(listener); callbackRef.current = listener; useEffect(() => { const eventListener = state => callbackRef.current(state); const subscriber = AppState.addEventListener(type, eventListener); return () => { if (subscriber !== null && subscriber !== void 0 && subscriber.remove) subscriber.remove(); }; }, []); }; /** * To display a new modal in React-Native, you should ensure that a new modal is opened only after the existing modal has been dismissed to avoid conflicts. * To achieve this, you can use a deferred onClose that can be awaited until the onDismiss is called. * */ export const useDeferredModalState = () => { const resolveRef = useRef(); const [visible, setVisible] = useState(false); return { onClose: () => { return new Promise(resolve => { resolveRef.current = resolve; setVisible(false); }); }, onDismiss: () => { var _resolveRef$current; (_resolveRef$current = resolveRef.current) === null || _resolveRef$current === void 0 || _resolveRef$current.call(resolveRef); }, visible, setVisible }; }; //# sourceMappingURL=react-native.js.map