Skip to content

Commit

Permalink
feat : create image component
Browse files Browse the repository at this point in the history
  • Loading branch information
SamTheKorean committed Jun 14, 2024
1 parent 115c61d commit cc69306
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 3 deletions.
56 changes: 56 additions & 0 deletions components/image.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
class ImageComponent extends HTMLElement {
constructor() {
super();
this.attachShadow({ mode: 'open' });

this.validateAttributes();
this.setDefaultAttributes();

this.render();
}

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

// Validate allowed attributes
const allowedAttributes = ['src', 'alt', 'width', 'height'];
for (const attr of this.attributes) {
if (!allowedAttributes.includes(attr.name)) {
throw new Error(`The attribute "${attr.name}" is not allowed.`);
}
}
}

setDefaultAttributes() {
if (!this.hasAttribute('width')) {
this.setAttribute('width', '100%');
}
if (!this.hasAttribute('height')) {
this.setAttribute('height', 'auto');
}
}

render() {
const src = this.getAttribute('src');
const alt = this.getAttribute('alt');
const width = this.getAttribute('width');
const height = this.getAttribute('height');

this.shadowRoot.innerHTML = `
<style>
img {
display: block;
max-width: 100%;
height: auto;
}
</style>
<img src="${src}" alt="${alt}" width="${width}" height="${height}">
`;
}
}

customElements.define('image-component', ImageComponent);

6 changes: 3 additions & 3 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -243,13 +243,13 @@
</div>

<div class="intro-image">
<img src="images/roadmap.png" alt="#" />
<image-component src="images/roadmap.png" alt="Study roadmap"></image-component>
</div>
</div>

<div id="language-container">
<div class="language-image">
<img src="images/languages.png" alt="#" />
<image-component src="images/languages.png" alt="Languages"></image-component>
</div>
<div class="hero">
<p>์ง€์—ญ์— ๊ด€๊ณ„์—†์ด ๋‹ค์–‘ํ•œ ์–ธ์–ด๋กœ ์ฐธ์—ฌํ•  ์ˆ˜ ์žˆ์–ด์š”</p>
Expand All @@ -263,7 +263,7 @@
<link-button size="big" variant="secondary" href="https://discord.gg/43UkheRV">๋””์Šค์ฝ”๋“œ ์ฐธ์—ฌํ•˜๊ธฐ</link-button>
</div>
<div class="review-image">
<img src="images/review.png" />
<image-component src="images/review.png" alt="Review image"></image-component>
</div>
</div>

Expand Down
1 change: 1 addition & 0 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import "./components/divider/divider.js";
import "./components/footer-link/footer-link.js";
import "./components/header.js"
import "./components/hero/hero.js";
import "./components/image.js";
import "./components/link-button/link-button.js";
import "./components/review-item/review-item.js";
import "./components/seo-meta-tag/seo-meta-tag.js";
Expand Down

0 comments on commit cc69306

Please sign in to comment.