useUserTaskbarTabLink.ts 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import { storeToRefs } from 'pinia'
  3. import { computed, ref, watch, type Ref } from 'vue'
  4. import type { UserTaskbarTab } from '#desktop/components/UserTaskbarTabs/types.ts'
  5. import { useUserCurrentTaskbarTabsStore } from '#desktop/entities/user/current/stores/taskbarTabs.ts'
  6. type CommonLinkInstance = {
  7. $el?: HTMLElement
  8. }
  9. export const useUserTaskbarTabLink = (
  10. taskbarTab: Ref<UserTaskbarTab>,
  11. exactActiveCallback?: () => void,
  12. ) => {
  13. const tabLinkInstance = ref<CommonLinkInstance>()
  14. const { activeTaskbarTabEntityKey } = storeToRefs(
  15. useUserCurrentTaskbarTabsStore(),
  16. )
  17. const taskbarTabActive = computed(
  18. () => activeTaskbarTabEntityKey.value === taskbarTab.value.tabEntityKey,
  19. )
  20. watch(
  21. [taskbarTabActive, tabLinkInstance],
  22. ([isActive, instance]) => {
  23. if (!isActive || !instance) return
  24. exactActiveCallback?.()
  25. // Scroll the tab into view when it becomes active and is not visible within the viewport.
  26. /**
  27. * @checkVisibility supported by all major browser
  28. * @source https://caniuse.com/?search=checkVisibility
  29. * In case if not we return true
  30. */
  31. setTimeout(() => {
  32. // We have to set this to the event loop tick to wait the route has been updated
  33. if (instance.$el?.checkVisibility?.() ?? true) {
  34. instance.$el?.scrollIntoView?.({ block: 'nearest' })
  35. }
  36. }, 0)
  37. },
  38. { immediate: true, flush: 'post' },
  39. )
  40. return {
  41. tabLinkInstance,
  42. taskbarTabActive,
  43. }
  44. }