useAboutPopup.ts 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import { reactive, readonly, ref } from 'vue'
  2. export type AboutItemKey
  3. = | 'privacyPolicy'
  4. | 'termsOfService'
  5. | 'communityGuideline'
  6. | 'broadcasterAgreement'
  7. | 'aboutUs'
  8. | 'contactUs'
  9. | 'childSafetyPolicy'
  10. | 'playmateAgreement'
  11. export interface AboutPopupOptions {
  12. /**
  13. * Called when user clicks one of the items.
  14. * The popup will be closed automatically after calling it.
  15. */
  16. onSelect?: (key: AboutItemKey) => void
  17. }
  18. export interface AboutPopupState {
  19. visible: boolean
  20. }
  21. const state = reactive<AboutPopupState>({
  22. visible: false,
  23. })
  24. const selectHandler = ref<((key: AboutItemKey) => void) | null>(null)
  25. const open = (options?: AboutPopupOptions) => {
  26. selectHandler.value = options?.onSelect ?? null
  27. state.visible = true
  28. }
  29. const close = () => {
  30. state.visible = false
  31. }
  32. const select = (key: AboutItemKey) => {
  33. const handler = selectHandler.value
  34. if (handler) handler(key)
  35. state.visible = false
  36. }
  37. export const useAboutPopup = () => {
  38. return {
  39. state: readonly(state),
  40. open,
  41. close,
  42. select,
  43. }
  44. }