-
Notifications
You must be signed in to change notification settings - Fork 3
[Feat] update breakpoint + add useBreakpoint #275
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| import { useSyncExternalStore } from 'react'; | ||
|
|
||
| import { Breakpoint, breakpoints } from '@/styles/mediaQuery'; | ||
|
|
||
| function getBreakpoint(): Breakpoint { | ||
| if (window.matchMedia(`(min-width: ${breakpoints.desktopLarge}px)`).matches) | ||
| return 'desktopLarge'; | ||
| if (window.matchMedia(`(min-width: ${breakpoints.desktop}px)`).matches) | ||
| return 'desktop'; | ||
| if (window.matchMedia(`(min-width: ${breakpoints.tablet}px)`).matches) | ||
| return 'tablet'; | ||
| return 'mobile'; | ||
| } | ||
|
|
||
| function subscribe(callback: () => void) { | ||
| const mqls = [ | ||
| window.matchMedia(`(max-width: ${breakpoints.tablet - 1}px)`), | ||
| window.matchMedia( | ||
| `(min-width: ${breakpoints.tablet}px) and (max-width: ${breakpoints.desktop - 1}px)`, | ||
| ), | ||
| window.matchMedia( | ||
| `(min-width: ${breakpoints.desktop}px) and (max-width: ${breakpoints.desktopLarge - 1}px)`, | ||
| ), | ||
| window.matchMedia(`(min-width: ${breakpoints.desktopLarge}px)`), | ||
| ]; | ||
| mqls.forEach((m) => m.addEventListener('change', callback)); | ||
| return () => mqls.forEach((m) => m.removeEventListener('change', callback)); | ||
| } | ||
|
Comment on lines
+5
to
+28
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💡 성능 개선 및 코드 단순화 제안현재 구현된
const getMqls = () => {
if (typeof window === 'undefined') return [];
return [
{ key: 'desktopLarge' as const, mql: window.matchMedia(`(min-width: ${breakpoints.desktopLarge}px)`) },
{ key: 'desktop' as const, mql: window.matchMedia(`(min-width: ${breakpoints.desktop}px)`) },
{ key: 'tablet' as const, mql: window.matchMedia(`(min-width: ${breakpoints.tablet}px)`) },
];
};
let cachedMqls: ReturnType<typeof getMqls> | null = null;
function getCachedMqls() {
if (!cachedMqls) {
cachedMqls = getMqls();
}
return cachedMqls;
}
function getBreakpoint(): Breakpoint {
const mqls = getCachedMqls();
for (const { key, mql } of mqls) {
if (mql.matches) return key;
}
return 'mobile';
}
function subscribe(callback: () => void) {
const mqls = getCachedMqls();
mqls.forEach(({ mql }) => mql.addEventListener('change', callback));
return () => mqls.forEach(({ mql }) => mql.removeEventListener('change', callback));
} |
||
|
|
||
| export function useBreakpoint() { | ||
| const bp = useSyncExternalStore( | ||
| subscribe, | ||
| getBreakpoint, | ||
| () => 'desktop' as Breakpoint, | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Admin 서비스여서 데탑 사용 비중이 높아서 SSR fallback을
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 네 맞습니다! default 값을 지정해야하는데 어드민이라 desktop으로 지정했어요! |
||
| ); | ||
|
|
||
| return { | ||
| breakpoint: bp, | ||
| isMobile: bp === 'mobile', | ||
| isTablet: bp === 'tablet', | ||
| isDesktop: bp === 'desktop', | ||
| isDesktopLarge: bp === 'desktopLarge', | ||
| isMobileOrTablet: bp === 'mobile' || bp === 'tablet', | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 사실 breakpoint.ts에서 두개 합친 것이 없긴해서 일관성 측면에서는 빼는게 맞긴하겠네요! |
||
| }; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,20 +1,15 @@ | ||
| const bp = { | ||
| mobile: 480, | ||
| tablet: 1024, | ||
| }; | ||
|
|
||
| const mq = (label: keyof typeof bp) => { | ||
| const bpArray = Object.keys(bp).map((key) => [ | ||
| key, | ||
| bp[key as keyof typeof bp], | ||
| ]); | ||
|
|
||
| const [result] = bpArray.reduce((acc, [name, size]) => { | ||
| if (label === name) return [...acc, `@media (max-width: ${size}px)`]; | ||
| return acc; | ||
| }, []); | ||
|
|
||
| return result; | ||
| }; | ||
|
|
||
| export default mq; | ||
| export const breakpoints = { | ||
| mobile: 0, | ||
| tablet: 768, | ||
| desktop: 1024, | ||
| desktopLarge: 1260, | ||
| } as const; | ||
|
|
||
| export const media = { | ||
| mobile: `@media (max-width: ${breakpoints.tablet - 1}px)`, | ||
| tablet: `@media (min-width: ${breakpoints.tablet}px) and (max-width: ${breakpoints.desktop - 1}px)`, | ||
| desktop: `@media (min-width: ${breakpoints.desktop}px) and (max-width: ${breakpoints.desktopLarge - 1}px)`, | ||
| desktopLarge: `@media (min-width: ${breakpoints.desktopLarge}px)`, | ||
| } as const; | ||
|
|
||
| export type Breakpoint = keyof typeof media; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mqls이 어떤 약자인가욥??There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
아 MediaQueryLists인것 같네용
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
맞습니다!