directix 1.11.0 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs CHANGED
@@ -1,6 +1,6 @@
1
1
  /*!
2
- * directix v1.11.0
3
- * A comprehensive, easy-to-use, and high-performance Vue custom directives library supporting both Vue 2 and Vue 3
2
+ * directix v2.1.0
3
+ * A comprehensive, easy-to-use, and high-performance Vue custom directives library supporting both Vue 2 and Vue 3, with Web Components support
4
4
  * (c) 2021-present saqqdy <https://github.com/saqqdy>
5
5
  * Released under the MIT License.
6
6
  */
@@ -58,13 +58,13 @@ var __async = (__this, __arguments, generator) => {
58
58
  });
59
59
  };
60
60
  /*!
61
- * directix v1.11.0
62
- * A comprehensive, easy-to-use, and high-performance Vue custom directives library supporting both Vue 2 and Vue 3
61
+ * directix v2.1.0
62
+ * A comprehensive, easy-to-use, and high-performance Vue custom directives library supporting both Vue 2 and Vue 3, with Web Components support
63
63
  * (c) 2021-present saqqdy <https://github.com/saqqdy>
64
64
  * Released under the MIT License.
65
65
  */
66
66
  var _a;
67
- import { ref, unref, onUnmounted, watch, readonly, shallowRef, computed, onMounted, reactive, watchEffect, isRef, toRaw, markRaw } from "vue";
67
+ import { markRaw, shallowReactive, ref, unref, onUnmounted, watch, readonly, shallowRef, computed, onMounted, reactive, watchEffect, isRef, toRaw } from "vue";
68
68
  function createVue2Directive(hooks) {
69
69
  const directive = {
70
70
  bind(el, binding, vnode) {
@@ -77,7 +77,7 @@ function createVue2Directive(hooks) {
77
77
  },
78
78
  inserted(el, binding, vnode) {
79
79
  if (hooks.mounted) {
80
- hooks.mounted(el, normalizeBinding$1(binding), vnode);
80
+ hooks.mounted(el, normalizeBinding$2(binding), vnode);
81
81
  }
82
82
  },
83
83
  update(el, binding, vnode, oldVnode) {
@@ -85,9 +85,9 @@ function createVue2Directive(hooks) {
85
85
  if (hooks.updated) {
86
86
  hooks.updated(
87
87
  el,
88
- normalizeBinding$1(binding),
88
+ normalizeBinding$2(binding),
89
89
  vnode,
90
- normalizeBinding$1(__spreadProps(__spreadValues({}, binding), { value: binding.oldValue })),
90
+ normalizeBinding$2(__spreadProps(__spreadValues({}, binding), { value: binding.oldValue })),
91
91
  oldVnode
92
92
  );
93
93
  }
@@ -100,7 +100,7 @@ function createVue2Directive(hooks) {
100
100
  },
101
101
  unbind(el, binding, vnode) {
102
102
  if (hooks.unmounted) {
103
- hooks.unmounted(el, normalizeBinding$1(binding), vnode);
103
+ hooks.unmounted(el, normalizeBinding$2(binding), vnode);
104
104
  }
105
105
  const state2 = el.__directix_state__;
106
106
  if (state2 == null ? void 0 : state2.cleanup) {
@@ -111,7 +111,7 @@ function createVue2Directive(hooks) {
111
111
  };
112
112
  return directive;
113
113
  }
114
- function normalizeBinding$1(binding) {
114
+ function normalizeBinding$2(binding) {
115
115
  var _a2;
116
116
  return {
117
117
  value: binding.value,
@@ -128,20 +128,20 @@ function addCleanup$1(el, fn) {
128
128
  }
129
129
  }
130
130
  function createVue3Directive(hooks) {
131
- const directive = {
131
+ return {
132
132
  created(el, binding, vnode) {
133
- const state2 = {
134
- value: binding.value,
133
+ const state2 = shallowReactive({
134
+ value: binding.value != null ? markRaw(binding.value) : binding.value,
135
135
  vnode,
136
136
  cleanup: []
137
- };
137
+ });
138
138
  el.__directix_state__ = state2;
139
139
  },
140
140
  beforeMount(_el, _binding, _vnode) {
141
141
  },
142
142
  mounted(el, binding, vnode) {
143
143
  if (hooks.mounted) {
144
- hooks.mounted(el, normalizeBindingVue3(binding), vnode);
144
+ hooks.mounted(el, normalizeBinding$1(binding), vnode);
145
145
  }
146
146
  },
147
147
  beforeUpdate(_el, _binding, _vnode, _prevVnode) {
@@ -151,14 +151,14 @@ function createVue3Directive(hooks) {
151
151
  if (hooks.updated) {
152
152
  hooks.updated(
153
153
  el,
154
- normalizeBindingVue3(binding),
154
+ normalizeBinding$1(binding),
155
155
  vnode,
156
- normalizeBindingVue3(__spreadProps(__spreadValues({}, binding), { value: binding.oldValue })),
156
+ normalizeBinding$1(__spreadProps(__spreadValues({}, binding), { value: binding.oldValue })),
157
157
  prevVnode
158
158
  );
159
159
  }
160
160
  if (state2) {
161
- state2.value = binding.value;
161
+ state2.value = binding.value != null ? markRaw(binding.value) : binding.value;
162
162
  state2.vnode = vnode;
163
163
  }
164
164
  },
@@ -166,7 +166,7 @@ function createVue3Directive(hooks) {
166
166
  },
167
167
  unmounted(el, binding, vnode) {
168
168
  if (hooks.unmounted) {
169
- hooks.unmounted(el, normalizeBindingVue3(binding), vnode);
169
+ hooks.unmounted(el, normalizeBinding$1(binding), vnode);
170
170
  }
171
171
  const state2 = el.__directix_state__;
172
172
  if (state2 == null ? void 0 : state2.cleanup) {
@@ -175,9 +175,8 @@ function createVue3Directive(hooks) {
175
175
  delete el.__directix_state__;
176
176
  }
177
177
  };
178
- return directive;
179
178
  }
180
- function normalizeBindingVue3(binding) {
179
+ function normalizeBinding$1(binding) {
181
180
  var _a2;
182
181
  return {
183
182
  value: binding.value,
@@ -21951,6 +21950,163 @@ function teleportContent(content, options) {
21951
21950
  }
21952
21951
  };
21953
21952
  }
21953
+ function isCustomElement(el) {
21954
+ return el.tagName.includes("-") || customElements.get(el.tagName.toLowerCase()) !== void 0;
21955
+ }
21956
+ function applyDirectiveToCustomElement(el, directive, value, options) {
21957
+ const binding = {
21958
+ value,
21959
+ oldValue: null,
21960
+ arg: options == null ? void 0 : options.arg,
21961
+ modifiers: (options == null ? void 0 : options.modifiers) || {},
21962
+ instance: null
21963
+ };
21964
+ const vnode = { el };
21965
+ const mountedHook = directive.mounted;
21966
+ if (mountedHook) {
21967
+ mountedHook(el, binding, vnode, null);
21968
+ }
21969
+ return () => {
21970
+ const unmountedHook = directive.unmounted;
21971
+ if (unmountedHook) {
21972
+ unmountedHook(el, binding, vnode, null);
21973
+ }
21974
+ };
21975
+ }
21976
+ function defineCustomElementDirective(options) {
21977
+ const { name, directive, defaultValue, shadow = false, shadowMode = "open" } = options;
21978
+ class DirectiveCustomElement extends HTMLElement {
21979
+ constructor() {
21980
+ super();
21981
+ __publicField(this, "cleanup");
21982
+ __publicField(this, "currentValue", defaultValue);
21983
+ if (shadow) {
21984
+ this.attachShadow({ mode: shadowMode });
21985
+ }
21986
+ }
21987
+ connectedCallback() {
21988
+ this.cleanup = applyDirectiveToCustomElement(
21989
+ this,
21990
+ directive,
21991
+ this.currentValue
21992
+ );
21993
+ }
21994
+ disconnectedCallback() {
21995
+ if (this.cleanup) {
21996
+ this.cleanup();
21997
+ this.cleanup = void 0;
21998
+ }
21999
+ }
22000
+ // Allow setting value via attribute
22001
+ static get observedAttributes() {
22002
+ return ["value"];
22003
+ }
22004
+ attributeChangedCallback(attrName, oldValue, newValue) {
22005
+ if (attrName === "value" && oldValue !== newValue) {
22006
+ this.currentValue = newValue;
22007
+ if (this.cleanup) {
22008
+ this.cleanup();
22009
+ }
22010
+ this.cleanup = applyDirectiveToCustomElement(
22011
+ this,
22012
+ directive,
22013
+ this.currentValue
22014
+ );
22015
+ }
22016
+ }
22017
+ }
22018
+ customElements.define(name, DirectiveCustomElement);
22019
+ }
22020
+ function createDirectiveElement(_name, directive, options) {
22021
+ const { defaultValue, shadow = false, shadowMode = "open" } = options || {};
22022
+ return class extends HTMLElement {
22023
+ constructor() {
22024
+ super();
22025
+ __publicField(this, "cleanup");
22026
+ __publicField(this, "currentValue", defaultValue);
22027
+ if (shadow) {
22028
+ this.attachShadow({ mode: shadowMode });
22029
+ }
22030
+ }
22031
+ connectedCallback() {
22032
+ this.cleanup = applyDirectiveToCustomElement(
22033
+ this,
22034
+ directive,
22035
+ this.currentValue
22036
+ );
22037
+ }
22038
+ disconnectedCallback() {
22039
+ if (this.cleanup) {
22040
+ this.cleanup();
22041
+ this.cleanup = void 0;
22042
+ }
22043
+ }
22044
+ static get observedAttributes() {
22045
+ return ["value"];
22046
+ }
22047
+ attributeChangedCallback(_attrName, oldValue, newValue) {
22048
+ if (oldValue !== newValue) {
22049
+ this.currentValue = newValue;
22050
+ if (this.cleanup) {
22051
+ this.cleanup();
22052
+ }
22053
+ this.cleanup = applyDirectiveToCustomElement(
22054
+ this,
22055
+ directive,
22056
+ this.currentValue
22057
+ );
22058
+ }
22059
+ }
22060
+ };
22061
+ }
22062
+ function registerDirectiveElements(elements) {
22063
+ Object.entries(elements).forEach(([elementName, elementDirective]) => {
22064
+ const elementClass = createDirectiveElement(elementName, elementDirective);
22065
+ customElements.define(elementName, elementClass);
22066
+ });
22067
+ }
22068
+ function isCustomElementDefined(name) {
22069
+ return customElements.get(name) !== void 0;
22070
+ }
22071
+ function whenCustomElementDefined(name) {
22072
+ return __async(this, null, function* () {
22073
+ yield customElements.whenDefined(name);
22074
+ });
22075
+ }
22076
+ function getRegisteredCustomElements() {
22077
+ const registry = customElements.__registry;
22078
+ if (registry && typeof registry.keys === "function") {
22079
+ return Array.from(registry.keys());
22080
+ }
22081
+ return [];
22082
+ }
22083
+ function hydrateCustomElements(root = document.body) {
22084
+ const customElementsList = root.querySelectorAll("*");
22085
+ customElementsList.forEach((el) => {
22086
+ var _a2;
22087
+ if (isCustomElement(el)) {
22088
+ const clone = el.cloneNode(true);
22089
+ (_a2 = el.parentNode) == null ? void 0 : _a2.replaceChild(clone, el);
22090
+ }
22091
+ });
22092
+ }
22093
+ function createSSRSafeCustomElement(name, directive, options) {
22094
+ const { styles, shadow = false } = options || {};
22095
+ const ssrRender = (attrs = {}) => {
22096
+ const attrString = Object.entries(attrs).map(([key, value]) => `${key}="${value}"`).join(" ");
22097
+ if (shadow) {
22098
+ const stylesString = styles ? `<style>${Array.isArray(styles) ? styles.join("") : styles}</style>` : "";
22099
+ return `<${name} ${attrString}><template shadowroot="open">${stylesString}<slot></slot></template></${name}>`;
22100
+ }
22101
+ return `<${name} ${attrString}></${name}>`;
22102
+ };
22103
+ const elementClass = typeof window === "undefined" ? class extends HTMLElement {
22104
+ } : createDirectiveElement(name, directive, options);
22105
+ return {
22106
+ elementClass,
22107
+ ssrRender
22108
+ };
22109
+ }
21954
22110
  const zhCN = {
21955
22111
  directives: {
21956
22112
  debounce: {
@@ -24066,6 +24222,7 @@ export {
24066
24222
  addPassiveListener,
24067
24223
  announce,
24068
24224
  applyAriaAttributes,
24225
+ applyDirectiveToCustomElement,
24069
24226
  assert,
24070
24227
  assertPositive,
24071
24228
  assertRange,
@@ -24113,6 +24270,7 @@ export {
24113
24270
  createDelayedClick,
24114
24271
  createDeprecationWarning,
24115
24272
  createDirectiveBenchmark,
24273
+ createDirectiveElement,
24116
24274
  createDirectiveTemplate,
24117
24275
  createEventDirective,
24118
24276
  createI18n,
@@ -24123,6 +24281,7 @@ export {
24123
24281
  createPerformanceBudget,
24124
24282
  createPermissionCheck,
24125
24283
  createPermissionChecker,
24284
+ createSSRSafeCustomElement,
24126
24285
  createSafeContentHandler,
24127
24286
  createSafeDirectiveWrapper,
24128
24287
  createStyleDirective,
@@ -24137,6 +24296,7 @@ export {
24137
24296
  deepMerge,
24138
24297
  deferNonCriticalDirective,
24139
24298
  deferTask,
24299
+ defineCustomElementDirective,
24140
24300
  defineDirective,
24141
24301
  defineDirectiveGroup,
24142
24302
  definePlugin,
@@ -24242,6 +24402,7 @@ export {
24242
24402
  getPluginManager,
24243
24403
  getPluginRegistry,
24244
24404
  getPolyfillStatus,
24405
+ getRegisteredCustomElements,
24245
24406
  getSlowestDirectives,
24246
24407
  getSupportedRegions,
24247
24408
  getTimezoneInfo,
@@ -24252,6 +24413,7 @@ export {
24252
24413
  handleTouchConflict,
24253
24414
  hasPermission$2 as hasPermission,
24254
24415
  hasPermissionSync,
24416
+ hydrateCustomElements,
24255
24417
  importConfig,
24256
24418
  incrementCounter,
24257
24419
  info,
@@ -24265,6 +24427,8 @@ export {
24265
24427
  isBoolean,
24266
24428
  isBrowser,
24267
24429
  isBrowserSupported,
24430
+ isCustomElement,
24431
+ isCustomElementDefined,
24268
24432
  isDOMReady,
24269
24433
  isDevtoolsAvailable,
24270
24434
  isEmpty,
@@ -24314,6 +24478,7 @@ export {
24314
24478
  preloadModule,
24315
24479
  quickPrint,
24316
24480
  recordHistogram,
24481
+ registerDirectiveElements,
24317
24482
  registerPolyfill,
24318
24483
  removeHealthCheck,
24319
24484
  requestIdleCallback,
@@ -24501,6 +24666,7 @@ export {
24501
24666
  warnUnsupportedFeatures,
24502
24667
  watchConfig,
24503
24668
  watchEffectBinding,
24669
+ whenCustomElementDefined,
24504
24670
  withAuditLog,
24505
24671
  withErrorRecovery,
24506
24672
  withPerformanceMonitoring,