stylesheet.ts 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. import {
  2. type TextStyle,
  3. type ViewStyle,
  4. StyleSheet
  5. } from "react-native";
  6. import {
  7. type ButtonVariantConstantType,
  8. type ButtonTypeConstantType,
  9. type ButtonSizeConstantType,
  10. type ButtonDynamicStyleType,
  11. type ButtonMeasuresKeys,
  12. type ButtonMeasures,
  13. type ButtonVariants,
  14. type ButtonVariant,
  15. type ButtonTypes,
  16. type ButtonSize,
  17. type ButtonType
  18. } from "./type";
  19. import type {
  20. Mutable
  21. } from "../../types";
  22. export const BUTTON_SIZES: Record<ButtonSize, ButtonMeasuresKeys> = {
  23. small: {
  24. paddingHorizontal: "spacingMd",
  25. paddingVertical: "spacingSm",
  26. fontSize: "labelSmallSize"
  27. },
  28. medium: {
  29. paddingHorizontal: "spacingLg",
  30. paddingVertical: "spacingMd",
  31. fontSize: "labelMediumSize"
  32. },
  33. large: {
  34. paddingHorizontal: "spacingXl",
  35. paddingVertical: "spacingLg",
  36. fontSize: "labelLargeSize"
  37. }
  38. };
  39. export const BUTTON_VARIANT_STYLES: Record<
  40. ButtonVariant,
  41. {
  42. titleColor: "type" | keyof NCoreUIKit.TextContentColors;
  43. iconColor: "type" | keyof NCoreUIKit.ColorsMerged;
  44. borderColor: "transparent" | "type" | "container";
  45. iconColorScheme?: keyof NCoreUIKit.ThemeTokens;
  46. containerColor: "transparent" | "type";
  47. }
  48. > = {
  49. filled: {
  50. borderColor: "container",
  51. containerColor: "type",
  52. titleColor: "type",
  53. iconColor: "type"
  54. },
  55. outline: {
  56. containerColor: "transparent",
  57. borderColor: "type",
  58. titleColor: "type",
  59. iconColor: "type"
  60. },
  61. ghost: {
  62. containerColor: "transparent",
  63. borderColor: "transparent",
  64. titleColor: "type",
  65. iconColor: "type"
  66. }
  67. };
  68. export const BUTTON_TYPE_STYLES: Record<
  69. ButtonType,
  70. {
  71. containerColor: keyof NCoreUIKit.ContainerContentColors;
  72. borderColor: keyof NCoreUIKit.BorderContentColors;
  73. titleColor: keyof NCoreUIKit.TextContentColors;
  74. iconColor: keyof NCoreUIKit.IconContentColors;
  75. }
  76. > = {
  77. primary: {
  78. containerColor: "primary",
  79. borderColor: "emphasized",
  80. titleColor: "onPrimary",
  81. iconColor: "onPrimary"
  82. },
  83. danger: {
  84. containerColor: "danger",
  85. borderColor: "danger",
  86. titleColor: "danger",
  87. iconColor: "danger"
  88. },
  89. success: {
  90. containerColor: "success",
  91. borderColor: "success",
  92. titleColor: "success",
  93. iconColor: "success"
  94. },
  95. warning: {
  96. containerColor: "warning",
  97. borderColor: "warning",
  98. titleColor: "warning",
  99. iconColor: "warning"
  100. },
  101. info: {
  102. containerColor: "info",
  103. borderColor: "info",
  104. titleColor: "info",
  105. iconColor: "info"
  106. },
  107. neutral: {
  108. containerColor: "subtle",
  109. borderColor: "subtle",
  110. titleColor: "mid",
  111. iconColor: "mid"
  112. }
  113. };
  114. export const getButtonType = ({
  115. type
  116. }: ButtonTypeConstantType): ButtonTypes => {
  117. const currentType = BUTTON_TYPE_STYLES[type];
  118. return currentType;
  119. };
  120. export const getButtonVariant = ({
  121. variant
  122. }: ButtonVariantConstantType): ButtonVariants => {
  123. const currentVariant = BUTTON_VARIANT_STYLES[variant];
  124. return currentVariant;
  125. };
  126. export const getButtonSize = ({
  127. spaces,
  128. size
  129. }: ButtonSizeConstantType): ButtonMeasures => {
  130. const currentSize = BUTTON_SIZES[size];
  131. return {
  132. paddingHorizontal: spaces[currentSize.paddingHorizontal],
  133. paddingVertical: spaces[currentSize.paddingVertical],
  134. fontSize: currentSize.fontSize
  135. };
  136. };
  137. const stylesheet = StyleSheet.create({
  138. container: {
  139. backgroundColor: "transparent",
  140. borderColor: "transparent",
  141. flexDirection: "row",
  142. borderStyle: "solid",
  143. alignItems: "center",
  144. position: "relative",
  145. display: "flex"
  146. },
  147. title: {
  148. margin: "0",
  149. },
  150. loading: {},
  151. overlay: {
  152. position: "absolute",
  153. display: "none",
  154. bottom: 0,
  155. right: 0,
  156. left: 0,
  157. top: 0
  158. }
  159. });
  160. export const useStyles = ({
  161. displayBehaviourWhileLoading,
  162. spreadBehaviour,
  163. isCustomPadding,
  164. currentVariant,
  165. iconDirection,
  166. currentType,
  167. currentSize,
  168. isDisabled,
  169. isLoading,
  170. radiuses,
  171. variant,
  172. borders,
  173. colors,
  174. spaces,
  175. title,
  176. icon,
  177. type
  178. }: ButtonDynamicStyleType) => {
  179. const styleType = type === "danger" ? "error" : type;
  180. const styles = {
  181. container: {
  182. paddingRight: currentSize.paddingHorizontal,
  183. paddingBottom: currentSize.paddingVertical,
  184. paddingLeft: currentSize.paddingHorizontal,
  185. paddingTop: currentSize.paddingVertical,
  186. borderRadius: radiuses.md,
  187. borderWidth: borders.line
  188. } as Mutable<ViewStyle>,
  189. title: {
  190. } as Mutable<TextStyle>,
  191. loading: {
  192. } as Mutable<ViewStyle>,
  193. overlay: {
  194. borderRadius: radiuses.md - 2
  195. } as Mutable<ViewStyle>
  196. };
  197. if (currentVariant.containerColor === "type") {
  198. styles.container.backgroundColor = colors.content.container[currentType.containerColor];
  199. } else {
  200. styles.container.backgroundColor = "transparent";
  201. }
  202. if (currentVariant.borderColor === "type") {
  203. styles.container.borderColor = colors.content.border[currentType.borderColor];
  204. } else if (currentVariant.borderColor === "container") {
  205. styles.container.borderColor = styles.container.backgroundColor;
  206. } else {
  207. styles.container.borderColor = "transparent";
  208. }
  209. if (isLoading) {
  210. styles.title.marginLeft = spaces.spacingSm;
  211. styles.overlay.display = "flex";
  212. if (displayBehaviourWhileLoading === "disabled") {
  213. if (variant !== "ghost") {
  214. styles.container.borderColor = colors.system.state.overlay.disabled[styleType];
  215. if (variant === "filled") {
  216. styles.overlay.backgroundColor = colors.system.state.overlay.disabled[styleType];
  217. }
  218. }
  219. }
  220. }
  221. if (isLoading && spreadBehaviour === "stretch") {
  222. styles.title.marginLeft = spaces.spacingSm;
  223. styles.title.margin = "initial";
  224. }
  225. if (icon && !isLoading) {
  226. styles.title.marginLeft = spaces.spacingSm;
  227. styles.title.margin = "initial";
  228. }
  229. if (spreadBehaviour === "baseline") {
  230. styles.container.alignSelf = spreadBehaviour;
  231. styles.container.width = "auto";
  232. } else if (spreadBehaviour === "stretch") {
  233. styles.container.alignSelf = spreadBehaviour;
  234. styles.container.justifyContent = "center";
  235. styles.container.flexShrink = 1;
  236. styles.container.width = "100%";
  237. }
  238. if (isDisabled) {
  239. if (variant !== "ghost") {
  240. styles.container.borderColor = colors.system.state.overlay.disabled[styleType];
  241. if (variant === "filled") {
  242. styles.overlay.backgroundColor = colors.system.state.overlay.disabled[styleType];
  243. }
  244. }
  245. }
  246. if (icon && title) {
  247. if (iconDirection === "left") {
  248. styles.title.marginLeft = spaces.spacingSm;
  249. } else {
  250. styles.title.marginRight = spaces.spacingSm;
  251. }
  252. }
  253. if(icon && !title) {
  254. styles.container.paddingBottom = currentSize.paddingVertical;
  255. styles.container.paddingRight = currentSize.paddingVertical;
  256. styles.container.paddingLeft = currentSize.paddingVertical;
  257. styles.container.paddingTop = currentSize.paddingVertical;
  258. }
  259. if(isCustomPadding) {
  260. delete styles.container.paddingBottom;
  261. delete styles.container.paddingRight;
  262. delete styles.container.paddingLeft;
  263. delete styles.container.paddingTop;
  264. }
  265. return styles;
  266. };
  267. export default stylesheet;