Skip to content

Commit

Permalink
feat: added vh100 functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
Derek Burgman committed Jan 30, 2022
1 parent 84cbdf2 commit 10b86b4
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/browser/src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from './lib/window';
export * from './lib';
2 changes: 2 additions & 0 deletions packages/browser/src/lib/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './window';
export * from './vh100';
23 changes: 23 additions & 0 deletions packages/browser/src/lib/vh100.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// https://dev.to/maciejtrzcinski/100vh-problem-with-ios-safari-3ge9

export const DEFAULT_VH100_VARIABLE_NAME = 'vh100';

export function refreshVh100Function(cssVariableName = DEFAULT_VH100_VARIABLE_NAME) {
const cssProperty = `--${cssVariableName}`;
return () => {
const doc = document.documentElement;
doc.style.setProperty(cssProperty, `${window.innerHeight}px`);
}
};

/**
* Adds window event listeners to populate the css variable `vh100`, or another input variable name, with the current window height.
*/
export function watchWindowAndUpdateVh100StyleProperty(cssVariableName?: string) {
const refreshPropertyValue = refreshVh100Function(cssVariableName);

window.addEventListener('resize', refreshPropertyValue);
window.addEventListener('orientationchange', refreshPropertyValue);
refreshPropertyValue();

}

0 comments on commit 10b86b4

Please sign in to comment.