index.tsx 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import { useAppStore } from "@/store/appStore";
  2. import { Text, View } from "@tarojs/components";
  3. import Taro, { useUnload, usePageScroll } from "@tarojs/taro";
  4. import React, { useState } from "react";
  5. import IconArrowLeft from "@/components/icon/icon-arrow-left";
  6. import style from "./index.module.less";
  7. interface Props {
  8. children?: React.ReactChild;
  9. center?: boolean;
  10. leftColumn?: () => JSX.Element;
  11. backText?: string;
  12. blur?: boolean
  13. onNavBack?: () => Promise<boolean | void> | void;
  14. backButtonRound?: boolean;
  15. navDelta?: number;
  16. scrollFadeIn?: boolean; // 上下滚动背景渐变
  17. }
  18. const Index: React.FC<Props> = ({
  19. leftColumn,
  20. children,
  21. center = true,
  22. onNavBack,
  23. blur = false,
  24. navDelta = 1,
  25. backText,
  26. scrollFadeIn = false,
  27. }) => {
  28. const statusBarHeight = useAppStore.getState().statusBarHeight;
  29. //
  30. const [opacity, setOpacity] = useState(scrollFadeIn ? 1 : 0);
  31. usePageScroll((e) => {
  32. if (!scrollFadeIn) {
  33. return;
  34. }
  35. const opacity = Math.max(0, e.scrollTop / 50);
  36. setOpacity(opacity);
  37. // 如果 setShow 此处打开,滚动页内如果有图片会导致转自跳动 bug
  38. // setShow(opacity !== 0)
  39. });
  40. const handleNav = async () => {
  41. // 无法将 导航栏隐藏,只能用 opacity 为 0
  42. // 否则滚动页内如果有图片会导致转自跳动 bug
  43. if (onNavBack) {
  44. console.log("nav");
  45. await onNavBack();
  46. Taro.navigateBack({
  47. delta: navDelta,
  48. complete: () => {
  49. },
  50. });
  51. } else {
  52. Taro.navigateBack({
  53. complete: () => {
  54. },
  55. });
  56. }
  57. };
  58. // todo: 改成传参决定是否显示:箭头|文本|箭头与文本
  59. const renderBackButton = () => {
  60. // 如果显示背景色,则返回按钮不需要圆角及背景
  61. // 文本返回按钮
  62. return (
  63. <View className={`${style.navBarBackButtonNormal}`}>
  64. <IconArrowLeft />
  65. {/* <View className={`${style.iconArrowLeftPure}`}></View> */}
  66. <Text className={style.backText}>{backText}</Text>
  67. </View>
  68. );
  69. };
  70. const renderLeftColumn = () => {
  71. if (leftColumn) {
  72. return <View className={style.leftColumn}>{leftColumn()}</View>;
  73. } else {
  74. return (
  75. <View className={style.leftColumn} onClick={handleNav}>
  76. {renderBackButton()}
  77. </View>
  78. );
  79. }
  80. };
  81. const renderNavBar = () => {
  82. return (
  83. <View
  84. className={`${style.navBarContainer} ${blur ? style.backdropFilter : ''}`}
  85. id="leaf-nav-bar"
  86. style={{
  87. paddingTop: statusBarHeight,
  88. }}
  89. >
  90. <View className={style.navBarBg} style={{opacity: opacity}}></View>
  91. <View className={`${style.navBar}`}>
  92. {renderLeftColumn()}
  93. <View
  94. className={`flex flex-1 ${center ? "justify-center" : ""} ${
  95. style.title
  96. } truncate`}
  97. >
  98. {children}
  99. </View>
  100. <View className={style.rightColumn}></View>
  101. </View>
  102. </View>
  103. );
  104. };
  105. return <>{renderNavBar()}</>;
  106. };
  107. export default Index;