Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/native-safe-area-insets.md
Original file line number Diff line number Diff line change
@@ -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.
8 changes: 6 additions & 2 deletions docs/pages/api-references/plugins/plugin-basic-ui.en.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,16 @@ Include `viewport-fit=cover` in the viewport meta tag so the browser exposes the
<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover" />
```

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)));
}
```

Expand Down
8 changes: 6 additions & 2 deletions docs/pages/api-references/plugins/plugin-basic-ui.ko.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,16 @@ const Something = () => {
<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover" />
```

각 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)));
}
```

Expand Down
17 changes: 4 additions & 13 deletions extensions/plugin-basic-ui/src/components/AppBar.css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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([
Expand All @@ -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,
}),
Expand Down
25 changes: 6 additions & 19 deletions extensions/plugin-basic-ui/src/components/AppScreen.css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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}))`,
},
},
},
Expand Down Expand Up @@ -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": {
Expand Down
17 changes: 4 additions & 13 deletions extensions/plugin-basic-ui/src/components/BottomSheet.css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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%",
Expand Down
22 changes: 5 additions & 17 deletions extensions/plugin-basic-ui/src/components/Modal.css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions extensions/plugin-basic-ui/src/styles/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * as f from "./f.css";
export { safeAreaInset } from "./safeAreaInset.css";
32 changes: 32 additions & 0 deletions extensions/plugin-basic-ui/src/styles/safeAreaInset.css.ts
Original file line number Diff line number Diff line change
@@ -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<Side, string>;

const withFallback = (fallback: (side: Side) => string) =>
Object.fromEntries(
SIDES.map((side) => [
property(side),
`var(--native-safe-area-inset-${side}, ${fallback(side)})`,
]),
);

globalStyle(":root", {
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
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})`),
},
},
});
Loading