index.tsx 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. import {
  2. useEffect,
  3. useState
  4. } from "react";
  5. import {
  6. View
  7. } from "react-native";
  8. import type IMainHeader from "./type";
  9. import stylesheet, {
  10. useStyles
  11. } from "./stylesheet";
  12. import {
  13. NCoreUIKitTheme
  14. } from "../../core/hooks";
  15. import {
  16. SafeAreaView
  17. } from "react-native-safe-area-context";
  18. import {
  19. Portal
  20. } from "../../helpers/portalize";
  21. import SiteLogo from "../siteLogo";
  22. const MainHeader = ({
  23. isWorkWithSafeAreaView = true,
  24. isWorkWithStickySpace = false,
  25. isSetAutoHeaderSpace = true,
  26. isWorkWithSeperator = false,
  27. backgroundColor = "default",
  28. portalName = "main-header",
  29. isWorkWithPortal = false,
  30. isWorkWithSticky = false,
  31. renderRight: RenderRight,
  32. renderLeft: RenderLeft,
  33. customBackgroundColor,
  34. onContentHeight,
  35. customLocalize,
  36. siteLogoProps,
  37. customTheme,
  38. glassEffect,
  39. children,
  40. style,
  41. ...props
  42. }: IMainHeader) => {
  43. const {
  44. borders,
  45. spaces,
  46. colors
  47. } = NCoreUIKitTheme.useContext();
  48. const {
  49. safeAreaView: safeAreaViewDynamicStyle,
  50. container: containerDynamicStyle
  51. } = useStyles({
  52. isWorkWithSafeAreaView,
  53. customBackgroundColor,
  54. isWorkWithSeperator,
  55. isWorkWithSticky,
  56. backgroundColor,
  57. glassEffect,
  58. borders,
  59. spaces,
  60. colors
  61. });
  62. const [
  63. contentHeight,
  64. setContentHeight
  65. ] = useState<null | number>(null);
  66. useEffect(() => {
  67. if(onContentHeight) onContentHeight(contentHeight);
  68. }, [
  69. contentHeight
  70. ]);
  71. const renderRight = () => {
  72. if(RenderRight) {
  73. return <RenderRight/>;
  74. }
  75. return <View/>;
  76. };
  77. const renderLeft = () => {
  78. if(RenderLeft) {
  79. return <RenderLeft/>;
  80. }
  81. return <SiteLogo
  82. isWorkWithFlex1={false}
  83. customLocalize={customLocalize}
  84. customTheme={customTheme}
  85. size="medium"
  86. {...siteLogoProps}
  87. />;
  88. };
  89. const renderContent = () => {
  90. return <View
  91. {...props}
  92. style={[
  93. style,
  94. stylesheet.container,
  95. containerDynamicStyle
  96. ]}
  97. onLayout={(event) => {
  98. if(props && props.onLayout) {
  99. props.onLayout(event);
  100. }
  101. if(isWorkWithSticky) {
  102. const _contentHeight = event.nativeEvent.layout.height;
  103. setContentHeight(_contentHeight);
  104. if(isSetAutoHeaderSpace) {
  105. NCoreUIKitTheme.updateConfigs({
  106. headerSpace: _contentHeight
  107. });
  108. }
  109. }
  110. }}
  111. >
  112. {renderLeft()}
  113. {renderRight()}
  114. </View>;
  115. };
  116. const renderContentContainer = () => {
  117. if(isWorkWithPortal) {
  118. return <Portal
  119. name={portalName}
  120. >
  121. {renderContent()}
  122. </Portal>;
  123. }
  124. return renderContent();
  125. };
  126. const renderSafeAreaView = () => {
  127. return <SafeAreaView
  128. edges={[
  129. "top"
  130. ]}
  131. style={[
  132. safeAreaViewDynamicStyle
  133. ]}
  134. >
  135. {renderContentContainer()}
  136. </SafeAreaView>;
  137. };
  138. const renderWithChildren = () => {
  139. return <View
  140. style={[
  141. stylesheet.baseContainer
  142. ]}
  143. >
  144. {isWorkWithSafeAreaView ? renderSafeAreaView() : renderContentContainer()}
  145. {
  146. isWorkWithSticky && isWorkWithStickySpace ?
  147. <View
  148. style={[
  149. stylesheet.stickySpacer,
  150. {
  151. backgroundColor: customBackgroundColor ? colors.project[customBackgroundColor] : colors.content.container[backgroundColor],
  152. height: contentHeight
  153. }
  154. ]}
  155. />
  156. :
  157. null
  158. }
  159. {children}
  160. </View>;
  161. };
  162. if(children) {
  163. return renderWithChildren();
  164. }
  165. return isWorkWithSafeAreaView ? renderSafeAreaView() : renderContentContainer();
  166. };
  167. export default MainHeader;