nhb-toolbox 3.1.0 → 3.1.1

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.
@@ -4,11 +4,15 @@
4
4
  * @param offset Additional vertical offset in pixels (positive moves down, negative moves up).
5
5
  */
6
6
  export declare function smoothScrollTo(element: HTMLElement, offset?: number): void;
7
- /** * Toggle full-screen using browser API. */
8
- export declare function toggleFullScreen(): void;
9
7
  /**
10
- * * Copy text to clipboard.
11
- * @param text Text to copy in clipboard.
8
+ * * Toggles full-screen mode for a given element (or the `document` by default).
9
+ * @param element The element to toggle fullscreen mode for (default: document root).
10
+ */
11
+ export declare function toggleFullScreen(element?: HTMLElement): void;
12
+ /**
13
+ * * Copies text to the clipboard, falling back to legacy methods if needed.
14
+ * @param text - The text to copy.
15
+ * @returns A promise that resolves when the text is copied.
12
16
  */
13
17
  export declare function copyToClipboard(text: string): Promise<void>;
14
18
  //# sourceMappingURL=utils.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../src/dom/utils.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,wBAAgB,cAAc,CAAC,OAAO,EAAE,WAAW,EAAE,MAAM,SAAI,QAQ9D;AAED,8CAA8C;AAC9C,wBAAgB,gBAAgB,SAM/B;AAED;;;GAGG;AACH,wBAAsB,eAAe,CAAC,IAAI,EAAE,MAAM,iBAMjD"}
1
+ {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../src/dom/utils.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,wBAAgB,cAAc,CAAC,OAAO,EAAE,WAAW,EAAE,MAAM,SAAI,QAQ9D;AAED;;;GAGG;AACH,wBAAgB,gBAAgB,CAAC,OAAO,cAA2B,QAuBlE;AAED;;;;GAIG;AACH,wBAAsB,eAAe,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CA0BjE"}
package/dist/dom/utils.js CHANGED
@@ -11,24 +11,56 @@ export function smoothScrollTo(element, offset = 0) {
11
11
  }, 300); // Delay to ensure smooth scrolling effect
12
12
  }
13
13
  }
14
- /** * Toggle full-screen using browser API. */
15
- export function toggleFullScreen() {
16
- if (!document.fullscreenElement) {
17
- document.documentElement.requestFullscreen();
14
+ /**
15
+ * * Toggles full-screen mode for a given element (or the `document` by default).
16
+ * @param element The element to toggle fullscreen mode for (default: document root).
17
+ */
18
+ export function toggleFullScreen(element = document.documentElement) {
19
+ const doc = document;
20
+ const elem = element;
21
+ if (!doc.fullscreenElement && !doc.webkitFullscreenElement) {
22
+ if (elem.requestFullscreen) {
23
+ elem.requestFullscreen();
24
+ }
25
+ else if (elem.webkitRequestFullscreen) {
26
+ elem.webkitRequestFullscreen();
27
+ }
18
28
  }
19
- else if (document.exitFullscreen) {
20
- document.exitFullscreen();
29
+ else {
30
+ if (doc.exitFullscreen) {
31
+ doc.exitFullscreen();
32
+ }
33
+ else if (doc.webkitExitFullscreen) {
34
+ doc.webkitExitFullscreen();
35
+ }
21
36
  }
22
37
  }
23
38
  /**
24
- * * Copy text to clipboard.
25
- * @param text Text to copy in clipboard.
39
+ * * Copies text to the clipboard, falling back to legacy methods if needed.
40
+ * @param text - The text to copy.
41
+ * @returns A promise that resolves when the text is copied.
26
42
  */
27
43
  export async function copyToClipboard(text) {
28
44
  try {
29
- await navigator.clipboard.writeText(text);
45
+ if (navigator?.clipboard?.writeText) {
46
+ await navigator.clipboard.writeText(text);
47
+ }
48
+ else {
49
+ const textArea = document.createElement('textarea');
50
+ textArea.value = text;
51
+ textArea.style.position = 'fixed';
52
+ textArea.style.opacity = '0';
53
+ document.body.appendChild(textArea);
54
+ textArea.select();
55
+ textArea.setSelectionRange(0, textArea.value.length);
56
+ const success = document.execCommand('copy');
57
+ document.body.removeChild(textArea);
58
+ if (!success) {
59
+ throw new Error('Cannot execute command in this environment!');
60
+ }
61
+ }
30
62
  }
31
- catch (err) {
32
- console.error('Failed to copy:', err);
63
+ catch (error) {
64
+ console.error('Failed to copy text:', error);
33
65
  }
34
66
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nhb-toolbox",
3
- "version": "3.1.0",
3
+ "version": "3.1.1",
4
4
  "description": "A versatile collection of smart, efficient, and reusable utility functions for everyday development needs.",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",