tiny-essentials 1.21.3 → 1.21.4

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.
@@ -2526,6 +2526,27 @@ class TinyHtml {
2526
2526
  blur() {
2527
2527
  return TinyHtml.blur(this);
2528
2528
  }
2529
+ /**
2530
+ * Select the text content of an input or textarea element.
2531
+ *
2532
+ * @param {TinyHtml|HTMLInputElement|HTMLTextAreaElement} el - The element or a selector string.
2533
+ * @returns {TinyHtml|HTMLInputElement|HTMLTextAreaElement}
2534
+ */
2535
+ static select(el) {
2536
+ const elem = TinyHtml._preHtmlElem(el, 'select');
2537
+ if (elem instanceof HTMLInputElement || elem instanceof HTMLTextAreaElement)
2538
+ elem.select();
2539
+ else
2540
+ throw new Error('Element must be an <input> or <textarea> to use select().');
2541
+ return el;
2542
+ }
2543
+ /**
2544
+ * Select the text content of an input or textarea element.
2545
+ * @returns {TinyHtml|HTMLInputElement|HTMLTextAreaElement}
2546
+ */
2547
+ select() {
2548
+ return TinyHtml.select(this);
2549
+ }
2529
2550
  /**
2530
2551
  * Interprets a value as a boolean `true` if it matches a common truthy representation.
2531
2552
  *
@@ -4056,135 +4077,155 @@ class TinyHtml {
4056
4077
  * Registers an event listener on the specified element.
4057
4078
  *
4058
4079
  * @param {TinyEventTarget|TinyEventTarget[]} el - The target to listen on.
4059
- * @param {string} event - The event type (e.g. 'click', 'keydown').
4080
+ * @param {string|string[]} events - The event type (e.g. 'click', 'keydown').
4060
4081
  * @param {EventListenerOrEventListenerObject|null} handler - The callback function to run on event.
4061
4082
  * @param {EventRegistryOptions} [options] - Optional event listener options.
4062
4083
  * @returns {TinyEventTarget|TinyEventTarget[]}
4063
4084
  */
4064
- static on(el, event, handler, options) {
4065
- if (typeof event !== 'string')
4066
- throw new TypeError('The event name must be a string.');
4067
- TinyHtml._preEventTargetElems(el, 'on').forEach((elem) => {
4068
- elem.addEventListener(event, handler, options);
4069
- if (!__eventRegistry.has(elem))
4070
- __eventRegistry.set(elem, {});
4071
- const events = __eventRegistry.get(elem);
4072
- if (!events)
4073
- return;
4074
- if (!Array.isArray(events[event]))
4075
- events[event] = [];
4076
- events[event].push({ handler, options });
4077
- });
4085
+ static on(el, events, handler, options) {
4086
+ if (typeof events !== 'string' &&
4087
+ (!Array.isArray(events) || !events.every((event) => typeof event === 'string')))
4088
+ throw new TypeError('The events must be a string or array of strings.');
4089
+ for (const event of Array.isArray(events) ? Array.from(new Set(events)) : [events]) {
4090
+ if (typeof event !== 'string')
4091
+ throw new TypeError('The event name must be a string.');
4092
+ TinyHtml._preEventTargetElems(el, 'on').forEach((elem) => {
4093
+ elem.addEventListener(event, handler, options);
4094
+ if (!__eventRegistry.has(elem))
4095
+ __eventRegistry.set(elem, {});
4096
+ const events = __eventRegistry.get(elem);
4097
+ if (!events)
4098
+ return;
4099
+ if (!Array.isArray(events[event]))
4100
+ events[event] = [];
4101
+ events[event].push({ handler, options });
4102
+ });
4103
+ }
4078
4104
  return el;
4079
4105
  }
4080
4106
  /**
4081
4107
  * Registers an event listener on the specified element.
4082
4108
  *
4083
- * @param {string} event - The event type (e.g. 'click', 'keydown').
4109
+ * @param {string|string[]} events - The event type (e.g. 'click', 'keydown').
4084
4110
  * @param {EventListenerOrEventListenerObject|null} handler - The callback function to run on event.
4085
4111
  * @param {EventRegistryOptions} [options] - Optional event listener options.
4086
4112
  * @returns {TinyEventTarget|TinyEventTarget[]}
4087
4113
  */
4088
- on(event, handler, options) {
4089
- return TinyHtml.on(this, event, handler, options);
4114
+ on(events, handler, options) {
4115
+ return TinyHtml.on(this, events, handler, options);
4090
4116
  }
4091
4117
  /**
4092
4118
  * Registers an event listener that runs only once, then is removed.
4093
4119
  *
4094
4120
  * @param {TinyEventTarget|TinyEventTarget[]} el - The target to listen on.
4095
- * @param {string} event - The event type (e.g. 'click', 'keydown').
4121
+ * @param {string|string[]} events - The event type (e.g. 'click', 'keydown').
4096
4122
  * @param {EventListenerOrEventListenerObject} handler - The callback function to run on event.
4097
4123
  * @param {EventRegistryOptions} [options={}] - Optional event listener options.
4098
4124
  * @returns {TinyEventTarget|TinyEventTarget[]}
4099
4125
  */
4100
- static once(el, event, handler, options = {}) {
4101
- if (typeof event !== 'string')
4102
- throw new TypeError('The event name must be a string.');
4103
- TinyHtml._preEventTargetElems(el, 'once').forEach((elem) => {
4104
- /** @type {EventListenerOrEventListenerObject} */
4105
- const wrapped = (e) => {
4106
- TinyHtml.off(elem, event, wrapped);
4107
- if (typeof handler === 'function')
4108
- handler(e);
4109
- };
4110
- TinyHtml.on(elem, event, wrapped, typeof options === 'boolean' ? options : { ...options, once: true });
4111
- });
4126
+ static once(el, events, handler, options = {}) {
4127
+ if (typeof events !== 'string' &&
4128
+ (!Array.isArray(events) || !events.every((event) => typeof event === 'string')))
4129
+ throw new TypeError('The events must be a string or array of strings.');
4130
+ for (const event of Array.isArray(events) ? Array.from(new Set(events)) : [events]) {
4131
+ if (typeof event !== 'string')
4132
+ throw new TypeError('The event name must be a string.');
4133
+ TinyHtml._preEventTargetElems(el, 'once').forEach((elem) => {
4134
+ /** @type {EventListenerOrEventListenerObject} */
4135
+ const wrapped = (e) => {
4136
+ TinyHtml.off(elem, event, wrapped);
4137
+ if (typeof handler === 'function')
4138
+ handler(e);
4139
+ };
4140
+ TinyHtml.on(elem, event, wrapped, typeof options === 'boolean' ? options : { ...options, once: true });
4141
+ });
4142
+ }
4112
4143
  return el;
4113
4144
  }
4114
4145
  /**
4115
4146
  * Registers an event listener that runs only once, then is removed.
4116
4147
  *
4117
- * @param {string} event - The event type (e.g. 'click', 'keydown').
4148
+ * @param {string|string[]} events - The event type (e.g. 'click', 'keydown').
4118
4149
  * @param {EventListenerOrEventListenerObject} handler - The callback function to run on event.
4119
4150
  * @param {EventRegistryOptions} [options={}] - Optional event listener options.
4120
4151
  * @returns {TinyEventTarget|TinyEventTarget[]}
4121
4152
  */
4122
- once(event, handler, options = {}) {
4123
- return TinyHtml.once(this, event, handler, options);
4153
+ once(events, handler, options = {}) {
4154
+ return TinyHtml.once(this, events, handler, options);
4124
4155
  }
4125
4156
  /**
4126
4157
  * Removes a specific event listener from an element.
4127
4158
  *
4128
4159
  * @param {TinyEventTarget|TinyEventTarget[]} el - The target element.
4129
- * @param {string} event - The event type.
4160
+ * @param {string|string[]} events - The event type.
4130
4161
  * @param {EventListenerOrEventListenerObject|null} handler - The function originally bound to the event.
4131
4162
  * @param {boolean|EventListenerOptions} [options] - Optional listener options.
4132
4163
  * @returns {TinyEventTarget|TinyEventTarget[]}
4133
4164
  */
4134
- static off(el, event, handler, options) {
4135
- if (typeof event !== 'string')
4136
- throw new TypeError('The event name must be a string.');
4137
- TinyHtml._preEventTargetElems(el, 'off').forEach((elem) => {
4138
- elem.removeEventListener(event, handler, options);
4139
- const events = __eventRegistry.get(elem);
4140
- if (events && events[event]) {
4141
- events[event] = events[event].filter((entry) => entry.handler !== handler);
4142
- if (events[event].length === 0)
4143
- delete events[event];
4144
- }
4145
- });
4165
+ static off(el, events, handler, options) {
4166
+ if (typeof events !== 'string' &&
4167
+ (!Array.isArray(events) || !events.every((event) => typeof event === 'string')))
4168
+ throw new TypeError('The events must be a string or array of strings.');
4169
+ for (const event of Array.isArray(events) ? Array.from(new Set(events)) : [events]) {
4170
+ if (typeof event !== 'string')
4171
+ throw new TypeError('The event name must be a string.');
4172
+ TinyHtml._preEventTargetElems(el, 'off').forEach((elem) => {
4173
+ elem.removeEventListener(event, handler, options);
4174
+ const events = __eventRegistry.get(elem);
4175
+ if (events && events[event]) {
4176
+ events[event] = events[event].filter((entry) => entry.handler !== handler);
4177
+ if (events[event].length === 0)
4178
+ delete events[event];
4179
+ }
4180
+ });
4181
+ }
4146
4182
  return el;
4147
4183
  }
4148
4184
  /**
4149
4185
  * Removes a specific event listener from an element.
4150
4186
  *
4151
- * @param {string} event - The event type.
4187
+ * @param {string|string[]} events - The event type.
4152
4188
  * @param {EventListenerOrEventListenerObject|null} handler - The function originally bound to the event.
4153
4189
  * @param {boolean|EventListenerOptions} [options] - Optional listener options.
4154
4190
  * @returns {TinyEventTarget|TinyEventTarget[]}
4155
4191
  */
4156
- off(event, handler, options) {
4157
- return TinyHtml.off(this, event, handler, options);
4192
+ off(events, handler, options) {
4193
+ return TinyHtml.off(this, events, handler, options);
4158
4194
  }
4159
4195
  /**
4160
4196
  * Removes all event listeners of a specific type from the element.
4161
4197
  *
4162
4198
  * @param {TinyEventTarget|TinyEventTarget[]} el - The target element.
4163
- * @param {string} event - The event type to remove (e.g. 'click').
4199
+ * @param {string|string[]} events - The event type to remove (e.g. 'click').
4164
4200
  * @returns {TinyEventTarget|TinyEventTarget[]}
4165
4201
  */
4166
- static offAll(el, event) {
4167
- if (typeof event !== 'string')
4168
- throw new TypeError('The event name must be a string.');
4169
- TinyHtml._preEventTargetElems(el, 'offAll').forEach((elem) => {
4170
- const events = __eventRegistry.get(elem);
4171
- if (events && events[event]) {
4172
- for (const entry of events[event]) {
4173
- elem.removeEventListener(event, entry.handler, entry.options);
4202
+ static offAll(el, events) {
4203
+ if (typeof events !== 'string' &&
4204
+ (!Array.isArray(events) || !events.every((event) => typeof event === 'string')))
4205
+ throw new TypeError('The events must be a string or array of strings.');
4206
+ for (const event of Array.isArray(events) ? Array.from(new Set(events)) : [events]) {
4207
+ if (typeof event !== 'string')
4208
+ throw new TypeError('The event name must be a string.');
4209
+ TinyHtml._preEventTargetElems(el, 'offAll').forEach((elem) => {
4210
+ const events = __eventRegistry.get(elem);
4211
+ if (events && events[event]) {
4212
+ for (const entry of events[event]) {
4213
+ elem.removeEventListener(event, entry.handler, entry.options);
4214
+ }
4215
+ delete events[event];
4174
4216
  }
4175
- delete events[event];
4176
- }
4177
- });
4217
+ });
4218
+ }
4178
4219
  return el;
4179
4220
  }
4180
4221
  /**
4181
4222
  * Removes all event listeners of a specific type from the element.
4182
4223
  *
4183
- * @param {string} event - The event type to remove (e.g. 'click').
4224
+ * @param {string|string[]} events - The event type to remove (e.g. 'click').
4184
4225
  * @returns {TinyEventTarget|TinyEventTarget[]}
4185
4226
  */
4186
- offAll(event) {
4187
- return TinyHtml.offAll(this, event);
4227
+ offAll(events) {
4228
+ return TinyHtml.offAll(this, events);
4188
4229
  }
4189
4230
  /**
4190
4231
  * Removes all event listeners of all types from the element.
@@ -4226,34 +4267,39 @@ class TinyHtml {
4226
4267
  * Triggers all handlers associated with a specific event on the given element.
4227
4268
  *
4228
4269
  * @param {TinyEventTarget|TinyEventTarget[]} el - Target element where the event should be triggered.
4229
- * @param {string} event - Name of the event to trigger.
4270
+ * @param {string|string[]} events - Name of the event to trigger.
4230
4271
  * @param {Event|CustomEvent|CustomEventInit} [payload] - Optional event object or data to pass.
4231
4272
  * @returns {TinyEventTarget|TinyEventTarget[]}
4232
4273
  */
4233
- static trigger(el, event, payload = {}) {
4234
- if (typeof event !== 'string')
4235
- throw new TypeError('The event name must be a string.');
4236
- TinyHtml._preEventTargetElems(el, 'trigger').forEach((elem) => {
4237
- const evt = payload instanceof Event || payload instanceof CustomEvent
4238
- ? payload
4239
- : new CustomEvent(event, {
4240
- bubbles: true,
4241
- cancelable: true,
4242
- detail: payload,
4243
- });
4244
- elem.dispatchEvent(evt);
4245
- });
4274
+ static trigger(el, events, payload = {}) {
4275
+ if (typeof events !== 'string' &&
4276
+ (!Array.isArray(events) || !events.every((event) => typeof event === 'string')))
4277
+ throw new TypeError('The events must be a string or array of strings.');
4278
+ for (const event of Array.isArray(events) ? Array.from(new Set(events)) : [events]) {
4279
+ if (typeof event !== 'string')
4280
+ throw new TypeError('The event name must be a string.');
4281
+ TinyHtml._preEventTargetElems(el, 'trigger').forEach((elem) => {
4282
+ const evt = payload instanceof Event || payload instanceof CustomEvent
4283
+ ? payload
4284
+ : new CustomEvent(event, {
4285
+ bubbles: true,
4286
+ cancelable: true,
4287
+ detail: payload,
4288
+ });
4289
+ elem.dispatchEvent(evt);
4290
+ });
4291
+ }
4246
4292
  return el;
4247
4293
  }
4248
4294
  /**
4249
4295
  * Triggers all handlers associated with a specific event on the given element.
4250
4296
  *
4251
- * @param {string} event - Name of the event to trigger.
4297
+ * @param {string|string[]} events - Name of the event to trigger.
4252
4298
  * @param {Event|CustomEvent|CustomEventInit} [payload] - Optional event object or data to pass.
4253
4299
  * @returns {TinyEventTarget|TinyEventTarget[]}
4254
4300
  */
4255
- trigger(event, payload = {}) {
4256
- return TinyHtml.trigger(this, event, payload);
4301
+ trigger(events, payload = {}) {
4302
+ return TinyHtml.trigger(this, events, payload);
4257
4303
  }
4258
4304
  ///////////////////////////////////////////////////////////////
4259
4305
  /**
@@ -587,6 +587,17 @@ Checks whether the element exists at the given index.
587
587
 
588
588
  ---
589
589
 
590
+ ### `forEach(callback)`
591
+
592
+ Iterates over all elements in the current instance, executing the provided callback on each one.
593
+
594
+ * **Parameters**:
595
+
596
+ * `callback`: `(element: TinyHtml, index: number, items: TinyHtml[]) => void`
597
+ Function invoked for each element. Receives the current `TinyHtml`, its index, and the full list.
598
+
599
+ ---
600
+
590
601
  ### `get(index)`
591
602
  Returns the raw DOM element associated with this instance.
592
603
 
@@ -2238,6 +2249,20 @@ tinyElem.blur();
2238
2249
 
2239
2250
  ---
2240
2251
 
2252
+ ### 🖊️ `TinyHtml.select(el)` / `el.select()`
2253
+
2254
+ Selects all the text inside the given element.
2255
+
2256
+ ```js
2257
+ TinyHtml.select(element);
2258
+ // or
2259
+ tinyElem.select();
2260
+ ```
2261
+
2262
+ ⚠️ Throws an error if the target element is not an `<input>` or `<textarea>`.
2263
+
2264
+ ---
2265
+
2241
2266
  ## 🌐 Window Scroll & Viewport Helpers
2242
2267
 
2243
2268
  These methods let you control and query scroll positions and viewport size with simple, readable functions.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tiny-essentials",
3
- "version": "1.21.3",
3
+ "version": "1.21.4",
4
4
  "description": "Collection of small, essential scripts designed to be used across various projects. These simple utilities are crafted for speed, ease of use, and versatility.",
5
5
  "scripts": {
6
6
  "test": "npm run test:mjs && npm run test:cjs && npm run test:js",