layout.tsx 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. import { ScrollView, View, Text } from "@tarojs/components";
  2. import './layout.scss'
  3. import { NaviBarTitleShowType, TemplateType } from "@/utils/types";
  4. import { useEffect, useState } from "react";
  5. import Taro, { usePageScroll } from "@tarojs/taro";
  6. import { rpxToPx } from "@/utils/tools";
  7. let useNavigation;
  8. let GradientText
  9. if (process.env.TARO_ENV == 'rn') {
  10. GradientText = require('@/components/basic/GradientText').default
  11. useNavigation = require("@react-navigation/native").useNavigation
  12. }
  13. export default function Layout(props: {
  14. children: React.ReactNode,
  15. type: TemplateType,
  16. refresh?: Function,
  17. more?: Function,
  18. triggered?: boolean,
  19. title?: string,
  20. titleColor?: string,
  21. header?: React.ReactNode,
  22. secondPage?: boolean,
  23. isFastSleepTheme?: boolean,
  24. titleShowStyle: NaviBarTitleShowType
  25. }) {
  26. const { children, type, triggered } = props;
  27. const [showTitle, setShowTitle] = useState(props.titleShowStyle == NaviBarTitleShowType.alwayShow)
  28. let navigation;
  29. if (useNavigation) {
  30. navigation = useNavigation()
  31. }
  32. useEffect(() => {
  33. Taro.setNavigationBarTitle({
  34. title: showTitle ? props.title ? props.title : '' : ''
  35. })
  36. if (navigation) {
  37. navigation.setOptions({
  38. headerTitle: showTitle ? props.title ? props.title : '' : '',
  39. });
  40. }
  41. console.log(showTitle)
  42. }, [showTitle])
  43. function onScroll(e) {
  44. if (props.titleShowStyle == NaviBarTitleShowType.scrollToShow) {
  45. if (e.detail.scrollTop > 70) {
  46. setShowTitle(true)
  47. }
  48. else {
  49. setShowTitle(false)
  50. }
  51. }
  52. }
  53. usePageScroll((e) => {
  54. if (props.titleShowStyle == NaviBarTitleShowType.scrollToShow) {
  55. if (e.scrollTop > 70) {
  56. setShowTitle(true)
  57. }
  58. else {
  59. setShowTitle(false)
  60. }
  61. }
  62. })
  63. function fastSleepTitle() {
  64. if (process.env.TARO_ENV == 'rn') {
  65. return <GradientText style={{
  66. fontSize: props.secondPage ? rpxToPx(56) : rpxToPx(72),
  67. fontWeight:'bold',
  68. marginLeft:rpxToPx(46),
  69. marginTop:rpxToPx(24),
  70. marginBottom:rpxToPx(24)
  71. }}>{props.title}</GradientText>
  72. }
  73. return <Text className='layout_title fast_sleep_text'
  74. style={{
  75. fontSize: props.secondPage ? rpxToPx(56) : rpxToPx(72)
  76. }}
  77. >{props.title}</Text>
  78. }
  79. function pageDetail() {
  80. return <View>
  81. {
  82. props.title && <View className="layout_title_view" style={{ backgroundColor: global.isDebug ? 'red' : 'transparent' }}>
  83. {
  84. props.isFastSleepTheme ? fastSleepTitle() :
  85. <Text className='layout_title'
  86. style={{
  87. color: props.titleColor ? props.titleColor : '#fff',
  88. fontSize: props.secondPage ? rpxToPx(56) : rpxToPx(72)
  89. }}
  90. >{props.title}</Text>
  91. }
  92. </View>
  93. }
  94. {
  95. children
  96. }
  97. <View style={{ height: 0 }} />
  98. </View>
  99. }
  100. switch (type) {
  101. case TemplateType.list:
  102. return <ScrollView className="layout_container"
  103. style={{ WebkitOverflowScrolling: 'auto' }}
  104. onScroll={onScroll}
  105. scrollY refresherEnabled={true}
  106. refresherThreshold={100} refresherBackground="#000"
  107. refresherDefaultStyle="white"
  108. onRefresherRefresh={() => { props.refresh!() }}
  109. upperThreshold={50}
  110. onScrollToLower={() => { props.more ? props.more() : null }}
  111. refresherTriggered={triggered}>
  112. {
  113. props.title && <View className="layout_title_view" style={{ backgroundColor: global.isDebug ? 'red' : 'transparent' }}>
  114. <Text className='layout_title'
  115. style={{
  116. color: props.titleColor ? props.titleColor : '#fff',
  117. fontSize: props.secondPage ? 28 : 36
  118. }}
  119. >{props.title}</Text>
  120. </View>
  121. }
  122. {children}
  123. <View style={{ height: 50 }} />
  124. </ScrollView>
  125. case TemplateType.grid:
  126. return <ScrollView className="layout_container"
  127. enableFlex={true}
  128. onScroll={onScroll}
  129. style={{ WebkitOverflowScrolling: 'auto' }}
  130. scrollY refresherEnabled={true}
  131. refresherThreshold={100} refresherBackground="#000"
  132. refresherDefaultStyle="white"
  133. onRefresherRefresh={() => { props.refresh!() }}
  134. upperThreshold={50}
  135. onScrollToLower={() => { props.more ? props.more() : null }}
  136. refresherTriggered={triggered}>
  137. {
  138. props.title && <View className="layout_title_view" style={{ backgroundColor: global.isDebug ? 'red' : 'transparent' }}>
  139. <Text className='layout_title'
  140. style={{
  141. color: props.titleColor ? props.titleColor : '#fff',
  142. fontSize: props.secondPage ? 28 : 36
  143. }}
  144. >{props.title}</Text>
  145. </View>
  146. }
  147. <View className="grid_bg">
  148. {children}
  149. <View style={{ height: 50 }} />
  150. </View>
  151. </ScrollView>
  152. case TemplateType.flex:
  153. return <View className='flex'>
  154. {/* <View className="flex-expand" /> */}
  155. {
  156. process.env.TARO_ENV == 'weapp' ? pageDetail() :
  157. <ScrollView onScroll={onScroll}>
  158. {
  159. pageDetail()
  160. }
  161. </ScrollView>
  162. }
  163. </View>
  164. case TemplateType.customHeader:
  165. if (process.env.TARO_ENV == 'rn') {
  166. return <ScrollView onScroll={onScroll}>
  167. <View className='flex'>
  168. {
  169. props.header
  170. }
  171. {children}
  172. <View style={{ height: 50 }} />
  173. </View>
  174. </ScrollView>
  175. }
  176. return <View className='flex'>
  177. {/* <View className="flex-expand" /> */}
  178. {
  179. props.header
  180. }
  181. {children}
  182. <View style={{ height: 50 }} />
  183. </View>
  184. }
  185. return <View />
  186. }