Skip to content

Commit

Permalink
feat: icon-link-list, icon-link component
Browse files Browse the repository at this point in the history
- name changed: footer-icon -> icon-link
  • Loading branch information
sounmind committed Jul 8, 2024
1 parent d6cc184 commit f4c4c70
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 64 deletions.
63 changes: 0 additions & 63 deletions components/footer-icon.js

This file was deleted.

114 changes: 114 additions & 0 deletions components/icon-link-list.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import { css, html } from "../html-css-utils.js";

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

this.validateAttributes();
this.render();
}

validateAttributes() {
const attributes = ["href", "icon-src", "icon-alt"];

for (let i = 0; i < attributes.length; i++) {
if (!this.hasAttribute(attributes[i])) {
throw new Error(`The "${attributes[i]}" attribute is required.`);
}
}

const iconSrc = this.getAttribute("icon-src");
const validExtensions = [".svg", ".png", ".jpg", ".jpeg"];
const extension = iconSrc.slice(iconSrc.lastIndexOf(".")).toLowerCase();

if (!validExtensions.includes(extension)) {
throw new Error(
`The "icon-src" attribute must end with one of the following extensions: ${validExtensions.join(
", "
)}.`
);
}
}

render() {
this.attachShadow({ mode: "open" });
this.shadowRoot.innerHTML = this.createCss() + this.createHtml();
}

createCss() {
const width = this.getAttribute("width");
const height = this.getAttribute("height");

return css`
a {
display: block;
}
img {
${width ? `width: ${width};` : ""}
${height ? `height: ${height};` : ""}
}
`;
}

createHtml() {
const href = this.getAttribute("href");
const iconSrc = this.getAttribute("icon-src");
const iconAlt = this.getAttribute("icon-alt");

return html`
<a href="${href}" target="_blank">
<img src="${iconSrc}" alt="${iconAlt}" />
</a>
`;
}
}

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

this.render();
this.validateAttributes();
}

validateAttributes() {
const slot = this.shadowRoot.querySelector("slot");
const nodes = slot.assignedNodes();

nodes.forEach((node) => {
if (
node.nodeType === Node.ELEMENT_NODE &&
node.tagName.toLowerCase() !== "ds-icon-link"
) {
throw new Error(
"The children of ds-icon-link-list must be ds-icon-link."
);
}
});
}

render() {
this.attachShadow({ mode: "open" });
this.shadowRoot.innerHTML = this.createCss() + this.createHtml();
}

createCss() {
return css`
section {
display: flex;
align-items: center;
gap: 40px;
}
`;
}

createHtml() {
return html`<section>
<slot></slot>
</section>`;
}
}

customElements.define("ds-icon-link", IconLink);
customElements.define("ds-icon-link-list", IconLinkList);
Binary file added images/blog-icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/github-icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/linked-in-icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/youtube-icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion main.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import "./components/button-link.js";
import "./components/footer-icon.js";
import "./components/footer-link-list.js";
import "./components/header.js";
import "./components/hero.js";
import "./components/icon-link-list.js";
import "./components/image.js";
import "./components/intro-section.js";
import "./components/review-item.js";
Expand Down

0 comments on commit f4c4c70

Please sign in to comment.