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(slider): prevent excessive tick rendering #7421

Merged
merged 4 commits into from
Aug 3, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ export const CSS = {
tickMin: "tick__label--min",
tickMax: "tick__label--max",
};

export const maxTickElementThreshold = 250;
Original file line number Diff line number Diff line change
Expand Up @@ -393,3 +393,18 @@ export const WithLargeFontSize_TestOnly = (): string => html`<html lang="en">
</div>
</body>
</html>`;

export const maxTickRendering_TestOnly = (): string => html`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

<calcite-slider min="-100" max="100" ticks="1"></calcite-slider>
<calcite-slider min="-100" max="100" ticks="5"></calcite-slider>
<calcite-slider min="-100" max="100" ticks="10"></calcite-slider>
<calcite-slider min="-250" max="250" ticks="1"></calcite-slider>
<calcite-slider min="-250" max="250" ticks="5"></calcite-slider>
<calcite-slider min="-250" max="250" ticks="10"></calcite-slider>
<calcite-slider min="-500" max="500" ticks="1"></calcite-slider>
<calcite-slider min="-500" max="500" ticks="5"></calcite-slider>
<calcite-slider min="-500" max="500" ticks="10"></calcite-slider>
<calcite-slider min="-1000" max="1000" ticks="1"></calcite-slider>
<calcite-slider min="-1000" max="1000" ticks="5"></calcite-slider>
<calcite-slider min="-1000" max="1000" ticks="10"></calcite-slider>
`;
19 changes: 14 additions & 5 deletions packages/calcite-components/src/components/slider/slider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import {
updateHostInteraction,
} from "../../utils/interactive";
import { isActivationKey } from "../../utils/key";
import { connectLabel, disconnectLabel, LabelableComponent, getLabelText } from "../../utils/label";
import { connectLabel, disconnectLabel, getLabelText, LabelableComponent } from "../../utils/label";
import {
componentFocusable,
LoadableComponent,
Expand All @@ -46,7 +46,7 @@ import {
import { clamp, decimalPlaces } from "../../utils/math";
import { ColorStop, DataSeries } from "../graph/interfaces";
import { Scale } from "../interfaces";
import { CSS } from "./resources";
import { CSS, maxTickElementThreshold } from "./resources";

type ActiveSliderProperty = "minValue" | "maxValue" | "value" | "minMaxValue";
type SetValueProperty = Exclude<ActiveSliderProperty, "minMaxValue">;
Expand Down Expand Up @@ -1031,13 +1031,22 @@ export class Slider
);
}

private getTickRatio(): number {
const totalTicks = (this.max - this.min) / this.ticks;
return totalTicks / maxTickElementThreshold;
}

private generateTickValues(): number[] {
const ticks = [];
let current = this.min;
while (this.ticks && current < this.max + this.ticks) {
const ratio = this.getTickRatio();
const effectiveRatio = ratio < 1 ? 1 : ratio;
let current = this.min * effectiveRatio;

while (this.ticks > 0 && current < this.max + this.ticks) {
ticks.push(Math.min(current, this.max));
current = current + this.ticks;
current += this.ticks * effectiveRatio;
}

return ticks;
}

Expand Down