media query를 활용해서 디바이스 사이즈에 따라 화면이 다르게 보이게 설정한다.
기준은 최소 반응형 레이아웃으로, break point를 각각 다음과 같이 했다.
//media query를 작성
/* 데스크탑 */
@media screen and (max-width:1023px) {
/* 타블렛 */
}
@media screen and (max-width:767px) {
/* 모바일 */
}
🟥 props만으로 구현
//LogoAndSearch.tsx
export const SmallHeader = () => {
return (
<Vertical>
<LogoSlideStyle hoverBack={window.outerWidth < 768 ? '#c5f4c8' : 'none'}> (1)
<HomeLogo />
</LogoSlideStyle>
<Searchbar />
</Vertical>
);
};
//ChangingStyle.tsx
import styled from 'styled-components';
type StyleProps = { (2)
hoverBack: string;
};
export const LogoSlideStyle = styled.div<StyleProps>` (3)
display: flex;
justify-content: center;
width: 100%;
height: 100%;
background: white;
border: 5px solid ${(props) => props.hoverBack}; (4)
&:hover {
background: ${(props) => props.hoverBack || 'none'};
} (5)
`;
✅ media query를 사용
//LogoAndSearch.tsx
export const SmallHeader = () => {
return (
<Vertical>
<LogoSlideStyle> (1)
<HomeLogo
margin={window.outerWidth < 768 ? '0' : RESPONSIVE.HEADER_MARGIN}
/>
</LogoSlideStyle>
<Searchbar />
</Vertical>
);
};
//ChangingStyle.tsx
import styled from 'styled-components';
import { RESPONSIVE } from 'constants/style';
export const LogoSlideStyle = styled.div`
display: flex;
justify-content: center;
width: 100%;
height: 100%;
background: white;
@media screen and (max-width: ${RESPONSIVE.SMALL_PX}) { (2)
width: 97vw;
border: 5px solid #c5f4c8; (3)
&:hover { (4)
background: #c5f4c8;
}
}
`;
//Section.tsx
<img
alt='advertise'
style={{ border: '1px solid blue' }}
src={img}
width={CONDITION.SMALL ? '100%' : SECTION.ADV_WIDTH} (1)
height={SECTION.ADV_HEIGHT}
/>
//styleConstants.ts
export const CONDITION = {
SMALL: 'window.outerWidth < 768 ? ',
};
//LogoAndSearch.tsx
export const SmallHeader = () => {
return (
<Vertical>
<LogoSlideStyle>
<HomeLogo
margin={window.outerWidth < 768 ? '0' : RESPONSIVE.HEADER_MARGIN}
/> (2)
</LogoSlideStyle>
<Searchbar />
</Vertical>
);
};