@degreesign/ui 1.1.0 → 1.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.
- package/README.md +56 -17
- package/dist/browser/degreesign.min.js +1 -1
- package/dist/code/populate.d.ts +11 -0
- package/dist/code/scripts.d.ts +5 -0
- package/dist/code/select.d.ts +13 -0
- package/dist/index.d.ts +7 -11
- package/dist/node/degreesign.node.min.js +1 -1
- package/dist/types.d.ts +29 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,9 +1,6 @@
|
|
|
1
1
|
# DegreeSign UI functions
|
|
2
2
|
|
|
3
|
-
A lightweight TypeScript library for
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
3
|
+
A lightweight, dependency-free TypeScript library for selecting, showing, hiding, repeating, and dynamically loading UI elements and resources in web applications.
|
|
7
4
|
|
|
8
5
|
## Setup
|
|
9
6
|
|
|
@@ -13,25 +10,52 @@ Install the package via npm:
|
|
|
13
10
|
npm install @degreesign/ui
|
|
14
11
|
```
|
|
15
12
|
|
|
13
|
+
OR via yarn:
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
yarn add @degreesign/ui
|
|
17
|
+
```
|
|
18
|
+
|
|
16
19
|
OR use in browsers through CDN
|
|
17
20
|
|
|
18
21
|
```html
|
|
19
|
-
<script
|
|
22
|
+
<script
|
|
23
|
+
src="https://cdn.jsdelivr.net/npm/@degreesign/ui@1.1.1/dist/browser/degreesign.min.js"
|
|
24
|
+
></script>
|
|
20
25
|
```
|
|
21
26
|
|
|
22
27
|
## Usage
|
|
23
28
|
|
|
24
29
|
Import the functions from the `@degreesign/ui` package in your TypeScript or JavaScript project:
|
|
25
30
|
|
|
26
|
-
```
|
|
27
|
-
import { selectElement, selectAll, showElement, hideElement, repeatElements } from '@degreesign/ui';
|
|
31
|
+
```ts
|
|
32
|
+
import { selectElement, selectAll, showElement, hideElement, repeatElements, loadScript } from '@degreesign/ui';
|
|
28
33
|
```
|
|
29
34
|
|
|
30
35
|
Below are the available functions and their usage examples.
|
|
31
36
|
|
|
37
|
+
## CDN Usage
|
|
38
|
+
|
|
39
|
+
Use the package directly in the browser without a build step by loading the UMD bundle from a CDN:
|
|
40
|
+
|
|
41
|
+
```html
|
|
42
|
+
<script src="https://cdn.jsdelivr.net/npm/@degreesign/ui@1.1.1/dist/browser/degreesign.min.js"></script>
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
The bundle exposes a global `dsUI` object containing all exported functions and enums:
|
|
46
|
+
|
|
47
|
+
```html
|
|
48
|
+
<div id="myDiv">Hello</div>
|
|
49
|
+
<script>
|
|
50
|
+
const el = dsUI.selectElement('#myDiv');
|
|
51
|
+
dsUI.showElement(el);
|
|
52
|
+
dsUI.hideElement(el);
|
|
53
|
+
</script>
|
|
54
|
+
```
|
|
55
|
+
|
|
32
56
|
## Functions
|
|
33
57
|
|
|
34
|
-
###
|
|
58
|
+
### Select Element
|
|
35
59
|
|
|
36
60
|
Selects a single DOM element by its CSS selector, optionally within a parent element.
|
|
37
61
|
|
|
@@ -42,7 +66,7 @@ Selects a single DOM element by its CSS selector, optionally within a parent ele
|
|
|
42
66
|
**Returns:** An `HTMLElement`.
|
|
43
67
|
|
|
44
68
|
**Example:**
|
|
45
|
-
```
|
|
69
|
+
```ts
|
|
46
70
|
// Select an element by ID
|
|
47
71
|
const myDiv = selectElement('#myDiv');
|
|
48
72
|
myDiv.textContent = 'Hello, World!';
|
|
@@ -53,7 +77,7 @@ const child = selectElement('.child', parent);
|
|
|
53
77
|
child.style.color = 'blue';
|
|
54
78
|
```
|
|
55
79
|
|
|
56
|
-
###
|
|
80
|
+
### Select All
|
|
57
81
|
|
|
58
82
|
Selects all DOM elements matching a CSS selector, optionally within a parent element.
|
|
59
83
|
|
|
@@ -64,7 +88,7 @@ Selects all DOM elements matching a CSS selector, optionally within a parent ele
|
|
|
64
88
|
**Returns:** A `NodeListOf<Element>`.
|
|
65
89
|
|
|
66
90
|
**Example:**
|
|
67
|
-
```
|
|
91
|
+
```ts
|
|
68
92
|
// Select all elements with a class
|
|
69
93
|
const items = selectAll('.item');
|
|
70
94
|
items.forEach(item => item.style.backgroundColor = 'lightgray');
|
|
@@ -75,7 +99,7 @@ const divs = selectAll('div', parent);
|
|
|
75
99
|
divs.forEach(div => div.classList.add('highlight'));
|
|
76
100
|
```
|
|
77
101
|
|
|
78
|
-
###
|
|
102
|
+
### Show Element
|
|
79
103
|
|
|
80
104
|
Sets an element's display style to `flex`, making it visible.
|
|
81
105
|
|
|
@@ -83,12 +107,12 @@ Sets an element's display style to `flex`, making it visible.
|
|
|
83
107
|
- `element`: The `HTMLElement` to show.
|
|
84
108
|
|
|
85
109
|
**Example:**
|
|
86
|
-
```
|
|
110
|
+
```ts
|
|
87
111
|
const myDiv = selectElement('#myDiv');
|
|
88
112
|
showElement(myDiv); // Displays the element with flex layout
|
|
89
113
|
```
|
|
90
114
|
|
|
91
|
-
###
|
|
115
|
+
### Hide Element
|
|
92
116
|
|
|
93
117
|
Sets an element's display style to `none`, hiding it.
|
|
94
118
|
|
|
@@ -96,12 +120,12 @@ Sets an element's display style to `none`, hiding it.
|
|
|
96
120
|
- `element`: The `HTMLElement` to hide.
|
|
97
121
|
|
|
98
122
|
**Example:**
|
|
99
|
-
```
|
|
123
|
+
```ts
|
|
100
124
|
const myDiv = selectElement('#myDiv');
|
|
101
125
|
hideElement(myDiv); // Hides the element
|
|
102
126
|
```
|
|
103
127
|
|
|
104
|
-
###
|
|
128
|
+
### Repeat Elements
|
|
105
129
|
|
|
106
130
|
Repeats or removes child elements within a parent to match a target count by cloning or removing the first child.
|
|
107
131
|
|
|
@@ -111,7 +135,7 @@ Repeats or removes child elements within a parent to match a target count by clo
|
|
|
111
135
|
- `targetCount`: The desired number of child elements.
|
|
112
136
|
|
|
113
137
|
**Example:**
|
|
114
|
-
```
|
|
138
|
+
```ts
|
|
115
139
|
// HTML structure:
|
|
116
140
|
// <div id="parent">
|
|
117
141
|
// <div class="child">Item</div>
|
|
@@ -128,6 +152,21 @@ repeatElements({ parent, children: selectAll('.child', parent), targetCount: 2 }
|
|
|
128
152
|
// Result: 2 child divs inside #parent
|
|
129
153
|
```
|
|
130
154
|
|
|
155
|
+
### Load Script
|
|
156
|
+
|
|
157
|
+
Loads an external script or stylesheet once and caches the returned promise.
|
|
158
|
+
|
|
159
|
+
**Parameters:**
|
|
160
|
+
- `src`: The resource URL.
|
|
161
|
+
- `hideConsoleErrors` (optional): Set to `true` to disable console logging.
|
|
162
|
+
|
|
163
|
+
**Returns:** A `Promise<void>`.
|
|
164
|
+
|
|
165
|
+
**Example:**
|
|
166
|
+
```ts
|
|
167
|
+
loadScript({ src: 'https://example.com/lib.js', hideConsoleErrors: true });
|
|
168
|
+
```
|
|
169
|
+
|
|
131
170
|
## Error Handling
|
|
132
171
|
|
|
133
172
|
The `repeatElements` function includes error handling to catch and log issues when manipulating elements. For example, if the `children` NodeList is invalid or cloning fails, an error will be logged to the console.
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
/*! For license information please see degreesign.min.js.LICENSE.txt */
|
|
2
|
-
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.
|
|
2
|
+
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.dsUI=t():e.dsUI=t()}(this,(()=>(()=>{"use strict";var e={516:(e,t,n)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.repeatElements=void 0;const r=n(698);t.repeatElements=({children:e,parent:t,targetCount:n})=>{try{const o=(0,r.parentValid)(t);if(e=e||o?.children,e?.length){const t=e?.length||0;if(n>t){const r=e?.[0];if(r)for(let e=t;e<n;e++)o?o.appendChild(r.cloneNode(!0)):r?.after(r.cloneNode(!0))}else if(n<t&&0!=n)for(let r=t-1;r>=n;r--)e[r]?.remove()}}catch(e){console.log("repeatElements failed",n,e)}}},613:(e,t)=>{var n,r,o,l;Object.defineProperty(t,"__esModule",{value:!0}),t.TypeName=t.Display=t.TagName=t.ResourceType=void 0,function(e){e.Script="text/javascript",e.Stylesheet="stylesheet"}(n||(t.ResourceType=n={})),function(e){e.Link="link",e.Script="script"}(r||(t.TagName=r={})),function(e){e.Flex="flex",e.None="none"}(o||(t.Display=o={})),function(e){e.String="string"}(l||(t.TypeName=l={}))},698:(e,t,n)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.hideElement=t.showElement=t.selectAll=t.selectElement=t.parentValid=void 0;const r=n(613),o=e=>typeof e==r.TypeName.String?document.querySelector(e):e;t.parentValid=o,t.selectElement=(e,t)=>(o(t)||document)?.querySelector(e),t.selectAll=(e,t)=>(o(t)||document)?.querySelectorAll(e),t.showElement=e=>e&&(e.style.display=r.Display.Flex),t.hideElement=e=>e&&(e.style.display=r.Display.None)},924:(e,t,n)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.loadScript=void 0;const r=n(613),o={};t.loadScript=({src:e,type:t=r.ResourceType.Script,ready:n,interval:l=100,timeout:c=3e4,hideConsoleErrors:i})=>{try{const s=o[e];if(s)return s;const a=new Promise(((o,s)=>{let a,p,d=!1;const u=e=>{d||(d=!0,p&&clearTimeout(p),a&&clearTimeout(a),e?s():o())},m=()=>{if(!d)return!n||n()?u():void(a=setTimeout(m,l))};let y;if(p=setTimeout((()=>{i||console.log("loadScript timed out",e),u(!0)}),c),t===r.ResourceType.Stylesheet){const t=document.createElement(r.TagName.Link);t.rel=r.ResourceType.Stylesheet,t.href=e,y=t}else{const t=document.createElement(r.TagName.Script);t.type=r.ResourceType.Script,t.src=e,t.async=!0,y=t}y.onload=m,y.onerror=t=>{i||console.log("loadScript failed onload",e,t),u(t)},document.head.appendChild(y)}));return o[e]=a,a.catch((()=>{o[e]===a&&delete o[e]})),a}catch(e){i||console.log("loadScript failed",e)}}}},t={};function n(r){var o=t[r];if(void 0!==o)return o.exports;var l=t[r]={exports:{}};return e[r](l,l.exports,n),l.exports}var r={};return(()=>{var e=r;Object.defineProperty(e,"__esModule",{value:!0}),e.Display=e.ResourceType=e.loadScript=e.repeatElements=e.hideElement=e.showElement=e.selectAll=e.selectElement=void 0;const t=n(698);Object.defineProperty(e,"selectElement",{enumerable:!0,get:function(){return t.selectElement}}),Object.defineProperty(e,"selectAll",{enumerable:!0,get:function(){return t.selectAll}}),Object.defineProperty(e,"showElement",{enumerable:!0,get:function(){return t.showElement}}),Object.defineProperty(e,"hideElement",{enumerable:!0,get:function(){return t.hideElement}});const o=n(516);Object.defineProperty(e,"repeatElements",{enumerable:!0,get:function(){return o.repeatElements}});const l=n(924);Object.defineProperty(e,"loadScript",{enumerable:!0,get:function(){return l.loadScript}});const c=n(613);Object.defineProperty(e,"ResourceType",{enumerable:!0,get:function(){return c.ResourceType}}),Object.defineProperty(e,"Display",{enumerable:!0,get:function(){return c.Display}})})(),r})()));
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
declare const
|
|
2
|
+
/** Repeat Elements */
|
|
3
|
+
repeatElements: ({ children, parent, targetCount, }: {
|
|
4
|
+
/** Child Element Nodes */
|
|
5
|
+
children?: NodeListOf<Element> | HTMLCollection | HTMLElement[];
|
|
6
|
+
/** Parent Element */
|
|
7
|
+
parent?: Element | HTMLElement | string;
|
|
8
|
+
/** Target Count */
|
|
9
|
+
targetCount: number;
|
|
10
|
+
}) => void;
|
|
11
|
+
export { repeatElements, };
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Display } from '../types';
|
|
2
|
+
declare const
|
|
3
|
+
/** Validate parent element */
|
|
4
|
+
parentValid: (parent?: Element | string) => Element | undefined,
|
|
5
|
+
/** Select element by selector */
|
|
6
|
+
selectElement: (id: string, parent?: Element | string) => HTMLElement,
|
|
7
|
+
/** Select all by selector */
|
|
8
|
+
selectAll: (id: string, parent?: Element | string) => NodeListOf<Element>,
|
|
9
|
+
/** Show element */
|
|
10
|
+
showElement: (element?: HTMLElement | null) => Display.Flex | null | undefined,
|
|
11
|
+
/** Hide element */
|
|
12
|
+
hideElement: (element?: HTMLElement | null) => Display.None | null | undefined;
|
|
13
|
+
export { parentValid, selectElement, selectAll, showElement, hideElement, };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,11 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
/** Target Count */
|
|
9
|
-
targetCount: number;
|
|
10
|
-
}) => void;
|
|
11
|
-
export { selectElement, selectAll, showElement, hideElement, repeatElements, };
|
|
1
|
+
import { selectElement, selectAll, showElement, hideElement } from './code/select';
|
|
2
|
+
import { repeatElements } from './code/populate';
|
|
3
|
+
import { loadScript } from './code/scripts';
|
|
4
|
+
import { ResourceType, Display } from './types';
|
|
5
|
+
import type { LoadScriptParams } from './types';
|
|
6
|
+
export { selectElement, selectAll, showElement, hideElement, repeatElements, loadScript, ResourceType, Display, };
|
|
7
|
+
export type { LoadScriptParams, };
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
/*! For license information please see degreesign.node.min.js.LICENSE.txt */
|
|
2
|
-
(()=>{"use strict";var e={};(()=>{
|
|
2
|
+
(()=>{"use strict";var e={516:(e,t,r)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.repeatElements=void 0;const n=r(698);t.repeatElements=({children:e,parent:t,targetCount:r})=>{try{const o=(0,n.parentValid)(t);if(e=e||o?.children,e?.length){const t=e?.length||0;if(r>t){const n=e?.[0];if(n)for(let e=t;e<r;e++)o?o.appendChild(n.cloneNode(!0)):n?.after(n.cloneNode(!0))}else if(r<t&&0!=r)for(let n=t-1;n>=r;n--)e[n]?.remove()}}catch(e){console.log("repeatElements failed",r,e)}}},613:(e,t)=>{var r,n,o,l;Object.defineProperty(t,"__esModule",{value:!0}),t.TypeName=t.Display=t.TagName=t.ResourceType=void 0,function(e){e.Script="text/javascript",e.Stylesheet="stylesheet"}(r||(t.ResourceType=r={})),function(e){e.Link="link",e.Script="script"}(n||(t.TagName=n={})),function(e){e.Flex="flex",e.None="none"}(o||(t.Display=o={})),function(e){e.String="string"}(l||(t.TypeName=l={}))},698:(e,t,r)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.hideElement=t.showElement=t.selectAll=t.selectElement=t.parentValid=void 0;const n=r(613),o=e=>typeof e==n.TypeName.String?document.querySelector(e):e;t.parentValid=o,t.selectElement=(e,t)=>(o(t)||document)?.querySelector(e),t.selectAll=(e,t)=>(o(t)||document)?.querySelectorAll(e),t.showElement=e=>e&&(e.style.display=n.Display.Flex),t.hideElement=e=>e&&(e.style.display=n.Display.None)},924:(e,t,r)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.loadScript=void 0;const n=r(613),o={};t.loadScript=({src:e,type:t=n.ResourceType.Script,ready:r,interval:l=100,timeout:c=3e4,hideConsoleErrors:i})=>{try{const s=o[e];if(s)return s;const a=new Promise(((o,s)=>{let a,u,p=!1;const d=e=>{p||(p=!0,u&&clearTimeout(u),a&&clearTimeout(a),e?s():o())},m=()=>{if(!p)return!r||r()?d():void(a=setTimeout(m,l))};let y;if(u=setTimeout((()=>{i||console.log("loadScript timed out",e),d(!0)}),c),t===n.ResourceType.Stylesheet){const t=document.createElement(n.TagName.Link);t.rel=n.ResourceType.Stylesheet,t.href=e,y=t}else{const t=document.createElement(n.TagName.Script);t.type=n.ResourceType.Script,t.src=e,t.async=!0,y=t}y.onload=m,y.onerror=t=>{i||console.log("loadScript failed onload",e,t),d(t)},document.head.appendChild(y)}));return o[e]=a,a.catch((()=>{o[e]===a&&delete o[e]})),a}catch(e){i||console.log("loadScript failed",e)}}}},t={};function r(n){var o=t[n];if(void 0!==o)return o.exports;var l=t[n]={exports:{}};return e[n](l,l.exports,r),l.exports}var n={};(()=>{var e=n;Object.defineProperty(e,"__esModule",{value:!0}),e.Display=e.ResourceType=e.loadScript=e.repeatElements=e.hideElement=e.showElement=e.selectAll=e.selectElement=void 0;const t=r(698);Object.defineProperty(e,"selectElement",{enumerable:!0,get:function(){return t.selectElement}}),Object.defineProperty(e,"selectAll",{enumerable:!0,get:function(){return t.selectAll}}),Object.defineProperty(e,"showElement",{enumerable:!0,get:function(){return t.showElement}}),Object.defineProperty(e,"hideElement",{enumerable:!0,get:function(){return t.hideElement}});const o=r(516);Object.defineProperty(e,"repeatElements",{enumerable:!0,get:function(){return o.repeatElements}});const l=r(924);Object.defineProperty(e,"loadScript",{enumerable:!0,get:function(){return l.loadScript}});const c=r(613);Object.defineProperty(e,"ResourceType",{enumerable:!0,get:function(){return c.ResourceType}}),Object.defineProperty(e,"Display",{enumerable:!0,get:function(){return c.Display}})})();var o=exports;for(var l in n)o[l]=n[l];n.__esModule&&Object.defineProperty(o,"__esModule",{value:!0})})();
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
export declare enum ResourceType {
|
|
2
|
+
Script = "text/javascript",
|
|
3
|
+
Stylesheet = "stylesheet"
|
|
4
|
+
}
|
|
5
|
+
export declare enum TagName {
|
|
6
|
+
Link = "link",
|
|
7
|
+
Script = "script"
|
|
8
|
+
}
|
|
9
|
+
export declare enum Display {
|
|
10
|
+
Flex = "flex",
|
|
11
|
+
None = "none"
|
|
12
|
+
}
|
|
13
|
+
export declare enum TypeName {
|
|
14
|
+
String = "string"
|
|
15
|
+
}
|
|
16
|
+
export type LoadScriptParams = {
|
|
17
|
+
/** Resource URL */
|
|
18
|
+
src: string;
|
|
19
|
+
/** Resource type */
|
|
20
|
+
type?: ResourceType;
|
|
21
|
+
/** Defaults to load event */
|
|
22
|
+
ready?: () => boolean;
|
|
23
|
+
/** Readiness poll (ms) */
|
|
24
|
+
interval?: number;
|
|
25
|
+
/** Max wait (ms) */
|
|
26
|
+
timeout?: number;
|
|
27
|
+
/** Disable console logging */
|
|
28
|
+
hideConsoleErrors?: boolean;
|
|
29
|
+
};
|