diff --git a/.changeset/native-safe-area-insets.md b/.changeset/native-safe-area-insets.md
new file mode 100644
index 000000000..994235914
--- /dev/null
+++ b/.changeset/native-safe-area-insets.md
@@ -0,0 +1,7 @@
+---
+"@stackflow/plugin-basic-ui": minor
+---
+
+Read safe area insets from the `--native-safe-area-inset-*` CSS variables first, falling back to `env(safe-area-inset-*)` when they are not defined. A host app that injects these variables on the root element before the first render, and updates them whenever the insets change, no longer causes a layout shift in `AppScreen`, `AppBar`, `Modal`, and `BottomSheet`.
+
+If your host app injects these variables and you set `appBar.minSafeAreaInsetTop` or its CSS variable to a cached inset to avoid that layout shift, remove the override. `AppBar` uses the larger of the two, so a stale cached value still wins over the native variable. Without the native variables, `env()` can still be `0px` on the first render, so a cached override still prevents the shift there.
diff --git a/docs/pages/api-references/plugins/plugin-basic-ui.en.mdx b/docs/pages/api-references/plugins/plugin-basic-ui.en.mdx
index e2a9fe04f..cff6d85bb 100644
--- a/docs/pages/api-references/plugins/plugin-basic-ui.en.mdx
+++ b/docs/pages/api-references/plugins/plugin-basic-ui.en.mdx
@@ -89,12 +89,16 @@ Include `viewport-fit=cover` in the viewport meta tag so the browser exposes the
```
+Each inset reads the `--native-safe-area-inset-top`, `--native-safe-area-inset-right`, `--native-safe-area-inset-bottom`, and `--native-safe-area-inset-left` CSS variables first, and falls back to `env(safe-area-inset-*)` when a variable is not defined. A host app can inject these variables on the root element (`document.documentElement`) before the first render to avoid the layout shift that occurs while `env()` values are not yet available. The host app must also update the variables whenever the insets change, for example after a rotation or when a foldable device unfolds. A defined variable always takes precedence over `env()`, so a value that is never updated stays stale. To fall back to `env()`, remove the variables. Variables set on any other element, such as `body`, are ignored.
+
+If your host app injects these variables and you set `appBar.minSafeAreaInsetTop` or its CSS variable to a previously measured inset to work around that layout shift, remove the override. `AppBar` uses the larger of `minSafeAreaInsetTop` and the current inset, so a stale value still wins after the inset changes, for example after a rotation.
+
To extend media or other content to the screen edges, wrap it in a block element and cancel the insets with negative margins on the wrapper:
```css copy
.full-bleed {
- margin-left: calc(-1 * env(safe-area-inset-left, 0px));
- margin-right: calc(-1 * env(safe-area-inset-right, 0px));
+ margin-left: calc(-1 * var(--native-safe-area-inset-left, env(safe-area-inset-left, 0px)));
+ margin-right: calc(-1 * var(--native-safe-area-inset-right, env(safe-area-inset-right, 0px)));
}
```
diff --git a/docs/pages/api-references/plugins/plugin-basic-ui.ko.mdx b/docs/pages/api-references/plugins/plugin-basic-ui.ko.mdx
index c56112244..dc0e8068c 100644
--- a/docs/pages/api-references/plugins/plugin-basic-ui.ko.mdx
+++ b/docs/pages/api-references/plugins/plugin-basic-ui.ko.mdx
@@ -87,12 +87,16 @@ const Something = () => {
```
+각 Inset은 `--native-safe-area-inset-top`, `--native-safe-area-inset-right`, `--native-safe-area-inset-bottom`, `--native-safe-area-inset-left` CSS 변수를 먼저 사용하고, 변수가 정의되지 않은 환경에서는 `env(safe-area-inset-*)`를 사용해요. 호스트 앱이 첫 렌더링 전에 루트 요소(`document.documentElement`)에 이 변수를 주입하면 `env()` 값이 늦게 반영되면서 생기는 레이아웃 시프트를 막을 수 있어요. 화면 회전이나 폴더블 기기를 펼칠 때처럼 Inset이 바뀌면 호스트 앱이 변수도 함께 갱신해야 해요. 정의된 변수는 항상 `env()`보다 우선하기 때문에, 갱신하지 않으면 이전 값이 계속 적용돼요. `env()`로 돌아가려면 변수를 제거해 주세요. `body` 등 다른 요소에 설정한 변수는 사용되지 않아요.
+
+호스트 앱이 이 변수를 주입하는 환경에서, 레이아웃 시프트를 피하려고 이전에 측정한 Inset 값을 `appBar.minSafeAreaInsetTop`이나 해당 CSS 변수에 넣고 있다면 제거해 주세요. `AppBar`는 `minSafeAreaInsetTop`과 현재 Inset 중 큰 값을 사용하기 때문에, 화면 회전 등으로 Inset이 바뀌어도 이전 값이 계속 적용돼요.
+
이미지 등 본문 요소를 화면 끝까지 넓히려면 블록 요소로 감싸고, 감싼 요소에 음수 마진을 지정해 Inset을 상쇄해 주세요.
```css copy
.full-bleed {
- margin-left: calc(-1 * env(safe-area-inset-left, 0px));
- margin-right: calc(-1 * env(safe-area-inset-right, 0px));
+ margin-left: calc(-1 * var(--native-safe-area-inset-left, env(safe-area-inset-left, 0px)));
+ margin-right: calc(-1 * var(--native-safe-area-inset-right, env(safe-area-inset-right, 0px)));
}
```
diff --git a/extensions/plugin-basic-ui/src/components/AppBar.css.ts b/extensions/plugin-basic-ui/src/components/AppBar.css.ts
index 142390231..e265373f5 100644
--- a/extensions/plugin-basic-ui/src/components/AppBar.css.ts
+++ b/extensions/plugin-basic-ui/src/components/AppBar.css.ts
@@ -2,7 +2,7 @@ import { style } from "@vanilla-extract/css";
import { recipe } from "@vanilla-extract/recipes";
import { android, cupertino, globalVars } from "../basicUIPlugin.css";
-import { f } from "../styles";
+import { f, safeAreaInset } from "../styles";
import {
enterActive,
enterDone,
@@ -184,10 +184,7 @@ export const appBar = recipe({
});
export const safeArea = style({
- height: [
- `max(${globalVars.appBar.minSafeAreaInsetTop}, constant(safe-area-inset-top))`,
- `max(${globalVars.appBar.minSafeAreaInsetTop}, env(safe-area-inset-top))`,
- ],
+ height: `max(${globalVars.appBar.minSafeAreaInsetTop}, ${safeAreaInset.top})`,
});
export const container = style([
@@ -196,14 +193,8 @@ export const container = style([
appBarOverflow,
{
height: globalVars.appBar.height,
- marginLeft: [
- "constant(safe-area-inset-left)",
- "env(safe-area-inset-left, 0px)",
- ],
- marginRight: [
- "constant(safe-area-inset-right)",
- "env(safe-area-inset-right, 0px)",
- ],
+ marginLeft: safeAreaInset.left,
+ marginRight: safeAreaInset.right,
transition: transitions({
height: globalVars.appBar.heightTransitionDuration,
}),
diff --git a/extensions/plugin-basic-ui/src/components/AppScreen.css.ts b/extensions/plugin-basic-ui/src/components/AppScreen.css.ts
index 746729bcd..c2b303144 100644
--- a/extensions/plugin-basic-ui/src/components/AppScreen.css.ts
+++ b/extensions/plugin-basic-ui/src/components/AppScreen.css.ts
@@ -2,7 +2,7 @@ import { createThemeContract, style } from "@vanilla-extract/css";
import { recipe } from "@vanilla-extract/recipes";
import { android, cupertino, globalVars } from "../basicUIPlugin.css";
-import { f } from "../styles";
+import { f, safeAreaInset } from "../styles";
export const vars = createThemeContract({
transitionDuration: null,
@@ -55,22 +55,15 @@ export const appScreen = recipe({
[vars.appBar.topMargin]: globalVars.appBar.height,
},
/**
- * When `max()` and `env()` (or `constant()`) supported
+ * When `max()` supported
*
- * - https://caniuse.com/css-env-function
* - https://caniuse.com/css-math-functions
*/
"@supports": {
- "(padding: max(0px)) and (padding: constant(safe-area-inset-top))": {
+ "(padding: max(0px))": {
vars: {
[vars.appBar.topMargin]:
- `calc(${globalVars.appBar.height} + max(${globalVars.appBar.minSafeAreaInsetTop}, constant(safe-area-inset-top)))`,
- },
- },
- "(padding: max(0px)) and (padding: env(safe-area-inset-top))": {
- vars: {
- [vars.appBar.topMargin]:
- `calc(${globalVars.appBar.height} + max(${globalVars.appBar.minSafeAreaInsetTop}, env(safe-area-inset-top)))`,
+ `calc(${globalVars.appBar.height} + max(${globalVars.appBar.minSafeAreaInsetTop}, ${safeAreaInset.top}))`,
},
},
},
@@ -174,14 +167,8 @@ export const paperContent = recipe({
f.posAbsFull,
f.borderBox,
{
- paddingLeft: [
- "constant(safe-area-inset-left)",
- "env(safe-area-inset-left, 0px)",
- ],
- paddingRight: [
- "constant(safe-area-inset-right)",
- "env(safe-area-inset-right, 0px)",
- ],
+ paddingLeft: safeAreaInset.left,
+ paddingRight: safeAreaInset.right,
overflowY: "scroll",
WebkitOverflowScrolling: "touch",
"::-webkit-scrollbar": {
diff --git a/extensions/plugin-basic-ui/src/components/BottomSheet.css.ts b/extensions/plugin-basic-ui/src/components/BottomSheet.css.ts
index 07fc7c500..2864dd5dc 100644
--- a/extensions/plugin-basic-ui/src/components/BottomSheet.css.ts
+++ b/extensions/plugin-basic-ui/src/components/BottomSheet.css.ts
@@ -2,7 +2,7 @@ import { createThemeContract, style } from "@vanilla-extract/css";
import { recipe } from "@vanilla-extract/recipes";
import { globalVars } from "../basicUIPlugin.css";
-import { f } from "../styles";
+import { f, safeAreaInset } from "../styles";
export const vars = createThemeContract({
transitionDuration: null,
@@ -60,18 +60,9 @@ export const paper = style([
f.overflowHidden,
allTransitions,
{
- paddingRight: [
- "constant(safe-area-inset-right)",
- "env(safe-area-inset-right, 0px)",
- ],
- paddingBottom: [
- "constant(safe-area-inset-bottom)",
- "env(safe-area-inset-bottom, 0px)",
- ],
- paddingLeft: [
- "constant(safe-area-inset-left)",
- "env(safe-area-inset-left, 0px)",
- ],
+ paddingRight: safeAreaInset.right,
+ paddingBottom: safeAreaInset.bottom,
+ paddingLeft: safeAreaInset.left,
backgroundColor: globalVars.backgroundColor,
backgroundImage: globalVars.backgroundImage,
width: "100%",
diff --git a/extensions/plugin-basic-ui/src/components/Modal.css.ts b/extensions/plugin-basic-ui/src/components/Modal.css.ts
index 657a07a5a..811d36d6d 100644
--- a/extensions/plugin-basic-ui/src/components/Modal.css.ts
+++ b/extensions/plugin-basic-ui/src/components/Modal.css.ts
@@ -2,7 +2,7 @@ import { createThemeContract, style } from "@vanilla-extract/css";
import { recipe } from "@vanilla-extract/recipes";
import { globalVars } from "../basicUIPlugin.css";
-import { f } from "../styles";
+import { f, safeAreaInset } from "../styles";
export const vars = createThemeContract({
transitionDuration: null,
@@ -43,22 +43,10 @@ export const dim = style([
f.flexJustifyCenter,
allTransitions,
{
- paddingTop: [
- "constant(safe-area-inset-top)",
- "env(safe-area-inset-top, 0px)",
- ],
- paddingRight: [
- "constant(safe-area-inset-right)",
- "env(safe-area-inset-right, 0px)",
- ],
- paddingBottom: [
- "constant(safe-area-inset-bottom)",
- "env(safe-area-inset-bottom, 0px)",
- ],
- paddingLeft: [
- "constant(safe-area-inset-left)",
- "env(safe-area-inset-left, 0px)",
- ],
+ paddingTop: safeAreaInset.top,
+ paddingRight: safeAreaInset.right,
+ paddingBottom: safeAreaInset.bottom,
+ paddingLeft: safeAreaInset.left,
backgroundColor: globalVars.dimBackgroundColor,
zIndex: vars.zIndexes.dim,
opacity: 0,
diff --git a/extensions/plugin-basic-ui/src/styles/index.ts b/extensions/plugin-basic-ui/src/styles/index.ts
index 773ae6c30..3f71fd511 100644
--- a/extensions/plugin-basic-ui/src/styles/index.ts
+++ b/extensions/plugin-basic-ui/src/styles/index.ts
@@ -1 +1,2 @@
export * as f from "./f.css";
+export { safeAreaInset } from "./safeAreaInset.css";
diff --git a/extensions/plugin-basic-ui/src/styles/safeAreaInset.css.ts b/extensions/plugin-basic-ui/src/styles/safeAreaInset.css.ts
new file mode 100644
index 000000000..b9cf29b7d
--- /dev/null
+++ b/extensions/plugin-basic-ui/src/styles/safeAreaInset.css.ts
@@ -0,0 +1,32 @@
+import { globalStyle } from "@vanilla-extract/css";
+
+const SIDES = ["top", "right", "bottom", "left"] as const;
+
+type Side = (typeof SIDES)[number];
+
+const property = (side: Side) =>
+ `--stackflow-plugin-basic-ui-safe-area-inset-${side}`;
+
+export const safeAreaInset = Object.fromEntries(
+ SIDES.map((side) => [side, `var(${property(side)})`]),
+) as Record;
+
+const withFallback = (fallback: (side: Side) => string) =>
+ Object.fromEntries(
+ SIDES.map((side) => [
+ property(side),
+ `var(--native-safe-area-inset-${side}, ${fallback(side)})`,
+ ]),
+ );
+
+globalStyle(":root", {
+ vars: withFallback(() => "0px"),
+ "@supports": {
+ "(left: constant(safe-area-inset-left))": {
+ vars: withFallback((side) => `constant(safe-area-inset-${side})`),
+ },
+ "(left: env(safe-area-inset-left))": {
+ vars: withFallback((side) => `env(safe-area-inset-${side})`),
+ },
+ },
+});