UNPKG

react-native-ui-lib

Version:

<p align="center"> <img src="https://user-images.githubusercontent.com/1780255/105469025-56759000-5ca0-11eb-993d-3568c1fd54f4.png" height="250px" style="display:block"/> </p> <p align="center">UI Toolset & Components Library for React Native</p> <p a

66 lines (60 loc) 2.13 kB
import React, { useCallback, useState, forwardRef, useImperativeHandle } from 'react'; import TouchableOpacity from "../../components/touchableOpacity"; import View from "../../components/view"; import Modal from "../../components/modal"; import Dialog from "../../components/dialog"; const ExpandableOverlay = (props, ref) => { const { children, expandableContent, useDialog, modalProps, dialogProps, showTopBar, topBarProps, renderCustomOverlay, disabled, testID, ...others } = props; const [visible, setExpandableVisible] = useState(false); const openExpandable = useCallback(() => setExpandableVisible(true), []); const closeExpandable = useCallback(() => { setExpandableVisible(false); useDialog ? dialogProps?.onDismiss?.() : modalProps?.onDismiss?.(); }, [useDialog, dialogProps?.onDismiss, modalProps?.onDismiss]); const toggleExpandable = useCallback(() => visible ? closeExpandable() : openExpandable(), [visible, openExpandable, closeExpandable]); useImperativeHandle(ref, () => ({ openExpandable, closeExpandable, toggleExpandable })); const renderModal = () => { return <Modal testID={`${testID}.overlay`} {...modalProps} visible={visible} onDismiss={closeExpandable}> {showTopBar && <Modal.TopBar onDone={closeExpandable} {...topBarProps} />} {expandableContent} </Modal>; }; const renderDialog = () => { return <Dialog testID={`${testID}.overlay`} {...dialogProps} visible={visible} onDismiss={closeExpandable}> {expandableContent} </Dialog>; }; const renderOverlay = () => { if (renderCustomOverlay) { return renderCustomOverlay({ visible, openExpandable, closeExpandable, toggleExpandable }); } else { return useDialog ? renderDialog() : renderModal(); } }; return <TouchableOpacity {...others} onPress={openExpandable} disabled={disabled} testID={testID}> <View pointerEvents="none">{children}</View> {renderOverlay()} </TouchableOpacity>; }; export default forwardRef(ExpandableOverlay);