-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathmeter-linear.js
139 lines (123 loc) · 3.35 KB
/
meter-linear.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import { css, html, LitElement, nothing } from 'lit';
import { bodySmallStyles } from '../typography/styles.js';
import { classMap } from 'lit/directives/class-map.js';
import { MeterMixin } from './meter-mixin.js';
import { RtlMixin } from '../../mixins/rtl/rtl-mixin.js';
/**
* A horizontal progress bar.
*/
class MeterLinear extends MeterMixin(RtlMixin(LitElement)) {
static get properties() {
return {
/**
* Keeps the meter to a single line
* @type {boolean}
*/
textInline: { type: Boolean, attribute: 'text-inline', reflect: true }
};
}
static get styles() {
return [bodySmallStyles, css`
:host {
display: block;
position: relative;
}
:host > div {
display: flex;
flex-direction: column;
gap: 0.45rem;
}
:host([text-inline]) > div {
align-items: center;
flex-direction: row;
}
.d2l-meter-linear-full-bar,
.d2l-meter-linear-inner-bar {
border-radius: 0.225rem;
flex-grow: 1;
flex-shrink: 1;
height: 0.45rem;
}
.d2l-meter-linear-full-bar {
background-color: var(--d2l-color-gypsum);
position: relative;
}
:host([foreground-light]) .d2l-meter-linear-full-bar {
background-color: rgba(255, 255, 255, 0.5);
}
.d2l-meter-linear-inner-bar {
background-color: var(--d2l-color-celestine);
inset-inline-start: 0;
max-width: 100%;
position: absolute;
top: 0;
}
:host([foreground-light]) .d2l-meter-linear-inner-bar {
background-color: white;
}
.d2l-meter-linear-text {
color: var(--d2l-color-ferrite);
display: flex;
flex-direction: row;
gap: 0.45rem;
line-height: 1em;
width: 100%;
}
:host([foreground-light]) .d2l-meter-linear-text {
color: white;
}
:host([text-inline]) .d2l-meter-linear-text {
width: auto;
}
.d2l-meter-linear-text-space-between {
justify-content: space-between;
}
.d2l-meter-linear-secondary {
align-self: flex-end;
}
:host([dir="rtl"]) .d2l-meter-linear-primary-ltr {
direction: ltr;
}
`];
}
constructor() {
super();
this.textInline = false;
}
render() {
let percentage = this.max > 0 ? this.value / this.max * 100 : 0;
if (percentage < 0.5) {
percentage = 0;
}
const primary = this._primary(this.value, this.max);
const secondary = this._secondary(this.value, this.max, this.text);
const primaryAria = this._primary(this.value, this.max, true);
const secondaryAria = this._secondary(this.value, this.max, this.text, true);
const textClasses = {
'd2l-meter-linear-text-space-between': !this.textInline && secondary !== this.text,
'd2l-body-small': true,
'd2l-meter-linear-text': true
};
const primaryTextClasses = {
'd2l-meter-linear-primary-ltr': !this.percent,
'd2l-meter-linear-primary': true
};
const secondaryTextElement = secondary && !this.textHidden
? html`<div class="d2l-meter-linear-secondary">${secondary}</div>`
: nothing;
return html `
<div
role="img"
aria-label="${this._ariaLabel(primaryAria, secondaryAria)}">
<div class="d2l-meter-linear-full-bar">
<div class="d2l-meter-linear-inner-bar" style="width:${percentage}%;"></div>
</div>
<div class=${classMap(textClasses)}>
<div class=${classMap(primaryTextClasses)}>${primary}</div>
${secondaryTextElement}
</div>
</div>
`;
}
}
customElements.define('d2l-meter-linear', MeterLinear);