index.js 3.8 KB

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