@webreflection/elements 0.1.3 → 0.1.5

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 CHANGED
@@ -1,5 +1,8 @@
1
1
  # @webreflection/elements
2
2
 
3
+ <sup>**Social Media Photo by [Marek Piwnicki](https://unsplash.com/@marekpiwnicki) on [Unsplash](https://unsplash.com/)**</sup>
4
+
5
+
3
6
  HTML & SVG Custom Elements, glueing [qsa-observer](https://github.com/WebReflection/qsa-observer#readme) and [nonchalance](https://github.com/WebReflection/nonchalance#readme) together.
4
7
 
5
8
  **[Live Test](https://webreflection.github.io/elements/test.html)**
package/auto.js CHANGED
@@ -6,14 +6,10 @@ try {
6
6
  const LI = class extends HTMLLIElement {};
7
7
  customElements.define('li-' + Date.now(), LI, { extends: 'li' });
8
8
  new LI;
9
- SVG = new Proxy(module, {
10
- get() {
11
- throw new DOMException('SVG extends not natively supported');
12
- }
13
- });
14
9
  }
15
10
  catch {
16
- ({ HTML, SVG, elements } = await import('./min.js'));
11
+ // @ts-ignore
12
+ ({ HTML, elements } = await import(/* webpackIgnore: true */ './index.js'));
17
13
  }
18
14
 
19
15
  export { HTML, SVG, elements };
package/index.js CHANGED
@@ -31,6 +31,11 @@ const wait = name => {
31
31
  export const { HTML, SVG } = createRegistry();
32
32
 
33
33
  export const elements = {
34
+ /**
35
+ * @param {string} name
36
+ * @param {CustomElementConstructor} constructor
37
+ * @returns
38
+ */
34
39
  define(name, constructor) {
35
40
  if (registry.has(name) || customElements.get(name))
36
41
  throw new DOMException(`Element ${name} already defined`);
@@ -42,6 +47,16 @@ export const elements = {
42
47
  query.push(selector);
43
48
  parse(document.querySelectorAll(selector));
44
49
  },
50
+
51
+ /**
52
+ * @param {string} name
53
+ * @returns {CustomElementConstructor?}
54
+ */
45
55
  get: name => registry.get(name),
56
+
57
+ /**
58
+ * @param {string} name
59
+ * @returns {Promise<CustomElementConstructor>}
60
+ */
46
61
  whenDefined: name => wait(name).promise,
47
62
  };
package/native.js CHANGED
@@ -1,260 +1,62 @@
1
- /**
2
- * @typedef {Object} Options An object with a `document` and zero, one, or more custom namespaces.
3
- * @property {Document} [document] the document to use, defaults to the global one.
4
- * @property {'http://www.w3.org/1999/xhtml'} [HTML] the namespace to use for HTML classes.
5
- * @property {'http://www.w3.org/2000/svg'} [SVG] the namespace to use for SVG classes.
6
- * @property {'...'} [CustomML] any desired extra namespace.
7
- */
1
+ const DOM = new Map;
8
2
 
9
- /** @type {Options} */
10
- let W3 = {
11
- HTML: 'http://www.w3.org/1999/xhtml',
12
- SVG: 'http://www.w3.org/2000/svg',
3
+ const create = document.createElement.bind(document);
4
+
5
+ const set = (map, tag) => {
6
+ let Class = DOM.get(tag);
7
+ if (!Class) DOM.set(tag, Class = create(tag).constructor);
8
+ class CustomElement extends Class {
9
+ static tag = tag;
10
+ }
11
+ map.set(tag, CustomElement);
12
+ return CustomElement;
13
13
  };
14
14
 
15
- /**
16
- * @typedef {Object} HTML
17
- * @property {new () => HTMLAnchorElement} A
18
- * @property {new () => HTMLElement} Abbr
19
- * @property {new () => HTMLElement} Address
20
- * @property {new () => HTMLAreaElement} Area
21
- * @property {new () => HTMLElement} Article
22
- * @property {new () => HTMLElement} Aside
23
- * @property {new () => HTMLAudioElement} Audio
24
- * @property {new () => HTMLElement} B
25
- * @property {new () => HTMLBaseElement} Base
26
- * @property {new () => HTMLElement} Bdi
27
- * @property {new () => HTMLElement} Bdo
28
- * @property {new () => HTMLQuoteElement} BlockQuote
29
- * @property {new () => HTMLBodyElement} Body
30
- * @property {new () => HTMLBRElement} BR
31
- * @property {new () => HTMLButtonElement} Button
32
- * @property {new () => HTMLCanvasElement} Canvas
33
- * @property {new () => HTMLTableCaptionElement} Caption
34
- * @property {new () => HTMLElement} Cite
35
- * @property {new () => HTMLElement} Code
36
- * @property {new () => HTMLTableColElement} Col
37
- * @property {new () => HTMLTableColElement} ColGroup
38
- * @property {new () => HTMLDataElement} Data
39
- * @property {new () => HTMLDataListElement} DataList
40
- * @property {new () => HTMLElement} DD
41
- * @property {new () => HTMLModElement} Del
42
- * @property {new () => HTMLDetailsElement} Details
43
- * @property {new () => HTMLElement} Dfn
44
- * @property {new () => HTMLDialogElement} Dialog
45
- * @property {new () => HTMLDivElement} Div
46
- * @property {new () => HTMLDListElement} DL
47
- * @property {new () => HTMLElement} DT
48
- * @property {new () => HTMLElement} Element
49
- * @property {new () => HTMLElement} Em
50
- * @property {new () => HTMLEmbedElement} Embed
51
- * @property {new () => HTMLFieldSetElement} FieldSet
52
- * @property {new () => HTMLElement} FigCaption
53
- * @property {new () => HTMLElement} Figure
54
- * @property {new () => HTMLElement} Footer
55
- * @property {new () => HTMLFormElement} Form
56
- * @property {new () => HTMLHeadingElement} H1
57
- * @property {new () => HTMLHeadingElement} H2
58
- * @property {new () => HTMLHeadingElement} H3
59
- * @property {new () => HTMLHeadingElement} H4
60
- * @property {new () => HTMLHeadingElement} H5
61
- * @property {new () => HTMLHeadingElement} H6
62
- * @property {new () => HTMLHeadElement} Head
63
- * @property {new () => HTMLElement} Header
64
- * @property {new () => HTMLHRElement} HR
65
- * @property {new () => HTMLHtmlElement} Html
66
- * @property {new () => HTMLElement} I
67
- * @property {new () => HTMLIFrameElement} IFrame
68
- * @property {new () => HTMLImageElement} Img
69
- * @property {new () => HTMLInputElement} Input
70
- * @property {new () => HTMLModElement} Ins
71
- * @property {new () => HTMLElement} Kbd
72
- * @property {new () => HTMLLabelElement} Label
73
- * @property {new () => HTMLLegendElement} Legend
74
- * @property {new () => HTMLLIElement} LI
75
- * @property {new () => HTMLLinkElement} Link
76
- * @property {new () => HTMLElement} Main
77
- * @property {new () => HTMLMapElement} Map
78
- * @property {new () => HTMLElement} Mark
79
- * @property {new () => HTMLMenuElement} Menu
80
- * @property {new () => HTMLMetaElement} Meta
81
- * @property {new () => HTMLMeterElement} Meter
82
- * @property {new () => HTMLElement} Nav
83
- * @property {new () => HTMLElement} NoScript
84
- * @property {new () => HTMLObjectElement} Object
85
- * @property {new () => HTMLOListElement} OL
86
- * @property {new () => HTMLOptGroupElement} OptGroup
87
- * @property {new () => HTMLOptionElement} Option
88
- * @property {new () => HTMLOutputElement} Output
89
- * @property {new () => HTMLParagraphElement} P
90
- * @property {new () => HTMLPictureElement} Picture
91
- * @property {new () => HTMLUnknownElement} Portal
92
- * @property {new () => HTMLPreElement} Pre
93
- * @property {new () => HTMLProgressElement} Progress
94
- * @property {new () => HTMLQuoteElement} Q
95
- * @property {new () => HTMLElement} RP
96
- * @property {new () => HTMLElement} RT
97
- * @property {new () => HTMLElement} Ruby
98
- * @property {new () => HTMLElement} S
99
- * @property {new () => HTMLElement} Samp
100
- * @property {new () => HTMLScriptElement} Script
101
- * @property {new () => HTMLElement} Section
102
- * @property {new () => HTMLSelectElement} Select
103
- * @property {new () => HTMLSlotElement} Slot
104
- * @property {new () => HTMLElement} Small
105
- * @property {new () => HTMLSourceElement} Source
106
- * @property {new () => HTMLSpanElement} Span
107
- * @property {new () => HTMLElement} Strong
108
- * @property {new () => HTMLStyleElement} Style
109
- * @property {new () => HTMLElement} Sub
110
- * @property {new () => HTMLElement} Summary
111
- * @property {new () => HTMLElement} Sup
112
- * @property {new () => HTMLTableElement} Table
113
- * @property {new () => HTMLTableSectionElement} TBody
114
- * @property {new () => HTMLTableCellElement} TD
115
- * @property {new () => HTMLTemplateElement} Template
116
- * @property {new () => HTMLTextAreaElement} TextArea
117
- * @property {new () => HTMLTableSectionElement} TFoot
118
- * @property {new () => HTMLTableCellElement} TH
119
- * @property {new () => HTMLTableSectionElement} THead
120
- * @property {new () => HTMLTimeElement} Time
121
- * @property {new () => HTMLTitleElement} Title
122
- * @property {new () => HTMLTableRowElement} TR
123
- * @property {new () => HTMLTrackElement} Track
124
- * @property {new () => HTMLElement} U
125
- * @property {new () => HTMLUListElement} UL
126
- * @property {new () => HTMLElement} Var
127
- * @property {new () => HTMLVideoElement} Video
128
- * @property {new () => HTMLElement} Wbr
129
- */
15
+ // @ts-ignore
16
+ const HTML = /** @type {import("nonchalance/ce").HTML} */(new Proxy(new Map, {
17
+ get(map, tag) {
18
+ let _ = /** @type {string} */ (tag).toLowerCase();
19
+ return map.get(_) || set(map, _);
20
+ }
21
+ }));
130
22
 
131
- /**
132
- * @typedef {Object} SVG
133
- * @property {new () => SVGAElement} A
134
- * @property {new () => SVGAnimateElement} Animate
135
- * @property {new () => SVGAnimateMotionElement} AnimateMotion
136
- * @property {new () => SVGAnimateTransformElement} AnimateTransform
137
- * @property {new () => SVGAnimationElement} Animation
138
- * @property {new () => SVGCircleElement} Circle
139
- * @property {new () => SVGClipPathElement} ClipPath
140
- * @property {new () => SVGComponentTransferFunctionElement} ComponentTransferFunction
141
- * @property {new () => SVGDefsElement} Defs
142
- * @property {new () => SVGDescElement} Desc
143
- * @property {new () => SVGElement} Element
144
- * @property {new () => SVGEllipseElement} Ellipse
145
- * @property {new () => SVGFEBlendElement} FEBlend
146
- * @property {new () => SVGFEColorMatrixElement} FEColorMatrix
147
- * @property {new () => SVGFEComponentTransferElement} FEComponentTransfer
148
- * @property {new () => SVGFECompositeElement} FEComposite
149
- * @property {new () => SVGFEConvolveMatrixElement} FEConvolveMatrix
150
- * @property {new () => SVGFEDiffuseLightingElement} FEDiffuseLighting
151
- * @property {new () => SVGFEDisplacementMapElement} FEDisplacementMap
152
- * @property {new () => SVGFEDistantLightElement} FEDistantLight
153
- * @property {new () => SVGFEDropShadowElement} FEDropShadow
154
- * @property {new () => SVGFEFloodElement} FEFlood
155
- * @property {new () => SVGFEFuncAElement} FEFuncA
156
- * @property {new () => SVGFEFuncBElement} FEFuncB
157
- * @property {new () => SVGFEFuncGElement} FEFuncG
158
- * @property {new () => SVGFEFuncRElement} FEFuncR
159
- * @property {new () => SVGFEGaussianBlurElement} FEGaussianBlur
160
- * @property {new () => SVGFEImageElement} FEImage
161
- * @property {new () => SVGFEMergeElement} FEMerge
162
- * @property {new () => SVGFEMergeNodeElement} FEMergeNode
163
- * @property {new () => SVGFEMorphologyElement} FEMorphology
164
- * @property {new () => SVGFEOffsetElement} FEOffset
165
- * @property {new () => SVGFEPointLightElement} FEPointLight
166
- * @property {new () => SVGFESpecularLightingElement} FESpecularLighting
167
- * @property {new () => SVGFESpotLightElement} FESpotLight
168
- * @property {new () => SVGFETileElement} FETile
169
- * @property {new () => SVGFETurbulenceElement} FETurbulence
170
- * @property {new () => SVGFilterElement} Filter
171
- * @property {new () => SVGForeignObjectElement} ForeignObject
172
- * @property {new () => SVGGElement} G
173
- * @property {new () => SVGGeometryElement} Geometry
174
- * @property {new () => SVGGradientElement} Gradient
175
- * @property {new () => SVGGraphicsElement} Graphics
176
- * @property {new () => SVGImageElement} Image
177
- * @property {new () => SVGLineElement} Line
178
- * @property {new () => SVGLinearGradientElement} LinearGradient
179
- * @property {new () => SVGMPathElement} MPath
180
- * @property {new () => SVGMarkerElement} Marker
181
- * @property {new () => SVGMaskElement} Mask
182
- * @property {new () => SVGMetadataElement} Metadata
183
- * @property {new () => SVGPathElement} Path
184
- * @property {new () => SVGPatternElement} Pattern
185
- * @property {new () => SVGPolygonElement} Polygon
186
- * @property {new () => SVGPolylineElement} Polyline
187
- * @property {new () => SVGRadialGradientElement} RadialGradient
188
- * @property {new () => SVGRectElement} Rect
189
- * @property {new () => SVGSVGElement} SVG
190
- * @property {new () => SVGScriptElement} Script
191
- * @property {new () => SVGSetElement} Set
192
- * @property {new () => SVGStopElement} Stop
193
- * @property {new () => SVGStyleElement} Style
194
- * @property {new () => SVGSwitchElement} Switch
195
- * @property {new () => SVGSymbolElement} Symbol
196
- * @property {new () => SVGTSpanElement} TSpan
197
- * @property {new () => SVGTextContentElement} TextContent
198
- * @property {new () => SVGTextElement} Text
199
- * @property {new () => SVGTextPathElement} TextPath
200
- * @property {new () => SVGTextPositioningElement} TextPositioning
201
- * @property {new () => SVGTitleElement} Title
202
- * @property {new () => SVGUseElement} Use
203
- * @property {new () => SVGViewElement} View
204
- */
23
+ // @ts-ignore
24
+ const SVG = /** @type {import("nonchalance/ce").SVG} */(new Proxy(new Map, {
25
+ get() {
26
+ throw new DOMException('SVG extends not natively supported');
27
+ }
28
+ }));
205
29
 
206
- /**
207
- * @typedef {Object} Namespace
208
- * @property {HTML} HTML
209
- * @property {SVG} SVG
210
- */
30
+ export { HTML, SVG };
211
31
 
212
- /**
213
- * Given an optional global context, returns a proxy that resolves
214
- * all tag names into their global constructors.
215
- * @property {Options} [options]
216
- * @returns {Namespace}
217
- */
218
- const createRegistry = (options = W3) => new Proxy(new Map, {
219
- get(map, Namespace) {
220
- let Proxied = map.get(Namespace);
221
- if (!Proxied) {
222
- map.set(Namespace, Proxied = new Proxy(new Map, {
223
- get(map, tag) {
224
- let _ = tag.toLowerCase();
225
- return map.get(_) || set(map, _);
226
- }
227
- }));
228
- let DOM = new Map;
229
- let doc = options.document || document;
230
- let create = doc.createElementNS.bind(
231
- doc, options[Namespace] || W3[Namespace]
232
- );
233
- let set = (map, tag) => {
234
- let Class = DOM.get(tag);
235
- if (!Class)
236
- DOM.set(tag, Class = create(tag).constructor);
237
- class CustomElement extends Class {
238
- static tag = tag;
239
- /* c8 ignore next 3 */
240
- constructor(element) {
241
- return super(element || create(tag));
242
- }
243
- }
244
- map.set(tag, CustomElement);
245
- return CustomElement;
246
- };
32
+ export const elements = {
33
+ /**
34
+ * @param {string} name
35
+ * @param {CustomElementConstructor} constructor
36
+ * @returns
37
+ */
38
+ define: (name, constructor) => {
39
+ const args = [name, constructor];
40
+
41
+ //@ts-ignore
42
+ if (constructor.tag !== 'element') {
43
+ //@ts-ignore
44
+ args.push({ extends: constructor.tag });
247
45
  }
248
- return Proxied;
249
- }
250
- });
251
46
 
252
- export const { HTML, SVG } = createRegistry();
47
+ //@ts-ignore
48
+ return customElements.define(...args);
49
+ },
253
50
 
254
- export const elements = {
255
- define: (name, constructor) => customElements.define(
256
- name, constructor, { extends: constructor.tag }
257
- ),
51
+ /**
52
+ * @param {string} name
53
+ * @returns {CustomElementConstructor?}
54
+ */
258
55
  get: name => customElements.get(name),
56
+
57
+ /**
58
+ * @param {string} name
59
+ * @returns {Promise<CustomElementConstructor>}
60
+ */
259
61
  whenDefined: name => customElements.whenDefined(name),
260
62
  };
package/package.json CHANGED
@@ -1,20 +1,32 @@
1
1
  {
2
2
  "name": "@webreflection/elements",
3
- "version": "0.1.3",
3
+ "version": "0.1.5",
4
4
  "description": "HTML & SVG Custom Elements",
5
5
  "main": "index.js",
6
6
  "module": "index.js",
7
+ "types": "./types/index.d.ts",
7
8
  "scripts": {
8
- "build": "rollup -c rollup.js"
9
+ "build": "rollup -c rollup.js && npm run types",
10
+ "types": "rm -rf types && tsc --allowJs --checkJs --lib esnext,dom --moduleResolution nodenext --module NodeNext --target esnext -d --emitDeclarationOnly --outDir ./types ./index.js ./auto.js ./native.js"
9
11
  },
10
12
  "exports": {
11
- ".": "./index.js",
12
- "./auto": "./auto.js",
13
+ ".": {
14
+ "import": "./index.js",
15
+ "types": "./types/index.d.ts"
16
+ },
17
+ "./auto": {
18
+ "import": "./auto.js",
19
+ "types": "./types/auto.d.ts"
20
+ },
21
+ "./native": {
22
+ "import": "./native.js",
23
+ "types": "./types/native.d.ts"
24
+ },
13
25
  "./min": "./min.js",
14
- "./native": "./native.js",
15
26
  "./package.json": "./package.json"
16
27
  },
17
28
  "files": [
29
+ "types/*",
18
30
  "auto.js",
19
31
  "index.js",
20
32
  "min.js",
@@ -39,7 +51,8 @@
39
51
  "devDependencies": {
40
52
  "@rollup/plugin-node-resolve": "^16.0.3",
41
53
  "@rollup/plugin-terser": "^0.4.4",
42
- "rollup": "^4.57.1"
54
+ "rollup": "^4.57.1",
55
+ "typescript": "^5.9.3"
43
56
  },
44
57
  "repository": {
45
58
  "type": "git",
@@ -0,0 +1,7 @@
1
+ export let HTML: import("nonchalance/ce").HTML;
2
+ export let SVG: import("nonchalance/ce").SVG;
3
+ export let elements: {
4
+ define: (name: string, constructor: CustomElementConstructor) => void;
5
+ get: (name: string) => CustomElementConstructor | null;
6
+ whenDefined: (name: string) => Promise<CustomElementConstructor>;
7
+ };
@@ -0,0 +1,12 @@
1
+ export const HTML: import("nonchalance/ce").HTML;
2
+ export const SVG: import("nonchalance/ce").SVG;
3
+ export namespace elements {
4
+ /**
5
+ * @param {string} name
6
+ * @param {CustomElementConstructor} constructor
7
+ * @returns
8
+ */
9
+ function define(name: string, constructor: CustomElementConstructor): void;
10
+ function get(name: string): CustomElementConstructor | null;
11
+ function whenDefined(name: string): Promise<CustomElementConstructor>;
12
+ }
@@ -0,0 +1,7 @@
1
+ export namespace elements {
2
+ function define(name: string, constructor: CustomElementConstructor): void;
3
+ function get(name: string): CustomElementConstructor | null;
4
+ function whenDefined(name: string): Promise<CustomElementConstructor>;
5
+ }
6
+ export const HTML: import("nonchalance/ce").HTML;
7
+ export const SVG: import("nonchalance/ce").SVG;