@penskemediacorp/larva-js 0.7.0 → 0.7.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@penskemediacorp/larva-js",
3
- "version": "0.7.0",
3
+ "version": "0.7.4",
4
4
  "description": "Larva JS patterns.",
5
5
  "author": "laras126 <laras126@gmail.com>",
6
6
  "homepage": "https://github.com/penske-media-corp/pmc-larva#readme",
@@ -26,5 +26,5 @@
26
26
  "webpack": "^4.43.0",
27
27
  "webpack-cli": "^3.3.12"
28
28
  },
29
- "gitHead": "4f647cfb574dcae8905f806957c7dfa75420dbbe"
29
+ "gitHead": "87d72c42360db2e0d2aeac4eda0b5dfdd5734b35"
30
30
  }
@@ -0,0 +1,63 @@
1
+ export default class ExpandableSearch {
2
+ constructor(el) {
3
+ this.el = el;
4
+ this.isOpen = false;
5
+ this.trigger = this.el.querySelector('.js-ExpandableSearch-trigger');
6
+ this.target = this.el.querySelector('.js-ExpandableSearch-target');
7
+ this.targetInput = this.target.querySelector('input');
8
+
9
+ this.trigger.addEventListener('click', () => {
10
+ this.toggleSearch();
11
+ });
12
+
13
+ // TODO: remove these event listeners when search is closed.
14
+
15
+ // Collapse search on ESC.
16
+ document.addEventListener('keydown', (e) => {
17
+ if (27 === e.keyCode) {
18
+ this.collapseSearch();
19
+ }
20
+ });
21
+
22
+ // Collapse search on body click, outside of search el.
23
+ document.body.addEventListener('click', (e) => {
24
+ if (!this.el.contains(e.target)) {
25
+ this.collapseSearch();
26
+ }
27
+ });
28
+
29
+ // Collapse search when focus leaves search el.
30
+ document.addEventListener('focusin', () => {
31
+ // eslint-disable-next-line @wordpress/no-global-active-element
32
+ if (!this.el.contains(document.activeElement) && this.isOpen) {
33
+ this.collapseSearch();
34
+ }
35
+ });
36
+ }
37
+
38
+ updateState() {
39
+ this.isOpen = this.isOpen ? false : true;
40
+ }
41
+
42
+ toggleSearch() {
43
+ this.target.toggleAttribute('hidden');
44
+ this.target.classList.toggle('js-fade-is-out');
45
+ this.target.classList.toggle('js-fade-is-in');
46
+ this.trigger.classList.toggle('is-ExpandableSearch-open');
47
+
48
+ this.updateState();
49
+
50
+ // Switch focus to input if it is opened
51
+ if (this.isOpen) {
52
+ this.targetInput.focus();
53
+ }
54
+ }
55
+
56
+ // Switch focus back to trigger when search is closed.
57
+ collapseSearch() {
58
+ if (this.isOpen) {
59
+ this.toggleSearch();
60
+ this.trigger.focus();
61
+ }
62
+ }
63
+ }
@@ -0,0 +1,9 @@
1
+ import ExpandableSearch from './ExpandableSearch.js';
2
+
3
+ export default function initExpandableSearch() {
4
+ const searches = [...document.querySelectorAll('.js-ExpandableSearch')];
5
+
6
+ searches.forEach((el) => {
7
+ return new ExpandableSearch(el);
8
+ });
9
+ }
@@ -0,0 +1,124 @@
1
+ // Copied from Vibe
2
+ export default class Header {
3
+ constructor(el) {
4
+ this.el = el;
5
+ this.el.pmcHeader = this;
6
+ this.container = document.documentElement;
7
+
8
+ this.initSticky();
9
+
10
+ // Uncomment when ready to work on search.
11
+ this.initSearch();
12
+ }
13
+
14
+ destroy() {
15
+ this.destroySticky();
16
+
17
+ this.destroySearch();
18
+ delete this.el.pmcHeader;
19
+ }
20
+
21
+ get stickyClass() {
22
+ return this.el.dataset.headerStickyClass || 'is-sticky';
23
+ }
24
+
25
+ get readyClass() {
26
+ return this.el.dataset.headerReadyClass || 'is-header-ready';
27
+ }
28
+
29
+ get searchClass() {
30
+ return this.el.dataset.headerSearchClass || 'is-search-expanded';
31
+ }
32
+
33
+ initSticky() {
34
+ if (window.IntersectionObserver) {
35
+ this.observerOptions = {
36
+ root: null,
37
+ rootMargin: '350px',
38
+ threshold: [1.0],
39
+ };
40
+
41
+ this.toggleSticky = this.toggleSticky.bind(this);
42
+
43
+ // eslint-disable-next-line no-undef
44
+ this.observer = new IntersectionObserver(
45
+ this.toggleSticky,
46
+ this.observerOptions
47
+ );
48
+ this.observer.observe(this.el);
49
+
50
+ // Delay the initialization of the transition effects on the header.
51
+ this.container.classList.add(this.readyClass);
52
+ }
53
+ }
54
+
55
+ destroySticky() {
56
+ if (window.IntersectionObserver) {
57
+ this.observer.disconnect();
58
+ this.container.classList.remove(this.stickyClass, this.readyClass);
59
+ }
60
+ }
61
+
62
+ toggleSticky(entries) {
63
+ if (window.IntersectionObserver) {
64
+ entries.forEach((entry) => {
65
+ const ratio = entry.intersectionRatio;
66
+
67
+ if (1 === ratio) {
68
+ this.container.classList.remove(this.stickyClass);
69
+ } else if (1 > ratio) {
70
+ this.container.classList.add(this.stickyClass);
71
+ }
72
+ });
73
+ }
74
+ }
75
+
76
+ initSearch() {
77
+ // TODO: Note to @Lara Schenck that this should be updated to be a class, probably
78
+ this.searchTrigger = this.el.querySelector(
79
+ '[data-header-search-trigger]'
80
+ );
81
+
82
+ if (null === this.searchTrigger) {
83
+ return;
84
+ }
85
+
86
+ this.expandSearch = this.expandSearch.bind(this);
87
+ this.collapseSearch = this.collapseSearch.bind(this);
88
+ this.searchTrigger.addEventListener('click', this.expandSearch);
89
+ }
90
+
91
+ destroySearch() {
92
+ document.body.removeEventListener('click', this.collapseSearch);
93
+ this.searchTrigger.removeEventListener('click', this.expandSearch);
94
+ this.container.classList.remove(this.searchClass);
95
+ }
96
+
97
+ expandSearch(e) {
98
+ e.preventDefault();
99
+ e.stopPropagation();
100
+ this.container.classList.add(this.searchClass);
101
+
102
+ this.searchTrigger.removeEventListener('click', this.expandSearch);
103
+
104
+ // Delay new event listener set up to prevent immediate search form collapsing.
105
+ setTimeout(
106
+ () => document.body.addEventListener('click', this.collapseSearch),
107
+ 1
108
+ );
109
+ }
110
+
111
+ collapseSearch(e) {
112
+ // Do not collapse if the search form element was clicked.
113
+ if (
114
+ e.target === this.searchTrigger ||
115
+ this.searchTrigger.contains(e.target)
116
+ ) {
117
+ return;
118
+ }
119
+
120
+ this.container.classList.remove(this.searchClass);
121
+ this.searchTrigger.addEventListener('click', this.expandSearch);
122
+ document.body.removeEventListener('click', this.collapseSearch);
123
+ }
124
+ }
@@ -0,0 +1,7 @@
1
+ import Header from './Header.js';
2
+
3
+ export default function initHeader() {
4
+ // TODO: For Lara, This maybe should be turned into a class
5
+ const headers = [...document.querySelectorAll('.js-Header')];
6
+ headers.forEach((el) => (el.pmcHeader = new Header(el)));
7
+ }