要素
セーフエリア
セーフエリアは、ノッチや Dynamic Island、ステータスバー、ホームインジケーター、ナビゲーションヘッダー、Mac Catalyst のタイトルバーからコンテンツを離しておきます。どのビューでも、CSS の env() である env(safe-area-inset-top)、env(safe-area-inset-right)、env(safe-area-inset-bottom)、env(safe-area-inset-left) を padding や margin に指定することで、その分のパディングを確保できます。これらは、ノードが描画されるルートビューのインセットです。NavPage の内側ではページのヘッダーより下のインセット、Dialog の内側ではダイアログのインセット、それ以外ではサーフェスのセーフエリアになります。ブラウザーではページがルートとなり、変数はブラウザー自身のものです。
import { mount, signal, Text, View } from "nsh";
import { safeArea, safeAreaInsets } from "nsh";
import { button, buttonText, desktop, tint, ui } from "./palette";
type Edge = "top" | "right" | "bottom" | "left";
const EDGES: Edge[] = ["top", "right", "bottom", "left"];
// `calc(env(safe-area-inset-<edge>) + 16px)` on the edges chosen, 16 px on the others.
const side = (edges: Edge[], e: Edge) => (edges.includes(e) ? (`calc(env(safe-area-inset-${e}) + 16px)` as const) : 16);
const padding = (edges: Edge[]) => ({
paddingTop: side(edges, "top"),
paddingRight: side(edges, "right"),
paddingBottom: side(edges, "bottom"),
paddingLeft: side(edges, "left"),
});
const format = (i: { top: number; right: number; bottom: number; left: number }) =>
`top ${Math.round(i.top)} · right ${Math.round(i.right)} · bottom ${Math.round(i.bottom)} · left ${Math.round(i.left)}`;
// Called in a component: the insets where it renders (the root here, so the
// surface's safe area; inside a page or a dialog, theirs).
function Insets() {
const insets = safeAreaInsets();
return <Text style={{ color: ui().text, fontVariant: ["tabular-nums"] }}>{format(insets())}</Text>;
}
function App() {
const edges = signal<Edge[]>([...EDGES]);
const toggle = (edge: Edge) => edges.update((list) => (list.includes(edge) ? list.filter((e) => e !== edge) : EDGES.filter((e) => e === edge || list.includes(e))));
const label = () => ({ fontSize: 12, color: ui().muted });
return (
// The tinted band around the card is the view's padding: the insets of
// the edges chosen, plus 16 pt.
<View style={{ flex: 1, backgroundColor: tint(ui().accent, 0.16), ...padding(edges()) }}>
<View
style={{
flex: 1,
width: "100%",
maxWidth: 560,
alignSelf: "center",
backgroundColor: ui().page,
borderRadius: 16,
borderWidth: 1,
borderStyle: "dashed",
borderColor: ui().faint,
padding: 20,
gap: 14,
}}
>
<Text style={{ fontSize: 22, fontWeight: "700", color: ui().text }}>safe area insets</Text>
<Text style={{ color: ui().muted, lineHeight: 20 }}>
{desktop
? "A desktop window or a browser page has no system UI over it: the insets are 0, so the view pads only by its 16 pt (the tinted band)."
: "On a phone the insets are the status bar or notch on top and the home indicator below; env(safe-area-inset-*) adds them to its 16 pt. This browser frame has none, so they are 0 here."}
</Text>
<Text style={label()}>edges ({desktop ? "click" : "tap"} to toggle)</Text>
<View style={{ flexDirection: "row", flexWrap: "wrap", gap: 8 }}>
<Each of={EDGES}>
{(edge) => {
const on = () => edges().includes(edge());
return (
<View
role="button"
onPress={() => toggle(edge())}
{...button("secondary", { on })}
>
<Text style={buttonText("secondary", { on })}>{edge()}</Text>
</View>
);
}}
</Each>
</View>
<View style={{ ...ui().inset, borderRadius: 12, padding: 14, gap: 6 }}>
<Text style={label()}>edges</Text>
<Text style={{ color: ui().text }}>{edges().length ? `[${edges().map((e) => `"${e}"`).join(", ")}]` : "[] (no inset padded)"}</Text>
<Text style={label()}>safeArea() · the surface's insets</Text>
<Text style={{ color: ui().text, fontVariant: ["tabular-nums"] }}>{format(safeArea())}</Text>
<Text style={label()}>safeAreaInsets() · where it renders</Text>
<Insets />
</View>
</View>
</View>
);
}
mount(App);use nsh_core::prelude::*;
fn app() -> impl IntoElement {
view()
.flex_1()
.bg(rgb(0xfff8c5))
.p_safe_area()
.child(text("Clear of the notch").text_2xl().font_weight(FontWeight::BOLD))
}import { mount, Text, View } from "nsh";
function App() {
return (
<View
style={{
flex: 1,
backgroundColor: "#fff8c5",
paddingTop: "env(safe-area-inset-top)",
paddingRight: "env(safe-area-inset-right)",
paddingBottom: "env(safe-area-inset-bottom)",
paddingLeft: "env(safe-area-inset-left)",
}}
>
<Text style={{ fontSize: 24, fontWeight: "700" }}>Clear of the notch</Text>
</View>
);
}
mount(App);背景色はステータスバーの背後も含めて領域全体を塗りつぶし、コンテンツはその下から始まります。値はインセットに追従します。回転したときや、別のページやダイアログに移ったときも、パディングはネイティブに更新されます。
#値
| 値 | 例 | 意味 |
|---|---|---|
env(name) | paddingTop: "env(safe-area-inset-top)" | インセットそのもの。 |
env(name, <length>) | env(safe-area-inset-top, 20px) | インセット。CSS と同様に、長さは未知の名前の代わりに指定します。 |
calc(env(name) + <length>) | calc(env(safe-area-inset-bottom) + 16px) | インセットに長さを加えた値。独自の余白を保ちたいコンテンツ向け。 |
calc(<length> + env(name)) | calc(16px + env(safe-area-inset-left)) | 同上。 |
これらは padding、paddingHorizontal、paddingVertical、paddingTop、paddingRight、paddingBottom、paddingLeft、および同じ margin のキーに適用できます。Tailwind では、p、px、py、pt、pr、pb、pl と m ユーティリティの任意の値として使えます(スペースは省略可能)。例: pt-[env(safe-area-inset-top)]、pb-[calc(env(safe-area-inset-bottom)+16px)]。これらの値の TypeScript 型は EnvValue です。スタイルリファレンスを参照してください。
Rust では、Styled のメソッドで設定します。.p_safe_area()、.px_safe_area()、.py_safe_area()、.pt_safe_area()、.pr_safe_area()、.pb_safe_area()、.pl_safe_area()、同じ m のメソッド、そしてインセットに長さを加える場合は .pt_safe_area_plus(px(16.0))(および pr、pb、pl、mt、mr、mb、ml)です。
#辺を選ぶ
たとえば、独自のタブバーやボトムバーが下側のインセットを処理する場合など、必要な辺だけにパディングを指定します。
view()
.flex_1()
.pt_safe_area()
.px_safe_area()<View class="flex-1 pt-[env(safe-area-inset-top)] px-[calc(env(safe-area-inset-left)+16px)]">
…
</View>#使う場面
表示されている不透明なヘッダーを持つ NavPage は、すでにコンテンツをヘッダーの下に配置するため、その場合の上側のインセットは 0 です。セーフエリアのインセットは次の場合に使います。
- ナビゲーションスタックを持たないアプリのルート
headerVisible={false}またはheaderTransparentを指定したページ- フルスクリーンの Dialog のコンテンツ
- 画面の端に置く独自のバー
#safeArea と safeAreaInsets
コード内で数値が必要な場合は、直接読み取れます。TSX のアクセサーはどちらも nsh から提供され、Edges オブジェクト({ top, right, bottom, left }、単位はポイント)を返し、リアクティブです。これらを読み取るコードは、たとえば回転などで値が変わると再実行されます。
| TSX | Rust | 戻り値 |
|---|---|---|
safeArea() | safe_area() | サーフェスのセーフエリア。システム UI(ノッチ、ホームインジケーター、タイトルバー)にかからない部分。 |
safeAreaInsets() | cx.insets() | コンポーネントが描画される位置のインセット。最も近いページの(ヘッダーより下の)インセット、最も近いダイアログのインセット、またはそれらがなければサーフェスのセーフエリア。これらは safe-area-inset-* 変数の値です。TSX ではコンポーネント内で呼び出すと、アクセサーを返します。お使いのクレートの Rust コンポーネントでは cx.insets() で読み取ります。 |
use nsh_core::prelude::*;
fn bottom_bar() -> impl IntoElement {
view()
.flex_row()
.p_2()
.pb(move || if safe_area().bottom > Points::ZERO { px(4.0) } else { px(8.0) })
.child(text("Bottom bar"))
}import { Text, View, safeArea } from "nsh";
function BottomBar() {
const insets = safeArea();
return (
<View style={{ flexDirection: "row", padding: 8, paddingBottom: insets().bottom > 0 ? 4 : 8 }}>
<Text>Bottom bar</Text>
</View>
);
}インセットそのものでパディングするだけなら、env() のほうが簡単です。コードを実行せずにネイティブに更新されます。
#プラットフォーム別の注意
- iOS: インセットは UIKit のセーフエリアに由来します。ステータスバー、ノッチまたは Dynamic Island、ホームインジケーターが対象です。
- Android: NSH は、NSH ビューに重なるシステムバーとディスプレイカットアウトの部分を報告します。NSH をネイティブのツールバーの下に埋め込んだ場合、ビューの上側のインセットは 0 です。
- Mac Catalyst: 上側のインセットはウィンドウのタイトルバーです。
- デスクトップ(macOS、Windows、Linux、GPUI)と Web: ページのコンテンツは
headerの下から始まり、ダイアログはスマートフォンと同様に、コンテンツに独自のインセットを与えます。