allWAIs-Link ํ๊ณ (22.11.16)
๐ ๋ฏธ์
- Link ์ญํ ๋ก์จ spanํ๊ทธ, imgํ๊ทธ ํ์ธ
- tabindex=โ0โ๋ฅผ ํฌํจ
- imgํ๊ทธ๋ alt ์์ฑ์ผ๋ก ์ ๊ทผ ๊ฐ๋ฅํ ์ด๋ฆ ์ ์
๐ ๋ฌธ์
๐คฟ ์ปดํฌ๋ํธ props โasโ๋ฅผ img๋ก ์ฃผ๋ฉด, img ํ๊ทธ๋ก ๋ ๋๋ง ํด์ฃผ๊ธฐ
1 | import styled from "styled-components"; |
Link ์ปดํฌ๋ํธ ์์๋ค๊ฐ styled-component๋ฅผ ์์ฑํ๊ณ as props๋ฅผ ์ ๋ฌํด์ฃผ์๋ค.
์์ ๊ฐ์ด ํ ๊ฒฝ์ฐ, as props์ img ํ๊ทธ๊ฐ ๋ค์ด์ค๊ฒ ๋๋ค๋ฉด
์ปดํฌ๋ํธ ๋ช ๋ช ๊ท์น์ ์ด๊ธ๋๋ค๊ณ ๋์จ๋ค. ์ด๋ฅผ ํด๊ฒฐํ๊ธฐ ์ํด ์กฐ๊ฑด๋ถ ๋ ๋๋ง์ ํด์ฃผ์๋ค.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35import styled from "styled-components";
import { goToLink } from "../../utils";
export function Link({ as = ComponentName, href, children }) {
const StyledLink = styled(`${as}`)`
... styles
`;
return (
<>
{as === "img" ? (
<img
tabIndex="0"
role="link"
onClick={(e) => goToLink(e, href)}
onKeyDown={(e) => goToLink(e, href)}
src="./gitprofile.jpeg"
alt="W3C Website"
/>
) : (
<StyledLink
tabIndex="0"
role="link"
onClick={(e) => goToLink(e, href)}
onKeyDown={(e) => goToLink(e, href)}
>
{children}
</StyledLink>
)}
</>
);
}
Link.defaultProps = {
as: "span",
};๐ฅ img๊ฐ ์ ๋ก๋ ๋์ง ์๋ ๋ฌธ์
- img ์ ๋ก๋ ๋ฌธ์ ํด๊ฒฐ
๋ฌธ์ ์ ์์ธ์ npm ๋ช ๋ น์ด๋ก webpack server๋ง ์คํ์์ผฐ๋๋ฐ, webpack
config.js
ํ์ผ์static
์์ฑ๊ฐ์[build]
๋ก ์ฃผ์๊ธฐ ๋๋ฌธ์ ์ ์ ์ด๋ฏธ์ง ํ์ผ์ ๋ถ๋ฌ์ค๊ธฐ ์ํด์๋build
ํด๋์์assets
์ ์ฐพ๋๋ค.๊ทธ๋ฌ๋ฏ๋ก
npm run build
๋ช ๋ น์ด๋ก build ํด๋๋ฅผ ์์ฑํ ํ ๊ทธ ์์๋ค๊ฐ img๋ฅผ ๋ฃ์ด์ฃผ๊ณ ๊ฒฝ๋ก๋ฅผ ์ค์ ํด์ฃผ์ด ํด๊ฒฐํ์๋ค.1
2
3
4
5
6<Link
as="img"
alt="test์ด๋ฏธ์ง"
src="./assets/img/gitprofile.jpeg"
href="https://www.w3.org/WAI/ARIA/apg/example-index/link/link.html"
/>๐ฅ
aria-label
์์ฑ ์์ ๋, ์กฐ๊ฑด๋ถ๋ก css ์ถ๊ฐ(์ญ์ ) ์ด ์์ฑ์ ์ญ์ ํ ์ด์ ๋ span ํ๊ทธ๋ฅผ ์ฌ์ฉํ์ฌ Link ์ปดํฌ๋ํธ๋ฅผ ๊ตฌํํ์์ ๋,
role="link"
๋ก ํด์ฃผ์๊ธฐ ๋๋ฌธ์aria-label
์์ฑ์ ๊ตณ์ด ํด์ฃผ์ง ์์๋ ๋๋ฏ๋ก ์ญ์ ํ์๋ค.
๐ ํ๊ณ
- styled-component๋ฅผ ์ฌ์ฉํ๋ฉด์ ์ด๋ป๊ฒ props๋ฅผ ๋ฐ์์ App์์ ๋ ๋๋ง ์์ผ์ผํ ์ง ํท๊ฐ๋ ธ์๋๋ฐ ์ง์ ํด๋ณด๋ ์ดํด๊ฐ ์๋์๋ค.
- ์์ ์๊ฐ์ ๋ฐฐ์ ๋ ์กฐ๊ฑด๋ถ ๋ ๋๋ง์ ์ค์ ๋ก ์ฌ์ฉํ๋ฉด์ ์ตํ๋ ๋์ฑ ์ดํด๊ฐ ์๋์๋ค. ์ญ์ ๋ฌด์ธ๊ฐ๋ฅผ ๋ง๋ค์ด๋ณด๋ฉด์ ๋ฐฐ์ฐ๋ ๊ฒ์ด ํ๋ค์ง๋ง ๋ ๋์๋๋ ๊ฒ ๊ฐ๋ค.