-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add visual progress indicators #5186
Merged
alexander-akait
merged 3 commits into
webpack:master
from
issacgerges:igerges_visual_progress
Jun 5, 2024
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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,205 @@ | ||
class WebpackDevServerProgress extends HTMLElement { | ||
constructor() { | ||
super(); | ||
this.attachShadow({ mode: "open" }); | ||
this.maxDashOffset = -219.99078369140625; | ||
this.animationTimer = null; | ||
} | ||
|
||
#reset() { | ||
clearTimeout(this.animationTimer); | ||
this.animationTimer = null; | ||
|
||
const typeAttr = this.getAttribute("type")?.toLowerCase(); | ||
this.type = typeAttr === "circular" ? "circular" : "linear"; | ||
|
||
const innerHTML = | ||
this.type === "circular" | ||
? WebpackDevServerProgress.#circularTemplate() | ||
: WebpackDevServerProgress.#linearTemplate(); | ||
this.shadowRoot.innerHTML = innerHTML; | ||
|
||
this.initialProgress = Number(this.getAttribute("progress")) ?? 0; | ||
|
||
this.#update(this.initialProgress); | ||
} | ||
|
||
static #circularTemplate() { | ||
return ` | ||
<style> | ||
:host { | ||
width: 200px; | ||
height: 200px; | ||
position: fixed; | ||
right: 5%; | ||
top: 5%; | ||
transition: opacity .25s ease-in-out; | ||
z-index: 2147483645; | ||
} | ||
|
||
circle { | ||
fill: #282d35; | ||
} | ||
|
||
path { | ||
fill: rgba(0, 0, 0, 0); | ||
stroke: rgb(186, 223, 172); | ||
stroke-dasharray: 219.99078369140625; | ||
stroke-dashoffset: -219.99078369140625; | ||
stroke-width: 10; | ||
transform: rotate(90deg) translate(0px, -80px); | ||
} | ||
|
||
text { | ||
font-family: 'Open Sans', sans-serif; | ||
font-size: 18px; | ||
fill: #ffffff; | ||
dominant-baseline: middle; | ||
text-anchor: middle; | ||
} | ||
|
||
tspan#percent-super { | ||
fill: #bdc3c7; | ||
font-size: 0.45em; | ||
baseline-shift: 10%; | ||
} | ||
|
||
@keyframes fade { | ||
0% { opacity: 1; transform: scale(1); } | ||
100% { opacity: 0; transform: scale(0); } | ||
} | ||
|
||
.disappear { | ||
animation: fade 0.3s; | ||
animation-fill-mode: forwards; | ||
animation-delay: 0.5s; | ||
} | ||
|
||
.hidden { | ||
display: none; | ||
} | ||
</style> | ||
<svg id="progress" class="hidden noselect" viewBox="0 0 80 80"> | ||
<circle cx="50%" cy="50%" r="35"></circle> | ||
<path d="M5,40a35,35 0 1,0 70,0a35,35 0 1,0 -70,0"></path> | ||
<text x="50%" y="51%"> | ||
<tspan id="percent-value">0</tspan> | ||
<tspan id="percent-super">%</tspan> | ||
</text> | ||
</svg> | ||
`; | ||
} | ||
|
||
static #linearTemplate() { | ||
return ` | ||
<style> | ||
:host { | ||
position: fixed; | ||
top: 0; | ||
left: 0; | ||
height: 4px; | ||
width: 100vw; | ||
z-index: 2147483645; | ||
} | ||
|
||
#bar { | ||
width: 0%; | ||
height: 4px; | ||
background-color: rgb(186, 223, 172); | ||
} | ||
|
||
@keyframes fade { | ||
0% { opacity: 1; } | ||
100% { opacity: 0; } | ||
} | ||
|
||
.disappear { | ||
animation: fade 0.3s; | ||
animation-fill-mode: forwards; | ||
animation-delay: 0.5s; | ||
} | ||
|
||
.hidden { | ||
display: none; | ||
} | ||
</style> | ||
<div id="progress"></div> | ||
`; | ||
} | ||
|
||
connectedCallback() { | ||
this.#reset(); | ||
} | ||
|
||
static get observedAttributes() { | ||
return ["progress", "type"]; | ||
} | ||
|
||
attributeChangedCallback(name, oldValue, newValue) { | ||
if (name === "progress") { | ||
this.#update(Number(newValue)); | ||
} else if (name === "type") { | ||
this.#reset(); | ||
} | ||
} | ||
|
||
#update(percent) { | ||
const element = this.shadowRoot.querySelector("#progress"); | ||
if (this.type === "circular") { | ||
const path = this.shadowRoot.querySelector("path"); | ||
const value = this.shadowRoot.querySelector("#percent-value"); | ||
const offset = ((100 - percent) / 100) * this.maxDashOffset; | ||
|
||
path.style.strokeDashoffset = offset; | ||
value.textContent = percent; | ||
} else { | ||
element.style.width = `${percent}%`; | ||
} | ||
|
||
if (percent >= 100) { | ||
this.#hide(); | ||
} else if (percent > 0) { | ||
this.#show(); | ||
} | ||
} | ||
|
||
#show() { | ||
const element = this.shadowRoot.querySelector("#progress"); | ||
element.classList.remove("hidden"); | ||
} | ||
|
||
#hide() { | ||
const element = this.shadowRoot.querySelector("#progress"); | ||
if (this.type === "circular") { | ||
element.classList.add("disappear"); | ||
element.addEventListener( | ||
"animationend", | ||
() => { | ||
element.classList.add("hidden"); | ||
this.#update(0); | ||
}, | ||
{ once: true }, | ||
); | ||
} else if (this.type === "linear") { | ||
element.classList.add("disappear"); | ||
this.animationTimer = setTimeout(() => { | ||
element.classList.remove("disappear"); | ||
element.classList.add("hidden"); | ||
element.style.width = "0%"; | ||
this.animationTimer = null; | ||
}, 800); | ||
} | ||
} | ||
} | ||
|
||
export function isProgressSupported() { | ||
return "customElements" in window && !!HTMLElement.prototype.attachShadow; | ||
} | ||
|
||
export function defineProgressElement() { | ||
if (!isProgressSupported() || customElements.get("wds-progress")) { | ||
return; | ||
} | ||
|
||
customElements.define("wds-progress", WebpackDevServerProgress); | ||
} |
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
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
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's move
defineProgressElement
after this check to avoid extra checks and havecustomElements.define("wds-progress", WebpackDevServerProgress);
only in this function