Skip to content

Instantly share code, notes, and snippets.

@brianmfear
Created February 23, 2018 17:15
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save brianmfear/d1308608c0facdc626b85c9c248fc7af to your computer and use it in GitHub Desktop.
Save brianmfear/d1308608c0facdc626b85c9c248fc7af to your computer and use it in GitHub Desktop.
LightningProgressRing component
<aura:component >
<aura:attribute name="value" type="Integer" default="0" />
<aura:attribute name="variant" type="String" />
<aura:attribute name="hasVariant" type="Boolean" access="private" default="{!false}" />
<aura:attribute name="ringClass" type="String" access="private" />
<aura:attribute name="iconName" type="String" access="private" />
<aura:attribute name="altText" type="String" access="private" />
<aura:handler name="init" value="{!this}" action="{!c.updateView}" />
<aura:handler name="change" value="{!v.value}" action="{!c.updateView}" />
<aura:handler name="change" value="{!v.variant}" action="{!c.updateView}" />
<div class="{!v.ringClass}">
<div id="progressContainer" class="slds-progress-ring__progress" role="progressbar" aria-valuemin="0" aria-valuemax="100" aria-valuenow="{!v.value}">
</div>
<div class="slds-progress-ring__content">
<aura:if isTrue="{!v.hasVariant}">
<lightning:icon iconName="{!v.iconName}" size="xx-small" title="{!v.altText}" alternativeText="{!v.altText}" />
</aura:if>
</div>
</div>
</aura:component>
({
updateView: function(component, event, helper) {
var variant = component.get("v.variant"),
hasVariant = variant == "warning" || variant == "expired",
style = "slds-progress-ring",
iconName,
altText;
if(hasVariant) {
style = style + " " + style + "_" + variant;
iconName = "utility:"+({ warning: "warning", expired: "error" }[variant]);
altText = { warning: "Warning", expired: "Expired" }[variant];
}
component.set("v.ringClass", style);
component.set("v.hasVariant", hasVariant);
component.set("v.iconName", iconName);
component.set("v.altText", altText)
}
})
({
// Create SVG, path, populate with default values from controller
render: function(component, helper) {
var result = this.superRender(),
xmlns = "http://www.w3.org/2000/svg",
updateContainer = result[0].querySelector("#progressContainer"),
value = component.get("v.value"),
dValue = "M 1 0 A 1 1 0 "+Math.floor(value / 50)+" 1 "+
Math.cos(2 * Math.PI * value / 100)+" "+
Math.sin(2 * Math.PI * value / 100)+" L 0 0",
svg = document.createElementNS(xmlns,"svg"),
path = document.createElementNS(xmlns,"path");
svg.setAttributeNS(null,"viewBox", "-1 -1 2 2");
path.setAttributeNS(null, "class", "slds-progress-ring__path");
path.setAttributeNS(null, "d", dValue);
svg.appendChild(path);
updateContainer.appendChild(svg);
return result;
},
// Update the progress bar on a rerender event
rerender: function(component, helper) {
var value = component.get("v.value"),
dValue = "M 1 0 A 1 1 0 "+Math.floor(value / 50)+" 1 "+
Math.cos(2 * Math.PI * value / 100)+" "+
Math.sin(2 * Math.PI * value / 100)+" L 0 0",
svg = component.getElement().querySelector("svg"),
path = svg.childNodes[0];
this.superRerender();
path.setAttributeNS(null, "d", dValue);
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment