-
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.
Merge pull request #30 from DaleStudy/29-heading
- Loading branch information
Showing
13 changed files
with
238 additions
and
15 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import type { Meta, StoryObj } from "@storybook/react"; | ||
import { vstack } from "../../../styled-system/patterns"; | ||
import { Heading } from "./Heading"; | ||
|
||
const meta = { | ||
component: Heading, | ||
parameters: { | ||
layout: "centered", | ||
}, | ||
args: { | ||
children: "제목", | ||
level: 1, | ||
}, | ||
} satisfies Meta<typeof Heading>; | ||
|
||
export default meta; | ||
|
||
export const Basic: StoryObj<typeof meta> = {}; | ||
|
||
export const Levels: StoryObj<typeof meta> = { | ||
render: (args) => { | ||
return ( | ||
<div className={vstack({ gap: "6" })}> | ||
<Heading {...args} level={1}> | ||
1 단계 | ||
</Heading> | ||
<Heading {...args} level={2}> | ||
2 단계 | ||
</Heading> | ||
<Heading {...args} level={3}> | ||
3 단계 | ||
</Heading> | ||
<Heading {...args} level={4}> | ||
4 단계 | ||
</Heading> | ||
<Heading {...args} level={5}> | ||
5 단계 | ||
</Heading> | ||
<Heading {...args} level={6}> | ||
6 단계 | ||
</Heading> | ||
</div> | ||
); | ||
}, | ||
argTypes: { | ||
children: { | ||
control: false, | ||
}, | ||
level: { | ||
control: false, | ||
}, | ||
}, | ||
}; | ||
|
||
export const Contrasts: StoryObj<typeof meta> = { | ||
render: (args) => { | ||
return ( | ||
<div className={vstack({ gap: "6" })}> | ||
<Heading {...args} muted> | ||
낮은 명암비 | ||
</Heading> | ||
<Heading {...args}>높은 명암비</Heading> | ||
</div> | ||
); | ||
}, | ||
argTypes: { | ||
children: { | ||
control: false, | ||
}, | ||
muted: { | ||
control: false, | ||
}, | ||
}, | ||
}; |
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,55 @@ | ||
import { faker } from "@faker-js/faker"; | ||
import { composeStories } from "@storybook/react"; | ||
import { render, screen } from "@testing-library/react"; | ||
import { expect, test } from "vitest"; | ||
import { fontSizes, fontWeights } from "../../tokens/typography"; | ||
import * as stories from "./Heading.stories"; | ||
|
||
const { Basic, Contrasts } = composeStories(stories); | ||
|
||
test("renders the heading with the correct text content", () => { | ||
render(<Basic>제목</Basic>); | ||
|
||
expect(screen.getByRole("heading")).toHaveTextContent("제목"); | ||
}); | ||
|
||
test.each([1, 2, 3, 4, 5, 6] as const)( | ||
"renders the correct HTML heading element for level %i", | ||
(level) => { | ||
render(<Basic level={level} />); | ||
|
||
expect(screen.getByRole("heading", { level })).toBeInTheDocument(); | ||
} | ||
); | ||
|
||
test("applies the correct font weight class based on the weight prop", () => { | ||
const weight = faker.helpers.arrayElement( | ||
Object.keys(fontWeights) | ||
) as keyof typeof fontWeights; | ||
|
||
render(<Basic weight={weight} />); | ||
|
||
expect(screen.getByRole("heading")).toHaveClass(`fw_${weight}`); | ||
}); | ||
|
||
test("applies the correct font size class based on the size prop", () => { | ||
const size = faker.helpers.arrayElement( | ||
Object.keys(fontSizes) | ||
) as keyof typeof fontSizes; | ||
|
||
render(<Basic size={size} />); | ||
|
||
expect(screen.getByRole("heading")).toHaveClass(`fs_${size}`); | ||
}); | ||
|
||
test("applies the correct color for low and high contrast", () => { | ||
render(<Contrasts />); | ||
|
||
expect(screen.getByRole("heading", { name: "낮은 명암비" })).toHaveClass( | ||
"c_text.muted" | ||
); | ||
|
||
expect(screen.getByRole("heading", { name: "높은 명암비" })).toHaveClass( | ||
"c_text" | ||
); | ||
}); |
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,73 @@ | ||
import type { ReactNode, HTMLAttributes } from "react"; | ||
import { css, cva } from "../../../styled-system/css"; | ||
import type { FontSize, FontWeight } from "../../tokens/typography"; | ||
|
||
type Level = 1 | 2 | 3 | 4 | 5 | 6; | ||
|
||
export interface HeadingProps extends HTMLAttributes<HTMLHeadingElement> { | ||
/** 텍스트 */ | ||
children: ReactNode; | ||
/** 단계 */ | ||
level: Level; | ||
/** 크기 */ | ||
size?: FontSize; | ||
/** 굵기 */ | ||
weight?: FontWeight; | ||
/** 명암비 */ | ||
muted?: boolean; | ||
} | ||
|
||
/** | ||
* - `level` 속성을 통해서 `<h1>`, `<h2>`, `<h3>`, `<h4>`, `<h5>`, `<h6>` 요소 중 하나를 선택할 수 있습니다. | ||
* - `level` 속성은 단계 별 기본 텍스트 스타일을 제공합니다. | ||
* - `size` 속성과 `weight` 속성을 통해서 기본 스타일을 변경할 수 있습니다. | ||
* - `muted` 속성을 주시면 글자색이 옅어집니다. 명암비가 낮아지므로 접근성 측면에서 주의해서 사용하세요. | ||
*/ | ||
export const Heading = ({ | ||
children, | ||
level, | ||
size, | ||
weight, | ||
muted = false, | ||
...rest | ||
}: HeadingProps) => { | ||
if (!level) { | ||
throw new Error( | ||
"The level prop is required and you can cause accessibility issues." | ||
); | ||
} | ||
|
||
const Tag = `h${level}` as const; | ||
|
||
return ( | ||
<Tag | ||
className={css( | ||
styles.raw({ level, muted }), | ||
css.raw({ | ||
fontSize: size, | ||
fontWeight: weight, | ||
}) | ||
)} | ||
{...rest} | ||
> | ||
{children} | ||
</Tag> | ||
); | ||
}; | ||
|
||
const styles = cva({ | ||
variants: { | ||
level: { | ||
1: { textStyle: "4xl" }, | ||
2: { textStyle: "3xl" }, | ||
3: { textStyle: "2xl" }, | ||
4: { textStyle: "xl" }, | ||
5: { textStyle: "lg" }, | ||
6: { textStyle: "md" }, | ||
}, | ||
muted: { | ||
true: { color: "text.muted" }, | ||
false: { color: "text" }, | ||
}, | ||
}, | ||
}); |
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 @@ | ||
export { Heading } from "./Heading"; |
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 |
---|---|---|
@@ -1,2 +1,2 @@ | ||
@layer reset, base, tokens, recipes, utilities; | ||
@import url(//spoqa.github.io/spoqa-han-sans/css/SpoqaHanSansNeo.css); | ||
@import url(https://spoqa.github.io/spoqa-han-sans/css/SpoqaHanSansNeo.css); |
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