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

Make LinkButton Component Responsive #51

Merged
merged 1 commit into from
Jun 22, 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
151 changes: 151 additions & 0 deletions components/button-link.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
const css = /*css*/ `
<style>
/* Default: Extra-small devices such as small phones (less than 640px) */
a {
display: flex;
justify-content: center;
align-items: center;

width: max-content;
text-align: center;
}

a.secondary {
color: #000;
background-color: #d9d9d9;
}

a.small {
height: 35px;
padding: 10px 20px;
font-size: 13px;
border-radius: 7px;
}

a.big {
height: 45px;
padding: 10px 25px;
font-size: 14px;
border-radius: 10px;
}

a.ghost {
border: none;
border-radius: 0;
height: min-content;
}

a.small.ghost {
padding: 0;
}

a.big.ghost {
padding: auto;
color: #000;
background-color: transparent;
border: 1px solid #000;
border-radius: 10px;
}

/* Medium devices such as tablets (768px and up) */
@media only screen and (min-width: 768px) {
a.small {
padding: 5px 25px;
font-size: 14px;
}

a.big {
padding: 16px 40px;
font-size: 20px;
}
}

/* Large devices such as laptops (1024px and up) */
@media only screen and (min-width: 1024px) {
a.small {
padding: 5px 30px;
font-size: 16px;
}
}
</style>
`;

const html = /*html*/ `
<link rel="stylesheet" href="./global-styles.css" />
<a>
<slot></slot>
</a>
`;

class ButtonLink extends HTMLElement {
constructor() {
super();

this.attachShadow({
mode: "open",
});
this.shadowRoot.innerHTML = css + html;

this.setDefaultAttributes();
this.validateAttributes();
this.updateAttributes();
}

validateAttributes() {
if (!this.hasAttribute("href")) {
throw new Error('The "href" attribute is required.');
}

if (this.hasAttribute("size")) {
const size = this.getAttribute("size");
const validSizes = ["big", "small"];

if (!validSizes.includes(size)) {
throw new Error(
`The "size" attribute must be one of ${validSizes.join(", ")}.`
);
}
}

if (this.hasAttribute("variant")) {
const variant = this.getAttribute("variant");
const validVariants = ["ghost", "secondary"];

if (!validVariants.includes(variant)) {
throw new Error(
`The "variant" attribute must be one of ${validVariants.join(", ")}.`
);
}
}
}

setDefaultAttributes() {
if (!this.hasAttribute("size")) {
this.setAttribute("size", "small");
}

if (!this.hasAttribute("variant")) {
this.setAttribute("variant", "ghost");
}
}

updateAttributes() {
const link = this.shadowRoot.querySelector("a");
link.setAttribute("href", this.getAttribute("href"));

// Set target to "_blank" only for external links
if (!this.getAttribute("href").startsWith("#")) {
link.setAttribute("target", "_blank");
}

if (this.hasAttribute("size")) {
link.classList.add(this.getAttribute("size"));
}

if (this.hasAttribute("variant")) {
link.classList.add(this.getAttribute("variant"));
}
}
}

customElements.define("ds-button-link", ButtonLink);
6 changes: 4 additions & 2 deletions components/header.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ template.innerHTML = `

.buttons-container {
display: flex;
align-items: center;
column-gap: 40px;
}

a {
Expand Down Expand Up @@ -48,8 +50,8 @@ template.innerHTML = `
</a>

<div class="buttons-container">
<link-button href="#join-instruction-container" size="small" variant="ghost">참여방법 안내</link-button>
<link-button href="https://discord.gg/43UkheRV" size="small" variant="secondary">디스코드 참여하기</link-button>
<ds-button-link href="#join-instruction-container" size="small" variant="ghost">참여방법 안내</ds-button-link>
<ds-button-link href="https://discord.gg/43UkheRV" size="small" variant="secondary">디스코드 참여하기</ds-button-link>
Comment on lines +53 to +54
Copy link
Member Author

@sounmind sounmind Jun 22, 2024

Choose a reason for hiding this comment

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

@SamTheKorean header 컴포넌트 내부에서 button link 컴포넌트를 사용하고 있습니다. 그런데 button link의 사용법에 변경이 발생하면(지금처럼 이름이 변경되거나 전달해야 하는 attribute가 달라졌을 경우), HTML 파일에서뿐만 아니라 header 안에서도 변경을 해야 합니다. 이런 컴포넌트 사용이 많아질수록 하나를 변경했을 때, 같이 변경해야 하는 지점이 많아집니다. 이는 미래에 유지보수가 힘들어질 수 있는 신호이기도 합니다. 이것은 정말 문제가 맞을지? 맞다면 어떻게 해결할 수 있을지? 천천히 생각해보아요!

cc. @DaleStudy/website

</div>
</header>
`;
Expand Down
58 changes: 0 additions & 58 deletions components/link-button/link-button.css

This file was deleted.

76 changes: 0 additions & 76 deletions components/link-button/link-button.js

This file was deleted.

12 changes: 6 additions & 6 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -239,11 +239,11 @@
<div id="intro-container">
<div class="hero">
<p>해외취업을 위한 커뮤니티 기반 알고리즘 스터디</p>
<link-button
<ds-button-link
size="big"
variant="secondary"
href="https://discord.gg/43UkheRV"
>디스코드 참여하기</link-button
>디스코드 참여하기</ds-button-link
>
</div>

Expand All @@ -258,24 +258,24 @@
</div>
<div class="hero">
<p>지역에 관계없이 다양한 언어로 참여할 수 있어요</p>
<link-button
<ds-button-link
size="big"
variant="ghost"
outline
href="#join-instruction-container"
>참여방법 안내</link-button
>참여방법 안내</ds-button-link
>
</div>
</div>

<div id="review-container">
<div class="hero">
<p>코드리뷰를 통해 새로운 관점을 배울 수 있어요</p>
<link-button
<ds-button-link
size="big"
variant="secondary"
href="https://discord.gg/43UkheRV"
>디스코드 참여하기</link-button
>디스코드 참여하기</ds-button-link
>
</div>
<div class="review-image">
Expand Down
2 changes: 1 addition & 1 deletion main.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import "./components/footer-link/footer-link.js";
import "./components/header.js";
import "./components/hero/hero.js";
import "./components/link-button/link-button.js";
import "./components/button-link.js";
import "./components/review-item/review-item.js";
import "./components/seo-meta-tag/seo-meta-tag.js";