Skip to content

Commit

Permalink
feat: make LinkButton Component Responsive
Browse files Browse the repository at this point in the history
  • Loading branch information
sounmind committed Jun 22, 2024
1 parent 93232f9 commit 9aa8f1f
Show file tree
Hide file tree
Showing 6 changed files with 162 additions and 143 deletions.
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>
</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";

0 comments on commit 9aa8f1f

Please sign in to comment.