directix 1.11.0 → 2.0.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.d.ts CHANGED
@@ -102,6 +102,23 @@ export declare interface AnnounceOptions {
102
102
  */
103
103
  export declare function applyAriaAttributes(el: HTMLElement, config: ARIAConfig): void;
104
104
 
105
+ /**
106
+ * Apply a Vue directive to a custom element
107
+ *
108
+ * @example
109
+ * ```ts
110
+ * import { vLazy } from 'directix'
111
+ * import { applyDirectiveToCustomElement } from 'directix/web-components'
112
+ *
113
+ * const myElement = document.querySelector('my-component')
114
+ * applyDirectiveToCustomElement(myElement, vLazy, { threshold: 0.5 })
115
+ * ```
116
+ */
117
+ export declare function applyDirectiveToCustomElement<T = any>(el: Element, directive: Directive, value?: T, options?: {
118
+ arg?: string;
119
+ modifiers?: Record<string, boolean>;
120
+ }): () => void;
121
+
105
122
  /**
106
123
  * ARIA attribute configuration
107
124
  */
@@ -1008,6 +1025,19 @@ export declare function createDirectiveBenchmark(directiveName: string, setup: (
1008
1025
  measureFullCycle: () => Promise<BenchmarkResult>;
1009
1026
  };
1010
1027
 
1028
+ /**
1029
+ * Create a directive element wrapper
1030
+ *
1031
+ * @example
1032
+ * ```ts
1033
+ * import { createDirectiveElement, vLazy } from 'directix'
1034
+ *
1035
+ * const LazyImage = createDirectiveElement('lazy-image', vLazy)
1036
+ * customElements.define('lazy-image', LazyImage)
1037
+ * ```
1038
+ */
1039
+ export declare function createDirectiveElement(_name: string, directive: Directive, options?: Omit<CustomElementDirectiveOptions, 'name' | 'directive'>): CustomElementConstructor;
1040
+
1011
1041
  /**
1012
1042
  * Create a directive from a template
1013
1043
  *
@@ -1264,8 +1294,9 @@ export declare function createUppercaser(first?: boolean): (text: string) => str
1264
1294
  export declare function createVue2Directive<T, B extends Element>(hooks: DirectiveHooks<T, B>): Record<string, any>;
1265
1295
 
1266
1296
  /**
1267
- * Vue 3 directive adapter
1268
- * @returns Vue 3 directive object with created/mounted/updated/unmounted hooks
1297
+ * Create Vue 3 directive with optimizations
1298
+ * Uses markRaw for DOM elements and shallowReactive for state
1299
+ * @returns Vue 3 directive object
1269
1300
  */
1270
1301
  export declare function createVue3Directive<T, B extends Element>(hooks: DirectiveHooks<T, B>): Record<string, any>;
1271
1302
 
@@ -1311,6 +1342,22 @@ export declare interface CSPConfig {
1311
1342
  nonce?: string;
1312
1343
  }
1313
1344
 
1345
+ /**
1346
+ * Options for creating a custom element directive
1347
+ */
1348
+ export declare interface CustomElementDirectiveOptions {
1349
+ /** The element name (must contain a hyphen) */
1350
+ name: string;
1351
+ /** The Vue directive to apply */
1352
+ directive: Directive;
1353
+ /** Default binding value */
1354
+ defaultValue?: any;
1355
+ /** Whether to use shadow DOM */
1356
+ shadow?: boolean;
1357
+ /** Shadow DOM mode */
1358
+ shadowMode?: 'open' | 'closed';
1359
+ }
1360
+
1314
1361
  declare interface CustomIntegration {
1315
1362
  enabled: boolean;
1316
1363
  pushMetrics: (metrics: Metric[]) => Promise<void>;
@@ -1418,6 +1465,23 @@ export declare interface DeferredTask {
1418
1465
  */
1419
1466
  export declare function deferTask(id: string, execute: () => void | Promise<void>, priority?: 'critical' | 'high' | 'medium' | 'low'): void;
1420
1467
 
1468
+ /**
1469
+ * Define a custom element that wraps a Vue directive
1470
+ *
1471
+ * @example
1472
+ * ```ts
1473
+ * import { vClickOutside, defineCustomElementDirective } from 'directix'
1474
+ *
1475
+ * defineCustomElementDirective({
1476
+ * name: 'click-outside',
1477
+ * directive: vClickOutside,
1478
+ * })
1479
+ *
1480
+ * // Now you can use: <click-outside></click-outside>
1481
+ * ```
1482
+ */
1483
+ export declare function defineCustomElementDirective(options: CustomElementDirectiveOptions): void;
1484
+
1421
1485
  /**
1422
1486
  * Define a cross-version compatible directive
1423
1487
  * @param definition The directive definition
@@ -2833,6 +2897,11 @@ export declare const isBrowser: () => boolean;
2833
2897
  */
2834
2898
  export declare function isBrowserSupported(browserName: string, version: number): boolean;
2835
2899
 
2900
+ /**
2901
+ * Check if an element is a custom element
2902
+ */
2903
+ export declare function isCustomElement(el: Element): boolean;
2904
+
2836
2905
  /**
2837
2906
  * Check if DevTools is available
2838
2907
  */
@@ -4013,6 +4082,21 @@ export declare function quickPrint(target: string | HTMLElement, options?: UsePr
4013
4082
  */
4014
4083
  export declare function recordHistogram(name: string, value: number, labels?: Record<string, string>): void;
4015
4084
 
4085
+ /**
4086
+ * Register multiple directives as custom elements
4087
+ *
4088
+ * @example
4089
+ * ```ts
4090
+ * import { registerDirectiveElements, vLazy, vClickOutside } from 'directix'
4091
+ *
4092
+ * registerDirectiveElements({
4093
+ * 'lazy-img': vLazy,
4094
+ * 'click-outside': vClickOutside,
4095
+ * })
4096
+ * ```
4097
+ */
4098
+ export declare function registerDirectiveElements(elements: Record<string, Directive>): void;
4099
+
4016
4100
  /**
4017
4101
  * Register a polyfill
4018
4102
  */
@@ -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.0.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,8 +58,8 @@ 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.0.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
  */
@@ -78,7 +78,7 @@ var __async = (__this, __arguments, generator) => {
78
78
  },
79
79
  inserted(el, binding, vnode) {
80
80
  if (hooks.mounted) {
81
- hooks.mounted(el, normalizeBinding$1(binding), vnode);
81
+ hooks.mounted(el, normalizeBinding$2(binding), vnode);
82
82
  }
83
83
  },
84
84
  update(el, binding, vnode, oldVnode) {
@@ -86,9 +86,9 @@ var __async = (__this, __arguments, generator) => {
86
86
  if (hooks.updated) {
87
87
  hooks.updated(
88
88
  el,
89
- normalizeBinding$1(binding),
89
+ normalizeBinding$2(binding),
90
90
  vnode,
91
- normalizeBinding$1(__spreadProps(__spreadValues({}, binding), { value: binding.oldValue })),
91
+ normalizeBinding$2(__spreadProps(__spreadValues({}, binding), { value: binding.oldValue })),
92
92
  oldVnode
93
93
  );
94
94
  }
@@ -101,7 +101,7 @@ var __async = (__this, __arguments, generator) => {
101
101
  },
102
102
  unbind(el, binding, vnode) {
103
103
  if (hooks.unmounted) {
104
- hooks.unmounted(el, normalizeBinding$1(binding), vnode);
104
+ hooks.unmounted(el, normalizeBinding$2(binding), vnode);
105
105
  }
106
106
  const state2 = el.__directix_state__;
107
107
  if (state2 == null ? void 0 : state2.cleanup) {
@@ -112,7 +112,7 @@ var __async = (__this, __arguments, generator) => {
112
112
  };
113
113
  return directive;
114
114
  }
115
- function normalizeBinding$1(binding) {
115
+ function normalizeBinding$2(binding) {
116
116
  var _a2;
117
117
  return {
118
118
  value: binding.value,
@@ -129,20 +129,20 @@ var __async = (__this, __arguments, generator) => {
129
129
  }
130
130
  }
131
131
  function createVue3Directive(hooks) {
132
- const directive = {
132
+ return {
133
133
  created(el, binding, vnode) {
134
- const state2 = {
135
- value: binding.value,
134
+ const state2 = vue.shallowReactive({
135
+ value: binding.value != null ? vue.markRaw(binding.value) : binding.value,
136
136
  vnode,
137
137
  cleanup: []
138
- };
138
+ });
139
139
  el.__directix_state__ = state2;
140
140
  },
141
141
  beforeMount(_el, _binding, _vnode) {
142
142
  },
143
143
  mounted(el, binding, vnode) {
144
144
  if (hooks.mounted) {
145
- hooks.mounted(el, normalizeBindingVue3(binding), vnode);
145
+ hooks.mounted(el, normalizeBinding$1(binding), vnode);
146
146
  }
147
147
  },
148
148
  beforeUpdate(_el, _binding, _vnode, _prevVnode) {
@@ -152,14 +152,14 @@ var __async = (__this, __arguments, generator) => {
152
152
  if (hooks.updated) {
153
153
  hooks.updated(
154
154
  el,
155
- normalizeBindingVue3(binding),
155
+ normalizeBinding$1(binding),
156
156
  vnode,
157
- normalizeBindingVue3(__spreadProps(__spreadValues({}, binding), { value: binding.oldValue })),
157
+ normalizeBinding$1(__spreadProps(__spreadValues({}, binding), { value: binding.oldValue })),
158
158
  prevVnode
159
159
  );
160
160
  }
161
161
  if (state2) {
162
- state2.value = binding.value;
162
+ state2.value = binding.value != null ? vue.markRaw(binding.value) : binding.value;
163
163
  state2.vnode = vnode;
164
164
  }
165
165
  },
@@ -167,7 +167,7 @@ var __async = (__this, __arguments, generator) => {
167
167
  },
168
168
  unmounted(el, binding, vnode) {
169
169
  if (hooks.unmounted) {
170
- hooks.unmounted(el, normalizeBindingVue3(binding), vnode);
170
+ hooks.unmounted(el, normalizeBinding$1(binding), vnode);
171
171
  }
172
172
  const state2 = el.__directix_state__;
173
173
  if (state2 == null ? void 0 : state2.cleanup) {
@@ -176,9 +176,8 @@ var __async = (__this, __arguments, generator) => {
176
176
  delete el.__directix_state__;
177
177
  }
178
178
  };
179
- return directive;
180
179
  }
181
- function normalizeBindingVue3(binding) {
180
+ function normalizeBinding$1(binding) {
182
181
  var _a2;
183
182
  return {
184
183
  value: binding.value,
@@ -21952,6 +21951,121 @@ ${(_a2 = new Error("Stack trace").stack) == null ? void 0 : _a2.split("\n").slic
21952
21951
  }
21953
21952
  };
21954
21953
  }
21954
+ function isCustomElement(el) {
21955
+ return el.tagName.includes("-") || customElements.get(el.tagName.toLowerCase()) !== void 0;
21956
+ }
21957
+ function applyDirectiveToCustomElement(el, directive, value, options) {
21958
+ const binding = {
21959
+ value,
21960
+ oldValue: null,
21961
+ arg: options == null ? void 0 : options.arg,
21962
+ modifiers: (options == null ? void 0 : options.modifiers) || {},
21963
+ instance: null
21964
+ };
21965
+ const vnode = { el };
21966
+ const mountedHook = directive.mounted;
21967
+ if (mountedHook) {
21968
+ mountedHook(el, binding, vnode, null);
21969
+ }
21970
+ return () => {
21971
+ const unmountedHook = directive.unmounted;
21972
+ if (unmountedHook) {
21973
+ unmountedHook(el, binding, vnode, null);
21974
+ }
21975
+ };
21976
+ }
21977
+ function defineCustomElementDirective(options) {
21978
+ const { name, directive, defaultValue, shadow = false, shadowMode = "open" } = options;
21979
+ class DirectiveCustomElement extends HTMLElement {
21980
+ constructor() {
21981
+ super();
21982
+ __publicField(this, "cleanup");
21983
+ __publicField(this, "currentValue", defaultValue);
21984
+ if (shadow) {
21985
+ this.attachShadow({ mode: shadowMode });
21986
+ }
21987
+ }
21988
+ connectedCallback() {
21989
+ this.cleanup = applyDirectiveToCustomElement(
21990
+ this,
21991
+ directive,
21992
+ this.currentValue
21993
+ );
21994
+ }
21995
+ disconnectedCallback() {
21996
+ if (this.cleanup) {
21997
+ this.cleanup();
21998
+ this.cleanup = void 0;
21999
+ }
22000
+ }
22001
+ // Allow setting value via attribute
22002
+ static get observedAttributes() {
22003
+ return ["value"];
22004
+ }
22005
+ attributeChangedCallback(attrName, oldValue, newValue) {
22006
+ if (attrName === "value" && oldValue !== newValue) {
22007
+ this.currentValue = newValue;
22008
+ if (this.cleanup) {
22009
+ this.cleanup();
22010
+ }
22011
+ this.cleanup = applyDirectiveToCustomElement(
22012
+ this,
22013
+ directive,
22014
+ this.currentValue
22015
+ );
22016
+ }
22017
+ }
22018
+ }
22019
+ customElements.define(name, DirectiveCustomElement);
22020
+ }
22021
+ function createDirectiveElement(_name, directive, options) {
22022
+ const { defaultValue, shadow = false, shadowMode = "open" } = options || {};
22023
+ return class extends HTMLElement {
22024
+ constructor() {
22025
+ super();
22026
+ __publicField(this, "cleanup");
22027
+ __publicField(this, "currentValue", defaultValue);
22028
+ if (shadow) {
22029
+ this.attachShadow({ mode: shadowMode });
22030
+ }
22031
+ }
22032
+ connectedCallback() {
22033
+ this.cleanup = applyDirectiveToCustomElement(
22034
+ this,
22035
+ directive,
22036
+ this.currentValue
22037
+ );
22038
+ }
22039
+ disconnectedCallback() {
22040
+ if (this.cleanup) {
22041
+ this.cleanup();
22042
+ this.cleanup = void 0;
22043
+ }
22044
+ }
22045
+ static get observedAttributes() {
22046
+ return ["value"];
22047
+ }
22048
+ attributeChangedCallback(_attrName, oldValue, newValue) {
22049
+ if (oldValue !== newValue) {
22050
+ this.currentValue = newValue;
22051
+ if (this.cleanup) {
22052
+ this.cleanup();
22053
+ }
22054
+ this.cleanup = applyDirectiveToCustomElement(
22055
+ this,
22056
+ directive,
22057
+ this.currentValue
22058
+ );
22059
+ }
22060
+ }
22061
+ };
22062
+ }
22063
+ function registerDirectiveElements(elements) {
22064
+ Object.entries(elements).forEach(([elementName, elementDirective]) => {
22065
+ const elementClass = createDirectiveElement(elementName, elementDirective);
22066
+ customElements.define(elementName, elementClass);
22067
+ });
22068
+ }
21955
22069
  const zhCN = {
21956
22070
  directives: {
21957
22071
  debounce: {
@@ -24066,6 +24180,7 @@ ${(_a2 = new Error("Stack trace").stack) == null ? void 0 : _a2.split("\n").slic
24066
24180
  exports.addPassiveListener = addPassiveListener;
24067
24181
  exports.announce = announce;
24068
24182
  exports.applyAriaAttributes = applyAriaAttributes;
24183
+ exports.applyDirectiveToCustomElement = applyDirectiveToCustomElement;
24069
24184
  exports.assert = assert;
24070
24185
  exports.assertPositive = assertPositive;
24071
24186
  exports.assertRange = assertRange;
@@ -24113,6 +24228,7 @@ ${(_a2 = new Error("Stack trace").stack) == null ? void 0 : _a2.split("\n").slic
24113
24228
  exports.createDelayedClick = createDelayedClick;
24114
24229
  exports.createDeprecationWarning = createDeprecationWarning;
24115
24230
  exports.createDirectiveBenchmark = createDirectiveBenchmark;
24231
+ exports.createDirectiveElement = createDirectiveElement;
24116
24232
  exports.createDirectiveTemplate = createDirectiveTemplate;
24117
24233
  exports.createEventDirective = createEventDirective;
24118
24234
  exports.createI18n = createI18n;
@@ -24137,6 +24253,7 @@ ${(_a2 = new Error("Stack trace").stack) == null ? void 0 : _a2.split("\n").slic
24137
24253
  exports.deepMerge = deepMerge;
24138
24254
  exports.deferNonCriticalDirective = deferNonCriticalDirective;
24139
24255
  exports.deferTask = deferTask;
24256
+ exports.defineCustomElementDirective = defineCustomElementDirective;
24140
24257
  exports.defineDirective = defineDirective;
24141
24258
  exports.defineDirectiveGroup = defineDirectiveGroup;
24142
24259
  exports.definePlugin = definePlugin;
@@ -24265,6 +24382,7 @@ ${(_a2 = new Error("Stack trace").stack) == null ? void 0 : _a2.split("\n").slic
24265
24382
  exports.isBoolean = isBoolean;
24266
24383
  exports.isBrowser = isBrowser;
24267
24384
  exports.isBrowserSupported = isBrowserSupported;
24385
+ exports.isCustomElement = isCustomElement;
24268
24386
  exports.isDOMReady = isDOMReady;
24269
24387
  exports.isDevtoolsAvailable = isDevtoolsAvailable;
24270
24388
  exports.isEmpty = isEmpty;
@@ -24314,6 +24432,7 @@ ${(_a2 = new Error("Stack trace").stack) == null ? void 0 : _a2.split("\n").slic
24314
24432
  exports.preloadModule = preloadModule;
24315
24433
  exports.quickPrint = quickPrint;
24316
24434
  exports.recordHistogram = recordHistogram;
24435
+ exports.registerDirectiveElements = registerDirectiveElements;
24317
24436
  exports.registerPolyfill = registerPolyfill;
24318
24437
  exports.removeHealthCheck = removeHealthCheck;
24319
24438
  exports.requestIdleCallback = requestIdleCallback;