-
Notifications
You must be signed in to change notification settings - Fork 185
/
Copy pathfunctions.ts
189 lines (170 loc) · 6.87 KB
/
functions.ts
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
import type { Exact } from '../../types';
import { type PlatformType } from '../platform';
import { BREAKPOINTS } from './breakpoints';
import {
type SizeTypeValues,
VIEW_WIDTH_TO_CSS_BREAKPOINT_MAP,
ViewHeight,
type ViewHeightType,
ViewWidth,
type ViewWidthType,
} from './constants';
import type { CSSBreakpointsClassNames, MediaQueries } from './types';
/**
* @public
*/
export function getViewWidthByViewportWidth(viewportWidth: number): ViewWidthType {
if (viewportWidth >= BREAKPOINTS.DESKTOP) {
return ViewWidth.DESKTOP;
}
if (viewportWidth >= BREAKPOINTS.TABLET) {
return ViewWidth.TABLET;
}
if (viewportWidth >= BREAKPOINTS.SMALL_TABLET) {
return ViewWidth.SMALL_TABLET;
}
if (viewportWidth >= BREAKPOINTS.MOBILE) {
return ViewWidth.MOBILE;
}
return ViewWidth.SMALL_MOBILE;
}
export function getViewWidthByMediaQueries(mediaQueries: MediaQueries): ViewWidthType {
/* eslint-disable no-restricted-properties */
if (mediaQueries.desktopPlus.matches) {
return ViewWidth.DESKTOP;
}
if (mediaQueries.tablet.matches) {
return ViewWidth.TABLET;
}
if (mediaQueries.smallTablet.matches) {
return ViewWidth.SMALL_TABLET;
}
if (mediaQueries.mobile.matches) {
return ViewWidth.MOBILE;
}
/* eslint-enable no-restricted-properties */
return ViewWidth.SMALL_MOBILE;
}
/**
* @public
*/
export function getViewHeightByViewportHeight(viewportHeight: number): ViewHeightType {
if (viewportHeight >= BREAKPOINTS.MEDIUM_HEIGHT) {
return ViewHeight.MEDIUM;
}
if (viewportHeight >= BREAKPOINTS.MOBILE_LANDSCAPE_HEIGHT) {
return ViewHeight.SMALL;
}
return ViewHeight.EXTRA_SMALL;
}
export function getViewHeightByMediaQueries(mediaQueries: MediaQueries): ViewHeightType {
/* eslint-disable no-restricted-properties */
if (mediaQueries.mediumHeight.matches) {
return ViewHeight.MEDIUM;
}
if (mediaQueries.mobileLandscapeHeight.matches) {
return ViewHeight.SMALL;
}
/* eslint-enable no-restricted-properties */
return ViewHeight.EXTRA_SMALL;
}
export function getSizeX(viewWidth: ViewWidthType): SizeTypeValues {
return viewWidth <= ViewWidth.MOBILE ? 'compact' : 'regular';
}
export function isCompactByViewWidth(
viewWidth: ViewWidthType | undefined,
hasPointer?: boolean,
): boolean | undefined {
return viewWidth !== undefined && viewWidth >= ViewWidth.SMALL_TABLET && hasPointer;
}
export function isCompactByViewHeight(viewHeight: ViewHeightType | undefined): boolean {
return viewHeight !== undefined && viewHeight <= ViewHeight.EXTRA_SMALL;
}
export function getSizeY(
viewWidth: ViewWidthType,
viewHeight: ViewHeightType,
hasPointer: boolean,
): SizeTypeValues {
if (isCompactByViewWidth(viewWidth, hasPointer) || isCompactByViewHeight(viewHeight)) {
return 'compact';
}
return 'regular';
}
/**
* Проверка на Desktop.
*
* Функция гарантировано вернёт `boolean` или `null` в зависимости от условий.
*
* Возвращаем `null` в случае, если у нас недостаточно данных, чтобы определить платформу.
*
* ⚠️ При передаче 'vkcom' всегда будет возвращать `true`.
*/
export function tryToCheckIsDesktop(viewWidth: ViewWidthType, viewHeight: ViewHeightType, hasPointer: undefined | boolean, platform?: PlatformType): boolean; // prettier-ignore
export function tryToCheckIsDesktop(viewWidth: ViewWidthType, viewHeight: undefined, hasPointer: boolean, platform?: PlatformType): boolean; // prettier-ignore
export function tryToCheckIsDesktop(viewWidth: undefined | ViewWidthType, viewHeight: undefined, hasPointer: undefined, platform?: PlatformType): null; // prettier-ignore
export function tryToCheckIsDesktop(viewWidth: undefined, viewHeight: undefined | ViewHeightType, hasPointer: undefined, platform?: PlatformType): null; // prettier-ignore
export function tryToCheckIsDesktop(viewWidth: undefined, viewHeight: undefined, hasPointer: undefined | boolean, platform?: PlatformType): null; // prettier-ignore
export function tryToCheckIsDesktop(viewWidth: undefined | ViewWidthType, viewHeight: undefined | ViewHeightType, hasPointer: undefined | boolean, platform?: PlatformType): null | boolean; // prettier-ignore
export function tryToCheckIsDesktop(
viewWidth: undefined | ViewWidthType,
viewHeight: undefined | ViewHeightType,
hasPointer: undefined | boolean,
platform?: PlatformType,
): null | boolean {
// см. https://github.com/VKCOM/VKUI/pull/2473
const IS_VKCOM_CRUTCH = platform === 'vkcom';
if (
((viewWidth === undefined || hasPointer === undefined) &&
(viewWidth === undefined || viewHeight === undefined)) ||
(hasPointer === undefined && viewHeight === undefined)
) {
return IS_VKCOM_CRUTCH ? true : null;
}
const widthIsLikeDesktop = viewWidth >= ViewWidth.SMALL_TABLET;
const otherParametersIsLikeDesktop =
hasPointer || (viewHeight !== undefined ? viewHeight >= ViewHeight.MEDIUM : false);
return (widthIsLikeDesktop && otherParametersIsLikeDesktop) || IS_VKCOM_CRUTCH;
}
/**
* Конвертирует `viewWidth` в CSS брейкпоинты (см. тесты для наглядности).
*
* > Note: используется восклицательный знак (!), чтобы принудить TS поверить, что св-во точно не может быть
* > `undefined`. Это всё из-за применения `Partial<...>` для объекта.
*/
export function viewWidthToClassName<T extends Partial<CSSBreakpointsClassNames>>(
breakpointClassNames: Exact<CSSBreakpointsClassNames, T>,
viewWidth: ViewWidthType | 'none' = 'none',
): string | null {
if (viewWidth === 'none') {
return breakpointClassNames.hasOwnProperty('none') ? breakpointClassNames['none']! : null;
}
const breakpoints: string[] = [];
const breakpointName = VIEW_WIDTH_TO_CSS_BREAKPOINT_MAP[viewWidth];
if (breakpointClassNames.hasOwnProperty(breakpointName)) {
breakpoints.push(breakpointClassNames[breakpointName]!);
}
if (viewWidth >= ViewWidth.MOBILE) {
if (breakpointClassNames.hasOwnProperty('mobilePlus')) {
breakpoints.push(breakpointClassNames['mobilePlus']!);
}
}
if (viewWidth >= ViewWidth.SMALL_TABLET) {
if (breakpointClassNames.hasOwnProperty('smallTabletPlus')) {
breakpoints.push(breakpointClassNames['smallTabletPlus']!);
}
} else {
if (breakpointClassNames.hasOwnProperty('smallTabletMinus')) {
breakpoints.push(breakpointClassNames['smallTabletMinus']!);
}
}
if (viewWidth >= ViewWidth.TABLET) {
if (breakpointClassNames.hasOwnProperty('tabletPlus')) {
breakpoints.push(breakpointClassNames['tabletPlus']!);
}
} else {
if (breakpointClassNames.hasOwnProperty('tabletMinus')) {
breakpoints.push(breakpointClassNames['tabletMinus']!);
}
}
return breakpoints.length > 0 ? breakpoints.join(' ') : null;
}