Skip to content
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

[FE] fix: media 유틸리티 함수 수정 #632

Merged
merged 2 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion frontend/src/components/layouts/Main/styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export const Contents = styled.div`

box-sizing: border-box;
width: 100%;
max-width: ${({ theme }) => theme.breakpoints.desktop};
max-width: ${({ theme }) => theme.breakpoint.medium}px;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

템플릿 리터럴 내부에서는 number 타입으로 지정하게 되면 px 단위가 자동으로 안 붙는 것으로 알고 있어서 따로 기입했습니다.

height: 100%;
min-height: inherit;

Expand Down
12 changes: 7 additions & 5 deletions frontend/src/styles/theme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ export const componentHeight = {
breadCrumb: '4.3rem',
};

export const breakpoints: ThemeProperty<string> = {
desktop: '102.4rem',
tablet: '72rem',
mobile: '57.6rem',
export const breakpoint = {
xxSmall: 320,
xSmall: 425,
small: 768,
medium: 1024,
large: 1025,
};
// NOTE: 1rem = 10px
export const fontSize: ThemeProperty<CSSProperties['fontSize']> = {
Expand Down Expand Up @@ -68,7 +70,7 @@ const theme: Theme = {
fontWeight,
zIndex,
colors,
breakpoints,
breakpoint,
sidebarWidth,
borderRadius,
formWidth,
Expand Down
6 changes: 3 additions & 3 deletions frontend/src/types/emotion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
fontSize,
fontWeight,
zIndex,
breakpoints,
breakpoint,
sidebarWidth,
borderRadius,
componentHeight,
Expand All @@ -16,7 +16,7 @@ export type Color = typeof colors;
export type ZIndex = typeof zIndex;
export type FontSize = typeof fontSize;
export type FontWeight = typeof fontWeight;
export type Breakpoints = typeof breakpoints;
export type Breakpoint = typeof breakpoint;
export type SidebarWidthStyle = typeof sidebarWidth;
export type BorderRadius = typeof borderRadius;
export type ComponentHeight = typeof componentHeight;
Expand All @@ -26,7 +26,7 @@ type ThemeType = {
fontWeight: FontWeight;
colors: Color;
zIndex: ZIndex;
breakpoints: Breakpoints;
breakpoint: Breakpoint;
sidebarWidth: SidebarWidthStyle;
borderRadius: BorderRadius;
formWidth: string;
Expand Down
42 changes: 12 additions & 30 deletions frontend/src/utils/media.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,17 @@
import { css, SerializedStyles } from '@emotion/react';
import { CSSObject } from '@emotion/styled';
import theme from '@/styles/theme';

export type Breakpoints = 'xxSmall' | 'xSmall' | 'small' | 'medium' | 'large';
const { breakpoint } = theme;

export const breakpoints: Record<Breakpoints, string> = {
xxSmall: '@media (max-width: 320px)',
xSmall: '@media (max-width: 425px)',
small: '@media (max-width: 768px)',
medium: '@media (max-width: 1024px)',
large: '@media (min-width: 1025px)',
};
export type Breakpoints = keyof typeof breakpoint;
type Media = { [key in Breakpoints]: string };

const media = Object.entries(breakpoints).reduce(
(acc, [key, value]) => {
acc[key as Breakpoints] = (
styles: CSSObject | TemplateStringsArray,
...interpolations: Array<CSSObject | SerializedStyles>
) => css`
${value} {
${css(styles, ...interpolations)}
}
`;
return acc;
},
{} as Record<
Breakpoints,
(
styles: CSSObject | TemplateStringsArray,
...interpolations: Array<CSSObject | SerializedStyles>
) => SerializedStyles
>,
);
const breakpointsKeyList = Object.keys(breakpoint) as Breakpoints[];

const media = breakpointsKeyList.reduce((prev, key, index) => {
const mediaType = index === breakpointsKeyList.length - 1 ? 'min' : 'max';

prev[key] = `@media (${mediaType}-width: ${breakpoint[key]}px)`;
return prev;
}, {} as Media);

export default media;