Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
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,24 @@ export const WithLargeFontSize_TestOnly = (): string => html`<html lang="en">
</div>
</body>
</html>`;

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

Choose a reason for hiding this comment

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

<style>
calcite-slider {
width: 60vw;
}
</style>

<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>
`;
30 changes: 25 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,33 @@ export class Slider
);
}

private getTickDensity(): number {
const density = (this.max - this.min) / this.ticks / maxTickElementThreshold;

return density < 1 ? 1 : density;
}

private generateTickValues(): number[] {
const ticks = [];
const tickInterval = this.ticks ?? 0;

if (tickInterval <= 0) {
return [];
}

const ticks: number[] = [this.min];
const density = this.getTickDensity();
const tickOffset = tickInterval * density;
let current = this.min;
while (this.ticks && current < this.max + this.ticks) {

while (current < this.max) {
current += tickOffset;
ticks.push(Math.min(current, this.max));
current = current + this.ticks;
}

if (!ticks.includes(this.max)) {
ticks.push(this.max);
}

return ticks;
}

Expand Down