Skip to content

Commit

Permalink
refactor: improve code quality
Browse files Browse the repository at this point in the history
  • Loading branch information
begonaalvarezd committed May 13, 2024
1 parent 9b5e991 commit f399ec7
Showing 1 changed file with 11 additions and 12 deletions.
23 changes: 11 additions & 12 deletions src/lib/stores/isMobile.store.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
import { writable } from 'svelte/store'
import { BREAKPOINTS } from '$lib/constants'

// Function to determine if the screen is mobile
function _isMobile() {
// Check if window is defined (runs only in browser environment)
if (typeof window !== 'undefined') {
return window.innerWidth < BREAKPOINTS.sm
}
// Return false if window is not defined (for server-side rendering)
return false
}
import { writable, type Writable } from 'svelte/store'

// Create a writable store with an initial value based on the current screen width
export const isMobile = writable(_isMobile())
export const isMobile: Writable<boolean> = writable(_isMobile())

// Update the store value when the screen is resized
if (typeof window !== 'undefined') {
window.addEventListener('resize', () => {
isMobile.set(_isMobile())
})
}

function _isMobile(): boolean {
// Check if window is defined (runs only in browser environment)
if (typeof window !== 'undefined') {
return window.innerWidth < BREAKPOINTS.sm
}
// Return false if window is not defined (for server-side rendering)
return false
}

0 comments on commit f399ec7

Please sign in to comment.