Skip to content

Commit 3eecba0

Browse files
Djfaucettestarryeyez024kylebuch8castastrophe
authored
fix: fixed offset to work more reliably and added some docs (#908)
* fixed offset to work more reliably and added some docs * fixed silly mistake * cleaned up a bit * removed duplicated header * changed selector to find the right element * removed unnecessary check * added support for using a css variable for offset * some small performance and best practices fixed, added support for stylesheets to determine offset * updated readme to have more about offset and the new css feature * adding an offset attribute observer * removing some console logs * fixing some if else logic * adding additional tests * adding a couple of enhancements to the demo file * switching from --pfe-jump-links-nav--offset to --pfe-jump-links-panel--offset We agreed that it makes more sense to have the offset on the panel instead of the nav since the panel is watching for scroll events. * Update elements/pfe-jump-links/README.md Co-authored-by: [ Cassondra ] <[email protected]> * Update elements/pfe-jump-links/README.md Co-authored-by: [ Cassondra ] <[email protected]> * Update elements/pfe-jump-links/README.md Co-authored-by: [ Cassondra ] <[email protected]> * Update elements/pfe-jump-links/src/pfe-jump-links.js Co-authored-by: [ Cassondra ] <[email protected]> * Update elements/pfe-jump-links/src/pfe-jump-links.js Co-authored-by: [ Cassondra ] <[email protected]> * Update elements/pfe-jump-links/src/pfe-jump-links.js Co-authored-by: [ Cassondra ] <[email protected]> * Update elements/pfe-jump-links/src/pfe-jump-links.js Co-authored-by: [ Cassondra ] <[email protected]> * Update elements/pfe-jump-links/src/pfe-jump-links.js Co-authored-by: [ Cassondra ] <[email protected]> * updating components with pfe-c prefixes Co-authored-by: Daniel Faucette <[email protected]> Co-authored-by: Kendall Totten <[email protected]> Co-authored-by: Kyle Buchanan <[email protected]> Co-authored-by: [ Cassondra ] <[email protected]>
1 parent 003f4dd commit 3eecba0

File tree

7 files changed

+160
-49
lines changed

7 files changed

+160
-49
lines changed

elements/pfe-jump-links/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
## 1.0.0-prerelease.50 ( TBD )
2+
3+
Tag: [1.0.0-prerelease.50](https://github.com/patternfly/patternfly-elements/releases/tag/1.0.0-prerelease.50)
4+
5+
- [code](url) offset - fixed offset attribute to work more reliably and updated docs
6+
17
## 1.0.0-prerelease.39 ( TBD )
28

39
Tag: [1.0.0-prerelease.39](https://github.com/patternfly/patternfly-elements/releases/tag/1.0.0-prerelease.39)

elements/pfe-jump-links/README.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,20 @@ No extra roles or aria labels are required because we're using standard html tag
6969

7070
## Slots
7171

72-
- `namedSlot`: The only slot this component has is the heading. The rest of the component works by creating a mirror shadowRoot based on the Light DOM markup.
72+
73+
- `pfe-jump-links-nav--heading`: The rest of the component works by creating a mirror shadowRoot based on the Light DOM markup.
74+
7375

7476
## Attributes
7577

76-
- `attr`: autobuild Flips the switch on the component to create its own markup for the navigation. If you use this attribute, then no experience is given for non-JS runtimes and the component will crawl the associated panel to come up with its own list of links.
78+
79+
- `pfe-c-autobuild`: Flips the switch on the component to create its own markup for the navigation. You can add `pfe-jump-links-panel__section` to each section that should show in the nav. If you want to use sub-sections add `has-sub-section` to the parent section that should always show and the `sub-section` class to the children of that section. If you use this attribute, keep in mind that non-JavaScript environments (some search engines, browsers with JS disabled) will not see the proper markup.
80+
81+
- `sr-text`: This attribute is read when the component upgrades to provide the innerText of the heading. If there is no `sr-text` attribute then the component defaults to "JUMP TO SECTION". This attribute is to enable translations and internationalization.
82+
83+
- `pfe-c-offset`: This attribute determines the distance from the top of the browser window to trigger a switch from one link being active to the next. For instance `pfe-c-offset="600"` would mean that threshold flips at 600px from the top. The default is set at 200, and if you desire 200px then you can leave this attribute off. The `pfe-c-offset` attribute should be placed on `pfe-jump-links-panel`. There is a css solution to control the offset, however the attribute value takes precedence over css. To read more about a css solution see below.
84+
85+
- `style="--pfe-jump-links-panel--offset: {integer};"`: You can control offset in your styling layer as well. This value can be set directly on the component inside a style attribute, e.g. `style="--pfe-jump-links-panel--offset: 100;"` or using the appropriate selector in another file. Please note that adding an attribute will take precedence over a css value. At the moment only integer values passed to this key are valid. No other values are supported. This means that passing "300px", "2rem","calc(100% - 12px)" will all result in JavaScript errors. You should pass a number that correlates to pixels. To read about the `pfe-c-offset` attribute, see above.
7786

7887
## Events
7988
This component fires an event when a link is made active.

elements/pfe-jump-links/demo/index.html

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,36 @@ <h1><code>pfe-jump-links</code></h1>
6161
top: 0;
6262
}
6363

64+
.special {
65+
--pfe-jump-links-panel--offset: 200;
66+
}
67+
68+
/*
69+
* Fixing the issue where the headers are under the stick nav
70+
* https://stackoverflow.com/questions/4086107/fixed-page-header-overlaps-in-page-anchors
71+
*/
72+
#horizontalJumpLinks .pfe-jump-links-panel__section::before {
73+
content: "";
74+
display: block;
75+
height: 96px;
76+
margin: -96px 0 0;
77+
}
6478

79+
@media(max-width: 992px) {
80+
/*
81+
* fix for when we have a mobile nav that is sticky
82+
*/
83+
.pfe-jump-links-panel__section::before {
84+
content: "";
85+
display: block;
86+
height: 68px;
87+
margin: -68px 0 0;
88+
}
89+
90+
.special{
91+
--pfe-jump-links-panel--offset: 220;
92+
}
93+
}
6594
</style>
6695

6796
<h2 style="text-align: center; margin: 60px auto;">PFE Jump Links - standard</h2>
@@ -81,7 +110,7 @@ <h4 slot="pfe-jump-links-nav--heading">Jump to section</h4>
81110
</pfe-jump-links-nav>
82111
</div>
83112
<div class="pfe-l-grid__item pfe-m-12-col pfe-m-9-col-on-lg">
84-
<pfe-jump-links-panel scrolltarget="jumplinks1">
113+
<pfe-jump-links-panel class="special" pfe-c-scrolltarget="jumplinks1">
85114
<div>
86115
<h1 class="pfe-jump-links-panel__section" id="section1">Section 1</h1>
87116
<p>Lorem ipsum dolor amet umami vaporware actually church-key keytar, hell of roof party unicorn helvetica
@@ -324,7 +353,7 @@ <h4 slot="pfe-jump-links-nav--heading">Jump to section</h4>
324353
</pfe-jump-links-nav>
325354
</div>
326355
<div class="pfe-l-grid__item pfe-m-12-col pfe-m-9-col-on-lg">
327-
<pfe-jump-links-panel scrolltarget="jumplinks2">
356+
<pfe-jump-links-panel pfe-c-scrolltarget="jumplinks2">
328357
<div>
329358
<h1 class="pfe-jump-links-panel__section" id="section10">Section 1</h1>
330359
<p>Lorem ipsum dolor amet umami vaporware actually church-key keytar, hell of roof party unicorn helvetica
@@ -554,7 +583,7 @@ <h4 slot="pfe-jump-links-nav--heading" id="pfe-jump-links-nav--heading">Jump to
554583
</pfe-jump-links-nav>
555584
</div>
556585
<div class="pfe-l-grid__item pfe-m-12-col pfe-m-9-col-on-lg">
557-
<pfe-jump-links-panel scrolltarget="jumplinks8">
586+
<pfe-jump-links-panel pfe-c-scrolltarget="jumplinks8">
558587
<div>
559588
<h1 class="pfe-jump-links-panel__section" id="section6">Section 6</h1>
560589
<p>Lorem ipsum dolor amet umami vaporware actually church-key keytar, hell of roof party unicorn helvetica
@@ -776,8 +805,8 @@ <h1 class="pfe-jump-links-panel__section" id="section1000">Section 10</h1>
776805
</pfe-jump-links-panel>
777806
</div>
778807
</div>
779-
<div>
780-
<pfe-jump-links-nav horizontal id="jumplinks9" offset="30" sr-text="Page navigation">
808+
<div id="horizontalJumpLinks">
809+
<pfe-jump-links-nav pfe-c-horizontal id="jumplinks9" sr-text="Page navigation">
781810
<h4 slot="pfe-jump-links-nav--heading">Jump to section</h4>
782811
<img src="https://static.redhat.com/libs/redhat/brand-assets/2/corp/logo--200.png" alt="" slot="pfe-jump-links-nav--logo">
783812
<ul>
@@ -790,7 +819,7 @@ <h4 slot="pfe-jump-links-nav--heading">Jump to section</h4>
790819
<pfe-cta pfe-priority="primary" class="PFElement" slot="pfe-jump-links-nav--cta" pfelement="" has_link=""
791820
on="dark"> <a href="https://www.redhat.com#primary">Link</a> </pfe-cta>
792821
</pfe-jump-links-nav>
793-
<pfe-jump-links-panel scrolltarget="jumplinks9" style="margin-bottom: 500px;">
822+
<pfe-jump-links-panel pfe-c-scrolltarget="jumplinks9" style="margin-bottom: 500px;">
794823
<div style="margin-bottom: 500px;">
795824
<h1 class="pfe-jump-links-panel__section" id="section100">Section 1</h1>
796825
<p>Lorem ipsum dolor amet umami vaporware actually church-key keytar, hell of roof party unicorn helvetica lomo
@@ -999,7 +1028,7 @@ <h4 slot="pfe-jump-links-nav--heading">Jump to section</h4>
9991028
</pfe-jump-links-nav>
10001029
</div>
10011030
<div class="pfe-l-grid__item pfe-m-12-col pfe-m-9-col-on-lg">
1002-
<pfe-jump-links-panel scrolltarget="jumplinks1000">
1031+
<pfe-jump-links-panel pfe-c-scrolltarget="jumplinks1000">
10031032
<div>
10041033
<h1 class="pfe-jump-links-panel__section" id="section10000">Section 1</h1>
10051034
<p>Lorem ipsum dolor amet umami vaporware actually church-key keytar, hell of roof party unicorn helvetica

elements/pfe-jump-links/src/pfe-jump-links-nav.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!-- ${this.hasAttribute("horizontal") ? `` :
1+
<!-- ${this.hasAttribute("pfe-c-horizontal") ? `` :
22
`<pfe-accordion>
33
<pfe-accordion-header>
44
</pfe-accordion-header>
@@ -21,13 +21,13 @@ <h2 class="sr-only" hidden>Page navigation</h2>
2121
: ``
2222
}
2323
</nav>
24-
${this.hasAttribute("horizontal") ? `` :
24+
${this.hasAttribute("pfe-c-horizontal") ? `` :
2525
`</pfe-accordion-panel>
2626
</pfe-accordion>`
2727
}
2828
old version^^ -->
2929

30-
${this.hasAttribute("horizontal") ? `` :
30+
${this.hasAttribute("pfe-c-horizontal") ? `` :
3131
`<pfe-accordion>
3232
<pfe-accordion-header>
3333
</pfe-accordion-header>
@@ -40,7 +40,7 @@ <h2 class="sr-only" hidden>Page navigation</h2>
4040
<div id="container"></div>
4141
<slot class="pfe-jump-links-nav__cta" name="pfe-jump-links-nav--cta"></slot>
4242
</nav>
43-
${this.hasAttribute("horizontal") ? `` :
43+
${this.hasAttribute("pfe-c-horizontal") ? `` :
4444
`</pfe-accordion-panel>
4545
</pfe-accordion>`
4646
}

elements/pfe-jump-links/src/pfe-jump-links-nav.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ nav {
284284
display: none;
285285
}
286286

287-
:host([horizontal]) {
287+
:host([pfe-c-horizontal]) {
288288
padding: 0;
289289
top: 0;
290290
width: 100%;

elements/pfe-jump-links/src/pfe-jump-links.js

Lines changed: 56 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,9 @@ class PfeJumpLinksNav extends PFElement {
6969
this._menuContainer = this.shadowRoot.querySelector("#container");
7070
this._observer = new MutationObserver(this._mutationCallback);
7171
this._reportHeight = this._reportHeight.bind(this);
72-
this.panel = document.querySelector(`[scrolltarget=${this.id}]`);
72+
this.panel = document.querySelector(`[pfe-c-scrolltarget=${this.id}]`);
73+
74+
window.addEventListener("resize", () => {});
7375
}
7476

7577
connectedCallback() {
@@ -95,23 +97,22 @@ class PfeJumpLinksNav extends PFElement {
9597
}
9698

9799
let html = "";
98-
if (this.querySelector(".pfe-jump-links-nav--heading")) {
99-
html = this.querySelector(".pfe-jump-links-nav--heading").cloneNode(
100-
true
101-
);
100+
if (this.querySelector("[slot='pfe-jump-links-nav--heading']")) {
101+
html = this.querySelector(
102+
"[slot='pfe-jump-links-nav--heading']"
103+
).cloneNode(true);
102104
}
103-
if (!this.hasAttribute("horizontal")) {
104-
if (html) {
105-
this.shadowRoot
106-
.querySelector("pfe-accordion-header")
107-
.appendChild(html);
108-
}
105+
if (!this.hasAttribute("pfe-c-horizontal") && html !== "") {
106+
this.shadowRoot
107+
.querySelector("pfe-accordion-header")
108+
.appendChild(html);
109109
} else {
110+
const heading = document.createElement("h3");
111+
heading.id = "pfe-jump-links-nav--heading";
112+
110113
this.shadowRoot
111114
.querySelector("pfe-accordion-header")
112-
.appendChild(
113-
document.createElement("h3#pfe-jump-links-nav--heading")
114-
);
115+
.appendChild(heading);
115116
this.shadowRoot.querySelector(
116117
"#pfe-jump-links-nav--heading"
117118
).innerHTML = "Jump to section";
@@ -122,7 +123,7 @@ class PfeJumpLinksNav extends PFElement {
122123

123124
this._observer.observe(this, pfeJumpLinksNavObserverConfig);
124125

125-
this.panel = document.querySelector(`[scrolltarget="${this.id}"]`);
126+
this.panel = document.querySelector(`[pfe-c-scrolltarget="${this.id}"]`);
126127

127128
this.panel.addEventListener(
128129
PfeJumpLinksPanel.events.change,
@@ -147,7 +148,9 @@ class PfeJumpLinksNav extends PFElement {
147148
const buildLinkList = () => {
148149
let linkList = ``;
149150
if (!this.panel) {
150-
this.panel = document.querySelector(`[scrolltarget="${this.id}"]`);
151+
this.panel = document.querySelector(
152+
`[pfe-c-scrolltarget="${this.id}"]`
153+
);
151154
}
152155
let panelSections = this.panel.querySelectorAll(
153156
".pfe-jump-links-panel__section"
@@ -234,14 +237,14 @@ class PfeJumpLinksNav extends PFElement {
234237
}
235238
if (
236239
(this.has_slot("logo") || this.has_slot("link")) &&
237-
!this.hasAttribute("horizontal")
240+
!this.hasAttribute("pfe-c-horizontal")
238241
) {
239242
console.warn(
240243
`${PfeJumpLinks.tag}: logo and link slots NOT supported in vertical jump links`
241244
);
242245
}
243246
if (this.children[1].tagName !== "UL") {
244-
if (!this.hasAttribute("horizontal")) {
247+
if (!this.hasAttribute("pfe-c-horizontal")) {
245248
console.warn(
246249
`${PfeJumpLinks.tag}: The top-level list of links MUST be a <ul>`
247250
);
@@ -284,30 +287,40 @@ class PfeJumpLinksPanel extends PFElement {
284287
};
285288
}
286289

290+
get offsetValue() {
291+
return this.sectionMargin || this.customVar;
292+
}
293+
287294
static get PfeType() {
288295
return PFElement.PfeTypes.Content;
289296
}
290297

298+
static get observedAttributes() {
299+
return ["pfe-c-offset"];
300+
}
301+
291302
constructor() {
292303
super(PfeJumpLinksPanel, { type: PfeJumpLinksPanel.PfeType });
293304
this._init = this._init.bind(this);
294305
this._slot = this.shadowRoot.querySelector("slot");
295306
this._slot.addEventListener("slotchange", this._init);
296307
this._scrollCallback = this._scrollCallback.bind(this);
297308
this._mutationCallback = this._mutationCallback.bind(this);
309+
this._handleResize = this._handleResize.bind(this);
298310
this._observer = new MutationObserver(this._mutationCallback);
299311
this.currentActive = null;
300-
this.sectionMargin = this.getAttribute("offset") || 200;
301312
this.currentActive = 0;
302313
this.current = -1;
303-
this.nav = this._getNav();
314+
window.addEventListener("resize", this._handleResize);
304315
}
305316

306317
connectedCallback() {
307318
super.connectedCallback();
319+
this.nav = this._getNav();
308320
this._init();
309-
310-
if (this.nav && this.nav.hasAttribute("autobuild")) {
321+
this.sectionMargin = this.getAttribute("pfe-c-offset");
322+
this.customVar = this.cssVariable("--pfe-jump-links-panel--offset") || 200;
323+
if (this.nav && this.nav.hasAttribute("pfe-c-autobuild")) {
311324
this.nav._rebuildNav();
312325
}
313326

@@ -318,11 +331,22 @@ class PfeJumpLinksPanel extends PFElement {
318331
this._observer.disconnect();
319332
window.removeEventListener("scroll");
320333
this._slot.removeEventListener("slotchange", this._init);
334+
window.removeEventListener("resize", this._handleResize);
335+
}
336+
337+
attributeChangedCallback(attr, oldVal, newVal) {
338+
super.attributeChangedCallback(attr, oldVal, newVal);
339+
340+
switch (attr) {
341+
case "pfe-c-offset":
342+
this.sectionMargin = newVal;
343+
break;
344+
}
321345
}
322346

323347
_init() {
324348
window.addEventListener("scroll", this._scrollCallback);
325-
this.scrollTarget = this.getAttribute("scrolltarget");
349+
this.scrollTarget = this.getAttribute("pfe-c-scrolltarget");
326350
this.JumpLinksNav = document.querySelector(`#${this.scrollTarget}`);
327351
this.sections = this.querySelectorAll(".pfe-jump-links-panel__section");
328352

@@ -331,9 +355,15 @@ class PfeJumpLinksPanel extends PFElement {
331355
}
332356
}
333357

358+
_handleResize() {
359+
this.nav._reportHeight();
360+
this.sectionMargin = this.getAttribute("pfe-c-offset");
361+
this.customVar = this.cssVariable("--pfe-jump-links-panel--offset") || 200;
362+
}
363+
334364
_getNav() {
335365
return document.querySelector(
336-
`pfe-jump-links-nav#${this.getAttribute("scrolltarget")}`
366+
`pfe-jump-links-nav#${this.getAttribute("pfe-c-scrolltarget")}`
337367
);
338368
}
339369

@@ -405,7 +435,7 @@ class PfeJumpLinksPanel extends PFElement {
405435
//If we didn't get nav in the constructor, grab it now
406436
if (!this.nav) {
407437
this.nav = document.querySelector(
408-
`pfe-jump-links-nav#${this.getAttribute("scrolltarget")}`
438+
`pfe-jump-links-nav#${this.getAttribute("pfe-c-scrolltarget")}`
409439
);
410440
}
411441
//If we want the nav to be built automatically, re-init panel and rebuild nav
@@ -423,7 +453,6 @@ class PfeJumpLinksPanel extends PFElement {
423453
_scrollCallback() {
424454
let sections;
425455
let menu_links;
426-
let sectionMargin;
427456
//Check sections to make sure we have them (if not, get them)
428457
if (!this.sections || typeof this.sections === "undefined") {
429458
this.sections = this.querySelectorAll(".pfe-jump-links-panel__section");
@@ -436,17 +465,11 @@ class PfeJumpLinksPanel extends PFElement {
436465
menu_links = this.menu_links;
437466
}
438467

439-
if (!this.sectionMargin) {
440-
sectionMargin = 200;
441-
} else {
442-
sectionMargin = this.sectionMargin;
443-
}
444-
445468
// Make an array from the node list
446469
const sectionArr = [...sections];
447470
// Get all the sections that match this point in the scroll
448471
const matches = sectionArr
449-
.filter(section => window.scrollY >= section.offsetTop - sectionMargin)
472+
.filter(section => window.scrollY >= section.offsetTop - this.offsetValue)
450473
.reverse();
451474

452475
//Identify the last one queried as the current section

0 commit comments

Comments
 (0)