Fix the viewport height (vh) issue in Safari, especially for PWAs and web apps with dynamic toolbars. Includes both a PostCSS plugin and runtime patch.
npm install @any-design/safari-vh-patch// postcss.config.js
module.exports = {
plugins: {
'@any-design/safari-vh-patch': {
// options (optional)
variableName: '--vh', // default
keepFallback: true // default
}
}
}Import the runtime patch in your entry file:
import '@any-design/safari-vh-patch/runtime';The plugin will automatically transform your vh units:
.element {
height: 100vh; /* Will be transformed automatically */
}
/* Outputs: */
.element {
height: 100vh; /* fallback */
height: calc(var(--vh, 1vh) * 100);
}// vite.config.ts
import { defineConfig } from 'vite'
import safariVhPatch from '@any-design/safari-vh-patch'
export default defineConfig({
css: {
postcss: {
plugins: [
safariVhPatch()
]
}
}
})- The PostCSS plugin transforms vh units in your CSS to use a CSS custom property
- The runtime script detects Safari and updates the
--vhvariable when viewport changes - Only applies the fix when needed (Safari browsers)
| Option | Type | Default | Description |
|---|---|---|---|
variableName |
string |
'--vh' |
Custom CSS variable name |
keepFallback |
boolean |
true |
Keep original vh value as fallback |
Based on the viewport unit trick: https://css-tricks.com/the-trick-to-viewport-units-on-mobile/
MIT