mirror of
https://github.com/home-assistant/frontend.git
synced 2026-11-23 13:18:54 +00:00
Compare commits
3
Commits
dev
...
skeletons/wave
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b50fbc985c | ||
|
|
3eb346abab | ||
|
|
26c6ee9afd |
No files matched your search
@@ -99,6 +99,8 @@ Pick the indicator by what is loading, following the [Loading pattern](https://c
|
||||
- Use `ha-spinner` only when a whole area loads and its shape is unknown.
|
||||
- Use `ha-button` with `loading`, or `ha-progress-button`, for an action started from a button. Don't put a separate spinner next to it.
|
||||
|
||||
Every skeleton uses the same soft wave (`ha-skeleton`'s default `effect="sheen"`). It's one slanted wave across all the skeletons on the page, from the top corner of the content on the side text starts from to the opposite bottom corner, which each skeleton shows its slice of, on one shared clock. It flows from one skeleton into the next, starts its next sweep as soon as it leaves the content, and a skeleton added mid-sweep joins in rather than starting over. Don't change the effect or speed (`--ha-skeleton-duration`) for one component.
|
||||
|
||||
Wrap the indicator in `ha-fade-in` from `src/components/animation/ha-fade-in.ts`:
|
||||
|
||||
- Fade a skeleton in straight away over about 300ms (`.duration=${300}`), as the data table does. It stands in for the content, so a delay would leave an empty gap.
|
||||
|
||||
@@ -5,8 +5,9 @@ import { HaSkeleton } from "./ha-skeleton";
|
||||
|
||||
/**
|
||||
* Placeholder for a line of text, matching the tile secondary text skeleton.
|
||||
* Its height follows the surrounding font size. Set `width` in CSS to fit the
|
||||
* expected text.
|
||||
* It takes the height of the surrounding line, so swapping it for the text
|
||||
* doesn't move anything, and draws a thinner bar in the middle of it. Set
|
||||
* `width` in CSS to fit the expected text.
|
||||
*
|
||||
* @cssprop --ha-skeleton-text-width - The width of the placeholder. Defaults to `140px`.
|
||||
*/
|
||||
@@ -19,11 +20,16 @@ export class HaSkeletonText extends HaSkeleton {
|
||||
:host {
|
||||
display: inline-flex;
|
||||
vertical-align: middle;
|
||||
align-items: center;
|
||||
width: var(--ha-skeleton-text-width, 140px);
|
||||
max-width: 100%;
|
||||
height: 1em;
|
||||
height: 1.2em;
|
||||
height: 1lh;
|
||||
min-height: 0;
|
||||
}
|
||||
.indicator {
|
||||
height: 0.75em;
|
||||
}
|
||||
`,
|
||||
];
|
||||
}
|
||||
|
||||
@@ -1,19 +1,152 @@
|
||||
import WaSkeleton from "@home-assistant/webawesome/dist/components/skeleton/skeleton";
|
||||
import type { CSSResultGroup } from "lit";
|
||||
import { css } from "lit";
|
||||
import { customElement } from "lit/decorators";
|
||||
import type { CSSResultGroup, PropertyValues } from "lit";
|
||||
import { css, html } from "lit";
|
||||
import { customElement, query } from "lit/decorators";
|
||||
|
||||
/* How far the wave leans from upright, so it starts at the top corner of the
|
||||
content and finishes at the opposite bottom corner */
|
||||
const WAVE_SLANT_DEG = 20;
|
||||
|
||||
const WAVE_SLANT = Math.tan((WAVE_SLANT_DEG * Math.PI) / 180);
|
||||
|
||||
/**
|
||||
* Placeholder shown while content loads. Sized by its container unless a
|
||||
* more specific variant such as `ha-skeleton-text` or `ha-skeleton-icon` is used.
|
||||
*
|
||||
* A soft wave sweeps across it by default. It's one slanted wave across all
|
||||
* the skeletons on the page, from the top corner of the content on the side
|
||||
* text starts from to the opposite bottom corner: each skeleton shows its
|
||||
* own slice of it, and all of them run in step, so the wave flows from one
|
||||
* skeleton into the next, and a skeleton added later joins mid-sweep instead
|
||||
* of starting over. Each sweep starts as the last one leaves the content.
|
||||
*
|
||||
* @cssprop --ha-skeleton-color - The fill color. Defaults to `var(--ha-color-fill-neutral-normal-resting)`.
|
||||
* @cssprop --ha-skeleton-sheen-color - The sheen color when `effect="sheen"`. Defaults to `var(--ha-color-fill-neutral-loud-resting)`.
|
||||
* @cssprop --ha-skeleton-sheen-color - The sheen color when `effect="sheen"`. Defaults to `var(--ha-color-fill-neutral-normal-hover)`.
|
||||
* @cssprop --ha-skeleton-border-radius - The corner radius. Defaults to `var(--ha-border-radius-sm)`.
|
||||
* @cssprop --ha-skeleton-duration - How long one sweep across the skeletons takes when `effect="sheen"`. Defaults to `3s`.
|
||||
*/
|
||||
@customElement("ha-skeleton")
|
||||
export class HaSkeleton extends WaSkeleton {
|
||||
override effect: WaSkeleton["effect"] = "pulse";
|
||||
override effect: WaSkeleton["effect"] = "sheen";
|
||||
|
||||
/* Every skeleton showing the wave, which share one track */
|
||||
private static _waving = new Set<HaSkeleton>();
|
||||
|
||||
private static _trackFrame?: number;
|
||||
|
||||
@query(".wave") private _wave?: HTMLDivElement;
|
||||
|
||||
private _resizeObserver?: ResizeObserver;
|
||||
|
||||
/**
|
||||
* Lay the one track the wave runs along over the content: the box around
|
||||
* all the skeletons, so no time goes on empty space around them. The wave
|
||||
* leans, so it enters at the box's top corner and has to travel further,
|
||||
* by the box's height times the slant, to clear the opposite bottom
|
||||
* corner. It's measured again whenever a skeleton comes, goes or changes
|
||||
* size, and at the end of each sweep, so it follows the layout as it moves.
|
||||
*/
|
||||
private static _scheduleTrack() {
|
||||
if (HaSkeleton._trackFrame !== undefined) {
|
||||
return;
|
||||
}
|
||||
|
||||
HaSkeleton._trackFrame = requestAnimationFrame(() => {
|
||||
HaSkeleton._trackFrame = undefined;
|
||||
|
||||
// Read every skeleton before writing any, so layout runs once
|
||||
const boxes = [...HaSkeleton._waving].flatMap((skeleton) => {
|
||||
const rect = skeleton.getBoundingClientRect();
|
||||
const rtl = getComputedStyle(skeleton).direction === "rtl";
|
||||
|
||||
return rect.width > 0 ? [{ skeleton, rect, rtl }] : [];
|
||||
});
|
||||
|
||||
if (boxes.length === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
const left = Math.min(...boxes.map(({ rect }) => rect.left));
|
||||
const right = Math.max(...boxes.map(({ rect }) => rect.right));
|
||||
const top = Math.min(...boxes.map(({ rect }) => rect.top));
|
||||
const bottom = Math.max(...boxes.map(({ rect }) => rect.bottom));
|
||||
const trackLength = right - left + (bottom - top) * WAVE_SLANT;
|
||||
|
||||
boxes.forEach(({ skeleton, rect, rtl }) => {
|
||||
// Right to left, the wave is drawn mirrored, so it's placed from the
|
||||
// right edge instead
|
||||
const start = rtl ? rect.right - right : left - rect.left;
|
||||
|
||||
// Lower down, the slanted wave arrives later
|
||||
skeleton._placeWave(start - (rect.top - top) * WAVE_SLANT, trackLength);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
public connectedCallback() {
|
||||
super.connectedCallback();
|
||||
this._resizeObserver ??= new ResizeObserver(() =>
|
||||
HaSkeleton._scheduleTrack()
|
||||
);
|
||||
this._resizeObserver.observe(this);
|
||||
|
||||
if (this.effect === "sheen") {
|
||||
HaSkeleton._waving.add(this);
|
||||
HaSkeleton._scheduleTrack();
|
||||
}
|
||||
}
|
||||
|
||||
public disconnectedCallback() {
|
||||
super.disconnectedCallback();
|
||||
this._resizeObserver?.disconnect();
|
||||
HaSkeleton._waving.delete(this);
|
||||
HaSkeleton._scheduleTrack();
|
||||
}
|
||||
|
||||
public override render() {
|
||||
return html`
|
||||
<div part="indicator" class="indicator">
|
||||
<div
|
||||
class="wave"
|
||||
@animationiteration=${HaSkeleton._scheduleTrack}
|
||||
></div>
|
||||
</div>
|
||||
`;
|
||||
}
|
||||
|
||||
protected updated(changedProperties: PropertyValues<this>) {
|
||||
super.updated(changedProperties);
|
||||
|
||||
if (changedProperties.has("effect")) {
|
||||
if (this.effect === "sheen") {
|
||||
HaSkeleton._waving.add(this);
|
||||
} else {
|
||||
HaSkeleton._waving.delete(this);
|
||||
}
|
||||
|
||||
HaSkeleton._scheduleTrack();
|
||||
}
|
||||
}
|
||||
|
||||
/* Put this skeleton's slice of the wave in its place on the track */
|
||||
private _placeWave(trackStart: number, trackLength: number) {
|
||||
const wave = this._wave;
|
||||
|
||||
if (!wave) {
|
||||
return;
|
||||
}
|
||||
|
||||
wave.style.left = `${trackStart}px`;
|
||||
wave.style.setProperty("--ha-skeleton-track-width", `${trackLength}px`);
|
||||
wave.style.setProperty("--ha-skeleton-slant", `${-WAVE_SLANT_DEG}deg`);
|
||||
|
||||
// All waves count from the same moment, so they stay in step
|
||||
wave.getAnimations().forEach((animation) => {
|
||||
if (animation.startTime !== 0) {
|
||||
animation.startTime = 0;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
static get styles(): CSSResultGroup {
|
||||
return [
|
||||
@@ -26,13 +159,59 @@ export class HaSkeleton extends WaSkeleton {
|
||||
);
|
||||
--sheen-color: var(
|
||||
--ha-skeleton-sheen-color,
|
||||
var(--ha-color-fill-neutral-loud-resting)
|
||||
var(--ha-color-fill-neutral-normal-hover)
|
||||
);
|
||||
--wa-border-radius-pill: var(
|
||||
--ha-skeleton-border-radius,
|
||||
var(--ha-border-radius-sm)
|
||||
);
|
||||
}
|
||||
.wave {
|
||||
display: none;
|
||||
}
|
||||
:host([effect="sheen"]) .indicator {
|
||||
position: relative;
|
||||
overflow: hidden;
|
||||
background: var(--color);
|
||||
animation: none;
|
||||
}
|
||||
/* Right to left, the wave is drawn mirrored, so it starts from the
|
||||
top right corner */
|
||||
:host([effect="sheen"]:dir(rtl)) .indicator {
|
||||
transform: scaleX(-1);
|
||||
}
|
||||
/* The wave crosses the track over all the skeletons, placed from its
|
||||
start and leaning back from its top edge, and moves with transform
|
||||
so it stays cheap */
|
||||
:host([effect="sheen"]) .wave {
|
||||
display: block;
|
||||
position: absolute;
|
||||
top: 0;
|
||||
bottom: 0;
|
||||
left: 0;
|
||||
width: 400px;
|
||||
background: linear-gradient(
|
||||
90deg,
|
||||
transparent,
|
||||
var(--sheen-color),
|
||||
transparent
|
||||
);
|
||||
transform-origin: top left;
|
||||
transform: translateX(-100%) skewX(var(--ha-skeleton-slant, 0deg));
|
||||
/* An even pace with no hold, so the next sweep enters as soon as
|
||||
the last one leaves */
|
||||
animation: ha-skeleton-sheen var(--ha-skeleton-duration, 3s) linear
|
||||
infinite;
|
||||
}
|
||||
@keyframes ha-skeleton-sheen {
|
||||
from {
|
||||
transform: translateX(-100%) skewX(var(--ha-skeleton-slant, 0deg));
|
||||
}
|
||||
to {
|
||||
transform: translateX(var(--ha-skeleton-track-width, 100vw))
|
||||
skewX(var(--ha-skeleton-slant, 0deg));
|
||||
}
|
||||
}
|
||||
@media (forced-colors: active) {
|
||||
:host {
|
||||
--color: GrayText;
|
||||
@@ -40,9 +219,12 @@ export class HaSkeleton extends WaSkeleton {
|
||||
}
|
||||
@media (prefers-reduced-motion: reduce) {
|
||||
:host([effect="pulse"]) .indicator,
|
||||
:host([effect="sheen"]) .indicator {
|
||||
:host([effect="sheen"]) .wave {
|
||||
animation: none;
|
||||
}
|
||||
:host([effect="sheen"]) .wave {
|
||||
display: none;
|
||||
}
|
||||
}
|
||||
`,
|
||||
];
|
||||
|
||||
Reference in new issue
Block a user