generated from adobe/aem-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 174
/
utils.js
55 lines (49 loc) · 1.53 KB
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
export function debounce(func, delay) {
let debounceTimer;
return function () {
const context = this;
const args = arguments;
clearTimeout(debounceTimer);
debounceTimer = setTimeout(() => func.apply(context, args), delay);
};
}
export const getSlotText = (element, name) =>
element.querySelector(`[slot="${name}"]`).textContent.trim();
/**
* Helper function to create an element with attributes
* @param {string} tag
* @param {Object} attributes
* @param {*} content
* @returns {HTMLElement}
*/
export function createTag(tag, attributes = {}, content) {
const element = document.createElement(tag);
if (content instanceof HTMLElement) {
element.appendChild(content);
} else {
element.innerHTML = content;
}
// Set attributes
for (const [key, value] of Object.entries(attributes)) {
element.setAttribute(key, value);
}
return element;
}
/**
* Checks if the current device is mobile based on the screen width.
* @returns {boolean} True if the device is mobile, otherwise false.
*/
export function isMobile() {
return window.matchMedia('(max-width: 767px)').matches;
}
/**
* Checks if the current device is mobile or tablet based on the screen width.
* @returns {boolean} True if the device is mobile, otherwise false.
*/
export function isMobileOrTablet() {
return window.matchMedia('(max-width: 1024px)').matches;
}
/* c8 ignore next 4 */
export function wait(ms = 1000) {
return new Promise((resolve) => setTimeout(resolve, ms));
}