All files / src/components AppNavRail.vue

94.59% Statements 35/37
83.33% Branches 10/12
100% Functions 8/8
100% Lines 34/34

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338  282x                           7x         7x                                                                   87x                         87x   87x 87x 87x 87x     221x 221x 221x 221x 221x 221x         221x       1x       1x       216x 216x 216x 216x   1x   216x 216x     87x 87x 87x     87x 12x     87x 219x 129x     87x                                                                                                                                                                                                                                                                                                                                                                                                                                              
<template>
  <div
    class="app-nav-rail sidebar-wrapper"
    :style="{ '--app-nav-pinned-width': pinnedWidth, '--app-nav-end-width': endWidth }"
  >
    <button
      v-if="showLeftArrow"
      type="button"
      class="scroll-arrow scroll-arrow-left"
      @click="scrollLeft"
    >
      <font-awesome-icon icon="fa-solid fa-chevron-left" />
    </button>
 
    <!-- Pinned part: stays visible while the rest of the menu scrolls -->
    <nav class="nav flex-column sidebar-pinned">
      <slot name="pinned" />
    </nav>
 
    <div ref="sidebarRef" class="sidebar" :data-tour="tour" @scroll="handleScroll">
      <nav class="nav flex-column">
        <slot />
      </nav>
    </div>
 
    <!-- End part: at the bottom of the column, at the right end of the bar -->
    <div v-if="$slots.end" class="sidebar-end">
      <slot name="end" />
    </div>
 
    <button
      v-if="showRightArrow"
      type="button"
      class="scroll-arrow scroll-arrow-right"
      @click="scrollRight"
    >
      <font-awesome-icon icon="fa-solid fa-chevron-right" />
    </button>
  </div>
</template>
 
<script setup lang="ts">
// @implements UC-ADMIN-001.1, UC-SPACE-015.1
/**
 * The frame of a menu: a column on the left on a computer, a bar at the
 * bottom on a phone, a pinned part, a part that scrolls and an optional end
 * part (bottom of the column, right end of the bar) (with arrows on a
 * phone, its position kept per `storageKey`). Shared by the app's menu and the
 * space's (UC-SPACE-015.1); the entries come in the slots, as `.nav-link`s.
 */
import { nextTick, onMounted, onUnmounted, ref, watch } from 'vue'
import { useRoute } from 'vue-router'
import { FontAwesomeIcon } from '@fortawesome/vue-fontawesome'
 
const props = withDefaults(
  defineProps<{
    /** Where the scroll position is kept for the tab (sessionStorage) */
    storageKey: string
    /** The pinned part's width in the bottom bar, where the left arrow sits */
    pinnedWidth?: string
    /** The end part's width in the bottom bar, where the right arrow sits */
    endWidth?: string
    tour?: string
  }>(),
  { pinnedWidth: '90px', endWidth: '0px', tour: undefined }
)
 
const emit = defineEmits<{ scroll: [] }>()
 
const route = useRoute()
const sidebarRef = ref<HTMLElement | null>(null)
const showLeftArrow = ref(false)
const showRightArrow = ref(false)
 
function handleScroll() {
  Iif (!sidebarRef.value) return
  const { scrollLeft, scrollWidth, clientWidth } = sidebarRef.value
  showLeftArrow.value = scrollLeft > 5
  showRightArrow.value = scrollLeft < scrollWidth - clientWidth - 5
  try {
    sessionStorage.setItem(props.storageKey, scrollLeft.toString())
  } catch {
    // Storage unavailable (private mode): the position is simply not kept
  }
  // Also bound to 'resize': lets the owner realign what it positions in JS
  emit('scroll')
}
 
function scrollLeft() {
  sidebarRef.value?.scrollBy({ left: -150, behavior: 'smooth' })
}
 
function scrollRight() {
  sidebarRef.value?.scrollBy({ left: 150, behavior: 'smooth' })
}
 
function restoreScrollPosition() {
  Iif (!sidebarRef.value) return
  let saved: string | null = null
  try {
    saved = sessionStorage.getItem(props.storageKey)
  } catch {
    saved = null
  }
  if (saved) sidebarRef.value.scrollLeft = parseInt(saved, 10)
  handleScroll()
}
 
onMounted(() => {
  nextTick(restoreScrollPosition)
  window.addEventListener('resize', handleScroll)
})
 
onUnmounted(() => {
  window.removeEventListener('resize', handleScroll)
})
 
watch(
  () => route.path,
  () => nextTick(restoreScrollPosition)
)
 
defineExpose({ scrollLeft, scrollRight, handleScroll, sidebarRef })
</script>
 
<style>
/* Not scoped: the entries come from the owner's slots, styled here so both
   menus look the same. Colors come from the --bw-sidebar-* variables, which
   the space redefines with its theme. */
.app-nav-rail.sidebar-wrapper {
  position: fixed;
  top: calc(74px + var(--bw-ribbons-height, 0px));
  left: 0;
  bottom: 0;
  width: 125px;
  z-index: 999;
  display: flex;
  flex-direction: column;
  background: linear-gradient(180deg, var(--bw-sidebar-start) 0%, var(--bw-sidebar-end) 100%);
}
 
/* Never scrolls away with the rest of the menu */
.app-nav-rail .sidebar-pinned {
  flex: 0 0 auto;
}
 
/* Scrollable part: takes whatever space the pinned part leaves */
.app-nav-rail .sidebar {
  flex: 1 1 auto;
  min-height: 0;
  min-width: 0;
  overflow-y: auto;
  overflow-x: hidden;
  -ms-overflow-style: none;
  scrollbar-width: none;
}
 
.app-nav-rail .sidebar-end {
  flex: 0 0 auto;
}
 
/* Hide the scrollbar but keep scrolling */
.app-nav-rail .sidebar::-webkit-scrollbar {
  display: none;
}
 
/* Bootstrap's .nav defaults to flex-wrap: wrap. Combined with
   flex-direction: column, a single unbreakable long string (a flow name)
   can force the column to wrap into extra flex lines, which then stretches
   every nav item to the widest line's content width instead of the
   sidebar's 125px. A vertical sidebar should never wrap into columns. */
.app-nav-rail .nav {
  flex-wrap: nowrap;
}
 
.app-nav-rail .nav-link {
  color: var(--bw-sidebar-text);
  padding: 0.75rem 1rem;
  transition: all 0.3s;
  font-size: 0.85rem;
  text-align: center;
  display: flex;
  flex-direction: column;
  align-items: center;
  font-weight: 500;
  position: relative;
}
 
.app-nav-rail .nav-link:hover {
  color: var(--bw-sidebar-text);
  background: var(--bw-sidebar-hover);
}
 
.app-nav-rail .nav-link.active {
  color: var(--bw-sidebar-text);
  background: var(--bw-sidebar-active);
  font-weight: 600;
}
 
.app-nav-rail .nav-link svg {
  font-size: 1.5rem;
  margin-bottom: 0.25rem;
}
 
.app-nav-rail .nav-separator {
  height: 1px;
  background: rgba(255, 255, 255, 0.2);
  margin: 0.5rem 0;
}
 
.app-nav-rail .nav-section-title {
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--bw-sidebar-section);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  padding: 0.5rem 1rem 0.25rem;
  margin-bottom: 0.25rem;
  text-align: center;
}
 
.app-nav-rail .nav-link .badge {
  position: absolute;
  top: 0.5rem;
  right: 0.5rem;
  font-size: 0.65rem;
  padding: 0.25em 0.5em;
  border-radius: 10px;
  pointer-events: none;
}
 
.app-nav-rail .scroll-arrow {
  display: none;
}
 
/* Mobile: bottom navigation */
@media (max-width: 767px) {
  .app-nav-rail.sidebar-wrapper {
    top: auto;
    bottom: 0;
    left: 0;
    right: 0;
    width: 100%;
    height: 60px;
    flex-direction: row;
    background: linear-gradient(90deg, var(--bw-sidebar-start) 0%, var(--bw-sidebar-end) 100%);
    box-shadow: 0 -2px 10px rgba(0, 0, 0, 0.1);
  }
 
  .app-nav-rail .sidebar {
    height: 100%;
    padding: 0;
    overflow-x: auto;
    overflow-y: hidden;
    -webkit-overflow-scrolling: touch;
  }
 
  .app-nav-rail .nav {
    display: flex;
    flex-direction: row !important;
    align-items: center;
    height: 100%;
    width: max-content;
  }
 
  .app-nav-rail .nav-link {
    flex: 0 0 auto;
    width: 90px;
    font-size: 0.7rem;
    margin: 0;
    white-space: nowrap;
  }
 
  .app-nav-rail .nav-link svg {
    font-size: 1.25rem;
    margin-bottom: 0.125rem;
  }
 
  .app-nav-rail .nav-separator,
  .app-nav-rail .nav-section-title {
    display: none;
  }
 
  .app-nav-rail .nav-link .badge {
    position: absolute;
    top: 0.25rem;
    right: 0.25rem;
    font-size: 0.6rem;
    padding: 0.15em 0.35em;
  }
 
  .app-nav-rail .scroll-arrow {
    display: flex;
    align-items: center;
    justify-content: center;
    position: fixed;
    bottom: 18px;
    width: 24px;
    height: 24px;
    background: transparent;
    border: none;
    cursor: pointer;
    z-index: 1000;
    transition: all 0.2s;
    padding: 0;
  }
 
  .app-nav-rail .scroll-arrow:hover {
    transform: scale(1.15);
  }
 
  .app-nav-rail .scroll-arrow:active {
    transform: scale(0.9);
  }
 
  /* Sits right after the pinned part */
  .app-nav-rail .scroll-arrow-left {
    left: calc(var(--app-nav-pinned-width) + 6px);
  }
 
  .app-nav-rail .scroll-arrow-right {
    right: calc(var(--app-nav-end-width) + 6px);
  }
 
  .app-nav-rail .sidebar-end {
    width: var(--app-nav-end-width);
    display: flex;
    align-items: stretch;
  }
 
  .app-nav-rail .scroll-arrow svg {
    color: var(--bw-sidebar-text);
    font-size: 1.1rem;
    filter: drop-shadow(0 1px 2px rgba(255, 255, 255, 0.5));
  }
}
</style>