@webreflection/elements 0.1.0 → 0.1.2
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 +11 -8
- package/auto.js +14 -0
- package/native.js +260 -0
- package/package.json +8 -3
package/README.md
CHANGED
|
@@ -4,15 +4,16 @@ HTML & SVG Custom Elements, glueing [qsa-observer](https://github.com/WebReflect
|
|
|
4
4
|
|
|
5
5
|
```html
|
|
6
6
|
<script type="module">
|
|
7
|
+
|
|
7
8
|
import { elements, HTML, SVG } from 'https://esm.run/@webreflection/elements';
|
|
8
9
|
|
|
9
10
|
// same Custom Elements API
|
|
10
|
-
elements.whenDefined('
|
|
11
|
-
console.log('
|
|
11
|
+
elements.whenDefined('my-h1').then(Class => {
|
|
12
|
+
console.log(Class.name, 'defined');
|
|
12
13
|
});
|
|
13
14
|
|
|
14
15
|
// native extends out of the box
|
|
15
|
-
elements.define('
|
|
16
|
+
elements.define('my-h1', class MyH1 extends HTML.H1 {
|
|
16
17
|
// anything that works with classes works here
|
|
17
18
|
#private;
|
|
18
19
|
|
|
@@ -30,12 +31,14 @@ HTML & SVG Custom Elements, glueing [qsa-observer](https://github.com/WebReflect
|
|
|
30
31
|
}
|
|
31
32
|
});
|
|
32
33
|
|
|
33
|
-
// automatic upgrades per element
|
|
34
|
-
elements.whenDefined('header').then(() => {
|
|
35
|
-
console.log('header defined');
|
|
36
|
-
});
|
|
37
34
|
</script>
|
|
38
|
-
<h1 is="
|
|
35
|
+
<h1 is="my-h1"></h1>
|
|
39
36
|
```
|
|
40
37
|
|
|
41
38
|
Enjoy 👋
|
|
39
|
+
|
|
40
|
+
### Native Support Included
|
|
41
|
+
|
|
42
|
+
If your browser supports natively custom elements builtin extends (i.e. Chrome/ium based) you can use [@webreflection/elements/native](./native.js) export instead, which offers exact same API without any dependency, hence it's lightweight, easier to reason about, and natively wonderful.
|
|
43
|
+
|
|
44
|
+
If you don't know if that's supported and you would like to feature detect this ability, you can pick the [@webreflection/elements/auto](./auto.js) export insted.
|
package/auto.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import * as module from './native.js';
|
|
2
|
+
|
|
3
|
+
let { HTML, SVG, elements } = module;
|
|
4
|
+
|
|
5
|
+
try {
|
|
6
|
+
const LI = class extends HTMLLIElement {};
|
|
7
|
+
customElements.define('extends-' + Date.now(), LI, { extends: 'li' });
|
|
8
|
+
new LI;
|
|
9
|
+
}
|
|
10
|
+
catch {
|
|
11
|
+
({ HTML, SVG, elements } = await import('./min.js'));
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export { HTML, SVG, elements };
|
package/native.js
ADDED
|
@@ -0,0 +1,260 @@
|
|
|
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
|
+
*/
|
|
8
|
+
|
|
9
|
+
/** @type {Options} */
|
|
10
|
+
let W3 = {
|
|
11
|
+
HTML: 'http://www.w3.org/1999/xhtml',
|
|
12
|
+
SVG: 'http://www.w3.org/2000/svg',
|
|
13
|
+
};
|
|
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
|
+
*/
|
|
130
|
+
|
|
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
|
+
*/
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* @typedef {Object} Namespace
|
|
208
|
+
* @property {HTML} HTML
|
|
209
|
+
* @property {SVG} SVG
|
|
210
|
+
*/
|
|
211
|
+
|
|
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
|
+
};
|
|
247
|
+
}
|
|
248
|
+
return Proxied;
|
|
249
|
+
}
|
|
250
|
+
});
|
|
251
|
+
|
|
252
|
+
export const { HTML, SVG } = createRegistry();
|
|
253
|
+
|
|
254
|
+
export const elements = {
|
|
255
|
+
define: (name, constructor) => customElements.define(
|
|
256
|
+
name, constructor, { extends: constructor.tag }
|
|
257
|
+
),
|
|
258
|
+
get: name => customElements.get(name),
|
|
259
|
+
whenDefined: name => customElements.whenDefined(name),
|
|
260
|
+
};
|
package/package.json
CHANGED
|
@@ -1,19 +1,24 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@webreflection/elements",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.2",
|
|
4
4
|
"description": "HTML & SVG Custom Elements",
|
|
5
5
|
"main": "index.js",
|
|
6
|
+
"module": "index.js",
|
|
6
7
|
"scripts": {
|
|
7
8
|
"build": "rollup -c rollup.js"
|
|
8
9
|
},
|
|
9
10
|
"exports": {
|
|
10
11
|
".": "./index.js",
|
|
11
|
-
"./
|
|
12
|
-
"./min": "./min.js"
|
|
12
|
+
"./auto": "./auto.js",
|
|
13
|
+
"./min": "./min.js",
|
|
14
|
+
"./native": "./native.js",
|
|
15
|
+
"./package.json": "./package.json"
|
|
13
16
|
},
|
|
14
17
|
"files": [
|
|
18
|
+
"auto.js",
|
|
15
19
|
"index.js",
|
|
16
20
|
"min.js",
|
|
21
|
+
"native.js",
|
|
17
22
|
"README.md",
|
|
18
23
|
"LICENSE"
|
|
19
24
|
],
|