jj 2.5.0 → 2.6.0

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.
Files changed (77) hide show
  1. package/README.md +29 -1
  2. package/SKILL.md +671 -0
  3. package/lib/bundle.cjs +2031 -0
  4. package/lib/bundle.cjs.map +1 -0
  5. package/lib/bundle.d.cts +1782 -0
  6. package/lib/bundle.d.ts +1782 -1
  7. package/lib/bundle.global.js +1953 -0
  8. package/lib/bundle.global.js.map +1 -0
  9. package/lib/bundle.js +232 -230
  10. package/lib/bundle.js.map +1 -7
  11. package/lib/bundle.min.cjs +2 -0
  12. package/lib/bundle.min.cjs.map +1 -0
  13. package/lib/bundle.min.d.cts +1782 -0
  14. package/lib/bundle.min.d.ts +1782 -1
  15. package/lib/bundle.min.global.js +2 -0
  16. package/lib/bundle.min.global.js.map +1 -0
  17. package/lib/bundle.min.js +2 -1
  18. package/lib/bundle.min.js.map +1 -0
  19. package/package.json +5 -5
  20. package/lib/JJD.d.ts +0 -87
  21. package/lib/JJD.js +0 -119
  22. package/lib/JJD.js.map +0 -1
  23. package/lib/JJDF.d.ts +0 -74
  24. package/lib/JJDF.js +0 -98
  25. package/lib/JJDF.js.map +0 -1
  26. package/lib/JJE.d.ts +0 -299
  27. package/lib/JJE.js +0 -401
  28. package/lib/JJE.js.map +0 -1
  29. package/lib/JJET.d.ts +0 -79
  30. package/lib/JJET.js +0 -114
  31. package/lib/JJET.js.map +0 -1
  32. package/lib/JJEx.d.ts +0 -63
  33. package/lib/JJEx.js +0 -83
  34. package/lib/JJEx.js.map +0 -1
  35. package/lib/JJHE.d.ts +0 -109
  36. package/lib/JJHE.js +0 -136
  37. package/lib/JJHE.js.map +0 -1
  38. package/lib/JJN-wrap.d.ts +0 -1
  39. package/lib/JJN-wrap.js +0 -46
  40. package/lib/JJN-wrap.js.map +0 -1
  41. package/lib/JJN.d.ts +0 -126
  42. package/lib/JJN.js +0 -166
  43. package/lib/JJN.js.map +0 -1
  44. package/lib/JJNx.d.ts +0 -126
  45. package/lib/JJNx.js +0 -157
  46. package/lib/JJNx.js.map +0 -1
  47. package/lib/JJSE.d.ts +0 -170
  48. package/lib/JJSE.js +0 -217
  49. package/lib/JJSE.js.map +0 -1
  50. package/lib/JJSR.d.ts +0 -71
  51. package/lib/JJSR.js +0 -90
  52. package/lib/JJSR.js.map +0 -1
  53. package/lib/JJT.d.ts +0 -92
  54. package/lib/JJT.js +0 -116
  55. package/lib/JJT.js.map +0 -1
  56. package/lib/case.d.ts +0 -60
  57. package/lib/case.js +0 -92
  58. package/lib/case.js.map +0 -1
  59. package/lib/components.d.ts +0 -147
  60. package/lib/components.js +0 -287
  61. package/lib/components.js.map +0 -1
  62. package/lib/helpers.d.ts +0 -159
  63. package/lib/helpers.js +0 -233
  64. package/lib/helpers.js.map +0 -1
  65. package/lib/index.d.ts +0 -33
  66. package/lib/index.js +0 -35
  67. package/lib/index.js.map +0 -1
  68. package/lib/internal.d.ts +0 -30
  69. package/lib/internal.js +0 -35
  70. package/lib/internal.js.map +0 -1
  71. package/lib/types.d.ts +0 -65
  72. package/lib/types.js +0 -2
  73. package/lib/types.js.map +0 -1
  74. package/lib/util.d.ts +0 -68
  75. package/lib/util.js +0 -90
  76. package/lib/util.js.map +0 -1
  77. package/llms.txt +0 -214
package/lib/components.js DELETED
@@ -1,287 +0,0 @@
1
- var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
2
- if (kind === "m") throw new TypeError("Private method is not writable");
3
- if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
4
- if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
5
- return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
6
- };
7
- var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
8
- if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
9
- if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
10
- return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
11
- };
12
- var _ShadowMaster_templateConfig, _ShadowMaster_stylesConfig, _ShadowMaster_normalizedConfig;
13
- import { hasProp, isA, isArr, isDef, isFn, isStr } from 'jty';
14
- import { JJHE } from './JJHE.js';
15
- import { cssToStyle } from './util.js';
16
- import { typeErr } from './internal.js';
17
- import { keb2cam } from './case.js';
18
- /**
19
- * Resolves a template configuration into a string suitable for Shadow DOM.
20
- *
21
- * Handles functions (sync/async), promises, strings, JJHE instances, and HTMLElements.
22
- *
23
- * @param templateConfig - The configuration to resolve.
24
- * @returns A promise resolving to the HTML string or undefined.
25
- * @throws {TypeError} If the resolved value is not a string, JJHE, or HTMLElement.
26
- * @see {@link https://developer.mozilla.org/en-US/docs/Web/API/Element/outerHTML | Element.outerHTML}
27
- */
28
- async function templatePromise(templateConfig) {
29
- if (!isDef(templateConfig)) {
30
- return undefined;
31
- }
32
- if (isFn(templateConfig)) {
33
- templateConfig = await templateConfig();
34
- }
35
- templateConfig = await templateConfig;
36
- if (isStr(templateConfig)) {
37
- return templateConfig;
38
- }
39
- if (isA(templateConfig, JJHE)) {
40
- return templateConfig.getHTML();
41
- }
42
- if (isA(templateConfig, HTMLElement)) {
43
- return templateConfig.outerHTML;
44
- }
45
- throw new TypeError(`Expected template to be a string, JJHE, or HTMLElement. Got ${typeof templateConfig}`);
46
- }
47
- /**
48
- * Resolves a style configuration into a CSSStyleSheet.
49
- *
50
- * Handles functions (sync/async), promises, CSSStyleSheet instances, and strings.
51
- * Strings are converted to CSSStyleSheet using `cssToStyle`.
52
- *
53
- * @param styleConfig - The configuration to resolve.
54
- * @returns A promise resolving to a CSSStyleSheet.
55
- * @throws {TypeError} If the resolved value is not a string or CSSStyleSheet.
56
- * @see {@link https://developer.mozilla.org/en-US/docs/Web/API/CSSStyleSheet | CSSStyleSheet}
57
- */
58
- async function stylePromise(styleConfig) {
59
- if (isFn(styleConfig)) {
60
- styleConfig = await styleConfig();
61
- }
62
- styleConfig = await styleConfig;
63
- if (isA(styleConfig, CSSStyleSheet)) {
64
- return styleConfig;
65
- }
66
- if (isStr(styleConfig)) {
67
- return await cssToStyle(styleConfig);
68
- }
69
- throw new TypeError(`Expected style to be a CSS string or CSSStyleSheet. Got ${typeof styleConfig}`);
70
- }
71
- /**
72
- * Maps an array of style configurations to an array of promises resolving to CSSStyleSheets.
73
- *
74
- * @param styleConfigs - Array of style configurations.
75
- * @returns Array of promises.
76
- */
77
- function stylePromises(styleConfigs) {
78
- if (!isArr(styleConfigs)) {
79
- return [];
80
- }
81
- return styleConfigs.map(stylePromise);
82
- }
83
- /**
84
- * Resolves both template and style configurations.
85
- *
86
- * @param templateConfig - The template configuration.
87
- * @param styleConfigs - The style configurations.
88
- * @returns A promise resolving to the final ShadowConfig.
89
- */
90
- async function resolveConfig(templateConfig, styleConfigs) {
91
- const [template, ...styles] = await Promise.all([templatePromise(templateConfig), ...stylePromises(styleConfigs)]);
92
- return { template, styles };
93
- }
94
- /**
95
- * Manages the resolution of Shadow DOM configuration (template and styles).
96
- *
97
- * Allows building up the configuration and resolving it lazily.
98
- *
99
- * @example
100
- * ```ts
101
- * const sm = ShadowMaster.create()
102
- * .setTemplate('<div>Hello World</div>')
103
- * .addStyles('div { color: red; }')
104
- *
105
- * class MyComponent extends HTMLElement {
106
- * async connectedCallback() {
107
- * // Resolves the config once and caches it
108
- * const shadowConfig = await sm.getResolved()
109
- * // ... init shadow root with shadowConfig
110
- * }
111
- * }
112
- * ```
113
- */
114
- export class ShadowMaster {
115
- /**
116
- * Creates a new instance of ShadowMaster.
117
- *
118
- * @returns A new ShadowMaster instance.
119
- */
120
- static create() {
121
- return new ShadowMaster();
122
- }
123
- constructor() {
124
- _ShadowMaster_templateConfig.set(this, undefined);
125
- _ShadowMaster_stylesConfig.set(this, []);
126
- _ShadowMaster_normalizedConfig.set(this, undefined
127
- /**
128
- * Creates a new instance of ShadowMaster.
129
- *
130
- * @returns A new ShadowMaster instance.
131
- */
132
- );
133
- }
134
- /**
135
- * Sets the template configuration.
136
- *
137
- * @param templateConfig - The template configuration.
138
- * @returns The instance for chaining.
139
- *
140
- * @example
141
- * ```ts
142
- * // Accepts string, promise, or fetchHtml result
143
- * sm.setTemplate(fetchHtml('./template.html'))
144
- * ```
145
- */
146
- setTemplate(templateConfig) {
147
- __classPrivateFieldSet(this, _ShadowMaster_templateConfig, templateConfig, "f");
148
- return this;
149
- }
150
- /**
151
- * Adds one or more style configurations.
152
- *
153
- * @param stylesConfig - Variable number of style configurations.
154
- * @returns The instance for chaining.
155
- *
156
- * @example
157
- * ```ts
158
- * sm.addStyles(
159
- * 'p { color: red; }',
160
- * fetchCss('./styles.css'),
161
- * () => fetchCss('../lazy-loaded-styles.css'),
162
- * )
163
- * ```
164
- */
165
- addStyles(...stylesConfig) {
166
- __classPrivateFieldGet(this, _ShadowMaster_stylesConfig, "f").push(...stylesConfig);
167
- return this;
168
- }
169
- /**
170
- * Resolves the configuration to something that can be fed to `JJHE.initShadow()` function
171
- *
172
- * The result is cached, so subsequent calls return the same promise.
173
- * Note: Any changes made to the ShadowMaster instance (via setTemplate/addStyles)
174
- * after the first call to getResolved() will be ignored.
175
- *
176
- * @returns A promise resolving to the ShadowConfig.
177
- */
178
- async getResolved() {
179
- if (!__classPrivateFieldGet(this, _ShadowMaster_normalizedConfig, "f")) {
180
- __classPrivateFieldSet(this, _ShadowMaster_normalizedConfig, resolveConfig(__classPrivateFieldGet(this, _ShadowMaster_templateConfig, "f"), __classPrivateFieldGet(this, _ShadowMaster_stylesConfig, "f")), "f");
181
- }
182
- return await __classPrivateFieldGet(this, _ShadowMaster_normalizedConfig, "f");
183
- }
184
- }
185
- _ShadowMaster_templateConfig = new WeakMap(), _ShadowMaster_stylesConfig = new WeakMap(), _ShadowMaster_normalizedConfig = new WeakMap();
186
- /**
187
- * A helper to bridge the attribute world (kebab-case) to the property world (camelCase).
188
- * It works in tandem with browser's `observedAttributes` feature which triggers
189
- * `attributeChangedCallback`.
190
- *
191
- * @remarks
192
- * Your custom component class MUST define `static observedAttributes[]` otherwise `attributeChangedCallback` won't trigger.
193
- * `observedAttributes` should contain kebab-based attribute names.
194
- *
195
- * @example
196
- * ```ts
197
- * class MyComponent extends HTMLElement {
198
- * static observedAttributes = ['user-name', 'counter']
199
- * userName = '' // Property MUST exist on the instance (or prototype setter)
200
- * #counter = 0 // You can also use private properties together with getter/setters
201
- *
202
- * attributeChangedCallback(name, oldValue, newValue) {
203
- * attr2prop(this, name, oldValue, newValue)
204
- * }
205
-
206
- * get counter() {
207
- * return this.#counter
208
- * }
209
- *
210
- * set counter(value) {
211
- * this.#counter = value
212
- * this.#render() // You can call your render function to update the DOM
213
- * }
214
- *
215
- * #render() {
216
- * const shadow = JJHE.from(this).shadow
217
- * if (shadow) {
218
- * shadow.byId('user').setText(this.userName)
219
- * shadow.byId('counter').setText(this.counter)
220
- * }
221
- * }
222
- * }
223
- * ```
224
- *
225
- * @param instance - A reference to the common component instance
226
- * @param name - kebab-case and in lower case exactly as it appears in `observedAttributes`.
227
- * @param oldValue - The previous value of the attribute.
228
- * @param newValue - The new value of the attribute.
229
- * @returns `true` if it tried to set the attribute; otherwise `false`.
230
- * @see {@link https://developer.mozilla.org/en-US/docs/Web/API/Web_components/Using_custom_elements#responding_to_attribute_changes | Responding to attribute changes}
231
- */
232
- export function attr2prop(instance, name, oldValue, newValue) {
233
- if (!isA(instance, HTMLElement)) {
234
- throw new TypeError(`Expected an HTMLElement or a custom element instance. Got ${instance} (${typeof instance})`);
235
- }
236
- // Called when observed attributes change.
237
- if (oldValue !== newValue) {
238
- const propName = keb2cam(name);
239
- if (hasProp(instance, propName)) {
240
- instance[propName] = newValue;
241
- return true;
242
- }
243
- }
244
- return false;
245
- }
246
- /**
247
- * Registers the custom element with the browser and waits till it is defined.
248
- *
249
- * @example
250
- * ```ts
251
- * class MyComponent extends HTMLElement {}
252
- * await registerComponent('my-component', MyComponent)
253
- * ```
254
- * Another convention is to have a `static async register()` function in the Custom Component.
255
- * ```ts
256
- * export class MyComponent extends HTMLElement {
257
- * static async register() {
258
- * return registerComponent('my-component', MyComponent)
259
- * }
260
- * }
261
- * ```
262
- * That way, you can import multiple components and do a `Promise.all()` on all their `.register()`s.
263
- * ```ts
264
- * import { MyComponent, YourComponent, TheirComponent } ...
265
- * await Promise.all([
266
- * MyComponent.register(),
267
- * YourComponent.register(),
268
- * TheirComponent.register(),
269
- * ])
270
- *
271
- * @throws {TypeError} If name is not a string or constructor is not a function
272
- * @see {@link https://developer.mozilla.org/en-US/docs/Web/API/CustomElementRegistry/define | customElements.define}
273
- * @see {@link https://developer.mozilla.org/en-US/docs/Web/API/CustomElementRegistry/whenDefined | customElements.whenDefined}
274
- */
275
- export async function registerComponent(name, constructor, options) {
276
- if (!isStr(name)) {
277
- throw typeErr('name', 'a string', name);
278
- }
279
- if (!isFn(constructor)) {
280
- throw typeErr('constructor', 'a function', constructor);
281
- }
282
- if (!customElements.get(name)) {
283
- customElements.define(name, constructor, options);
284
- await customElements.whenDefined(name);
285
- }
286
- }
287
- //# sourceMappingURL=components.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"components.js","sourceRoot":"","sources":["../src/components.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,OAAO,EAAE,OAAO,EAAE,GAAG,EAAE,KAAK,EAAE,KAAK,EAAE,IAAI,EAAE,KAAK,EAAE,MAAM,KAAK,CAAA;AAE7D,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAA;AAChC,OAAO,EAAE,UAAU,EAAE,MAAM,WAAW,CAAA;AACtC,OAAO,EAAE,OAAO,EAAE,MAAM,eAAe,CAAA;AACvC,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAA;AAEnC;;;;;;;;;GASG;AACH,KAAK,UAAU,eAAe,CAAC,cAAiC;IAC5D,IAAI,CAAC,KAAK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3
- /**
4
- * Hyperscript helper to create JJHE instances.
5
- * The `h` function provides a concise way to create DOM wrappers with attributes and children,
6
- * similar to hyperscript helpers found in other libraries.
7
- *
8
- *
9
- * @remarks
10
- * It returns a `JJHE` instance which wraps the native HTMLElement.
11
- *
12
- * You may recognize it from other libraries:
13
- * - [React](https://react.dev/reference/react/createElement)
14
- * - [Vue](https://vuejs.org/guide/extras/render-function)
15
- * - [Hyperscript](https://github.com/hyperhype/hyperscript)
16
- * - [Angular](https://angular.dev/guide/components/programmatic-rendering)
17
- * - [Lit](https://lit.dev/docs/components/rendering/)
18
- *
19
- * This is not exactly a replacement, but it roughly follows the same idea.
20
- *
21
- * @example
22
- * ```ts
23
- * // Create a simple div
24
- * h('div', { id: 'app' }, 'Hello World')
25
- *
26
- * // Create a nested structure
27
- * h('ul', { class: 'list' },
28
- * h('li', null, 'Item 1'),
29
- * h('li', null, 'Item 2')
30
- * )
31
- * ```
32
- *
33
- * @param tagName - The HTML tag name.
34
- * @param attributes - Attributes to set on the element. Can be null or undefined.
35
- * @param children - Children to append (strings, nodes, or other JJHE instances).
36
- * @returns The created JJHE instance.
37
- * @see {@link https://developer.mozilla.org/en-US/docs/Web/API/Document/createElement | document.createElement}
38
- */
39
- export declare function h(tagName: string, attributes?: Record<string, string> | null, ...children: Wrappable[]): JJHE;
40
- /**
41
- * Creates a `<link>` element for prefetching or preloading resources.
42
- *
43
- * @remarks
44
- * This function validates the input arguments and returns a wrapped `JJHE` instance.
45
- * It does not append the element to the document.
46
- *
47
- * @param href - The URL of the resource.
48
- * @param rel - The relationship of the linked resource ('prefetch' or 'preload').
49
- * @param as - The type of content being loaded ('fetch' for HTML, 'style' for CSS, or 'script' for JavaScript files).
50
- * If it's not provided or set to a falsy value, it runs heuristics to find the best match from the href parameter.
51
- *
52
- * @returns The JJHE instance representing the link element. The `<link>` is accessible via `.ref`
53
- * @throws {TypeError} If `href` is not a string or URL.
54
- * @throws {RangeError} If `rel` or `as` are not valid values.
55
- * @see {@link https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/rel/preload | Link types: preload}
56
- * @see {@link https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/rel/prefetch | Link types: prefetch}
57
- */
58
- export declare function createLinkPre(href: string | URL, rel: 'prefetch' | 'preload', as?: 'fetch' | 'style' | 'script'): JJHE;
59
- /**
60
- * Adds a `<link>` tag to the document head for prefetching or preloading resources.
61
- *
62
- * @remarks
63
- * This function helps in optimizing performance by telling the browser to fetch resources
64
- * that might be needed later (prefetch) or are needed immediately (preload).
65
- *
66
- * Please refer to {@link createLinkPre} for more details.
67
- *
68
- * @example
69
- * ```ts
70
- * // Preload a script
71
- * addLinkPre('https://example.com/script.js', 'preload', 'script')
72
- *
73
- * // Prefetch a future page's CSS
74
- * addLinkPre('/next-page.css', 'prefetch', 'style')
75
- * ```
76
- *
77
- * @param args - The arguments to be passed to {@link createLinkPre}.
78
- * @returns The JJHE instance representing the link element.
79
- * @throws {TypeError} If `href` is not a string or URL.
80
- * @throws {RangeError} If `rel` or `as` are not valid values.
81
- * @see {@link createLinkPre}
82
- * @see {@link https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/rel/preload | Link types: preload}
83
- * @see {@link https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/rel/prefetch | Link types: prefetch}
84
- */
85
- export declare function addLinkPre(...args: Parameters<typeof createLinkPre>): JJHE<HTMLElement>;
86
- /**
87
- * Fetches a file and returns its contents as string.
88
- *
89
- * @remarks
90
- * This is a wrapper around the native `fetch` API that handles the response status check
91
- * and text extraction. It sets the `Accept` header based on the provided mime type.
92
- *
93
- * @example
94
- * ```ts
95
- * const text = await fetchText('https://example.com/data.txt')
96
- * ```
97
- *
98
- * @param url - The file location.
99
- * @param mime - The HTTP Request Accept header. Defaults to 'text/*'.
100
- * @returns The file contents as a string.
101
- * @throws {TypeError} If `mime` is not a string.
102
- * @throws {Error} If the fetch fails or the response status is not OK (200-299).
103
- * @see {@link https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API | Fetch API}
104
- * @see {@link https://developer.mozilla.org/en-US/docs/Web/API/Response/text | Response.text()}
105
- */
106
- export declare function fetchText(url: URL | string, mime?: string): Promise<string>;
107
- /**
108
- * Fetches the contents of a HTML file as string.
109
- *
110
- * @remarks
111
- * Useful for loading HTML templates dynamically.
112
- * You can use `import.meta.resolve('./relative-path-to.html')` to resolve paths relative to the current module.
113
- *
114
- * @example
115
- * ```ts
116
- * const template = await fetchHtml('./template.html')
117
- * ```
118
- *
119
- * @param url - The HTML file location.
120
- * @returns The file content as a string.
121
- * @throws {Error} If the response is not ok.
122
- * @see {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types | MIME types}
123
- */
124
- export declare function fetchHtml(url: URL | string): Promise<string>;
125
- /**
126
- * Fetches the contents of a CSS file as string.
127
- *
128
- * @remarks
129
- * You can use `import.meta.resolve('./relative-path-to.css')` inside components to resolve relative paths.
130
- *
131
- * @example
132
- * ```ts
133
- * const css = await fetchCss('./style.css')
134
- * ```
135
- *
136
- * @param url - The CSS file location.
137
- * @returns The file content as a string.
138
- * @throws {Error} If the response is not ok.
139
- */
140
- export declare function fetchCss(url: URL | string): Promise<string>;
141
- /**
142
- * Fetches a CSS file and constructs a CSSStyleSheet.
143
- *
144
- * @remarks
145
- * This is particularly useful for Constructable Stylesheets, which can be shared across Shadow DOM boundaries.
146
- *
147
- * @example
148
- * ```ts
149
- * const sheet = await fetchStyle('./component.css')
150
- * shadowRoot.adoptedStyleSheets = [sheet]
151
- * ```
152
- *
153
- * @param url - The CSS file location.
154
- * @returns The CSSStyleSheet object constructed from the CSS contents.
155
- * @throws {Error} If the fetch fails.
156
- * @see {@link https://developer.mozilla.org/en-US/docs/Web/API/CSSStyleSheet | CSSStyleSheet}
157
- * @see {@link https://developer.mozilla.org/en-US/docs/Web/API/ShadowRoot/adoptedStyleSheets | adoptedStyleSheets}
158
- */
159
- export declare function fetchStyle(url: URL | string): Promise<CSSStyleSheet>;
package/lib/helpers.js DELETED
@@ -1,233 +0,0 @@
1
- import { isA, isStr } from 'jty';
2
- import { JJHE } from './JJHE.js';
3
- import { cssToStyle, fileExt } from './util.js';
4
- import { typeErr, errMsg } from './internal.js';
5
- /**
6
- * Hyperscript helper to create JJHE instances.
7
- * The `h` function provides a concise way to create DOM wrappers with attributes and children,
8
- * similar to hyperscript helpers found in other libraries.
9
- *
10
- *
11
- * @remarks
12
- * It returns a `JJHE` instance which wraps the native HTMLElement.
13
- *
14
- * You may recognize it from other libraries:
15
- * - [React](https://react.dev/reference/react/createElement)
16
- * - [Vue](https://vuejs.org/guide/extras/render-function)
17
- * - [Hyperscript](https://github.com/hyperhype/hyperscript)
18
- * - [Angular](https://angular.dev/guide/components/programmatic-rendering)
19
- * - [Lit](https://lit.dev/docs/components/rendering/)
20
- *
21
- * This is not exactly a replacement, but it roughly follows the same idea.
22
- *
23
- * @example
24
- * ```ts
25
- * // Create a simple div
26
- * h('div', { id: 'app' }, 'Hello World')
27
- *
28
- * // Create a nested structure
29
- * h('ul', { class: 'list' },
30
- * h('li', null, 'Item 1'),
31
- * h('li', null, 'Item 2')
32
- * )
33
- * ```
34
- *
35
- * @param tagName - The HTML tag name.
36
- * @param attributes - Attributes to set on the element. Can be null or undefined.
37
- * @param children - Children to append (strings, nodes, or other JJHE instances).
38
- * @returns The created JJHE instance.
39
- * @see {@link https://developer.mozilla.org/en-US/docs/Web/API/Document/createElement | document.createElement}
40
- */
41
- export function h(tagName, attributes, ...children) {
42
- const ret = JJHE.fromTag(tagName).append(...children);
43
- if (attributes) {
44
- ret.setAttr(attributes);
45
- }
46
- return ret;
47
- }
48
- /**
49
- * Tries to find the best match for the link.as attribute when it's omitted
50
- * @param href a relative, absolute, or URL resource
51
- * @returns a valid value for the link.as attribute
52
- * @throws {TypeError} if it cannot guess a valid value for the 'link.as' attribute
53
- */
54
- function linkAs(href) {
55
- switch (fileExt(href)) {
56
- case 'html':
57
- case 'htm':
58
- case 'md':
59
- return 'fetch';
60
- case 'css':
61
- return 'style';
62
- case 'js':
63
- case 'mjs':
64
- case 'cjs':
65
- return 'script';
66
- default:
67
- throw new Error(`No 'as' attribute was specified and we failed to guess it from the URL: ${href}`);
68
- }
69
- }
70
- /**
71
- * Creates a `<link>` element for prefetching or preloading resources.
72
- *
73
- * @remarks
74
- * This function validates the input arguments and returns a wrapped `JJHE` instance.
75
- * It does not append the element to the document.
76
- *
77
- * @param href - The URL of the resource.
78
- * @param rel - The relationship of the linked resource ('prefetch' or 'preload').
79
- * @param as - The type of content being loaded ('fetch' for HTML, 'style' for CSS, or 'script' for JavaScript files).
80
- * If it's not provided or set to a falsy value, it runs heuristics to find the best match from the href parameter.
81
- *
82
- * @returns The JJHE instance representing the link element. The `<link>` is accessible via `.ref`
83
- * @throws {TypeError} If `href` is not a string or URL.
84
- * @throws {RangeError} If `rel` or `as` are not valid values.
85
- * @see {@link https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/rel/preload | Link types: preload}
86
- * @see {@link https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/rel/prefetch | Link types: prefetch}
87
- */
88
- export function createLinkPre(href, rel, as) {
89
- if (!isStr(href)) {
90
- if (!isA(href, URL)) {
91
- throw typeErr('href', 'a string or URL', href);
92
- }
93
- href = href.toString();
94
- }
95
- if (!['prefetch', 'preload'].includes(rel)) {
96
- throw new RangeError(errMsg('rel', `'prefetch' or 'preload'`, rel));
97
- }
98
- if (!as) {
99
- as = linkAs(href);
100
- if (!as) {
101
- throw new Error(`Could not guess 'as' attribute from URL: ${href}`);
102
- }
103
- }
104
- if (!['fetch', 'style', 'script'].includes(as)) {
105
- throw new RangeError(errMsg('as', `'fetch', 'style', or 'script'`, as));
106
- }
107
- return JJHE.fromTag('link').setAttr({
108
- href,
109
- rel,
110
- as,
111
- });
112
- }
113
- /**
114
- * Adds a `<link>` tag to the document head for prefetching or preloading resources.
115
- *
116
- * @remarks
117
- * This function helps in optimizing performance by telling the browser to fetch resources
118
- * that might be needed later (prefetch) or are needed immediately (preload).
119
- *
120
- * Please refer to {@link createLinkPre} for more details.
121
- *
122
- * @example
123
- * ```ts
124
- * // Preload a script
125
- * addLinkPre('https://example.com/script.js', 'preload', 'script')
126
- *
127
- * // Prefetch a future page's CSS
128
- * addLinkPre('/next-page.css', 'prefetch', 'style')
129
- * ```
130
- *
131
- * @param args - The arguments to be passed to {@link createLinkPre}.
132
- * @returns The JJHE instance representing the link element.
133
- * @throws {TypeError} If `href` is not a string or URL.
134
- * @throws {RangeError} If `rel` or `as` are not valid values.
135
- * @see {@link createLinkPre}
136
- * @see {@link https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/rel/preload | Link types: preload}
137
- * @see {@link https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/rel/prefetch | Link types: prefetch}
138
- */
139
- export function addLinkPre(...args) {
140
- const link = createLinkPre(...args);
141
- document.head.append(link.ref);
142
- return link;
143
- }
144
- /**
145
- * Fetches a file and returns its contents as string.
146
- *
147
- * @remarks
148
- * This is a wrapper around the native `fetch` API that handles the response status check
149
- * and text extraction. It sets the `Accept` header based on the provided mime type.
150
- *
151
- * @example
152
- * ```ts
153
- * const text = await fetchText('https://example.com/data.txt')
154
- * ```
155
- *
156
- * @param url - The file location.
157
- * @param mime - The HTTP Request Accept header. Defaults to 'text/*'.
158
- * @returns The file contents as a string.
159
- * @throws {TypeError} If `mime` is not a string.
160
- * @throws {Error} If the fetch fails or the response status is not OK (200-299).
161
- * @see {@link https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API | Fetch API}
162
- * @see {@link https://developer.mozilla.org/en-US/docs/Web/API/Response/text | Response.text()}
163
- */
164
- export async function fetchText(url, mime = 'text/*') {
165
- if (!isStr(mime)) {
166
- throw new TypeError(`Expected a string mime like 'text/html' or 'text/css'. Got ${mime} (${typeof mime})`);
167
- }
168
- const response = await fetch(url, { headers: { Accept: mime } });
169
- if (!response.ok) {
170
- throw new Error(`GET ${url} failed: ${response.status} ${response.statusText}`);
171
- }
172
- return response.text();
173
- }
174
- /**
175
- * Fetches the contents of a HTML file as string.
176
- *
177
- * @remarks
178
- * Useful for loading HTML templates dynamically.
179
- * You can use `import.meta.resolve('./relative-path-to.html')` to resolve paths relative to the current module.
180
- *
181
- * @example
182
- * ```ts
183
- * const template = await fetchHtml('./template.html')
184
- * ```
185
- *
186
- * @param url - The HTML file location.
187
- * @returns The file content as a string.
188
- * @throws {Error} If the response is not ok.
189
- * @see {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types | MIME types}
190
- */
191
- export async function fetchHtml(url) {
192
- return await fetchText(url, 'text/html');
193
- }
194
- /**
195
- * Fetches the contents of a CSS file as string.
196
- *
197
- * @remarks
198
- * You can use `import.meta.resolve('./relative-path-to.css')` inside components to resolve relative paths.
199
- *
200
- * @example
201
- * ```ts
202
- * const css = await fetchCss('./style.css')
203
- * ```
204
- *
205
- * @param url - The CSS file location.
206
- * @returns The file content as a string.
207
- * @throws {Error} If the response is not ok.
208
- */
209
- export async function fetchCss(url) {
210
- return await fetchText(url, 'text/css');
211
- }
212
- /**
213
- * Fetches a CSS file and constructs a CSSStyleSheet.
214
- *
215
- * @remarks
216
- * This is particularly useful for Constructable Stylesheets, which can be shared across Shadow DOM boundaries.
217
- *
218
- * @example
219
- * ```ts
220
- * const sheet = await fetchStyle('./component.css')
221
- * shadowRoot.adoptedStyleSheets = [sheet]
222
- * ```
223
- *
224
- * @param url - The CSS file location.
225
- * @returns The CSSStyleSheet object constructed from the CSS contents.
226
- * @throws {Error} If the fetch fails.
227
- * @see {@link https://developer.mozilla.org/en-US/docs/Web/API/CSSStyleSheet | CSSStyleSheet}
228
- * @see {@link https://developer.mozilla.org/en-US/docs/Web/API/ShadowRoot/adoptedStyleSheets | adoptedStyleSheets}
229
- */
230
- export async function fetchStyle(url) {
231
- return await cssToStyle(await fetchCss(url));
232
- }
233
- //# sourceMappingURL=helpers.js.map