-
Notifications
You must be signed in to change notification settings - Fork 275
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
93 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { beforeEach, describe, expect, it } from 'vitest'; | ||
import { getWindowDimensions } from './getWindowDimensions'; // Adjust the import path as needed | ||
|
||
describe('getWindowDimensions', () => { | ||
beforeEach(() => { | ||
// Mock window.innerWidth and window.innerHeight | ||
Object.defineProperty(window, 'innerWidth', { | ||
writable: true, | ||
configurable: true, | ||
value: 1000, | ||
}); | ||
Object.defineProperty(window, 'innerHeight', { | ||
writable: true, | ||
configurable: true, | ||
value: 800, | ||
}); | ||
}); | ||
|
||
it('should return minimum width for small screens', () => { | ||
window.innerWidth = 300; | ||
window.innerHeight = 400; | ||
|
||
const result = getWindowDimensions('s'); | ||
expect(result).toEqual({ width: 270, height: 350 }); | ||
}); | ||
|
||
it('should calculate correct dimensions for medium size', () => { | ||
const result = getWindowDimensions('m'); | ||
expect(result).toEqual({ width: 290, height: 363 }); | ||
}); | ||
|
||
it('should calculate correct dimensions for large size', () => { | ||
const result = getWindowDimensions('l'); | ||
expect(result).toEqual({ width: 350, height: 438 }); | ||
}); | ||
|
||
it('should limit dimensions to 35vw for large viewport', () => { | ||
window.innerWidth = 2000; | ||
window.innerHeight = 1500; | ||
|
||
const result = getWindowDimensions('l'); | ||
expect(result).toEqual({ width: 700, height: 875 }); | ||
}); | ||
|
||
it('should handle different aspect ratios', () => { | ||
window.innerWidth = 1920; | ||
window.innerHeight = 1080; | ||
|
||
const result = getWindowDimensions('m'); | ||
expect(result).toEqual({ width: 557, height: 696 }); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import type { WindowSizes } from '../types'; | ||
|
||
const windowSizes: WindowSizes = { | ||
s: { width: '23vw', height: '28.75vw' }, | ||
m: { width: '29vw', height: '36.25vw' }, | ||
l: { width: '35vw', height: '43.75vw' }, | ||
}; | ||
|
||
export const getWindowDimensions = (size: keyof typeof windowSizes) => { | ||
const { width, height } = windowSizes[size]; | ||
|
||
// Define minimum sizes (in pixels) | ||
const minWidth = 280; | ||
const minHeight = 350; | ||
|
||
// Convert viewport units to pixels | ||
const vwToPx = (vw: number) => (vw / 100) * window.innerWidth; | ||
|
||
const widthPx = Math.max( | ||
minWidth, | ||
Math.round(vwToPx(Number.parseFloat(width))), | ||
); | ||
const heightPx = Math.max( | ||
minHeight, | ||
Math.round(vwToPx(Number.parseFloat(height))), | ||
); | ||
|
||
// Ensure the width and height don't exceed 90% of the viewport dimensions | ||
const maxWidth = Math.round(window.innerWidth * 0.9); | ||
const maxHeight = Math.round(window.innerHeight * 0.9); | ||
const adjustedWidthPx = Math.min(widthPx, maxWidth); | ||
const adjustedHeightPx = Math.min(heightPx, maxHeight); | ||
|
||
return { width: adjustedWidthPx, height: adjustedHeightPx }; | ||
}; |