-
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.
feat: icon-link-list, icon-link component
- name changed: footer-icon -> icon-link
- Loading branch information
Showing
7 changed files
with
115 additions
and
64 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,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); |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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