Skip to content

Commit

Permalink
Merge pull request #112 from indiana-university/release/3.0.1
Browse files Browse the repository at this point in the history
Release 3.0.1
  • Loading branch information
levimcg authored Feb 19, 2025
2 parents 94ae699 + 376e3a4 commit 9090adb
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 32 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "rivet-icons",
"version": "3.0.0",
"version": "3.0.1",
"description": "Icons for Indiana University's Rivet Design System",
"files": [
"dist/**/*",
Expand Down
3 changes: 2 additions & 1 deletion src/rivet-icon-element.css
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ rvt-icon {
width: min-content;
}

rvt-icon:not(:defined)::before {
rvt-icon:empty::before,
rvt-icon > svg {
content: "";
display: block;
height: 1rem;
Expand Down
66 changes: 36 additions & 30 deletions src/rivet-icon-element.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,35 +19,7 @@ style.setAttribute(`data-${elementName}`, '');
document.head.appendChild(style);

export function registerIcon (name, content) {
if (!name || typeof name !== 'string') {
throw new Error(`${packageName}: Name must be a string.`);
}
const template = document.createElement('template');
template.innerHTML = content;
if (template.content.children.length !== 1) {
throw new Error(`${packageName} (${name}): Content must contain one SVG element.`);
}
const svg = template.content.firstChild;
if (svg.nodeName.toLowerCase() !== 'svg') {
throw new Error(`${packageName} (${name}): Content must be a SVG element.`);
}
setDefaultAttributes(svg, {
'aria-hidden': 'true',
fill: 'currentColor',
focusable: 'false',
height: size,
viewBox: `0 0 ${size} ${size}`,
width: size,
xmlns: 'http://www.w3.org/2000/svg'
});
nameToTemplateMap.set(name, template);
const index = nameToTemplateMap.size;
indexToNameMap.set(index, name);
style.sheet.insertRule(`${elementName} { --${name}: ${index}; }`);
const event = new CustomEvent(registeredEventName, {
detail: { name }
});
document.dispatchEvent(event);
window.customElements.get(elementName).register?.(name, content);
}

class RivetIconElement extends window.HTMLElement {
Expand All @@ -58,6 +30,38 @@ class RivetIconElement extends window.HTMLElement {
return [nameAttributeName];
}

static register (name, content) {
if (!name || typeof name !== 'string') {
throw new Error(`${packageName}: Name must be a string.`);
}
const template = document.createElement('template');
template.innerHTML = content;
if (template.content.children.length !== 1) {
throw new Error(`${packageName} (${name}): Content must contain one SVG element.`);
}
const svg = template.content.firstChild;
if (svg.nodeName.toLowerCase() !== 'svg') {
throw new Error(`${packageName} (${name}): Content must be a SVG element.`);
}
setDefaultAttributes(svg, {
'aria-hidden': 'true',
fill: 'currentColor',
focusable: 'false',
height: size,
viewBox: `0 0 ${size} ${size}`,
width: size,
xmlns: 'http://www.w3.org/2000/svg'
});
nameToTemplateMap.set(name, template);
const index = nameToTemplateMap.size;
indexToNameMap.set(index, name);
style.sheet.insertRule(`${elementName} { --${name}: ${index}; }`);
const event = new CustomEvent(registeredEventName, {
detail: { name }
});
document.dispatchEvent(event);
}

attributeChangedCallback () {
this.#requestUpdate();
}
Expand Down Expand Up @@ -93,7 +97,9 @@ class RivetIconElement extends window.HTMLElement {
}
}

window.customElements.define(elementName, RivetIconElement);
if (!window.customElements.get(elementName)) {
window.customElements.define(elementName, RivetIconElement);
}

//
// Utilities
Expand Down

0 comments on commit 9090adb

Please sign in to comment.