index.mjs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import { getCurrentInstance, computed, watch, onMounted } from 'vue';
  2. import { buildProp, definePropType } from '../../utils/vue/props/runtime.mjs';
  3. import { isBoolean } from '../../utils/types.mjs';
  4. import { isFunction } from '@vue/shared';
  5. import { isClient } from '@vueuse/core';
  6. const _prop = buildProp({
  7. type: definePropType(Boolean),
  8. default: null
  9. });
  10. const _event = buildProp({
  11. type: definePropType(Function)
  12. });
  13. const createModelToggleComposable = (name) => {
  14. const updateEventKey = `update:${name}`;
  15. const updateEventKeyRaw = `onUpdate:${name}`;
  16. const useModelToggleEmits2 = [updateEventKey];
  17. const useModelToggleProps2 = {
  18. [name]: _prop,
  19. [updateEventKeyRaw]: _event
  20. };
  21. const useModelToggle2 = ({
  22. indicator,
  23. toggleReason,
  24. shouldHideWhenRouteChanges,
  25. shouldProceed,
  26. onShow,
  27. onHide
  28. }) => {
  29. const instance = getCurrentInstance();
  30. const { emit } = instance;
  31. const props = instance.props;
  32. const hasUpdateHandler = computed(() => isFunction(props[updateEventKeyRaw]));
  33. const isModelBindingAbsent = computed(() => props[name] === null);
  34. const doShow = (event) => {
  35. if (indicator.value === true) {
  36. return;
  37. }
  38. indicator.value = true;
  39. if (toggleReason) {
  40. toggleReason.value = event;
  41. }
  42. if (isFunction(onShow)) {
  43. onShow(event);
  44. }
  45. };
  46. const doHide = (event) => {
  47. if (indicator.value === false) {
  48. return;
  49. }
  50. indicator.value = false;
  51. if (toggleReason) {
  52. toggleReason.value = event;
  53. }
  54. if (isFunction(onHide)) {
  55. onHide(event);
  56. }
  57. };
  58. const show = (event) => {
  59. if (props.disabled === true || isFunction(shouldProceed) && !shouldProceed())
  60. return;
  61. const shouldEmit = hasUpdateHandler.value && isClient;
  62. if (shouldEmit) {
  63. emit(updateEventKey, true);
  64. }
  65. if (isModelBindingAbsent.value || !shouldEmit) {
  66. doShow(event);
  67. }
  68. };
  69. const hide = (event) => {
  70. if (props.disabled === true || !isClient)
  71. return;
  72. const shouldEmit = hasUpdateHandler.value && isClient;
  73. if (shouldEmit) {
  74. emit(updateEventKey, false);
  75. }
  76. if (isModelBindingAbsent.value || !shouldEmit) {
  77. doHide(event);
  78. }
  79. };
  80. const onChange = (val) => {
  81. if (!isBoolean(val))
  82. return;
  83. if (props.disabled && val) {
  84. if (hasUpdateHandler.value) {
  85. emit(updateEventKey, false);
  86. }
  87. } else if (indicator.value !== val) {
  88. if (val) {
  89. doShow();
  90. } else {
  91. doHide();
  92. }
  93. }
  94. };
  95. const toggle = () => {
  96. if (indicator.value) {
  97. hide();
  98. } else {
  99. show();
  100. }
  101. };
  102. watch(() => props[name], onChange);
  103. if (shouldHideWhenRouteChanges && instance.appContext.config.globalProperties.$route !== void 0) {
  104. watch(() => ({
  105. ...instance.proxy.$route
  106. }), () => {
  107. if (shouldHideWhenRouteChanges.value && indicator.value) {
  108. hide();
  109. }
  110. });
  111. }
  112. onMounted(() => {
  113. onChange(props[name]);
  114. });
  115. return {
  116. hide,
  117. show,
  118. toggle,
  119. hasUpdateHandler
  120. };
  121. };
  122. return {
  123. useModelToggle: useModelToggle2,
  124. useModelToggleProps: useModelToggleProps2,
  125. useModelToggleEmits: useModelToggleEmits2
  126. };
  127. };
  128. const { useModelToggle, useModelToggleProps, useModelToggleEmits } = createModelToggleComposable("modelValue");
  129. export { createModelToggleComposable, useModelToggle, useModelToggleEmits, useModelToggleProps };
  130. //# sourceMappingURL=index.mjs.map