forked from microsoft/fluentui-react-native
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathButton.styling.ts
More file actions
163 lines (152 loc) · 5.06 KB
/
Button.styling.ts
File metadata and controls
163 lines (152 loc) · 5.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import type { ColorValue } from 'react-native';
import { Platform } from 'react-native';
import type { Theme, UseStylingOptions } from '@fluentui-react-native/framework';
import { buildProps } from '@fluentui-react-native/framework';
import { getTextMarginAdjustment } from '@fluentui-react-native/styling-utils';
import { borderStyles, layoutStyles, fontStyles } from '@fluentui-react-native/tokens';
import type { FontTokens } from '@fluentui-react-native/tokens';
import { buttonName } from './Button.types';
import type { ButtonTokens, ButtonSlotProps, ButtonProps, ButtonSize, ButtonAppearance } from './Button.types';
import { defaultButtonColorTokens } from './ButtonColorTokens';
import { defaultButtonFontTokens } from './ButtonFontTokens';
import { defaultButtonTokens } from './ButtonTokens';
export const buttonStates: (keyof ButtonTokens)[] = [
'block',
'small',
'medium',
'large',
'hasContent',
'hasIconAfter',
'hasIconBefore',
'primary',
'subtle',
'outline',
'rounded',
'circular',
'square',
'hovered',
'focused',
'pressed',
'disabled',
];
const applyOverflowHiddenToRoot = Platform.OS === 'ios' || Platform.OS === 'macos';
export const stylingSettings: UseStylingOptions<ButtonProps, ButtonSlotProps, ButtonTokens> = {
tokens: [defaultButtonTokens, defaultButtonFontTokens, defaultButtonColorTokens, buttonName],
states: buttonStates,
slotProps: {
...(Platform.OS === 'android' && {
rippleContainer: buildProps(
(tokens: ButtonTokens) => {
return {
style: {
flexDirection: 'row',
alignSelf: 'baseline',
borderColor: tokens.borderInnerColor,
borderWidth: tokens.borderInnerWidth,
borderRadius: tokens.borderRadius,
overflow: 'hidden',
},
};
},
['borderRadius'],
),
}),
root: buildProps(
(tokens: ButtonTokens, theme: Theme) => ({
style: {
display: 'flex',
overflow: applyOverflowHiddenToRoot ? 'hidden' : undefined,
alignItems: 'center',
flexDirection: 'row',
alignSelf: 'flex-start',
justifyContent: 'center',
width: tokens.width,
backgroundColor: tokens.backgroundColor,
...borderStyles.from(tokens, theme),
...layoutStyles.from(tokens, theme),
},
android_ripple: {
color: tokens.rippleColor,
},
}),
['backgroundColor', 'width', 'rippleColor', ...borderStyles.keys, ...layoutStyles.keys],
),
content: buildProps(
(tokens: ButtonTokens, theme: Theme) => {
return {
style: {
...contentStyling(tokens, theme, tokens.color, tokens),
},
};
},
['color', 'spacingIconContentAfter', 'spacingIconContentBefore', ...fontStyles.keys],
),
icon: buildProps(
(tokens: ButtonTokens) => ({
color: tokens.iconColor,
height: tokens.iconSize,
width: tokens.iconSize,
}),
['iconColor', 'iconSize'],
),
focusInnerBorder: buildProps(
(tokens: ButtonTokens) => ({
style: {
position: 'absolute',
borderWidth: tokens.borderInnerWidth,
borderColor: tokens.borderInnerColor,
borderRadius: tokens.borderInnerRadius,
},
}),
['borderInnerWidth', 'borderInnerColor', 'borderInnerRadius'],
),
},
};
export const getDefaultSize = (): ButtonSize => {
if (Platform.OS === 'windows') {
return 'medium';
} else if ((Platform.OS as any) === 'win32') {
return 'small';
} else if (Platform.OS === 'macos') {
return 'small';
}
return 'medium';
};
export const getPlatformSpecificAppearance = (appearance: ButtonAppearance): ButtonAppearance => {
// Mobile platforms do not have seperate styling when no appearance is passed.
const hasDifferentDefaultAppearance = !(Platform.OS === 'android' || Platform.OS === 'ios');
switch (appearance) {
case 'accent': // Included to cover Mobile platform naming guidelines, maps to 'primary'.
return 'primary';
case 'primary':
case 'subtle':
case 'outline': // 'Outline' exists only for Mobile platforms, default picked on other platforms.
return appearance;
default:
if (hasDifferentDefaultAppearance) {
return null;
} else {
return 'primary';
}
}
};
export const contentStyling = (tokens: ButtonTokens, theme: Theme, contentColor: ColorValue, fontStylesTokens: FontTokens) => {
const textAdjustment = getTextMarginAdjustment();
const spacingIconContentBefore = tokens.spacingIconContentBefore
? {
marginStart: textAdjustment.marginStart + tokens.spacingIconContentBefore,
}
: {};
const spacingIconContentAfter = tokens.spacingIconContentAfter
? {
marginEnd: textAdjustment.marginEnd + tokens.spacingIconContentAfter,
}
: {};
return {
color: contentColor,
...getTextMarginAdjustment(),
...spacingIconContentBefore,
...spacingIconContentAfter,
...fontStyles.from(fontStylesTokens, theme),
};
};