-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add screenBreakpoint store and make isMobile a derived
- Loading branch information
Showing
10 changed files
with
70 additions
and
39 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
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
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
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,6 @@ | ||
export enum ScreenSize { | ||
Sm = 'sm', | ||
Md = 'md', | ||
Lg = 'lg', | ||
Xl = 'xl', | ||
} |
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 |
---|---|---|
@@ -1 +1 @@ | ||
export * from './isMobile.store' | ||
export * from './screen-breakpoint.store' |
This file was deleted.
Oops, something went wrong.
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,43 @@ | ||
import { BREAKPOINTS } from '$lib/constants' | ||
import { ScreenSize } from '$lib/enums' | ||
import { derived, writable, type Readable, type Writable } from 'svelte/store' | ||
|
||
// Create a writable store with an initial value based on the current screen width | ||
export const screenBreakpoint: Writable<ScreenSize> = writable(getCurrentScreenBreakpoint()) | ||
export const isMobile: Readable<boolean> = derived( | ||
screenBreakpoint, | ||
($screenBreakPoint) => $screenBreakPoint === ScreenSize.Sm | ||
) | ||
export const isSmallScreen: Readable<boolean> = derived( | ||
screenBreakpoint, | ||
($screenBreakPoint) => | ||
$screenBreakPoint === ScreenSize.Md || $screenBreakPoint === ScreenSize.Sm | ||
) | ||
|
||
// Update the store value when the screen is resized | ||
if (typeof window !== 'undefined') { | ||
window.addEventListener('resize', () => { | ||
screenBreakpoint.set(getCurrentScreenBreakpoint()) | ||
}) | ||
} | ||
|
||
function getCurrentScreenBreakpoint(): ScreenSize { | ||
let breakpoint: ScreenSize = ScreenSize.Xl | ||
|
||
// Check if window is defined (runs only in browser environment) | ||
if (typeof window !== 'undefined') { | ||
const width = window.innerWidth | ||
|
||
const sortedBreakpoints = Object.keys(BREAKPOINTS).sort( | ||
(a, b) => BREAKPOINTS[a as ScreenSize] - BREAKPOINTS[b as ScreenSize] | ||
) | ||
|
||
for (const key of sortedBreakpoints) { | ||
if (width < BREAKPOINTS[key as ScreenSize]) { | ||
breakpoint = key as ScreenSize | ||
break | ||
} | ||
} | ||
} | ||
return breakpoint | ||
} |