-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…etting [Feat/#474] responsive basic setting & main page 임시 responsive 뷰 적용
- Loading branch information
Showing
20 changed files
with
634 additions
and
94 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { Slot } from '@radix-ui/react-slot'; | ||
import { ReactNode, useContext, useEffect, useState } from 'react'; | ||
import { MOBILE_MEDIA_QUERY } from '../../../styles/mediaQuery'; | ||
import { ResponsiveContext } from './context'; | ||
interface ResponsivePropTypes { | ||
children: ReactNode; | ||
only: AvailableSize; | ||
asChild?: boolean; | ||
} | ||
|
||
type AvailableSize = 'mobile' | 'desktop'; | ||
|
||
const Responsive = ({ children, only, asChild }: ResponsivePropTypes) => { | ||
const [current, setCurrent] = useState<AvailableSize | null>(null); | ||
|
||
const Comp = asChild ? Slot : 'div'; | ||
|
||
const { mobileOnlyClassName, desktopOnlyClassName } = useContext(ResponsiveContext); | ||
|
||
const selectedClassName = () => { | ||
if (only === 'desktop') { | ||
return desktopOnlyClassName; | ||
} else if (only === 'mobile') { | ||
return mobileOnlyClassName; | ||
} else { | ||
throw new Error(`잘못된 타입의 only값 : ${only}`); | ||
} | ||
}; | ||
|
||
useEffect(() => { | ||
const mobileMedia = window.matchMedia(MOBILE_MEDIA_QUERY); | ||
const handleCurrentChange = (e: MediaQueryListEvent) => { | ||
setCurrent(e.matches ? 'mobile' : 'desktop'); | ||
}; | ||
mobileMedia.addEventListener('change', handleCurrentChange); | ||
|
||
return () => { | ||
mobileMedia.removeEventListener('change', handleCurrentChange); | ||
}; | ||
}, []); | ||
return current === null || only === current ? ( | ||
<Comp className={`${selectedClassName()}`}>{children}</Comp> | ||
) : ( | ||
<></> | ||
); | ||
}; | ||
|
||
export default Responsive; |
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,38 @@ | ||
import { Global, css } from '@emotion/react'; | ||
import { ReactNode } from 'react'; | ||
import { MOBILE_MEDIA_QUERY } from '../../../styles/mediaQuery'; | ||
import { ResponsiveContext } from './context'; | ||
interface ResponsiveProviderProps { | ||
children: ReactNode; | ||
} | ||
|
||
const ResponsiveProvider = ({ children }: ResponsiveProviderProps) => { | ||
const mobileOnlyClassName = `responsive-mobile-only`; | ||
const desktopOnlyClassName = `responsive-desktop-only`; | ||
|
||
const ResponsiveClassName = css` | ||
.${mobileOnlyClassName} { | ||
display: none !important; | ||
} | ||
.${desktopOnlyClassName} { | ||
display: block !important; | ||
} | ||
@media ${MOBILE_MEDIA_QUERY} { | ||
.${mobileOnlyClassName} { | ||
display: block !important; | ||
} | ||
.${desktopOnlyClassName} { | ||
display: none !important; | ||
} | ||
} | ||
`; | ||
return ( | ||
<ResponsiveContext.Provider value={{ mobileOnlyClassName, desktopOnlyClassName }}> | ||
<Global styles={ResponsiveClassName} /> | ||
{children} | ||
</ResponsiveContext.Provider> | ||
); | ||
}; | ||
|
||
export default ResponsiveProvider; |
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,15 @@ | ||
import { createContext } from 'react'; | ||
|
||
interface ResponsiveContextValue { | ||
mobileOnlyClassName: string; | ||
desktopOnlyClassName: string; | ||
} | ||
|
||
export const ResponsiveContext = createContext<ResponsiveContextValue>( | ||
//ResponsiveProvider로 감싸져있지 않은 컴포넌트에서 값을 접근했을 때 또는 Context가 제대로 초기화되지 않았을 때 에러를 발생시켜 디버깅을 쉽게하기 위함 | ||
new Proxy({} as ResponsiveContextValue, { | ||
get() { | ||
throw new Error('ResponsiveProvider가 필요합니다.'); | ||
}, | ||
}), | ||
); |
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
Oops, something went wrong.