-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix : adjust the new component writting styles and apply feedback pro…
…vided
- Loading branch information
1 parent
fb22748
commit 06a22ae
Showing
2 changed files
with
64 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
class Image extends HTMLElement { | ||
constructor() { | ||
super(); | ||
|
||
this.validateAttributes(); | ||
this.render(); | ||
} | ||
|
||
validateAttributes() { | ||
if (!this.hasAttribute("alt")) { | ||
throw new Error('The "alt" attribute is required.'); | ||
} | ||
|
||
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.`); | ||
} | ||
} | ||
} | ||
|
||
render() { | ||
this.attachShadow({ mode: "open" }); | ||
this.shadowRoot.innerHTML = this.createCss() + this.createHtml(); | ||
} | ||
|
||
createCss() { | ||
return ` | ||
<style> | ||
img { | ||
display: block; | ||
max-width: 100%; | ||
} | ||
</style> | ||
`; | ||
} | ||
|
||
createHtml() { | ||
const src = this.getAttribute("src") ?? ""; | ||
const alt = this.getAttribute("alt") ?? ""; | ||
|
||
let imgHtml = `<img src="${src}" alt="${alt}"`; | ||
|
||
if (this.hasAttribute("width")) { | ||
const width = this.getAttribute("width"); | ||
imgHtml += ` width="${width}"`; | ||
} | ||
|
||
if (this.hasAttribute("height")) { | ||
const height = this.getAttribute("height"); | ||
imgHtml += ` height="${height}"`; | ||
} | ||
|
||
imgHtml += `>`; | ||
|
||
return ` | ||
${imgHtml} | ||
`; | ||
} | ||
} | ||
|
||
customElements.define("ds-image", Image); |
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,6 +1,7 @@ | ||
import "./components/button-link.js"; | ||
import "./components/footer-link/footer-link.js"; | ||
import "./components/header.js"; | ||
import "./components/hero/hero.js"; | ||
import "./components/button-link.js"; | ||
import "./components/image.js"; | ||
import "./components/review-item/review-item.js"; | ||
import "./components/seo-meta-tag/seo-meta-tag.js"; |