index.tsx 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. import {
  2. useEffect,
  3. useState,
  4. type FC
  5. } from "react";
  6. import type IPaletteSwitcherProps from "./type";
  7. import type {
  8. ThemeSwitchVariants
  9. } from "./type";
  10. import {
  11. NCoreUIKitTheme
  12. } from "../../core/hooks";
  13. import type {
  14. NCoreUIKitIcon
  15. } from "../../types";
  16. import Button from "../button";
  17. import {
  18. NIBGATCommunityIcon,
  19. CayCoreIcon,
  20. NIBGATIcon
  21. } from "../../assets/svg";
  22. import HighlightButton from "../highlightButton";
  23. const PaletteSwitcher: FC<IPaletteSwitcherProps> = ({
  24. variants: variantsProps = [],
  25. isWorkWithHighlightButton,
  26. isWorkWithNextShowSystem,
  27. customTheme,
  28. color,
  29. style,
  30. ...props
  31. }) => {
  32. const {
  33. activePalette,
  34. paletteKeys
  35. } = NCoreUIKitTheme.useContext(customTheme);
  36. const [
  37. currentIndex,
  38. setCurrentIndex
  39. ] = useState(paletteKeys.findIndex(t => t === activePalette));
  40. useEffect(() => {
  41. const cI = paletteKeys.findIndex(tI => tI === activePalette);
  42. const nextCI = isWorkWithNextShowSystem ? cI + 1 > paletteKeys.length - 1 ? 0 : cI + 1 : cI;
  43. if(currentIndex !== nextCI) {
  44. setCurrentIndex(nextCI);
  45. }
  46. }, [activePalette]);
  47. const variantsPre: ThemeSwitchVariants = variantsProps;
  48. const isHaveNC = variantsProps.findIndex(iV => iV.paletteKey === "nibgat-community") !== -1;
  49. const isHaveCC = variantsProps.findIndex(iV => iV.paletteKey === "caycore") !== -1;
  50. const isHaveN = variantsProps.findIndex(iV => iV.paletteKey === "nibgat") !== -1;
  51. if(!isHaveN) {
  52. variantsPre.unshift({
  53. borderColor: "transparent",
  54. backgroundColor: "subtle",
  55. paletteKey: "nibgat",
  56. iconColor: "moon",
  57. icon: ({
  58. size
  59. }) => {
  60. return <NIBGATIcon
  61. size={size + 5}
  62. />;
  63. }
  64. });
  65. }
  66. if(!isHaveNC) {
  67. variantsPre.unshift({
  68. paletteKey: "nibgat-community",
  69. borderColor: "transparent",
  70. backgroundColor: "subtle",
  71. iconColor: "moon",
  72. icon: ({
  73. size
  74. }) => {
  75. return <NIBGATCommunityIcon
  76. size={size + 5}
  77. />;
  78. }
  79. });
  80. }
  81. if(!isHaveCC) {
  82. variantsPre.unshift({
  83. borderColor: "transparent",
  84. backgroundColor: "subtle",
  85. paletteKey: "caycore",
  86. iconColor: "moon",
  87. icon: ({
  88. size
  89. }) => {
  90. return <CayCoreIcon
  91. size={size + 5}
  92. />;
  93. }
  94. });
  95. }
  96. const variants = [...variantsPre].sort((a, b) => {
  97. return paletteKeys.indexOf(a.paletteKey) - paletteKeys.indexOf(b.paletteKey);
  98. });
  99. const currentVariantIndex = variants.findIndex(e => e.paletteKey === activePalette);
  100. if(currentVariantIndex === -1) {
  101. return null;
  102. }
  103. const nextVariantIndex = currentVariantIndex + 1;
  104. const viewVariantIndex = nextVariantIndex > variants.length - 1 ? 0 : nextVariantIndex;
  105. const currentVariant = variants[isWorkWithNextShowSystem ? viewVariantIndex : currentVariantIndex];
  106. const allProps = {
  107. ...props,
  108. customBorderColor: currentVariant && currentVariant.borderColor ?
  109. currentVariant.borderColor
  110. :
  111. color,
  112. customColor: currentVariant && currentVariant.backgroundColor ?
  113. currentVariant.backgroundColor
  114. :
  115. color,
  116. icon: ({
  117. color,
  118. size
  119. }: {
  120. color: keyof NCoreUIKit.IconContentColors;
  121. size: number;
  122. }) => {
  123. const Icon = currentVariant?.icon as NCoreUIKitIcon;
  124. return <Icon
  125. customColor={currentVariant && currentVariant.iconColor ? currentVariant.iconColor : undefined}
  126. color={color}
  127. size={size}
  128. />;
  129. },
  130. onPress: () => {
  131. NCoreUIKitTheme.switch({
  132. paletteKey: paletteKeys[viewVariantIndex]
  133. });
  134. },
  135. style: [
  136. style
  137. ]
  138. };
  139. if(isWorkWithHighlightButton) {
  140. return <HighlightButton
  141. {...allProps}
  142. />;
  143. }
  144. return <Button
  145. {...allProps}
  146. />;
  147. };
  148. export default PaletteSwitcher;