Skip to content

Commit 3fa441f

Browse files
mwczchrisdo1starryeyez024
authored
feat: pfe-navigation - report pfe-nav's height in a global CSS variable (#732)
* feat(pfe-navigation): report pfe-nav's height in a global CSS variable * update changelog * change the height var to be more BEMy * set nav height variable to use px * fixed merge conflict * fixed variable name in readme * fixed typo in readme * fixed nav test to use correct variable name * set height custom property to use underscores only if region is declared. * added px value back into variable. * removed px string from custom height property to match clientHeight. * Updated nav readme, fixed typo and replaced ellipses with colon Co-authored-by: Chris Doherty <[email protected]> Co-authored-by: Kendall Totten <[email protected]>
1 parent 1abe9d7 commit 3fa441f

File tree

5 files changed

+67
-8
lines changed

5 files changed

+67
-8
lines changed

CHANGELOG-prerelease.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ Tag: [v1.0.0-prerelease.39](https://github.com/patternfly/patternfly-elements/re
66
- [f93af30](https://github.com/patternfly/patternfly-elements/commit/f93af30cc7eef3e7728effb7cedb4773f54a5f43) feat: adding the ability for pfe-navigation to use the full width of the viewport (#718)
77
- [aac9925](https://github.com/patternfly/patternfly-elements/commit/aac992563a61495128c00d53f637d656bbe614dd) Update pfe-content-set to accept custom container queries
88
- [6e45ab4](https://github.com/patternfly/patternfly-elements/commit/6e45ab42617086fb7e229af3b0953ff6912c0273) feat: pfe-cta add support for disabled button, reduce lightdom needs
9-
- [](https://github.com/patternfly/patternfly-elements/commit/) feat: Move generator into the monorepo
9+
- [582ffc8](https://github.com/patternfly/patternfly-elements/commit/582ffc87fcbf2ef29aecf01967d19834a7aff116) feat: Move generator into the monorepo
10+
- [](https://github.com/patternfly/patternfly-elements/commit/) feat(pfe-navigation): report pfe-nav's height in a global CSS variable (#732)
1011

1112
## Prerelease 38 ( 2020-02-03 )
1213

elements/pfe-navigation/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ All slots are optional and can be left off if not needed. Slots prefixed with `m
6666
- `pfe-menu-label`: Translation for the Menu label on the main navigation dropdown.
6767
- `pfe-full-width`: Allows the navigation to span the full width of the viewport.
6868
69+
### CSS Variables
70+
71+
- `--pfe-navigation--Height--actual`: when pfe-navigation initializes, it will create a global (on `body`) CSS variable that contains the height of the pfe-navigation item. The primary uses for this is calculating the offset for anchor links, and for positioning a sticky sub-header below the pfe-navigation. Note that multiple pfe-navigation elements will write the same variable, unless:
72+
- `--pfe-navigation__${ID}--Height--actual`: like the previous variable, but if the pfe-navigation has an `id` attribute, it will be appended to the CSS variable name. This makes it possible to distinguish the heights of multiple pfe-navigation elements on the same page.
73+
6974
---
7075
7176
## Navigation item

elements/pfe-navigation/demo/index.html

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
<style>
4747
pfe-navigation {
4848
--pfe-navigation__logo--MinWidth: 280px;
49-
}
49+
}
5050
@media (max-width: 440px) {
5151
pfe-navigation {
5252
--pfe-navigation__logo--MinWidth: 160px;
@@ -567,7 +567,7 @@ <h3 slot="pfe-card--header">Aside: right body bottom</h3>
567567
</pfe-cta>
568568
</pfe-card>
569569
</pfe-band>
570-
570+
571571
<pfe-band pfe-color="lighter" pfe-aside-desktop="left">
572572
<header slot="pfe-band--header">
573573
<h2>Lighter band; no footer</h2>
@@ -595,7 +595,7 @@ <h3 slot="pfe-card--header">Aside: left body bottom</h3>
595595
</pfe-cta>
596596
</pfe-card>
597597
</pfe-band>
598-
598+
599599
<pfe-band pfe-aside-height="full">
600600
<header slot="pfe-band--header">
601601
<h2>Default band</h2>
@@ -626,7 +626,7 @@ <h3>Aside: right full bottom</h3>
626626
</pfe-cta>
627627
</pfe-card>
628628
</pfe-band>
629-
629+
630630
<pfe-band id="band" pfe-color="darker" pfe-aside-desktop="left" pfe-aside-height="full"
631631
pfe-img-src="https://www.redhat.com/profiles/rh/themes/redhatdotcom/img/header/header-hub-svsp-2000x1562.jpg">
632632
<header slot="pfe-band--header">
@@ -657,8 +657,8 @@ <h3 slot="pfe-card--header">Aside: left full bottom</h3>
657657
</pfe-cta>
658658
</pfe-card>
659659
</pfe-band>
660-
661-
660+
661+
662662
<pfe-band pfe-aside-desktop="left" pfe-aside-mobile="top">
663663
<header slot="pfe-band--header">
664664
<h2>Short content band</h2>

elements/pfe-navigation/src/pfe-navigation.js

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class PfeNavigation extends PFElement {
5555
}
5656

5757
static get observedAttributes() {
58-
return ["pfe-full-width"];
58+
return ["pfe-full-width", "id"];
5959
}
6060

6161
constructor() {
@@ -165,6 +165,15 @@ class PfeNavigation extends PFElement {
165165
this._observer.disconnect();
166166
}
167167

168+
attributeChangedCallback(name, oldValue, newValue) {
169+
super.attributeChangedCallback(name, oldValue, newValue);
170+
switch (name) {
171+
case "id":
172+
this._reportHeight();
173+
break;
174+
}
175+
}
176+
168177
_resizeHandler(event) {
169178
// Set the visibility of items
170179
this._setVisibility(this.offsetWidth);
@@ -194,6 +203,9 @@ class PfeNavigation extends PFElement {
194203
});
195204

196205
this.overlay = this._activeNavigationItems.length > 0;
206+
207+
// update the reported height
208+
this._reportHeight();
197209
}
198210

199211
_stickyHandler() {
@@ -335,6 +347,9 @@ class PfeNavigation extends PFElement {
335347
document.addEventListener("click", this._outsideListener);
336348
}
337349

350+
// report the height of this pfe-navigation element
351+
this._reportHeight();
352+
338353
// @IE11 This is necessary so the script doesn't become non-responsive
339354
if (window.ShadyCSS) {
340355
setTimeout(() => {
@@ -358,6 +373,25 @@ class PfeNavigation extends PFElement {
358373
this._activeNavigationItems.map(item => item.close());
359374
this.overlay = false;
360375
}
376+
377+
/**
378+
* Set a global CSS variable reporting the height of this navigation item.
379+
* Used to position sticky subnavigation items under this.
380+
*
381+
* The name of the global CSS variable is `--pfe-navigation--Height--actual`.
382+
* If this nav has an `id` attribute, the id will be appended to the variable
383+
* name to distinguish it from other pfe-navigation items on the page
384+
* (unlikely, but imagine a demo page with 20 example pfe-navigation elements
385+
* in different states, and one genuine pfe-navigation element at the top
386+
* used for actual navigation).
387+
*/
388+
_reportHeight() {
389+
const cssVarName = `--pfe-navigation${
390+
this.id ? `__${this.id}--` : "--"
391+
}Height--actual`;
392+
const height = this.clientHeight + "px";
393+
document.body.style.setProperty(cssVarName, height);
394+
}
361395
}
362396

363397
PFElement.create(PfeNavigationItem);

elements/pfe-navigation/test/pfe-navigation_test.html

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -602,6 +602,25 @@ <h3 slot="trigger"><a href="#">Products</a></h3>
602602
// backdrop.click();
603603
// assert.isUndefined(document.body.style.overlay);
604604
// });
605+
606+
test("it should report its height via a global CSS variable", () => {
607+
const pfeNavigation = document.querySelector('pfe-navigation');
608+
609+
const reportedHeight = document.body.style.getPropertyValue("--pfe-navigation--Height--actual");
610+
611+
// there are multiple pfe-navigations on this page, so don't try to
612+
// check the exact height (race condition over which writes its var
613+
// last). Just check that the var was created.
614+
assert.isDefined(reportedHeight, "pfe-navigation without id reports height to default css variable");
615+
616+
// should also create a namespaced var if the nav has an ID
617+
pfeNavigation.setAttribute("id", "ABCDEFG");
618+
619+
const reportedHeightNamespaced = document.body.style.getPropertyValue("--pfe-navigation__ABCDEFG--Height--actual").replace("px", "");
620+
621+
assert.isDefined(reportedHeightNamespaced, "pfe-navigation with id reports height to namespaced css variable");
622+
assert.equal(pfeNavigation.clientHeight.toString(), reportedHeightNamespaced, "pfe-navigation with id reports the correct height");
623+
});
605624
});
606625

607626

0 commit comments

Comments
 (0)