toggle-widgets.ts 436 B

12345678910111213141516171819202122
  1. import { watch } from 'vue'
  2. import { isClient } from '@vueuse/core'
  3. import type { Ref } from 'vue'
  4. export const useToggleWidgets = (
  5. watchSource: Ref<boolean>,
  6. handler: (e: Event) => void
  7. ) => {
  8. if (!isClient) return
  9. watch(
  10. () => watchSource.value,
  11. (val) => {
  12. if (val) {
  13. window.addEventListener('resize', handler)
  14. } else {
  15. window.removeEventListener('resize', handler)
  16. }
  17. }
  18. )
  19. }