Use tailwindcss-safe-area plugin in capacitor skill

Replaces the hand-rolled safe-area CSS utilities with the
tailwindcss-safe-area Tailwind plugin (same approach Shakespeare uses),
which gives us a richer utility set plus offset / or variants:

  pt-safe, pb-safe, px-safe, p-safe, mt-safe, inset-safe,
  top-safe, bottom-safe, h-dvh-safe, min-h-dvh-safe,
  pt-safe-offset-4 (safe area + 1rem),
  pb-safe-or-8 (max of safe area and 2rem),
  scroll-p-safe, border-t-safe, and more.

Critically, the SKILL.md now pins the plugin version per Tailwind
major \u2014 the plugin changed its registration API between 0.x and
1.x to match Tailwind's own v3\u2192v4 transition:

  Plugin 0.8.0  \u2192 Tailwind 3.x  (add to plugins: array in config)
  Plugin >=1.0  \u2192 Tailwind 4.x  (@import in main CSS, not @plugin)

mkstack ships Tailwind 3.4.x, so install targets 0.8.0 specifically.
Using @latest on a Tailwind 3 project silently produces no utilities
at all \u2014 documented in both the setup section and troubleshooting.

Also documents the Android WebView <140 caveat: Chromium bug 40699457
causes env(safe-area-inset-*) to return 0. Capacitor's SystemBars
plugin (insetsHandling: 'css', already preset in our capacitor.config.ts
template) works around it by injecting --safe-area-inset-* CSS variables,
but tailwindcss-safe-area uses env() directly and can't see them.
Two fixes:
  1. preferred: target WebView 140+ (Aug 2025+, auto-updated on any
     active Android >=7 via Play Services)
  2. fallback: new files/safe-area-shim.css that redeclares the common
     utilities with var(--safe-area-inset-*, env(..., 0px)) so the
     SystemBars-injected variables win. Copy to src/ and @import after
     @tailwind utilities; if targeting older Android fleets.

Changes:
- Remove files/safe-area.css (hand-rolled utilities)
- Add files/safe-area-shim.css (optional Android <140 override)
- SKILL.md: new step 3 'Register the Tailwind Safe-Area Plugin'
  inserted between 'Copy Skill Files' and 'Edit capacitor.config.ts';
  subsequent steps renumbered 4\u201310
- SKILL.md: 'Install Dependencies' adds the Tailwind plugin with a
  version matrix table and explicit v3-pinning rationale
- SKILL.md: 'Using the APIs \u2192 Safe-area utilities' rewritten with
  a real-world usage cheatsheet mapping plugin classes to use cases
- SKILL.md: expanded troubleshooting with three new safe-area
  entries (Android env-0 WebView<140, 'pt-safe doesn't exist' =
  plugin not registered or wrong major version, iOS missing
  viewport-fit=cover)

Validated: full stack staged into src/ + package.json (including
[email protected] and tailwind.config.ts plugins
registration). npm run test passes with only the 6 pre-existing
baseline errors; reverted cleanly.
This commit is contained in:
Alex Gleason committed 2026-04-26 19:53:28 -05:00
1 parent f927a00dcf
commit 3866c0a119
3 files changed
+191 -75

No files matched your search

+119 -22
View File
@@ -26,7 +26,7 @@ user wants to ship the app to the App Store or Google Play (or build an
| **`secureStorage` / `useSecureLocalStorage`** | `localStorage` | iOS Keychain / Android KeyStore, auto-migrates existing plaintext values |
| **`<DeepLinkHandler />`** | no-op | Forwards OS `appUrlOpen` events into React Router |
| **`bootstrapNative()`** | no-op | Hides iOS keyboard accessory bar; syncs system-bar icon style with theme |
| **Safe-area CSS utilities** | `env(safe-area-inset-*)` fallback | `var(--safe-area-inset-*)` injected by `SystemBars` plugin (Android) + iOS notch support |
| **Safe-area utilities** via `tailwindcss-safe-area` Tailwind plugin | `env(safe-area-inset-*)` | `env(safe-area-inset-*)` + `SystemBars` plugin back-fill on Android |
Everything is SSR-safe and web-safe — each helper does the right thing on
every platform, so you can import and call them unconditionally from shared
@@ -55,24 +55,45 @@ location:
| `files/capacitor.config.ts` | `capacitor.config.ts` (project root) — edit `appId`, `appName`, `scheme`, background colors |
| `files/scripts/patch-cap-config.mjs` | `scripts/patch-cap-config.mjs` — only needed if you add **local** (non-SPM) native plugin classes; otherwise skip |
### Snippets to merge
### Optional snippet
| Skill file | What to do |
|---|---|
| `files/safe-area.css` | Paste inside the `@layer utilities { … }` block of `src/index.css` |
| `files/safe-area-shim.css` | Copy to `src/safe-area-shim.css` **only if** you need to support older Android WebView (<140). See the file's header comment for details. Most modern deployments can skip it. |
## Setup Instructions
### 1. Install Dependencies
```bash
# Capacitor runtime + plugins
npm install @capacitor/core @capacitor/app @capacitor/filesystem \
@capacitor/haptics @capacitor/keyboard @capacitor/share \
capacitor-secure-storage-plugin
# Capacitor toolchain (dev)
npm install -D @capacitor/cli @capacitor/android @capacitor/ios
# Safe-area Tailwind plugin — VERSION DEPENDS ON YOUR TAILWIND MAJOR:
# Tailwind v3 (this project): [email protected] (last v3-compat release)
# Tailwind v4 (newer stacks): tailwindcss-safe-area@latest (>=1.0.0)
npm install -D [email protected]
```
Each package:
**Why the pinned version for `tailwindcss-safe-area`?** The plugin's API
changed between v0.x and v1.x to match Tailwind's own v3→v4 transition:
| Plugin version | Tailwind version | How to register |
|---|---|---|
| `^0.8.0` | 3.x | Import in `tailwind.config.ts` as a plugin (this project) |
| `>=1.0.0` | 4.x | `@import "tailwindcss-safe-area";` in your main CSS |
mkstack ships Tailwind **3.4.x**, so pin to `0.8.0`. Using `@latest`
on a Tailwind v3 project silently produces no utilities. If you later
upgrade the project to Tailwind v4, bump this dependency at the same
time and switch from the `plugins:` array to a CSS `@import`.
Each runtime package:
- **`@capacitor/core`** (runtime) — `Capacitor.isNativePlatform()`,
`Capacitor.getPlatform()`, `registerPlugin`, `SystemBars`
@@ -88,15 +109,39 @@ Each package:
- **`@capacitor/share`** (runtime) — native share sheet (used by `openUrl`)
- **`capacitor-secure-storage-plugin`** (runtime) — iOS Keychain / Android
KeyStore wrapper (used by `secureStorage`)
- **`tailwindcss-safe-area`** (dev) — safe-area utilities
(`pt-safe`, `pb-safe`, `px-safe`, `top-safe`, `h-dvh-safe`, …)
### 2. Copy the Skill Files
Copy every file listed in the tables above from
`.agents/skills/capacitor/files/` into its corresponding location.
Paste the contents of `files/safe-area.css` into the
`@layer utilities { … }` block of `src/index.css`.
### 3. Edit `capacitor.config.ts`
### 3. Register the Tailwind Safe-Area Plugin
Edit `tailwind.config.ts` to import the plugin and append it to the
`plugins` array:
```ts
// tailwind.config.ts
import type { Config } from "tailwindcss";
import tailwindcssAnimate from "tailwindcss-animate";
import safeArea from "tailwindcss-safe-area"; // add this
export default {
// ...existing config...
plugins: [tailwindcssAnimate, safeArea], // append safeArea
} satisfies Config;
```
This unlocks utilities like `pt-safe`, `pb-safe`, `px-safe`, `top-safe`,
`bottom-safe`, `h-dvh-safe`, `min-h-dvh-safe`, plus the `-offset-{n}` /
`-or-{n}` variants (e.g. `pb-safe-offset-4` = safe area + 1rem;
`pb-safe-or-8` = max of safe area and 2rem). See the plugin's
[README](https://github.com/mvllow/tailwindcss-safe-area) for the full
utility list.
### 4. Edit `capacitor.config.ts`
Open the copied `capacitor.config.ts` and fill in your app identity:
@@ -121,7 +166,7 @@ const config: CapacitorConfig = {
**Important:** `appId` cannot be changed after the app is published to
either store.
### 4. Update `index.html`
### 5. Update `index.html`
Ensure your viewport meta tag opts into safe-area insets — without
`viewport-fit=cover`, iOS will not expose `env(safe-area-inset-*)`:
@@ -131,7 +176,7 @@ Ensure your viewport meta tag opts into safe-area insets — without
content="width=device-width, initial-scale=1, viewport-fit=cover, interactive-widget=resizes-content" />
```
### 5. Wire `bootstrapNative()` into `src/main.tsx`
### 6. Wire `bootstrapNative()` into `src/main.tsx`
Call it **before** `createRoot(...).render(...)` so the system bars are
themed by the time the first React paint lands:
@@ -148,7 +193,7 @@ bootstrapNative();
createRoot(document.getElementById('root')!).render(<App />);
```
### 6. Wire `<DeepLinkHandler />` into `AppRouter.tsx`
### 7. Wire `<DeepLinkHandler />` into `AppRouter.tsx`
It must live **inside** `<BrowserRouter>` so `useNavigate()` works:
@@ -169,7 +214,7 @@ export default function AppRouter() {
}
```
### 7. Add the Native Platforms
### 8. Add the Native Platforms
```bash
npx cap add android
@@ -179,7 +224,7 @@ npx cap add ios # macOS only
This creates `android/` and `ios/` directories at the project root. Commit
them — they contain project-specific config (signing keys excluded).
### 8. Build & Sync
### 9. Build & Sync
```bash
npm run build # produces dist/
@@ -208,7 +253,7 @@ If you later add **local** (non-SPM) native plugin classes to `android/` or
}
```
### 9. Run on a Device or Emulator
### 10. Run on a Device or Emulator
```bash
npx cap open android # opens Android Studio
@@ -284,19 +329,50 @@ The hook has the same signature as `useLocalStorage` but returns a third
`ready` flag because native secure reads are async. While `!ready` you
should render a spinner or skip decisions that depend on the stored value.
### Safe-area CSS
### Safe-area utilities
Once the Tailwind plugin is registered (setup step 3), a rich set of
`*-safe` utilities becomes available across padding, margin, position,
height, border, and scroll properties:
```tsx
{/* sticky top bar that clears the notch */}
<header className="sticky top-0 safe-area-top bg-background">…</header>
<header className="sticky top-0 pt-safe bg-background">…</header>
{/* bottom nav that clears the home indicator */}
<nav className="fixed bottom-0 safe-area-bottom">…</nav>
<nav className="fixed inset-x-0 bottom-0 pb-safe">…</nav>
{/* full-height modal that respects both top notch and bottom gesture bar */}
<Dialog className="h-dvh-safe">…</Dialog>
{/* toast stack that sits below the status bar */}
<Toaster className="safe-area-inset-top" />
<Toaster className="top-safe" />
{/* "at least 2rem of bottom padding, more if the safe area is bigger" */}
<footer className="pb-safe-or-8">…</footer>
{/* safe area plus an extra 1rem */}
<div className="pt-safe-offset-4">…</div>
```
Common utilities:
| Utility | Meaning |
|---|---|
| `pt-safe`, `pr-safe`, `pb-safe`, `pl-safe` | Padding on one side |
| `px-safe`, `py-safe`, `p-safe` | Padding on multiple sides |
| `mt-safe`, …, `m-safe` | Margin equivalents |
| `top-safe`, `bottom-safe`, `left-safe`, `right-safe` | Absolute / fixed positioning |
| `inset-safe`, `inset-x-safe`, `inset-y-safe` | All-side inset |
| `h-dvh-safe`, `min-h-dvh-safe`, `max-h-dvh-safe` | Dynamic viewport height minus insets |
| `h-screen-safe` | Classic viewport height minus insets |
| `{prop}-safe-offset-{n}` | Safe area + `{n}` (e.g. `pb-safe-offset-4`) |
| `{prop}-safe-or-{n}` | `max(safe-area, {n})` (e.g. `pb-safe-or-8`) |
| `scroll-p-safe`, `scroll-m-safe`, … | Scroll padding / margin |
See the [plugin README](https://github.com/mvllow/tailwindcss-safe-area)
for the full list.
## Common Follow-ups
- **Custom native plugins** — If you build a local Swift/Kotlin Capacitor
@@ -325,11 +401,32 @@ should render a spinner or skip decisions that depend on the stored value.
## Troubleshooting
- **`env(safe-area-inset-top)` returns `0` on Android** — confirm the
`SystemBars` plugin is configured with `insetsHandling: 'css'` in
`capacitor.config.ts` (the skill's template already does this). That
injects `--safe-area-inset-*` CSS variables and the utilities in
`safe-area.css` prefer them over `env(…)`.
- **Safe-area utilities render as `0` on Android devices (`pt-safe` has
no effect on the top notch)** — Chromium WebView versions before 140
([bug 40699457](https://issues.chromium.org/issues/40699457)) report
`env(safe-area-inset-*)` as `0`. The `SystemBars` plugin with
`insetsHandling: 'css'` (already set in the skill's
`capacitor.config.ts`) injects `--safe-area-inset-*` CSS variables
with the correct values, but the `[email protected]`
plugin uses `env(…)` directly and can't see them. Two fixes:
1. **Preferred:** ensure your target Android devices are on WebView
140+ (released Aug 2025). Any actively-updated Android >=7 should
be, since WebView auto-updates via Play Services.
2. **Fallback:** copy `files/safe-area-shim.css` into `src/` and
`@import` it after `@tailwind utilities;` in `src/index.css` —
it re-declares the common utilities with
`var(--safe-area-inset-*, env(…, 0px))` so the SystemBars-injected
variables take over when `env()` reports 0. See the file's header
comment for details.
- **`pt-safe` doesn't exist at all** — the Tailwind plugin isn't
registered. Verify `import safeArea from "tailwindcss-safe-area"` and
that `safeArea` is listed in the `plugins` array of
`tailwind.config.ts`. Also verify you installed the **0.8.0** version
specifically (`npm ls tailwindcss-safe-area`). Version `1.x` is for
Tailwind v4 and emits no utilities on v3.
- **Safe-area utilities have 0px on iOS Safari/Xcode simulator** —
missing `viewport-fit=cover` in your `<meta name="viewport">` tag.
Without it iOS doesn't expose the insets.
- **Deep links don't navigate** — make sure `<DeepLinkHandler />` is
**inside** `<BrowserRouter>`. It uses `useNavigate()` and will throw
silently if it's mounted outside.
@@ -0,0 +1,72 @@
/* ─────────────────────────────────────────────────────────────────────────
* Optional Android WebView <140 shim for tailwindcss-safe-area
*
* The `tailwindcss-safe-area` plugin emits CSS that uses raw
* `env(safe-area-inset-*)`. On Android WebView versions before 140
* (roughly anything shipped before Aug 2025 that hasn't auto-updated),
* Chromium has a bug where those values report 0 even when the device
* has a notch or gesture area.
*
* Capacitor's SystemBars plugin (with `insetsHandling: 'css'`, which is
* the default and is already set in the skill's `capacitor.config.ts`)
* works around this by injecting `--safe-area-inset-*` CSS variables on
* the `html` element with the correct values. But the Tailwind plugin
* uses `env()` directly, so those variables alone don't reach it.
*
* This shim redeclares the most common safe-area utilities with the
* `var(--safe-area-inset-*, env(safe-area-inset-*, 0px))` pattern so they
* prefer the SystemBars-injected variables when present and fall back to
* `env()` otherwise.
*
* ONLY include this file if you are targeting older Android devices that
* may still be running WebView <140. On iOS and on Android 140+ the
* plugin's output works correctly and this shim is redundant.
*
* How to include:
* 1. Copy this file to `src/safe-area-shim.css`
* 2. Import it in `src/index.css` AFTER the `@tailwind utilities;`
* directive so these rules win via source order:
*
* @tailwind base;
* @tailwind components;
* @tailwind utilities;
* @import './safe-area-shim.css';
*
* Uses `@layer utilities` so these rules sit in the same cascade layer as
* the Tailwind utilities they're overriding; later-declared rules within
* the same layer win on specificity ties.
* ───────────────────────────────────────────────────────────────────────── */
@layer utilities {
/* Padding */
.pt-safe { padding-top: var(--safe-area-inset-top, env(safe-area-inset-top, 0px)); }
.pr-safe { padding-right: var(--safe-area-inset-right, env(safe-area-inset-right, 0px)); }
.pb-safe { padding-bottom: var(--safe-area-inset-bottom, env(safe-area-inset-bottom, 0px)); }
.pl-safe { padding-left: var(--safe-area-inset-left, env(safe-area-inset-left, 0px)); }
.px-safe {
padding-right: var(--safe-area-inset-right, env(safe-area-inset-right, 0px));
padding-left: var(--safe-area-inset-left, env(safe-area-inset-left, 0px));
}
.py-safe {
padding-top: var(--safe-area-inset-top, env(safe-area-inset-top, 0px));
padding-bottom: var(--safe-area-inset-bottom, env(safe-area-inset-bottom, 0px));
}
.p-safe {
padding-top: var(--safe-area-inset-top, env(safe-area-inset-top, 0px));
padding-right: var(--safe-area-inset-right, env(safe-area-inset-right, 0px));
padding-bottom: var(--safe-area-inset-bottom, env(safe-area-inset-bottom, 0px));
padding-left: var(--safe-area-inset-left, env(safe-area-inset-left, 0px));
}
/* Margin */
.mt-safe { margin-top: var(--safe-area-inset-top, env(safe-area-inset-top, 0px)); }
.mr-safe { margin-right: var(--safe-area-inset-right, env(safe-area-inset-right, 0px)); }
.mb-safe { margin-bottom: var(--safe-area-inset-bottom, env(safe-area-inset-bottom, 0px)); }
.ml-safe { margin-left: var(--safe-area-inset-left, env(safe-area-inset-left, 0px)); }
/* Position */
.top-safe { top: var(--safe-area-inset-top, env(safe-area-inset-top, 0px)); }
.right-safe { right: var(--safe-area-inset-right, env(safe-area-inset-right, 0px)); }
.bottom-safe { bottom: var(--safe-area-inset-bottom, env(safe-area-inset-bottom, 0px)); }
.left-safe { left: var(--safe-area-inset-left, env(safe-area-inset-left, 0px)); }
}
@@ -1,53 +0,0 @@
/* ─────────────────────────────────────────────────────────────────────────
* Safe-area inset utilities
*
* Append this block inside the `@layer utilities { … }` section of your
* `src/index.css` file (or import this file from your main stylesheet).
*
* Uses var(--safe-area-inset-*, …) as the outer wrapper so that
* Capacitor's SystemBars plugin (which injects --safe-area-inset-* CSS
* variables on Android to work around a Chromium <140 bug where
* env(safe-area-inset-*) reports 0) takes precedence when available. The
* inner env(safe-area-inset-*, 0px) is the standard fallback for iOS and
* for modern web browsers.
*
* Usage examples:
*
* <header className="sticky top-0 safe-area-top bg-background">…</header>
* <nav className="fixed bottom-0 safe-area-bottom">…</nav>
* <Toaster className="safe-area-inset-top" />
*
* Don't forget the matching viewport meta in index.html:
*
* <meta name="viewport" content="width=device-width, initial-scale=1,
* viewport-fit=cover, interactive-widget=resizes-content" />
*
* Without `viewport-fit=cover`, iOS won't expose the safe-area insets.
* ───────────────────────────────────────────────────────────────────────── */
.safe-area-top {
padding-top: var(--safe-area-inset-top, env(safe-area-inset-top, 0px));
}
.safe-area-bottom {
padding-bottom: var(--safe-area-inset-bottom, env(safe-area-inset-bottom, 0px));
}
.safe-area-left {
padding-left: var(--safe-area-inset-left, env(safe-area-inset-left, 0px));
}
.safe-area-right {
padding-right: var(--safe-area-inset-right, env(safe-area-inset-right, 0px));
}
/* Positional variants — apply the inset as `top` / `bottom` rather than padding.
Use these for absolutely/fixed-positioned overlays (e.g. a toast stack that
hugs the top of the screen). */
.safe-area-inset-top {
top: var(--safe-area-inset-top, env(safe-area-inset-top, 0px));
}
.safe-area-inset-bottom {
bottom: var(--safe-area-inset-bottom, env(safe-area-inset-bottom, 0px));
}