@triptease/tt-navbar 0.0.46 → 0.0.48

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.
@@ -1,5 +1,5 @@
1
1
  /**
2
- * @triptease/tt-navbar v0.0.46
2
+ * @triptease/tt-navbar v0.0.48
3
3
  */
4
4
 
5
5
  // src/urlMappings.ts
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "description": "Webcomponent tt-navbar following open-wc recommendations",
4
4
  "license": "MIT",
5
5
  "author": "tt-navbar",
6
- "version": "0.0.46",
6
+ "version": "0.0.48",
7
7
  "type": "module",
8
8
  "main": "dist/src/index.js",
9
9
  "module": "dist/src/index.js",
package/src/Config.ts ADDED
@@ -0,0 +1,7 @@
1
+ const Config = {
2
+ NavbarStateCookieName: 'tt-navbar-state',
3
+ NavbarStateCookieOpenValue: 'open',
4
+ NavbarStateCookieClosedValue: 'closed',
5
+ };
6
+
7
+ export { Config };
package/src/TtNavbar.ts CHANGED
@@ -19,6 +19,7 @@ import { styles } from './styles.js';
19
19
  import { tripteaseLogo } from './triptease-logo.js';
20
20
  import { urlMappings } from './urlMappings.js';
21
21
  import { Routes } from './Routes.js';
22
+ import { Config } from './Config.js';
22
23
 
23
24
  const jsonArrayConverter = (value: string | null) => {
24
25
  if (!value) return [];
@@ -179,7 +180,17 @@ export class TtNavbar extends LitElement {
179
180
  this.closeAllDetails();
180
181
  this.isOpen = !this.isOpen;
181
182
 
182
- // TODO - Set cookie
183
+ const {
184
+ NavbarStateCookieName,
185
+ NavbarStateCookieOpenValue,
186
+ NavbarStateCookieClosedValue,
187
+ } = Config;
188
+
189
+ const domain = window.location.hostname.endsWith('.triptease.io')
190
+ ? 'domain=.triptease.io'
191
+ : '';
192
+
193
+ document.cookie = `${NavbarStateCookieName}=${this.isOpen ? NavbarStateCookieOpenValue : NavbarStateCookieClosedValue}; path=/; max-age=31536000; ${domain}`;
183
194
  };
184
195
 
185
196
  private handleDetailsToggle = (e: ToggleEvent) => {
@@ -0,0 +1,30 @@
1
+ import { Config } from './Config.js';
2
+
3
+ const getInitialNavbarState = (cookieString?: string) => {
4
+ const cookies = cookieString || document.cookie;
5
+
6
+ if (!cookies) {
7
+ return undefined;
8
+ }
9
+
10
+ const stateCookie = cookies
11
+ .split(';')
12
+ .find(c => c.trim().startsWith(`${Config.NavbarStateCookieName}`));
13
+
14
+ if (!stateCookie) {
15
+ return undefined;
16
+ }
17
+
18
+ const value = stateCookie.split('=')[1];
19
+
20
+ if (
21
+ value !== Config.NavbarStateCookieOpenValue &&
22
+ value !== Config.NavbarStateCookieClosedValue
23
+ ) {
24
+ return undefined;
25
+ }
26
+
27
+ return value;
28
+ };
29
+
30
+ export { getInitialNavbarState };
package/src/index.ts CHANGED
@@ -1,2 +1,4 @@
1
1
  export { TtNavbar } from './TtNavbar.js';
2
2
  export { Routes } from './Routes.js';
3
+ export { Config } from './Config.js';
4
+ export { getInitialNavbarState } from './getInitialNavbarState.js';
@@ -6,7 +6,7 @@ import {
6
6
  nextFrame,
7
7
  waitUntil,
8
8
  } from '@open-wc/testing';
9
- import { TtNavbar } from '../src/index.js';
9
+ import { Config, getInitialNavbarState, TtNavbar } from '../src/index.js';
10
10
  import { Routes } from '../src/Routes.js';
11
11
 
12
12
  // eslint-disable-next-line no-undef
@@ -411,7 +411,7 @@ describe('TtNavbar', () => {
411
411
 
412
412
  it("should default to 'open' when the 'initial-state' attribute is 'open", async () => {
413
413
  const navbar = await fixture<TtNavbar>(
414
- `<tt-navbar client-key=${CLIENT_KEY} initial-state="open"></tt-navbar>`,
414
+ `<tt-navbar client-key=${CLIENT_KEY} initial-state=${Config.NavbarStateCookieOpenValue}></tt-navbar>`,
415
415
  );
416
416
 
417
417
  const logo = navbar.shadowRoot!.querySelector('.logo');
@@ -422,7 +422,7 @@ describe('TtNavbar', () => {
422
422
 
423
423
  it("should default to 'closed' when the 'initial-state' attribute is 'closed", async () => {
424
424
  const navbar = await fixture<TtNavbar>(
425
- `<tt-navbar client-key=${CLIENT_KEY} initial-state="closed"></tt-navbar>`,
425
+ `<tt-navbar client-key=${CLIENT_KEY} initial-state=${Config.NavbarStateCookieClosedValue}></tt-navbar>`,
426
426
  );
427
427
 
428
428
  const logo = navbar.shadowRoot!.querySelector('.logo');
@@ -430,4 +430,57 @@ describe('TtNavbar', () => {
430
430
  expect(logo).to.exist;
431
431
  expect(logo!.checkVisibility()).to.be.false;
432
432
  });
433
+
434
+ it('should create cookie containing navbar state onToggle if it is missing', async () => {
435
+ document.cookie = `${Config.NavbarStateCookieName}=; max-age=0; path=/;`;
436
+ const startingCookieValue = getInitialNavbarState();
437
+
438
+ expect(startingCookieValue).to.be.undefined;
439
+
440
+ const navbar = await fixture<TtNavbar>(
441
+ `<tt-navbar client-key=${CLIENT_KEY}></tt-navbar>`,
442
+ );
443
+ const navbarToggleBtn = navbar.shadowRoot!.querySelector(
444
+ '#navbar-toggle-btn',
445
+ ) as HTMLButtonElement;
446
+
447
+ navbarToggleBtn.click();
448
+ await elementUpdated(navbar);
449
+
450
+ const newState = getInitialNavbarState();
451
+
452
+ await expect(newState).to.equal(Config.NavbarStateCookieClosedValue);
453
+ });
454
+
455
+ it('should update the cookie value when navbar is expanded', async () => {
456
+ const navbar = await fixture<TtNavbar>(
457
+ `<tt-navbar client-key=${CLIENT_KEY} initial-state=${Config.NavbarStateCookieClosedValue}></tt-navbar>`,
458
+ );
459
+ const navbarToggleBtn = navbar.shadowRoot!.querySelector(
460
+ '#navbar-toggle-btn',
461
+ ) as HTMLButtonElement;
462
+
463
+ navbarToggleBtn.click();
464
+ await elementUpdated(navbar);
465
+
466
+ const newState = getInitialNavbarState();
467
+
468
+ await expect(newState).to.equal(Config.NavbarStateCookieOpenValue);
469
+ });
470
+
471
+ it('should update the cookie value when navbar is collapsed', async () => {
472
+ const navbar = await fixture<TtNavbar>(
473
+ `<tt-navbar client-key=${CLIENT_KEY} initial-state=${Config.NavbarStateCookieOpenValue}></tt-navbar>`,
474
+ );
475
+ const navbarToggleBtn = navbar.shadowRoot!.querySelector(
476
+ '#navbar-toggle-btn',
477
+ ) as HTMLButtonElement;
478
+
479
+ navbarToggleBtn.click();
480
+ await elementUpdated(navbar);
481
+
482
+ const newState = getInitialNavbarState();
483
+
484
+ await expect(newState).to.equal(Config.NavbarStateCookieClosedValue);
485
+ });
433
486
  });