useHeader.ts 1.0 KB

1234567891011121314151617181920212223242526272829303132333435
  1. // Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. import type { ComputedRef, Ref, UnwrapRef } from 'vue'
  3. import { onBeforeUnmount, onBeforeMount, ref, watch } from 'vue'
  4. import type { RouteLocationRaw } from 'vue-router'
  5. import useMetaTitle from '@shared/composables/useMetaTitle'
  6. export interface HeaderOptions {
  7. title?: string | ComputedRef<string>
  8. titleClass?: string | ComputedRef<string>
  9. backTitle?: string | ComputedRef<string>
  10. backUrl?: RouteLocationRaw | ComputedRef<RouteLocationRaw>
  11. actionTitle?: string | ComputedRef<string>
  12. actionDisabled?: boolean | ComputedRef<boolean> | Ref<boolean>
  13. onAction?(): unknown
  14. }
  15. export const headerOptions = ref<HeaderOptions>({})
  16. const { setViewTitle } = useMetaTitle()
  17. watch(
  18. () => headerOptions.value.title,
  19. (title) => title && setViewTitle(title),
  20. )
  21. export const useHeader = (options: HeaderOptions) => {
  22. onBeforeMount(() => {
  23. headerOptions.value = options as UnwrapRef<HeaderOptions>
  24. })
  25. onBeforeUnmount(() => {
  26. headerOptions.value = {}
  27. })
  28. }