| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294 |
- import {
- type TextStyle,
- type ViewStyle,
- StyleSheet
- } from "react-native";
- import {
- type ButtonVariantConstantType,
- type ButtonTypeConstantType,
- type ButtonSizeConstantType,
- type ButtonDynamicStyleType,
- type ButtonMeasuresKeys,
- type ButtonMeasures,
- type ButtonVariants,
- type ButtonVariant,
- type ButtonTypes,
- type ButtonSize,
- type ButtonType
- } from "./type";
- import type {
- Mutable
- } from "../../types";
- export const BUTTON_SIZES: Record<ButtonSize, ButtonMeasuresKeys> = {
- small: {
- paddingHorizontal: "spacingMd",
- paddingVertical: "spacingSm",
- fontSize: "labelSmallSize"
- },
- medium: {
- paddingHorizontal: "spacingLg",
- paddingVertical: "spacingMd",
- fontSize: "labelMediumSize"
- },
- large: {
- paddingHorizontal: "spacingXl",
- paddingVertical: "spacingLg",
- fontSize: "labelLargeSize"
- }
- };
- export const BUTTON_VARIANT_STYLES: Record<
- ButtonVariant,
- {
- titleColor: "type" | keyof NCoreUIKit.TextContentColors;
- iconColor: "type" | keyof NCoreUIKit.ColorsMerged;
- borderColor: "transparent" | "type" | "container";
- iconColorScheme?: keyof NCoreUIKit.ThemeTokens;
- containerColor: "transparent" | "type";
- }
- > = {
- filled: {
- borderColor: "container",
- containerColor: "type",
- titleColor: "type",
- iconColor: "type"
- },
- outline: {
- containerColor: "transparent",
- borderColor: "type",
- titleColor: "type",
- iconColor: "type"
- },
- ghost: {
- containerColor: "transparent",
- borderColor: "transparent",
- titleColor: "type",
- iconColor: "type"
- }
- };
- export const BUTTON_TYPE_STYLES: Record<
- ButtonType,
- {
- containerColor: keyof NCoreUIKit.ContainerContentColors;
- borderColor: keyof NCoreUIKit.BorderContentColors;
- titleColor: keyof NCoreUIKit.TextContentColors;
- iconColor: keyof NCoreUIKit.IconContentColors;
- }
- > = {
- primary: {
- containerColor: "primary",
- borderColor: "emphasized",
- titleColor: "onPrimary",
- iconColor: "onPrimary"
- },
- danger: {
- containerColor: "danger",
- borderColor: "danger",
- titleColor: "danger",
- iconColor: "danger"
- },
- success: {
- containerColor: "success",
- borderColor: "success",
- titleColor: "success",
- iconColor: "success"
- },
- warning: {
- containerColor: "warning",
- borderColor: "warning",
- titleColor: "warning",
- iconColor: "warning"
- },
- info: {
- containerColor: "info",
- borderColor: "info",
- titleColor: "info",
- iconColor: "info"
- },
- neutral: {
- containerColor: "subtle",
- borderColor: "subtle",
- titleColor: "mid",
- iconColor: "mid"
- }
- };
- export const getButtonType = ({
- type
- }: ButtonTypeConstantType): ButtonTypes => {
- const currentType = BUTTON_TYPE_STYLES[type];
- return currentType;
- };
- export const getButtonVariant = ({
- variant
- }: ButtonVariantConstantType): ButtonVariants => {
- const currentVariant = BUTTON_VARIANT_STYLES[variant];
- return currentVariant;
- };
- export const getButtonSize = ({
- spaces,
- size
- }: ButtonSizeConstantType): ButtonMeasures => {
- const currentSize = BUTTON_SIZES[size];
- return {
- paddingHorizontal: spaces[currentSize.paddingHorizontal],
- paddingVertical: spaces[currentSize.paddingVertical],
- fontSize: currentSize.fontSize
- };
- };
- const stylesheet = StyleSheet.create({
- container: {
- backgroundColor: "transparent",
- borderColor: "transparent",
- flexDirection: "row",
- borderStyle: "solid",
- alignItems: "center",
- position: "relative",
- display: "flex"
- },
- title: {
- margin: "0",
- },
- loading: {},
- overlay: {
- position: "absolute",
- display: "none",
- bottom: 0,
- right: 0,
- left: 0,
- top: 0
- }
- });
- export const useStyles = ({
- displayBehaviourWhileLoading,
- spreadBehaviour,
- isCustomPadding,
- currentVariant,
- iconDirection,
- currentType,
- currentSize,
- isDisabled,
- isLoading,
- radiuses,
- variant,
- borders,
- colors,
- spaces,
- title,
- icon,
- type
- }: ButtonDynamicStyleType) => {
- const styleType = type === "danger" ? "error" : type;
- const styles = {
- container: {
- paddingRight: currentSize.paddingHorizontal,
- paddingBottom: currentSize.paddingVertical,
- paddingLeft: currentSize.paddingHorizontal,
- paddingTop: currentSize.paddingVertical,
- borderRadius: radiuses.md,
- borderWidth: borders.line
- } as Mutable<ViewStyle>,
- title: {
- } as Mutable<TextStyle>,
- loading: {
- } as Mutable<ViewStyle>,
- overlay: {
- borderRadius: radiuses.md - 2
- } as Mutable<ViewStyle>
- };
- if (currentVariant.containerColor === "type") {
- styles.container.backgroundColor = colors.content.container[currentType.containerColor];
- } else {
- styles.container.backgroundColor = "transparent";
- }
- if (currentVariant.borderColor === "type") {
- styles.container.borderColor = colors.content.border[currentType.borderColor];
- } else if (currentVariant.borderColor === "container") {
- styles.container.borderColor = styles.container.backgroundColor;
- } else {
- styles.container.borderColor = "transparent";
- }
- if (isLoading) {
- styles.title.marginLeft = spaces.spacingSm;
- styles.overlay.display = "flex";
- if (displayBehaviourWhileLoading === "disabled") {
- if (variant !== "ghost") {
- styles.container.borderColor = colors.system.state.overlay.disabled[styleType];
- if (variant === "filled") {
- styles.overlay.backgroundColor = colors.system.state.overlay.disabled[styleType];
- }
- }
- }
- }
- if (isLoading && spreadBehaviour === "stretch") {
- styles.title.marginLeft = spaces.spacingSm;
- styles.title.margin = "initial";
- }
- if (icon && !isLoading) {
- styles.title.marginLeft = spaces.spacingSm;
- styles.title.margin = "initial";
- }
- if (spreadBehaviour === "baseline") {
- styles.container.alignSelf = spreadBehaviour;
- styles.container.width = "auto";
- } else if (spreadBehaviour === "stretch") {
- styles.container.alignSelf = spreadBehaviour;
- styles.container.justifyContent = "center";
- styles.container.flexShrink = 1;
- styles.container.width = "100%";
- }
- if (isDisabled) {
- if (variant !== "ghost") {
- styles.container.borderColor = colors.system.state.overlay.disabled[styleType];
- if (variant === "filled") {
- styles.overlay.backgroundColor = colors.system.state.overlay.disabled[styleType];
- }
- }
- }
- if (icon && title) {
- if (iconDirection === "left") {
- styles.title.marginLeft = spaces.spacingSm;
- } else {
- styles.title.marginRight = spaces.spacingSm;
- }
- }
- if(icon && !title) {
- styles.container.paddingBottom = currentSize.paddingVertical;
- styles.container.paddingRight = currentSize.paddingVertical;
- styles.container.paddingLeft = currentSize.paddingVertical;
- styles.container.paddingTop = currentSize.paddingVertical;
- }
- if(isCustomPadding) {
- delete styles.container.paddingBottom;
- delete styles.container.paddingRight;
- delete styles.container.paddingLeft;
- delete styles.container.paddingTop;
- }
- return styles;
- };
- export default stylesheet;
|