Skip to content
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

fix(scrim): Responsively set the scale of the loading spinner #7182

Merged
merged 5 commits into from
Jun 17, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions packages/calcite-components/src/components/scrim/resources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,8 @@ export const CSS = {
scrim: "scrim",
content: "content"
};

export const BREAKPOINTS = {
s: 72,
l: 480
driskull marked this conversation as resolved.
Show resolved Hide resolved
};
63 changes: 62 additions & 1 deletion packages/calcite-components/src/components/scrim/scrim.e2e.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { newE2EPage } from "@stencil/core/testing";
import { accessible, defaults, hidden, renders, t9n } from "../../tests/commonTests";
import { CSS } from "./resources";
import { BREAKPOINTS, CSS } from "./resources";
import { html } from "../../../support/formatting";
import { Scale } from "../interfaces";

describe("calcite-scrim", () => {
describe("renders", () => {
Expand Down Expand Up @@ -107,6 +109,65 @@ describe("calcite-scrim", () => {
expect(contentNode).not.toBeNull();
});

describe("Responsive loading spinner", () => {
const testValues: { width: number; height: number; scale: Scale }[] = [
{
width: BREAKPOINTS.s - 1,
height: 800,
scale: "s"
},
{
width: 800,
height: BREAKPOINTS.s - 1,
scale: "s"
},
{
width: BREAKPOINTS.l - 1,
height: 800,
scale: "m"
},
{
width: 800,
height: BREAKPOINTS.l - 1,
scale: "m"
},
{
width: BREAKPOINTS.l,
height: 800,
scale: "l"
},
{
width: 800,
height: BREAKPOINTS.l,
scale: "l"
}
];

testValues.forEach((scaleSize) => {
it(`should have a scale="${scaleSize.scale}" loading spinner`, async () => {
const page = await newE2EPage();
await page.setContent(html`<style>
.scrim-container {
position: relative;
overflow: auto;
width: ${scaleSize.width}px;
height: ${scaleSize.height}px;
}
</style>
<div class="scrim-container">
<calcite-scrim loading><p>I'm a panel that is loading.</p></calcite-scrim>
</div>`);
await page.waitForChanges();

const loader = await page.find("calcite-scrim >>> calcite-loader");

expect(loader).toBeDefined();
expect(await loader.isVisible()).toBe(true);
expect(await loader.getProperty("scale")).toBe(scaleSize.scale);
});
});
});

describe("CSS properties for light/dark modes", () => {
const scrimSnippet = `
<div style="position: relative; width: 200px; height: 200px; overflow: auto;">
Expand Down
49 changes: 42 additions & 7 deletions packages/calcite-components/src/components/scrim/scrim.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import {
updateMessages
} from "../../utils/t9n";
import { ScrimMessages } from "./assets/scrim/t9n";
import { CSS } from "./resources";
import { CSS, BREAKPOINTS } from "./resources";
import { createObserver } from "../../utils/observers";
import { Scale } from "../interfaces";

/**
* @slot - A slot for adding custom content, primarily loading information.
Expand Down Expand Up @@ -58,11 +60,11 @@ export class Scrim implements LocalizedComponent, T9nComponent {

@Element() el: HTMLCalciteScrimElement;

// --------------------------------------------------------------------------
//
driskull marked this conversation as resolved.
Show resolved Hide resolved
// Private State / Properties
//
// --------------------------------------------------------------------------
resizeObserver = createObserver("resize", () => this.handleResize());

loaderEl: HTMLCalciteLoaderElement;

@State() loaderScale: Scale;

@State() defaultMessages: ScrimMessages;

Expand Down Expand Up @@ -102,7 +104,9 @@ export class Scrim implements LocalizedComponent, T9nComponent {
render(): VNode {
const { el, loading, messages } = this;
const hasContent = el.innerHTML.trim().length > 0;
const loaderNode = loading ? <calcite-loader label={messages.loading} /> : null;
const loaderNode = loading ? (
<calcite-loader label={messages.loading} ref={this.storeLoaderEl} scale={this.loaderScale} />
) : null;
const contentNode = hasContent ? (
<div class={CSS.content}>
<slot />
Expand All @@ -116,4 +120,35 @@ export class Scrim implements LocalizedComponent, T9nComponent {
</div>
);
}

// --------------------------------------------------------------------------
//
// Private Methods
//
// --------------------------------------------------------------------------

private storeLoaderEl = (el: HTMLCalciteLoaderElement): void => {
this.loaderEl = el;
this.handleResize();
};

private getScale(size: number): Scale {
if (size < BREAKPOINTS.s) {
return "s";
} else if (size >= BREAKPOINTS.l) {
return "l";
} else {
return "m";
}
}

private handleResize = (): void => {
driskull marked this conversation as resolved.
Show resolved Hide resolved
const { loaderEl, el } = this;

if (!loaderEl) {
return;
}

this.loaderScale = this.getScale(Math.min(el.clientHeight, el.clientWidth) ?? 0);
};
}