Header.svelte 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <script>
  2. import {page} from '$app/stores';
  3. import profile from '$lib/images/profile.png';
  4. import {auth} from "./store.js";
  5. import {onMount} from "svelte";
  6. let username = null;
  7. let profileTitle = null;
  8. const unsubscribe = auth.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('auth');
  14. if (storedAuth) {
  15. try {
  16. auth.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 === '/' ? 'page' : undefined}>
  33. <a href="/">Home</a>
  34. </li>
  35. <li aria-current={$page.url.pathname === '/about' ? 'page' : undefined}>
  36. <a href="/about">About</a>
  37. </li>
  38. </ul>
  39. </nav>
  40. <div class="corner">
  41. <div class="login-message">{username}</div>
  42. <a href="/login">
  43. <img src={profile} alt="Profile" style="width: 16px; height: 16px;" title="{profileTitle}"/>
  44. </a>
  45. </div>
  46. </header>
  47. <style>
  48. header {
  49. display: flex;
  50. justify-content: space-between;
  51. background-color: #a5b1c2;
  52. width: 100%;
  53. }
  54. .corner {
  55. padding-right: 1em;
  56. height: 3em;
  57. display: flex; /* Enable flexbox layout */
  58. align-items: center; /* Center items vertically */
  59. }
  60. .login-message {
  61. margin-right: 8px; /* Add some space between the message and the profile image */
  62. color: var(--color-text); /* Ensure the text color is set */
  63. }
  64. .corner a {
  65. display: flex;
  66. align-items: center;
  67. justify-content: center;
  68. width: 100%;
  69. height: 100%;
  70. }
  71. .corner img {
  72. width: 2em;
  73. height: 2em;
  74. object-fit: contain;
  75. }
  76. nav {
  77. flex-grow: 1;
  78. justify-content: flex-start; /* Aligns the menu to the left */
  79. }
  80. ul {
  81. padding: 0;
  82. margin: 0;
  83. height: 3em;
  84. display: flex;
  85. align-items: center;
  86. list-style: none;
  87. background: var(--background);
  88. background-size: contain;
  89. }
  90. li {
  91. position: relative;
  92. height: 100%;
  93. }
  94. li[aria-current='page']::before {
  95. --size: 6px;
  96. content: '';
  97. width: 0;
  98. height: 0;
  99. position: absolute;
  100. top: 0;
  101. left: calc(50% - var(--size));
  102. border: var(--size) solid transparent;
  103. border-top: var(--size) solid var(--color-theme-1);
  104. }
  105. nav a {
  106. display: flex;
  107. height: 100%;
  108. align-items: center;
  109. padding: 0 0.5rem;
  110. color: var(--color-text);
  111. font-weight: 700;
  112. font-size: 0.8rem;
  113. text-transform: uppercase;
  114. letter-spacing: 0.1em;
  115. text-decoration: none;
  116. transition: color 0.2s linear;
  117. }
  118. a:hover {
  119. color: var(--color-theme-1);
  120. }
  121. </style>