Header.svelte 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <script>
  2. import { page } from '$app/stores';
  3. import profile from '$lib/images/profile.png';
  4. import { authentication } from '../routes/store.js';
  5. import { onMount } from 'svelte';
  6. let username = undefined;
  7. let profileTitle = undefined;
  8. const unsubscribe = authentication.subscribe((value) => {
  9. username = value ? value.username : '';
  10. profileTitle = value ? `You are logged in as ${value.username}` : 'Login';
  11. });
  12. onMount(() => {
  13. const storedAuth = localStorage.getItem('authentication');
  14. if (storedAuth) {
  15. try {
  16. authentication.set(JSON.parse(storedAuth));
  17. } catch (e) {
  18. console.error('Failed to parse auth from localStorage', e);
  19. }
  20. }
  21. return () => {
  22. unsubscribe();
  23. };
  24. });
  25. </script>
  26. <header>
  27. <div class="corner">
  28. <!-- logo -->
  29. </div>
  30. <nav>
  31. <ul>
  32. <li aria-current={$page.url.pathname === '/home' ? 'page' : undefined}>
  33. <a href="/home">Home</a>
  34. </li>
  35. {#if username !== ''}
  36. <li aria-current={$page.url.pathname === '/stocks' ? 'page' : undefined}>
  37. <a href="/stocks">Stocks</a>
  38. </li>
  39. <li aria-current={$page.url.pathname === '/portfolio' ? 'page' : undefined}>
  40. <a href="/portfolio">Portfolio</a>
  41. </li>
  42. <li aria-current={$page.url.pathname === '/insights' ? 'page' : undefined}>
  43. <a href="/insights">Insights</a>
  44. </li>
  45. {/if}
  46. <!-- <li aria-current={$page.url.pathname === '/about' ? 'page' : undefined}>-->
  47. <!-- <a href="/about">About</a>-->
  48. <!-- </li>-->
  49. </ul>
  50. </nav>
  51. <div class="corner">
  52. <a href="/profile">
  53. <div class="login-message">{username}</div>
  54. <img src={profile} alt="Profile" style="width: 16px; height: 16px;" title={profileTitle} />
  55. </a>
  56. </div>
  57. </header>
  58. <style>
  59. header {
  60. display: flex;
  61. justify-content: space-between;
  62. background-color: #a5b1c2;
  63. width: 100%;
  64. }
  65. .corner {
  66. padding-right: 1em;
  67. height: 3em;
  68. display: flex;
  69. align-items: center;
  70. }
  71. .login-message {
  72. margin-right: 8px;
  73. color: var(--color-text);
  74. }
  75. .corner a {
  76. display: flex;
  77. align-items: center;
  78. justify-content: center;
  79. width: 100%;
  80. height: 100%;
  81. }
  82. .corner img {
  83. width: 2em;
  84. height: 2em;
  85. object-fit: contain;
  86. }
  87. nav {
  88. flex-grow: 1;
  89. justify-content: flex-start;
  90. }
  91. ul {
  92. padding: 0;
  93. margin: 0;
  94. height: 3em;
  95. display: flex;
  96. align-items: center;
  97. list-style: none;
  98. background: var(--background);
  99. background-size: contain;
  100. }
  101. li {
  102. position: relative;
  103. height: 100%;
  104. }
  105. li[aria-current='page']::before {
  106. --size: 6px;
  107. content: '';
  108. width: 0;
  109. height: 0;
  110. position: absolute;
  111. top: 0;
  112. left: calc(50% - var(--size));
  113. border: var(--size) solid transparent;
  114. border-top: var(--size) solid var(--color-theme-1);
  115. }
  116. nav a {
  117. display: flex;
  118. height: 100%;
  119. align-items: center;
  120. padding: 0 0.5rem;
  121. color: var(--color-text);
  122. font-weight: 700;
  123. font-size: 0.8rem;
  124. text-transform: uppercase;
  125. letter-spacing: 0.1em;
  126. text-decoration: none;
  127. transition: color 0.2s linear;
  128. }
  129. a:hover {
  130. color: var(--color-theme-1);
  131. }
  132. </style>