Skip to content

Commit f727065

Browse files
fix: added resize observer for lining up progress bar (#1763)
* feat: added resize observer for lining up progress bar fixes #1710 * chore: update tests * chore: update changelog * feat: added resize observer for the element level * fix: add checks for ie11 with resize observer Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
1 parent 5924c35 commit f727065

File tree

4 files changed

+62
-5
lines changed

4 files changed

+62
-5
lines changed

CHANGELOG-1.x.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
- [b691da0](https://github.com/patternfly/patternfly-elements/commit/b691da07c4f3e150337698044369e64b9d2ad849) docs: Adjust example code block for typography classes
55
- [974ab6f](https://github.com/patternfly/patternfly-elements/commit/974ab6f1ab4047d4e94007d64a31e5a0cddf9b7a) fix: typos in package.json files
66
- [b18dd64](https://github.com/patternfly/patternfly-elements/commit/b18dd64da950b580b526ebe092b59eaadaf7d07e) fix: fix for re-renders when there is no selectedindex
7+
- [](https://github.com/patternfly/patternfly-elements/commit/) fix: added resize observer for lining up progress bar
78

89
# 1.11.0 (2021-08-18)
910

elements/pfe-progress-steps/src/pfe-progress-steps-item.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ $LOCAL-VARIABLES: map-deep-merge($LOCAL-VARIABLES, (
4141
$LOCAL: progress-steps;
4242
:host(:not([vertical])) {
4343
width: pfe-local(size, $region: item);
44-
min-width: fit-content;
44+
text-align: center;
4545
}
4646
:host([vertical]:not(:last-child)) {
4747
min-height: pfe-local(size, $region: item);

elements/pfe-progress-steps/src/pfe-progress-steps.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,25 @@ class PfeProgressSteps extends PFElement {
5353

5454
constructor() {
5555
super(PfeProgressSteps, { type: PfeProgressSteps.PfeType });
56+
if (!this.isIE11) this._resizeObserver = new ResizeObserver(this._build.bind(this));
5657
}
5758

5859
connectedCallback() {
5960
super.connectedCallback();
60-
this._build();
61+
if (this._resizeObserver) {
62+
// this will call _build initially and estabilish a resize observer for this element
63+
this._resizeObserver.observe(this);
64+
} else {
65+
// if we don't have access to resize observer then
66+
// manually call build.
67+
this._build();
68+
}
6169
}
6270

63-
disconnectedCallback() {}
71+
disconnectedCallback() {
72+
super.disconnectedCallback();
73+
if (this._resizeObserver) this._resizeObserver.disconnect();
74+
}
6475

6576
_build() {
6677
if (this.isIE11) return;

elements/pfe-progress-steps/test/pfe-progress-steps.spec.js

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,12 @@ import { createFixture } from '../../../test/utils/create-fixture.js';
99
// Import the element we're testing.
1010
import '../dist/pfe-progress-steps';
1111

12-
1312
const itemTemplate = (title = "", description = "", state = "", current = false) => `
1413
<pfe-progress-steps-item${state ? ` state="${state}"` : ""}${current ? ` current` : ""}>
1514
${title ? `<span slot="title">${title}</span>` : ""}
1615
${description ? `<span slot="description">${description}</span>` : ""}
1716
</pfe-progress-steps-item>`;
1817

19-
2018
// One element, defined here, is used
2119
// in multiple tests. It's torn down and recreated each time.
2220
const element =
@@ -81,4 +79,51 @@ describe("<pfe-progress-steps>", () => {
8179
const description = item.querySelector("[slot=description]");
8280
expect(description.textContent).to.equal("View first step");
8381
});
82+
83+
describe("<pfe-progress-steps-item>", () => {
84+
it("should be horizontal by default", async () => {
85+
const el = await createFixture(element);
86+
// get an array of the positions of each item
87+
const items = [...el.querySelectorAll("pfe-progress-steps-item")]
88+
.map(i => i.offsetTop);
89+
// see if these positions are stacked on top of one another
90+
// we use every so we can exit early.
91+
const isEven = items.every((i, index) => {
92+
// if there is a next item to work with
93+
if (typeof items[index + 1] === "undefined") return true;
94+
// check if the item offsets are equal to see if it's a row
95+
return items[index + 1] === items[index];
96+
});
97+
expect(isEven).to.be.true;
98+
});
99+
100+
it("should have a stacked layout on vertical", async () => {
101+
const el = await createFixture(element);
102+
el.setAttribute("vertical", "");
103+
// get an array of the positions of each item
104+
const items = [...el.querySelectorAll("pfe-progress-steps-item")]
105+
.map(i => i.offsetTop);
106+
// see if these positions are stacked on top of one another
107+
// we use every so we can exit early.
108+
const isStacked = items.every((i, index) => {
109+
// if there is a next item to work with
110+
if (typeof items[index + 1] === "undefined") return true;
111+
// if the next item offset is greater than the current then it's stacked
112+
return items[index + 1] >= items[index];
113+
});
114+
expect(isStacked).to.be.true;
115+
});
116+
})
117+
118+
describe("progress bar", () => {
119+
it("should have a length that spans from the middle of the first item to the middle of the last item", async () => {
120+
const el = await createFixture(element);
121+
const stepItems = [...el.querySelectorAll("pfe-progress-steps-item")];
122+
// get the centerpoint of the items
123+
let firstItemMidpoint = stepItems[0].offsetLeft + stepItems[0].offsetWidth / 2;
124+
let lastItemMidpoint = stepItems[stepItems.length - 1].offsetLeft + (stepItems[stepItems.length - 2].offsetWidth / 2);
125+
expect(el.shadowRoot.querySelector(".pfe-progress-steps__progress-bar").offsetWidth).to.equal(lastItemMidpoint - firstItemMidpoint);
126+
});
127+
});
128+
84129
});

0 commit comments

Comments
 (0)