Skip to content

Commit

Permalink
Fix window state
Browse files Browse the repository at this point in the history
  • Loading branch information
ShaitanLyss committed Sep 4, 2024
1 parent 8bfc841 commit 47661e2
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 30 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@selenite/commons",
"version": "0.24.3",
"version": "0.24.4",
"repository": "github:ShaitanLyss/selenite-commons",
"license": "MIT",
"keywords": [
Expand Down
42 changes: 15 additions & 27 deletions src/lib/utils/html.svelte.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,25 @@
import { readable, type Readable, type Subscriber } from 'svelte/store';
import type { Position } from './math';
import { untrack } from 'svelte';

const _windowState = $state({ width: NaN, height: NaN });
let _isWindowStateSetup = false;
/**
* Reactive window state for use in svelte 5.
*
* At the moment, it exposes with and height.
* At the moment, it exposes width and height.
*/
export class WindowState {
width = $state<number>(NaN);
height = $state<number>(NaN);
width = $derived(_windowState.width);
height = $derived(_windowState.height);

static #instance: WindowState | undefined = undefined;
private static get instance() {
if (!this.#instance) this.#instance = new WindowState();
return this.#instance;
}

static get width() {
return this.instance.width;
}

static get height() {
return this.instance.height;
}

private constructor() {
constructor() {
if (typeof window === 'undefined') return;
untrack(() => {
if (_isWindowStateSetup) return;
_isWindowStateSetup = true;
_windowState.width = window.innerWidth;
_windowState.height = window.innerHeight;
window.addEventListener('resize', (e) => {
this.width = window.innerWidth;
this.height = window.innerHeight;
window.addEventListener('resize', (e) => {
this.width = window.innerWidth;
this.height = window.innerHeight;
});
});
}
}
Expand Down Expand Up @@ -186,11 +173,12 @@ export type WindowBounds = {
export function getBounds(element?: Element): WindowBounds {
if (!element) return { top: 0, left: 0, right: 0, bottom: 0 };
const { x, y, width, height } = element.getBoundingClientRect();
const windowState = new WindowState();
return {
left: x,
top: y,
right: WindowState.width - x - width,
bottom: WindowState.height - y - height
right: windowState.width - x - width,
bottom: windowState.height - y - height
};
}

Expand Down
6 changes: 4 additions & 2 deletions src/lib/utils/html.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import { Window } from 'happy-dom';
describe('WindowState', () => {
it('should return the window width', () => {
window.innerWidth = 2;
expect(WindowState.width).toBe(2);
const windowState = new WindowState();
expect(windowState.width).toBe(2);
});
it('should return the window height', () => {
window.innerHeight = 2;
expect(WindowState.width).toBe(2);
const windowState = new WindowState();
expect(windowState.width).toBe(2);
});
});

Expand Down

0 comments on commit 47661e2

Please sign in to comment.