-
Notifications
You must be signed in to change notification settings - Fork 171
Expand file tree
/
Copy pathDivider.styling.ts
More file actions
199 lines (185 loc) · 6.4 KB
/
Divider.styling.ts
File metadata and controls
199 lines (185 loc) · 6.4 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import { useMemo } from 'react';
import type { ViewProps, ColorValue } from 'react-native';
import { Platform } from 'react-native';
import { memoize, mergeProps, mergeStyles } from '@fluentui-react-native/framework';
import type { Theme } from '@fluentui-react-native/framework';
import type { IconPropsV1 as IconProps } from '@fluentui-react-native/icon';
import type { TextProps } from '@fluentui-react-native/text';
import { globalTokens } from '@fluentui-react-native/theme-tokens';
import { fontStyles } from '@fluentui-react-native/tokens';
import type { DividerTokens, DividerProps, DividerAppearance } from './Divider.types';
const isMobile = Platform.OS === 'android' || Platform.OS === 'ios';
const getIconProps = (contentColor: ColorValue, icon: IconProps): IconProps => {
if (icon.fontSource) {
return {
fontSource: {
...icon.fontSource,
color: contentColor,
},
};
} else if (icon.svgSource) {
return {
svgSource: {
...icon.svgSource,
color: contentColor,
},
};
} else {
throw new Error('IconProps require either a fontSource or svgSource; neither has been passed.');
}
};
export const useDividerSlotProps = (props: DividerProps, tokens: DividerTokens, theme: Theme) => {
const rootProps: ViewProps = useMemo(
() => ({
style: {
alignItems: 'center',
justifyContent: 'center',
display: 'flex',
minWidth: tokens.minWidth,
maxWidth: tokens.maxWidth,
minHeight: tokens.minHeight,
maxHeight: tokens.maxHeight,
padding: tokens.padding,
paddingStart: tokens.paddingStart,
paddingEnd: tokens.paddingEnd,
paddingHorizontal: tokens.paddingHorizontal,
paddingVertical: tokens.paddingVertical,
...(props.vertical
? {
flexDirection: 'column',
paddingVertical: props.insetSize,
height: '100%',
}
: {
flexDirection: 'row',
...(isMobile ? { paddingStart: props.insetSize } : { paddingHorizontal: props.insetSize }), // insetSize on iOS and Android is on start only.
}),
},
}),
[
props.vertical,
props.insetSize,
tokens.minHeight,
tokens.maxHeight,
tokens.minWidth,
tokens.maxWidth,
tokens.padding,
tokens.paddingHorizontal,
tokens.paddingVertical,
tokens.paddingStart,
tokens.paddingEnd,
],
);
const beforeLineProps: ViewProps = useMemo(
() => ({
style: {
flexBasis: tokens.minLineSize,
flex: tokens.flexBefore,
borderColor: tokens.lineColor,
borderStyle: 'solid',
...(props.vertical
? { borderLeftWidth: tokens.thickness, minHeight: tokens.minLineSize }
: { borderTopWidth: tokens.thickness, minWidth: tokens.minLineSize }),
},
}),
[tokens.flexBefore, tokens.lineColor, tokens.minLineSize, tokens.thickness, props.vertical],
);
const afterLineProps: ViewProps = useMemo(
() => ({
style: {
flexBasis: tokens.minLineSize,
flex: tokens.flexAfter,
borderColor: tokens.lineColor,
borderStyle: 'solid',
...(props.vertical
? { borderLeftWidth: tokens.thickness, minHeight: tokens.minLineSize }
: { borderTopWidth: tokens.thickness, minWidth: tokens.minLineSize }),
},
}),
[tokens.flexAfter, tokens.lineColor, tokens.minLineSize, tokens.thickness, props.vertical],
);
const wrapperProps: ViewProps = useMemo(
() => ({
style: {
flex: 0,
...(props.vertical ? { paddingVertical: tokens.contentPadding } : { paddingHorizontal: tokens.contentPadding }),
},
}),
[tokens.contentPadding, props.vertical],
);
const textProps: TextProps = useMemo(
() => ({
style: {
textAlign: 'center',
color: tokens.contentColor,
...fontStyles.from(tokens, theme),
},
variant: tokens.variant,
}),
[tokens, theme],
);
const iconProps: IconProps = useMemo(
() => (props.icon ? getIconProps(tokens.contentColor, props.icon) : {}),
[props.icon, tokens.contentColor],
);
return { rootProps, beforeLineProps, afterLineProps, wrapperProps, textProps, iconProps };
};
/**
* Helper function to set color tokens on divider. If no color tokens are passed in, then the appearance prop will be used to provide colors
* for the line and content
*/
export const colorsFromAppearance = (
appearance: DividerAppearance,
tokens: DividerTokens,
theme: Theme,
): Pick<DividerTokens, 'contentColor' | 'lineColor'> => {
switch (appearance) {
case 'default':
return {
contentColor: tokens.contentColor ?? theme.colors.neutralForeground2,
lineColor: tokens.lineColor ?? theme.colors.neutralStroke2,
};
case 'subtle':
return {
contentColor: tokens.contentColor ?? theme.colors.neutralForeground3,
lineColor: tokens.lineColor ?? theme.colors.neutralStroke3,
};
case 'brand':
return {
contentColor: tokens.contentColor ?? theme.colors.brandForeground1,
lineColor: tokens.lineColor ?? theme.colors.brandStroke1,
};
case 'strong': {
return {
contentColor: tokens.contentColor ?? theme.colors.neutralForeground1,
lineColor: tokens.lineColor ?? theme.colors.neutralStroke1,
};
}
}
};
/**
* At the second render stage, if the minHeight token isn't set, we calculate and memoize a default value based on whether the
* Divider is vertical and has content.
*/
export const getRootSlotProps = memoize(rootSlotPropsWorker);
function rootSlotPropsWorker(dividerProps: DividerProps, rootProps: ViewProps, hasContent: boolean): ViewProps {
const { vertical, ...props } = dividerProps;
let minHeight = 0;
if (vertical) {
minHeight = hasContent ? 84 : globalTokens.size200;
}
return mergeProps(props, rootProps, {
style: mergeStyles(rootProps.style, props.style, { minHeight }),
});
}
/**
* At the second render stage, if there is not content passed, we override the existing beforeLine style to make the line take up
* the entirity of root.
*/
export const getBeforeLineSlotProps = memoize(beforeLineSlotPropsWorker);
function beforeLineSlotPropsWorker(beforeLineProps: ViewProps, hasContent: boolean) {
if (!hasContent) {
return mergeProps(beforeLineProps, { style: mergeStyles(beforeLineProps.style, { flex: 1 }) });
}
return beforeLineProps;
}