-
Notifications
You must be signed in to change notification settings - Fork 160
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
282 additions
and
12 deletions.
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
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,49 @@ | ||
:host{ | ||
display: inline-flex; | ||
align-items: center; | ||
gap: 8px; | ||
width: 150px; | ||
} | ||
|
||
.progress{ | ||
position: relative; | ||
border: 0; | ||
width: -webkit-fill-available; | ||
width: -moz-available; | ||
height: 8px; | ||
border-radius: 8px; | ||
background-color: var(--progress-bg, rgba(0,0,0,.06)); | ||
overflow: hidden; | ||
} | ||
.progress::before{ | ||
content: ''; | ||
position: absolute; | ||
height: 100%; | ||
border-radius: inherit; | ||
left: 0; | ||
width: var(--percent, 50%); | ||
background-color:var(--primary-color,royalblue); | ||
transition: var(--transition, .2s); | ||
} | ||
.progress[style="--percent:100%;"]::before{ | ||
background-color: var(--success-color, #52c41a); | ||
} | ||
.info{ | ||
width: 2em; | ||
font-size: 14px; | ||
} | ||
.progress[style="--percent:100%;"]+.info{ | ||
-webkit-mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 512 512'%3E%3C!--! Font Awesome Pro 6.4.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license (Commercial License) Copyright 2023 Fonticons, Inc.--%3E%3Cpath d='M256 512a256 256 0 1 0 0-512 256 256 0 1 0 0 512zm113-303L241 337c-9.4 9.4-24.6 9.4-33.9 0l-64-64c-9.4-9.4-9.4-24.6 0-33.9s24.6-9.4 33.9 0l47 47L335 175c9.4-9.4 24.6-9.4 33.9 0s9.4 24.6 0 33.9z'/%3E%3C/svg%3E") 0 center/14px no-repeat; | ||
color: transparent; | ||
pointer-events: none; | ||
background-color: var(--success-color, #52c41a); | ||
} | ||
:host([error]) .progress::before{ | ||
background-color: var(--error-color, #f4615c); | ||
} | ||
:host([error]) .info{ | ||
-webkit-mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 512 512'%3E%3C!--! Font Awesome Pro 6.4.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license (Commercial License) Copyright 2023 Fonticons, Inc.--%3E%3Cpath d='M256 512a256 256 0 1 0 0-512 256 256 0 1 0 0 512zm-81-337c9.4-9.4 24.6-9.4 33.9 0l47 47 47-47c9.4-9.4 24.6-9.4 33.9 0s9.4 24.6 0 33.9l-47 47 47 47c9.4 9.4 9.4 24.6 0 33.9s-24.6 9.4-33.9 0l-47-47-47 47c-9.4 9.4-24.6 9.4-33.9 0s-9.4-24.6 0-33.9l47-47-47-47c-9.4-9.4-9.4-24.6 0-33.9z'/%3E%3C/svg%3E") 0 center/14px no-repeat; | ||
color: transparent; | ||
pointer-events: none; | ||
background-color: var(--error-color, #f4615c); | ||
} |
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,60 @@ | ||
import Base from "../xy-base.js"; | ||
import style from "./index.css?inline" assert { type: "css" }; | ||
|
||
export default class Progress extends Base { | ||
#progress; | ||
#info; | ||
static get observedAttributes() { | ||
return ["value"]; | ||
} | ||
|
||
constructor() { | ||
super(); | ||
const shadowRoot = this.attachShadow({ mode: "open" }); | ||
this.adoptedStyle(style); | ||
shadowRoot.innerHTML = ` | ||
<div class="progress" id="progress" part="progress" role="progressbar"></div> | ||
<span class="info" id="info" part="info"></span> | ||
`; | ||
this.#progress = shadowRoot.getElementById("progress"); | ||
this.#info = shadowRoot.getElementById("info"); | ||
} | ||
|
||
#render() { | ||
this.#progress.style.setProperty("--percent", this.value * 100 + "%"); | ||
this.#info.textContent = this.value * 100 + "%"; | ||
} | ||
|
||
get type() { | ||
return this.getAttribute("type") || "line"; | ||
} | ||
|
||
get error() { | ||
return this.getAttribute("error") !== null; | ||
} | ||
|
||
get value() { | ||
const value = Number(this.getAttribute("value") || ".5"); | ||
return Math.min(Math.max(value), 1); | ||
} | ||
|
||
set value(value) { | ||
this.setAttribute("value", value); | ||
} | ||
|
||
set error(value) { | ||
this.toggleAttribute("error", value); | ||
} | ||
|
||
connectedCallback() {} | ||
|
||
attributeChangedCallback(name, oldValue, newValue) { | ||
if (name === "value") { | ||
this.#render(); | ||
} | ||
} | ||
} | ||
|
||
if (!customElements.get("xy-progress")) { | ||
customElements.define("xy-progress", Progress); | ||
} |
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
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,158 @@ | ||
<script setup> | ||
import { onMounted } from 'vue' | ||
import './index.css' | ||
onMounted(() => { | ||
import('../../components/progress/') | ||
}) | ||
</script> | ||
|
||
# progress | ||
|
||
进度条,用于展示当前操作进度。 | ||
|
||
## 使用方式 | ||
|
||
```html | ||
<!-- 引入 --> | ||
<script type="module"> | ||
import '../components/progress/index.js'; | ||
</script> | ||
<!-- 使用 --> | ||
<xy-progress value=".3"></xy-progress> | ||
``` | ||
|
||
## 值`value` | ||
|
||
通过`value`可以设置进度条进度,范围是`0~1`。 | ||
|
||
<div class="wrap" vertical> | ||
<xy-progress value=".3"></xy-progress> | ||
<xy-progress value=".5"></xy-progress> | ||
<xy-progress value=".8"></xy-progress> | ||
</div> | ||
|
||
```html | ||
<xy-progress value=".3"></xy-progress> | ||
<xy-progress value=".5"></xy-progress> | ||
<xy-progress value=".8"></xy-progress> | ||
``` | ||
|
||
当进度完成时,也就是`value`为`1`时,会出现成功状态 | ||
|
||
<div class="wrap" vertical> | ||
<xy-progress value="1"></xy-progress> | ||
</div> | ||
|
||
```html | ||
<xy-progress value="1"></xy-progress> | ||
``` | ||
|
||
JavaScript操作`get`、`set` | ||
|
||
```js | ||
progress.value; | ||
progress.value = .5; | ||
//原生属性操作 | ||
progress.getAttribute('value'); | ||
progress.setAttribute('value',.5); | ||
``` | ||
|
||
## 错误状态`error` | ||
|
||
有时候进度条会出错而中断,可以添加`error`属性 | ||
|
||
<div class="wrap" vertical> | ||
<xy-progress value=".3" error></xy-progress> | ||
<xy-progress value=".5" error></xy-progress> | ||
<xy-progress value=".8" error></xy-progress> | ||
</div> | ||
|
||
```html | ||
<xy-progress value=".3" error></xy-progress> | ||
<xy-progress value=".5" error></xy-progress> | ||
<xy-progress value=".8" error></xy-progress> | ||
``` | ||
|
||
JavaScript操作`get`、`set` | ||
|
||
```js | ||
progress.error; | ||
progress.error = true; | ||
//原生属性操作 | ||
progress.getAttribute('error'); | ||
progress.setAttribute('error', ''); | ||
progress.toggleAttribute('error', [force]); | ||
``` | ||
|
||
## 自定义样式`::part(progress)`、`::part(info)` | ||
|
||
默认情况下,进度条的宽度是`150px`,你可以指定任意宽度,比如 | ||
|
||
<style scope> | ||
.custom-progress xy-progress{ | ||
width: 100% | ||
} | ||
.custom-progress-2 xy-progress::part(progress){ | ||
border-radius: 0 | ||
} | ||
.custom-progress-3 xy-progress::part(info){ | ||
display: none | ||
} | ||
</style> | ||
|
||
<div class="wrap custom-progress" vertical> | ||
<xy-progress value=".3"></xy-progress> | ||
<xy-progress value=".5"></xy-progress> | ||
<xy-progress value=".8"></xy-progress> | ||
<xy-progress value=".8" error></xy-progress> | ||
<xy-progress value="1"></xy-progress> | ||
</div> | ||
|
||
```css | ||
xy-progress{ | ||
width: 100% | ||
} | ||
``` | ||
|
||
如果需要自定义进度条样式,比如圆角,需要深入到`shadow dom`中,这里暴露了内置伪元素`::part(progress)`用来自定义样式 | ||
|
||
内部结构如下(可查看控制台): | ||
|
||
```html | ||
<xy-progress> | ||
# shadow-root | ||
<div part="progress" role="progressbar"></div> | ||
<span part="info"></span> | ||
``` | ||
|
||
下面是一个直角进度条 | ||
|
||
<div class="wrap custom-progress-2" vertical> | ||
<xy-progress value=".3"></xy-progress> | ||
<xy-progress value=".5"></xy-progress> | ||
<xy-progress value=".8"></xy-progress> | ||
<xy-progress value=".8" error></xy-progress> | ||
<xy-progress value="1"></xy-progress> | ||
</div> | ||
|
||
```css | ||
xy-progress::part(progress){ | ||
border-radius: 0 | ||
} | ||
``` | ||
|
||
还可以隐藏进度条后面的信息提示 | ||
|
||
<div class="wrap custom-progress-3" vertical> | ||
<xy-progress value=".3"></xy-progress> | ||
<xy-progress value=".5"></xy-progress> | ||
<xy-progress value=".8"></xy-progress> | ||
<xy-progress value=".8" error></xy-progress> | ||
<xy-progress value="1"></xy-progress> | ||
</div> | ||
|
||
```css | ||
xy-progress::part(info){ | ||
display: none | ||
} | ||
``` |