kopia lustrzana https://github.com/shoelace-style/shoelace
88 wiersze
3.1 KiB
TypeScript
88 wiersze
3.1 KiB
TypeScript
import { customElement, property, query, state } from 'lit/decorators.js';
|
|
import { html } from 'lit';
|
|
import { LocalizeController } from '../../utilities/localize';
|
|
import ShoelaceElement from '../../internal/shoelace-element';
|
|
import styles from './progress-ring.styles';
|
|
import type { CSSResultGroup } from 'lit';
|
|
|
|
/**
|
|
* @summary Progress rings are used to show the progress of a determinate operation in a circular fashion.
|
|
* @documentation https://shoelace.style/components/progress-ring
|
|
* @status stable
|
|
* @since 2.0
|
|
*
|
|
* @slot - A label to show inside the ring.
|
|
*
|
|
* @csspart base - The component's base wrapper.
|
|
* @csspart label - The progress ring label.
|
|
*
|
|
* @cssproperty --size - The diameter of the progress ring (cannot be a percentage).
|
|
* @cssproperty --track-width - The width of the track.
|
|
* @cssproperty --track-color - The color of the track.
|
|
* @cssproperty --indicator-width - The width of the indicator. Defaults to the track width.
|
|
* @cssproperty --indicator-color - The color of the indicator.
|
|
* @cssproperty --indicator-transition-duration - The duration of the indicator's transition when the value changes.
|
|
*/
|
|
@customElement('sl-progress-ring')
|
|
export default class SlProgressRing extends ShoelaceElement {
|
|
static styles: CSSResultGroup = styles;
|
|
|
|
private readonly localize = new LocalizeController(this);
|
|
|
|
@query('.progress-ring__indicator') indicator: SVGCircleElement;
|
|
|
|
@state() indicatorOffset: string;
|
|
|
|
/** The current progress as a percentage, 0 to 100. */
|
|
@property({ type: Number, reflect: true }) value = 0;
|
|
|
|
/** A custom label for assistive devices. */
|
|
@property() label = '';
|
|
|
|
updated(changedProps: Map<string, unknown>) {
|
|
super.updated(changedProps);
|
|
|
|
//
|
|
// This block is only required for Safari because it doesn't transition the circle when the custom properties
|
|
// change, possibly because of a mix of pixel + unit-less values in the calc() function. It seems like a Safari bug,
|
|
// but I couldn't pinpoint it so this works around the problem.
|
|
//
|
|
if (changedProps.has('value')) {
|
|
const radius = parseFloat(getComputedStyle(this.indicator).getPropertyValue('r'));
|
|
const circumference = 2 * Math.PI * radius;
|
|
const offset = circumference - (this.value / 100) * circumference;
|
|
|
|
this.indicatorOffset = `${offset}px`;
|
|
}
|
|
}
|
|
|
|
render() {
|
|
return html`
|
|
<div
|
|
part="base"
|
|
class="progress-ring"
|
|
role="progressbar"
|
|
aria-label=${this.label.length > 0 ? this.label : this.localize.term('progress')}
|
|
aria-describedby="label"
|
|
aria-valuemin="0"
|
|
aria-valuemax="100"
|
|
aria-valuenow="${this.value}"
|
|
style="--percentage: ${this.value / 100}"
|
|
>
|
|
<svg class="progress-ring__image">
|
|
<circle class="progress-ring__track"></circle>
|
|
<circle class="progress-ring__indicator" style="stroke-dashoffset: ${this.indicatorOffset}"></circle>
|
|
</svg>
|
|
|
|
<slot id="label" part="label" class="progress-ring__label"></slot>
|
|
</div>
|
|
`;
|
|
}
|
|
}
|
|
|
|
declare global {
|
|
interface HTMLElementTagNameMap {
|
|
'sl-progress-ring': SlProgressRing;
|
|
}
|
|
}
|