-
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.
- Loading branch information
1 parent
18c2ff4
commit 5107dd4
Showing
4 changed files
with
74 additions
and
35 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
export type MediaQueryName = keyof typeof mediaQueryMap; | ||
import { writable, derived } from 'svelte/store'; | ||
|
||
export const mediaQueryMap = { | ||
sm: '(min-width: 640px)', | ||
md: '(min-width: 768px)', | ||
lg: '(min-width: 1024px)', | ||
xl: '(min-width: 1280px)', | ||
'2xl': '(min-width: 1536px)' | ||
}; | ||
|
||
const noop = (cb: (e: MediaQueryListEvent) => void) => () => cb; | ||
|
||
export default function mediaBreakPoints(bp: MediaQueryName) { | ||
if (typeof window === 'undefined') { | ||
return { matches: false, subscribe: noop }; | ||
} | ||
|
||
const matchMedia = window.matchMedia(mediaQueryMap[bp]); | ||
return { | ||
matches: matchMedia.matches, | ||
subscribe: (cb: (e: MediaQueryListEvent) => void) => { | ||
matchMedia.addEventListener('change', cb); | ||
return () => matchMedia.removeEventListener('change', cb); | ||
} | ||
}; | ||
} | ||
|
||
export const mediaBreakPoints$ = (bp: MediaQueryName) => { | ||
const mbp = mediaBreakPoints(bp); | ||
const matches$ = writable(mbp.matches); | ||
mbp.subscribe((e) => matches$.set(e.matches)); | ||
|
||
return derived(matches$, (v) => v); | ||
}; |
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