usePopover.ts 859 B

12345678910111213141516171819202122232425262728293031323334353637
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import { computed, shallowRef } from 'vue'
  3. import type { CommonPopoverInstance } from './types.ts'
  4. import type { ShallowRef, Ref } from 'vue'
  5. export const usePopover = (
  6. popoverRef?: Ref<CommonPopoverInstance | undefined>,
  7. ) => {
  8. const popover: ShallowRef<CommonPopoverInstance | undefined> =
  9. popoverRef || shallowRef()
  10. const popoverTarget: ShallowRef<HTMLDivElement | undefined> = shallowRef()
  11. const isOpen = computed(() => popover.value?.isOpen)
  12. const open = () => {
  13. popover.value?.openPopover()
  14. }
  15. const close = () => {
  16. popover.value?.closePopover()
  17. }
  18. const toggle = (isInteractive = false) => {
  19. popover.value?.togglePopover(isInteractive)
  20. }
  21. return {
  22. popover,
  23. popoverTarget,
  24. open,
  25. close,
  26. toggle,
  27. isOpen,
  28. }
  29. }