1
0
mirror of https://github.com/locomotivemtl/locomotive-boilerplate.git synced 2026-01-15 00:55:08 +08:00

Add vh/svh/dvh/lvh fallbacks

This commit is contained in:
Lucas Vallenet
2024-01-11 11:45:46 +01:00
parent 61b6222525
commit 3f7077b488
7 changed files with 112 additions and 29 deletions

View File

@@ -1,3 +1,3 @@
{ {
"version": 1704463500604 "version": 1704969922254
} }

View File

@@ -1,25 +1,66 @@
import modular from 'modujs'; import modular from 'modujs';
import * as modules from './modules'; import * as modules from './modules';
import globals from './globals'; import globals from './globals';
import { debounce } from './utils/tickers' import { debounce } from './utils/tickers';
import { $html } from './utils/dom'; import { $html } from './utils/dom';
import { ENV, FONT, CUSTOM_EVENT, CSS_CLASS } from './config' import { ENV, FONT, CUSTOM_EVENT, CSS_CLASS } from './config'
import { isFontLoadingAPIAvailable, loadFonts } from './utils/fonts'; import { isFontLoadingAPIAvailable, loadFonts } from './utils/fonts';
const app = new modular({ const app = new modular({
modules: modules, modules,
}); });
window.addEventListener('load', (event) => { const setViewportSizes = () => {
// Document styles
const documentStyles = document.documentElement.style;
// Viewport width
const vw = document.body.clientWidth * 0.01;
documentStyles.setProperty('--vw', `${vw}px`);
// Return if browser supports vh, svh, dvh, & lvh
if (ENV.SUPPORTS_VH) {
return
}
// Viewport height
const height = window.innerHeight;
const svh = document.documentElement.clientHeight * 0.01;
documentStyles.setProperty('--svh', `${svh}px`);
const dvh = height * 0.01;
documentStyles.setProperty('--dvh', `${dvh}px`);
if (document.body) {
const fixed = document.createElement('div');
fixed.style.width = '1px';
fixed.style.height = '100vh';
fixed.style.position = 'fixed';
fixed.style.left = '0';
fixed.style.top = '0';
fixed.style.bottom = '0';
fixed.style.visibility = 'hidden';
document.body.appendChild(fixed);
var fixedHeight = fixed.clientHeight;
fixed.remove();
const lvh = fixedHeight * 0.01;
documentStyles.setProperty('--lvh', `${lvh}px`);
}
}
window.addEventListener('load', () => {
const $style = document.getElementById('main-css'); const $style = document.getElementById('main-css');
if ($style) { if ($style) {
if ($style.isLoaded) { if ($style.isLoaded) {
init(); init();
} else { } else {
$style.addEventListener('load', (event) => { $style.addEventListener('load', init);
init();
});
} }
} else { } else {
console.warn('The "main-css" stylesheet not found'); console.warn('The "main-css" stylesheet not found');
@@ -28,6 +69,7 @@ window.addEventListener('load', (event) => {
function init() { function init() {
globals(); globals();
setViewportSizes();
app.init(app); app.init(app);
@@ -36,14 +78,18 @@ function init() {
$html.classList.remove(CSS_CLASS.LOADING); $html.classList.remove(CSS_CLASS.LOADING);
// Bind window resize event with default vars // Bind window resize event with default vars
const resizeEndEvent = new CustomEvent(CUSTOM_EVENT.RESIZE_END) const resizeEndEvent = new CustomEvent(CUSTOM_EVENT.RESIZE_END);
window.addEventListener('resize', () => { window.addEventListener('resize', () => {
$html.style.setProperty('--vw', `${document.documentElement.clientWidth * 0.01}px`) setViewportSizes();
debounce(() => { debounce(() => {
window.dispatchEvent(resizeEndEvent) window.dispatchEvent(resizeEndEvent);
}, 200, false) }, 200, false)
}) })
window.addEventListener('orientationchange', () => {
setViewportSizes();
})
/** /**
* Eagerly load the following fonts. * Eagerly load the following fonts.
*/ */
@@ -53,12 +99,12 @@ function init() {
if (ENV.IS_DEV) { if (ENV.IS_DEV) {
console.group('Eager fonts loaded!', eagerFonts.length, '/', document.fonts.size); console.group('Eager fonts loaded!', eagerFonts.length, '/', document.fonts.size);
console.group('State of eager fonts:') console.group('State of eager fonts:');
eagerFonts.forEach((font) => console.log(font.family, font.style, font.weight, font.status/*, font*/)) eagerFonts.forEach(font => console.log(font.family, font.style, font.weight, font.status));
console.groupEnd() console.groupEnd();
console.group('State of all fonts:') console.group('State of all fonts:');
document.fonts.forEach((font) => console.log(font.family, font.style, font.weight, font.status/*, font*/)) document.fonts.forEach(font => console.log(font.family, font.style, font.weight, font.status));
console.groupEnd() console.groupEnd();
} }
}); });
} }

View File

@@ -21,6 +21,15 @@ const ENV = Object.freeze({
// Device // Device
IS_MOBILE, IS_MOBILE,
IS_DESKTOP: !IS_MOBILE, IS_DESKTOP: !IS_MOBILE,
// Supports
SUPPORTS_VH: (
'CSS' in window
&& 'supports' in window.CSS
&& window.CSS.supports('height: 100svh')
&& window.CSS.supports('height: 100dvh')
&& window.CSS.supports('height: 100lvh')
)
}) })
// Main CSS classes used within the project // Main CSS classes used within the project

View File

@@ -132,18 +132,46 @@ $context: 'frontend' !default;
@return calc(#{$percentage} * (100vw - 2 * var(--grid-margin, 0px)) - (1 - #{$percentage}) * var(--grid-gutter, 0px) + #{$inset} * var(--grid-gutter, 0px)); @return calc(#{$percentage} * (100vw - 2 * var(--grid-margin, 0px)) - (1 - #{$percentage}) * var(--grid-gutter, 0px) + #{$inset} * var(--grid-gutter, 0px));
} }
// Returns calculation of a percentage of the viewport height. // Returns calculation of a percentage of the viewport small height.
// //
// ```scss // ```scss
// .c-box { // .c-box {
// height: vh(100); // height: svh(100);
// } // }
// ``` // ```
// //
// @param {number} $number - The percentage number // @param {number} $number - The percentage number
// @return {function<number>} in vh // @return {function<number>} in svh
@function vh($number) { @function svh($number) {
@return calc(#{$number} * var(--vh, 1vh)); @return calc(#{$number} * var(--svh, 1svh));
}
// Returns calculation of a percentage of the viewport large height.
//
// ```scss
// .c-box {
// height: lvh(100);
// }
// ```
//
// @param {number} $number - The percentage number
// @return {function<number>} in lvh
@function lvh($number) {
@return calc(#{$number} * var(--lvh, 1lvh));
}
// Returns calculation of a percentage of the viewport dynamic height.
//
// ```scss
// .c-box {
// height: dvh(100);
// }
// ```
//
// @param {number} $number - The percentage number
// @return {function<number>} in dvh
@function dvh($number) {
@return calc(#{$number} * var(--dvh, 1dvh));
} }
// Returns calculation of a percentage of the viewport width. // Returns calculation of a percentage of the viewport width.

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long