Skip to content

Commit

Permalink
Fixed buggy auto-sizing labels
Browse files Browse the repository at this point in the history
Rewrote the hacky programmatic sizing. Replaced with simply programmatically setting known stepper width and step count, and let CSS use `calc()` to figure out stepper control widths and step title widths
  • Loading branch information
torrobinson committed Dec 20, 2021
1 parent 5824b3f commit c8bd4e6
Showing 1 changed file with 28 additions and 37 deletions.
65 changes: 28 additions & 37 deletions src/progress-steps.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,17 +77,17 @@ class ProgressSteps extends HTMLElement {
/* Labels */
--step-title-display: inline-block;
--step-title-top-padding: 5px;
--step-title-width: 75px; /* Note: this is overriden immedidately and when browser is resized */
--step-title-font: sans-serif;
--step-title-weight: normal;
display: flex;
margin: 0 auto;
position: relative;
width: calc(100% - (var(--step-title-width)-var(--step-width))/1);
transition: width var(--animation-speed);
justify-content: space-between;
/* Dynamically set our width via the 2 'known' attributes updated on resizes and render */
width: calc(100% - (var(--known-available-width) / (var(--known-step-count) - 1)) * 1px);
}
/* The underlying grey line*/
Expand Down Expand Up @@ -193,7 +193,6 @@ class ProgressSteps extends HTMLElement {
position: absolute;
text-align: center;
top: calc( var(--step-width) + var(--step-title-top-padding));
width: var(--step-title-width);
font-family: var(--step-title-font);
color: var(--unfilled-color);
font-size: var(--font-size);
Expand All @@ -202,10 +201,15 @@ class ProgressSteps extends HTMLElement {
overflow: hidden;
text-overflow: ellipsis;
user-select: none;
}
.progress-steps .progress-step .progress-title {
font-weight: var(--step-title-weight);
/* Dynamically set our own title width via the 2 'known' attributes updated on resizes and render */
--title-clearance: 8px;
width: calc(
var(--known-available-width) / (var(--known-step-count) - 1) /* width per segment */
* 1px /* Cast to pixels */
- (var(--title-clearance) * 2) /* Account for clearance */
);
}
.progress-steps .progress-step.previous .progress-title {
Expand Down Expand Up @@ -235,46 +239,33 @@ class ProgressSteps extends HTMLElement {
// Set up auto resizing
let self = this;

// Handle debouncing window resizes to resize labels
this._recalculateStepperTitleWidths(self);
// Handle debouncing window resizes to resize labels & control
var stepperResizeDebounce;
let windowResizeDebounceIntervalMilliseconds = 300;
let debounceResizeFunction = function () {
let windowResizeDebounceIntervalMilliseconds = 100; /* To prevent slowdown, only look for window size changes 10 times a second */
this._updateKnownControlSize = function () {
clearTimeout(stepperResizeDebounce);
stepperResizeDebounce = setTimeout(function () {
self._recalculateStepperTitleWidths(self);
if (self._options === null || self._options.steps === null)
return;
self._container.style.setProperty(
'--known-step-count',
self._options.steps.length
);
self._container.style.setProperty(
'--known-available-width',
self.getBoundingClientRect().width
);
}, windowResizeDebounceIntervalMilliseconds);
};
window.addEventListener('resize', debounceResizeFunction, false);
this._updateKnownControlSize();
window.addEventListener('resize', this._updateKnownControlSize, false);
window.addEventListener(
'orientationchange',
debounceResizeFunction,
this._updateKnownControlSize,
false
);
}

// Recalculates the title widths to best fit and prevent overlap
_recalculateStepperTitleWidths(selfContext) {
// If we have steps
if (selfContext._options?.steps.length > 0) {
// Find our (potentially) new width
let padding = 3;
let totalWidth =
selfContext._container.getBoundingClientRect().width;

// Find the available space for each step, minus padding on either side
let widthPerSegment =
totalWidth / (selfContext._options.steps.length - 1) -
padding * 2;

// Set the widths of the titles via css
selfContext._container.style.setProperty(
'--step-title-width',
`${widthPerSegment}px`
);
}
}

// Sets options
init(options) {
// Overlay default options on user options
Expand Down Expand Up @@ -456,7 +447,7 @@ class ProgressSteps extends HTMLElement {

if (this !== undefined) this._setStepInternal();

this._recalculateStepperTitleWidths(self);
setTimeout(this._updateKnownControlSize, 100);
}

// Functions
Expand Down

0 comments on commit c8bd4e6

Please sign in to comment.