Repository navigation
Expand file tree
/
Copy pathuseBarInset.ts
More file actions
82 lines (73 loc) · 3.57 KB
/
Copy pathuseBarInset.ts
File metadata and controls
82 lines (73 loc) · 3.57 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
import { HeaderHeightContext } from "@react-navigation/elements";
import { use } from "react";
import { Platform } from "react-native";
import { useAdaptiveWorkspaceLayout } from "../../features/layout/adaptive-workspace-context";
import { NATIVE_MAIL_SEARCH_TOOLBAR_CONTENT_INSET } from "../../features/layout/native-mail-search-toolbar";
import { useNativeMailSearchToolbar } from "../../native/use-native-mail-search-toolbar";
import type { AppBarSearch } from "./AppBar.types";
import { useSafeAreaInsets } from "react-native-safe-area-context";
import { IOS_NAV_BAR_HEIGHT } from "../../lib/layoutMetrics";
import { useNativeColumnLayoutMetrics } from "../../native/native-layout-metrics";
import { useToolbarLayout } from "./useToolbarLayout";
/**
* How far below the screen's top edge its AppBar ends: where content that scrolls under a
* translucent bar starts, and where an overlay pinned under the bar goes. Measured by the native
* column or stack on iOS; Android draws the bar in-tree from the toolbar geometry.
*/
export function useBarInset(): number {
const headerHeight = use(HeaderHeightContext);
const columnMetrics = useNativeColumnLayoutMetrics();
const insets = useSafeAreaInsets();
const toolbar = useToolbarLayout();
if (Platform.OS === "android") {
return toolbar.paddingTop + toolbar.height + toolbar.paddingBottom;
}
return headerHeight ?? columnMetrics?.safeArea.top ?? insets.top + IOS_NAV_BAR_HEIGHT;
}
/**
* How far the bar covers the top of the body below it: the bar inset where the body runs under
* the iOS bar, 0 on Android, where the bar sits above the body in flow.
*/
export function useBarOverlap(): number {
const barInset = useBarInset();
return Platform.OS === "android" ? 0 : barInset;
}
/**
* The content's top safe area under the AppBar: the height of the iOS bar its column lays content
* under, glass or solid, and zero on Android, where the bar sits in flow. For manual scroll
* content insets.
*/
export function useContentTopInset(): number {
const insets = useSafeAreaInsets();
const columnMetrics = useNativeColumnLayoutMetrics();
return Platform.OS === "ios" ? (columnMetrics?.safeArea.top ?? insets.top) : 0;
}
/** The content's bottom safe area on iOS, which a list with manual insets must add itself. */
export function useContentBottomInset(): number {
const insets = useSafeAreaInsets();
const columnMetrics = useNativeColumnLayoutMetrics();
return Platform.OS === "ios" ? (columnMetrics?.safeArea.bottom ?? insets.bottom) : 0;
}
/** How much of the screen's bottom edge an AppBar `search` covers: iOS's bottom search toolbar. */
export function useSearchToolbarInset(search: AppBarSearch | undefined): number {
const { layout } = useAdaptiveWorkspaceLayout();
const usesNativeMailSearchToolbar = useNativeMailSearchToolbar();
return search !== undefined &&
(search.compactToolbar ?? !layout.usesSplitView) &&
usesNativeMailSearchToolbar
? NATIVE_MAIL_SEARCH_TOOLBAR_CONTENT_INSET
: 0;
}
/** Where a sheet's scrolling content ends: clear of the bottom safe area, with a little room. */
export function useSheetContentBottomInset(): number {
const insets = useSafeAreaInsets();
const columnMetrics = useNativeColumnLayoutMetrics();
return Platform.OS === "ios"
? (columnMetrics?.safeArea.bottom ?? insets.bottom) + 8
: Math.max(insets.bottom, 18) + 18;
}
/** The bottom safe area a list in a sheet pads itself: Android's, which its sheets don't adjust for. */
export function useUnadjustedBottomInset(): number {
const insets = useSafeAreaInsets();
return Platform.OS === "android" ? insets.bottom : 0;
}