stylesheet.ts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. import {
  2. type TextStyle,
  3. type ViewStyle,
  4. StyleSheet
  5. } from "react-native";
  6. import {
  7. type SwitchDynamicStyleType,
  8. type SwitchTypeConstantType,
  9. type SwitchTypes,
  10. type SwitchType
  11. } from "./type";
  12. import type {
  13. Mutable
  14. } from "../../types";
  15. export const SWITCH_TYPE_STYLES: Record<
  16. SwitchType,
  17. {
  18. containerColor: keyof NCoreUIKit.ContainerContentColors;
  19. indicatorColor: keyof NCoreUIKit.IconContentColors;
  20. subTitleColor: keyof NCoreUIKit.TextContentColors;
  21. titleColor: keyof NCoreUIKit.TextContentColors;
  22. }
  23. > = {
  24. primary: {
  25. indicatorColor: "default",
  26. containerColor: "mid",
  27. subTitleColor: "low",
  28. titleColor: "mid"
  29. },
  30. danger: {
  31. subTitleColor: "dangerLow",
  32. indicatorColor: "danger",
  33. containerColor: "danger",
  34. titleColor: "danger"
  35. },
  36. success: {
  37. subTitleColor: "successLow",
  38. indicatorColor: "success",
  39. containerColor: "success",
  40. titleColor: "success"
  41. },
  42. warning: {
  43. subTitleColor: "warningLow",
  44. indicatorColor: "warning",
  45. containerColor: "warning",
  46. titleColor: "warning"
  47. },
  48. info: {
  49. subTitleColor: "infoLow",
  50. indicatorColor: "info",
  51. containerColor: "info",
  52. titleColor: "info"
  53. },
  54. neutral: {
  55. indicatorColor: "default",
  56. containerColor: "mid",
  57. subTitleColor: "low",
  58. titleColor: "mid"
  59. }
  60. };
  61. export const getSwitchType = ({
  62. type
  63. }: SwitchTypeConstantType): SwitchTypes => {
  64. const currentType = SWITCH_TYPE_STYLES[type];
  65. return currentType;
  66. };
  67. const stylesheet = StyleSheet.create({
  68. container: {
  69. backgroundColor: "transparent",
  70. borderColor: "transparent",
  71. flexDirection: "row",
  72. borderStyle: "solid",
  73. alignItems: "center",
  74. display: "flex"
  75. },
  76. indicatorContainer: {
  77. position: "relative"
  78. },
  79. indicator: {
  80. },
  81. contentContainer: {
  82. justifyContent: "center",
  83. flexDirection: "column"
  84. },
  85. titleContainer: {
  86. justifyContent: "flex-start",
  87. flexDirection: "row",
  88. alignItems: "center"
  89. },
  90. title: {
  91. textAlign: "left",
  92. margin: 0
  93. },
  94. subTitle: {
  95. textAlign: "left"
  96. },
  97. loading: {},
  98. overlay: {
  99. position: "absolute",
  100. display: "none",
  101. zIndex: 999,
  102. bottom: 0,
  103. right: 0,
  104. left: 0,
  105. top: 0
  106. }
  107. });
  108. export const useStyles = ({
  109. displayBehaviourWhileLoading,
  110. SWITCH_INDICATOR_SIZE,
  111. spreadBehaviour,
  112. inlineSpaces,
  113. currentType,
  114. isDisabled,
  115. isLoading,
  116. isActive,
  117. colors,
  118. isFlip,
  119. spaces,
  120. type
  121. }: SwitchDynamicStyleType) => {
  122. const styleType = type === "danger" ? "error" : type;
  123. const styles = {
  124. container: {
  125. padding: spaces.spacingSm,
  126. alignSelf: "auto"
  127. } as Mutable<ViewStyle>,
  128. indicatorContainer: {
  129. backgroundColor: colors.content.container[currentType.containerColor],
  130. borderRadius: (SWITCH_INDICATOR_SIZE + (spaces.spacingSm * 2)) / 2,
  131. width: (SWITCH_INDICATOR_SIZE * 1.5) + (spaces.spacingSm * 2),
  132. padding: spaces.spacingXs
  133. } as Mutable<ViewStyle>,
  134. indicator: {
  135. backgroundColor: colors.content.icon[currentType.indicatorColor],
  136. borderRadius: SWITCH_INDICATOR_SIZE / 2,
  137. height: SWITCH_INDICATOR_SIZE,
  138. width: SWITCH_INDICATOR_SIZE
  139. } as Mutable<ViewStyle>,
  140. contentContainer: {
  141. marginLeft: spaces.spacingSm
  142. } as Mutable<ViewStyle>,
  143. titleContainer: {
  144. } as Mutable<ViewStyle>,
  145. title: {
  146. } as Mutable<TextStyle>,
  147. subTitle: {
  148. } as Mutable<TextStyle>,
  149. loading: {
  150. } as Mutable<ViewStyle>,
  151. overlay: {
  152. borderRadius: (SWITCH_INDICATOR_SIZE + (spaces.spacingSm * 2)) / 2
  153. } as Mutable<ViewStyle>,
  154. optionalText: {
  155. marginLeft: inlineSpaces.subTitle
  156. } as Mutable<TextStyle>
  157. };
  158. if (isLoading) {
  159. if (displayBehaviourWhileLoading === "disabled") {
  160. styles.overlay.backgroundColor = colors.system.state.overlay.disabled[styleType];
  161. styles.overlay.display = "flex";
  162. }
  163. }
  164. if (isFlip) {
  165. styles.contentContainer.marginRight = spaces.spacingSm;
  166. styles.titleContainer.justifyContent = "flex-end";
  167. styles.contentContainer.marginLeft = 0;
  168. styles.subTitle.textAlign = "right";
  169. styles.title.textAlign = "right";
  170. }
  171. if (spreadBehaviour === "baseline") {
  172. styles.container.alignSelf = spreadBehaviour;
  173. styles.container.width = "auto";
  174. } else if (spreadBehaviour === "stretch") {
  175. styles.contentContainer.alignSelf = spreadBehaviour;
  176. styles.container.alignSelf = spreadBehaviour;
  177. styles.container.justifyContent = "center";
  178. styles.contentContainer.flexShrink = 1;
  179. styles.contentContainer.width = "100%";
  180. styles.container.flexShrink = 1;
  181. styles.container.width = "100%";
  182. }
  183. if(isActive) {
  184. if(styleType === "neutral") {
  185. styles.indicatorContainer.backgroundColor = colors.content.container.emphasized;
  186. styles.indicator.backgroundColor = colors.content.icon.emphasized;
  187. }
  188. }
  189. if (isDisabled) {
  190. if(styleType === "neutral" && isActive) {
  191. styles.indicatorContainer.backgroundColor = colors.system.state.border.disabled.primary;
  192. }
  193. styles.overlay.backgroundColor = colors.system.state.overlay.disabled[styleType];
  194. styles.overlay.display = "flex";
  195. }
  196. return styles;
  197. };
  198. export default stylesheet;