@hamur/image 0.0.0-stage → 0.0.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.
Files changed (156) hide show
  1. package/LICENSE +22 -0
  2. package/README.md +24 -2
  3. package/dist/chunks/bun-index-n7xnn0a2.js +502 -0
  4. package/dist/src/adapters/angular.js +275 -0
  5. package/dist/src/adapters/html.js +70 -0
  6. package/dist/src/adapters/preact.js +82 -0
  7. package/dist/src/adapters/react.js +84 -0
  8. package/dist/src/adapters/solid.js +93 -0
  9. package/dist/src/adapters/svelte.js +15 -0
  10. package/dist/src/adapters/vue.js +147 -0
  11. package/dist/src/index.js +1442 -0
  12. package/dist/src/server/index.js +9 -0
  13. package/dist/types/src/adapters/angular-img.d.ts +43 -0
  14. package/dist/types/src/adapters/angular-picture.d.ts +39 -0
  15. package/dist/types/src/adapters/angular.d.ts +3 -0
  16. package/dist/types/src/adapters/html-img.d.ts +10 -0
  17. package/dist/types/src/adapters/html-picture.d.ts +6 -0
  18. package/dist/types/src/adapters/html.d.ts +6 -0
  19. package/dist/types/src/adapters/preact-img.d.ts +8 -0
  20. package/dist/types/src/adapters/preact-picture.d.ts +6 -0
  21. package/dist/types/src/adapters/preact.d.ts +6 -0
  22. package/dist/types/src/adapters/react-img.d.ts +8 -0
  23. package/dist/types/src/adapters/react-picture.d.ts +6 -0
  24. package/dist/types/src/adapters/react.d.ts +6 -0
  25. package/dist/types/src/adapters/solid-img.d.ts +8 -0
  26. package/dist/types/src/adapters/solid-picture.d.ts +6 -0
  27. package/dist/types/src/adapters/solid.d.ts +6 -0
  28. package/dist/types/src/adapters/svelte.d.ts +3 -0
  29. package/dist/types/src/adapters/vue-img.d.ts +119 -0
  30. package/dist/types/src/adapters/vue-picture.d.ts +121 -0
  31. package/dist/types/src/adapters/vue.d.ts +234 -0
  32. package/dist/types/src/build/endpoint.d.ts +12 -0
  33. package/dist/types/src/build/platform.d.ts +24 -0
  34. package/dist/types/src/build/runtime.d.ts +9 -0
  35. package/dist/types/src/build/types.d.ts +4 -0
  36. package/dist/types/src/build/variants.d.ts +8 -0
  37. package/dist/types/src/domains.d.ts +27 -0
  38. package/dist/types/src/index.d.ts +9 -0
  39. package/dist/types/src/module.d.ts +3 -0
  40. package/dist/types/src/options.d.ts +15 -0
  41. package/dist/types/src/providers/bunny.d.ts +9 -0
  42. package/dist/types/src/providers/cloudflare.d.ts +9 -0
  43. package/dist/types/src/providers/cloudflareImages.d.ts +9 -0
  44. package/dist/types/src/providers/cloudinary.d.ts +9 -0
  45. package/dist/types/src/providers/contentful.d.ts +9 -0
  46. package/dist/types/src/providers/fastly.d.ts +9 -0
  47. package/dist/types/src/providers/hamur-url.d.ts +3 -0
  48. package/dist/types/src/providers/hamur.d.ts +3 -0
  49. package/dist/types/src/providers/imagekit.d.ts +9 -0
  50. package/dist/types/src/providers/imgix.d.ts +9 -0
  51. package/dist/types/src/providers/imgproxy.d.ts +9 -0
  52. package/dist/types/src/providers/ipx.d.ts +9 -0
  53. package/dist/types/src/providers/netlify.d.ts +9 -0
  54. package/dist/types/src/providers/none.d.ts +8 -0
  55. package/dist/types/src/providers/sanity.d.ts +9 -0
  56. package/dist/types/src/providers/shared.d.ts +7 -0
  57. package/dist/types/src/providers/storyblok.d.ts +9 -0
  58. package/dist/types/src/providers/strapi.d.ts +9 -0
  59. package/dist/types/src/providers/supabase.d.ts +9 -0
  60. package/dist/types/src/providers/twicpics.d.ts +9 -0
  61. package/dist/types/src/providers/uploadcare.d.ts +9 -0
  62. package/dist/types/src/providers/vercel-url.d.ts +3 -0
  63. package/dist/types/src/providers/vercel.d.ts +3 -0
  64. package/dist/types/src/providers/weserv.d.ts +9 -0
  65. package/dist/types/src/runtime/image.d.ts +28 -0
  66. package/dist/types/src/runtime/img.d.ts +71 -0
  67. package/dist/types/src/runtime/index.d.ts +9 -0
  68. package/dist/types/src/runtime/picture.d.ts +24 -0
  69. package/dist/types/src/runtime/sizes.d.ts +8 -0
  70. package/dist/types/src/runtime/source.d.ts +3 -0
  71. package/dist/types/src/server/door.d.ts +1 -0
  72. package/dist/types/src/server/endpoint.d.ts +46 -0
  73. package/dist/types/src/server/handler.d.ts +1 -0
  74. package/dist/types/src/server/index.d.ts +1 -0
  75. package/dist/types/src/server/miss-handler.d.ts +1 -0
  76. package/dist/types/src/server/miss.d.ts +1 -0
  77. package/dist/types/src/server/parse.d.ts +30 -0
  78. package/dist/types/src/server/remote.d.ts +20 -0
  79. package/dist/types/src/types.d.ts +178 -0
  80. package/package.json +141 -4
  81. package/src/adapters/Img.svelte +33 -0
  82. package/src/adapters/Picture.svelte +39 -0
  83. package/src/adapters/angular-img.ts +143 -0
  84. package/src/adapters/angular-picture.ts +172 -0
  85. package/src/adapters/angular.ts +3 -0
  86. package/src/adapters/html-img.ts +42 -0
  87. package/src/adapters/html-picture.ts +30 -0
  88. package/src/adapters/html.ts +9 -0
  89. package/src/adapters/preact-img.ts +51 -0
  90. package/src/adapters/preact-picture.ts +40 -0
  91. package/src/adapters/preact.ts +9 -0
  92. package/src/adapters/react-img.ts +54 -0
  93. package/src/adapters/react-picture.ts +49 -0
  94. package/src/adapters/react.ts +9 -0
  95. package/src/adapters/solid-img.ts +47 -0
  96. package/src/adapters/solid-picture.ts +43 -0
  97. package/src/adapters/solid.ts +9 -0
  98. package/src/adapters/svelte.ts +3 -0
  99. package/src/adapters/test/ImgCustomPage.svelte +14 -0
  100. package/src/adapters/test/ImgPage.svelte +5 -0
  101. package/src/adapters/test/ImgPreloadPage.svelte +6 -0
  102. package/src/adapters/test/PicturePage.svelte +6 -0
  103. package/src/adapters/test/PicturePreloadPage.svelte +5 -0
  104. package/src/adapters/test/support.ts +116 -0
  105. package/src/adapters/vue-img.ts +72 -0
  106. package/src/adapters/vue-picture.ts +64 -0
  107. package/src/adapters/vue.ts +7 -0
  108. package/src/build/endpoint.ts +78 -0
  109. package/src/build/platform.ts +99 -0
  110. package/src/build/runtime.ts +118 -0
  111. package/src/build/types.ts +23 -0
  112. package/src/build/variants.ts +174 -0
  113. package/src/domains.ts +84 -0
  114. package/src/env.d.ts +24 -0
  115. package/src/index.ts +23 -0
  116. package/src/module.ts +29 -0
  117. package/src/options.ts +329 -0
  118. package/src/providers/bunny.ts +21 -0
  119. package/src/providers/cloudflare.ts +22 -0
  120. package/src/providers/cloudflareImages.ts +25 -0
  121. package/src/providers/cloudinary.ts +31 -0
  122. package/src/providers/contentful.ts +26 -0
  123. package/src/providers/fastly.ts +29 -0
  124. package/src/providers/hamur-url.ts +81 -0
  125. package/src/providers/hamur.ts +8 -0
  126. package/src/providers/imagekit.ts +30 -0
  127. package/src/providers/imgix.ts +26 -0
  128. package/src/providers/imgproxy.ts +24 -0
  129. package/src/providers/ipx.ts +26 -0
  130. package/src/providers/netlify.ts +23 -0
  131. package/src/providers/none.ts +9 -0
  132. package/src/providers/sanity.ts +29 -0
  133. package/src/providers/shared.ts +17 -0
  134. package/src/providers/storyblok.ts +24 -0
  135. package/src/providers/strapi.ts +24 -0
  136. package/src/providers/supabase.ts +26 -0
  137. package/src/providers/twicpics.ts +26 -0
  138. package/src/providers/uploadcare.ts +32 -0
  139. package/src/providers/vercel-url.ts +18 -0
  140. package/src/providers/vercel.ts +8 -0
  141. package/src/providers/weserv.ts +25 -0
  142. package/src/runtime/image.ts +266 -0
  143. package/src/runtime/img.ts +241 -0
  144. package/src/runtime/index.ts +18 -0
  145. package/src/runtime/picture.ts +62 -0
  146. package/src/runtime/sizes.ts +52 -0
  147. package/src/runtime/source.ts +8 -0
  148. package/src/server/door.ts +1 -0
  149. package/src/server/endpoint.ts +428 -0
  150. package/src/server/handler.ts +33 -0
  151. package/src/server/index.ts +1 -0
  152. package/src/server/miss-handler.ts +20 -0
  153. package/src/server/miss.ts +1 -0
  154. package/src/server/parse.ts +152 -0
  155. package/src/server/remote.ts +166 -0
  156. package/src/types.ts +177 -0
@@ -0,0 +1,143 @@
1
+ import { Directive, ElementRef, Injector, Renderer2, DestroyRef, afterNextRender, booleanAttribute, computed, effect, inject, input, numberAttribute, runInInjectionContext, signal, } from "@angular/core";
2
+ import { injectHead } from "@hamur/adapter-angular";
3
+ import { imgAttributes } from "../runtime/index.ts";
4
+ import { createImgEvents, imagePreloadLink, placeholderStyle } from "../runtime/img.ts";
5
+ import * as i0 from "@angular/core";
6
+ const optionalNumber = (value) => value === undefined ? undefined : numberAttribute(value);
7
+ /** An attribute binding takes a string, and `null` removes the attribute. */
8
+ export const strings = (attributes) => Object.fromEntries(Object.entries(attributes).map(([name, value]) => [name, value == null ? null : String(value)]));
9
+ /**
10
+ * `<img hamurImg src="/hero.jpg" width="1200" alt="">`. The host bindings write what the other adapters' `Img`
11
+ * writes on its `<img>`; every other attribute and listener stays the element's own. The props that are not attributes are
12
+ * taken off the element. `src`, `srcset` and `sizes` are written only when they differ: the browser loads the image again on
13
+ * every write, so a binding that rewrites the server's equal value during hydration fires `load` again.
14
+ */
15
+ export class Img {
16
+ src = input.required(/* @ts-ignore */
17
+ ...(ngDevMode ? [{ debugName: "src" }] : /* istanbul ignore next */ []));
18
+ alt = input(/* @ts-ignore */
19
+ ...(ngDevMode ? [undefined, { debugName: "alt" }] : /* istanbul ignore next */ []));
20
+ width = input(/* @ts-ignore */
21
+ ...(ngDevMode ? [undefined, { debugName: "width" }] : /* istanbul ignore next */ []));
22
+ height = input(/* @ts-ignore */
23
+ ...(ngDevMode ? [undefined, { debugName: "height" }] : /* istanbul ignore next */ []));
24
+ sizes = input(/* @ts-ignore */
25
+ ...(ngDevMode ? [undefined, { debugName: "sizes" }] : /* istanbul ignore next */ []));
26
+ densities = input(/* @ts-ignore */
27
+ ...(ngDevMode ? [undefined, { debugName: "densities" }] : /* istanbul ignore next */ []));
28
+ format = input(/* @ts-ignore */
29
+ ...(ngDevMode ? [undefined, { debugName: "format" }] : /* istanbul ignore next */ []));
30
+ quality = input(undefined, { ...(ngDevMode ? { debugName: "quality" } : /* istanbul ignore next */ {}), transform: optionalNumber });
31
+ fit = input(/* @ts-ignore */
32
+ ...(ngDevMode ? [undefined, { debugName: "fit" }] : /* istanbul ignore next */ []));
33
+ modifiers = input(/* @ts-ignore */
34
+ ...(ngDevMode ? [undefined, { debugName: "modifiers" }] : /* istanbul ignore next */ []));
35
+ provider = input(/* @ts-ignore */
36
+ ...(ngDevMode ? [undefined, { debugName: "provider" }] : /* istanbul ignore next */ []));
37
+ preset = input(/* @ts-ignore */
38
+ ...(ngDevMode ? [undefined, { debugName: "preset" }] : /* istanbul ignore next */ []));
39
+ loading = input(/* @ts-ignore */
40
+ ...(ngDevMode ? [undefined, { debugName: "loading" }] : /* istanbul ignore next */ []));
41
+ decoding = input(/* @ts-ignore */
42
+ ...(ngDevMode ? [undefined, { debugName: "decoding" }] : /* istanbul ignore next */ []));
43
+ fetchpriority = input(/* @ts-ignore */
44
+ ...(ngDevMode ? [undefined, { debugName: "fetchpriority" }] : /* istanbul ignore next */ []));
45
+ preload = input(false, { ...(ngDevMode ? { debugName: "preload" } : /* istanbul ignore next */ {}), transform: booleanAttribute });
46
+ placeholder = input(/* @ts-ignore */
47
+ ...(ngDevMode ? [undefined, { debugName: "placeholder" }] : /* istanbul ignore next */ []));
48
+ placeholderClass = input(/* @ts-ignore */
49
+ ...(ngDevMode ? [undefined, { debugName: "placeholderClass" }] : /* istanbul ignore next */ []));
50
+ injector = inject(Injector);
51
+ loaded = signal(false, /* @ts-ignore */
52
+ ...(ngDevMode ? [{ debugName: "loaded" }] : /* istanbul ignore next */ []));
53
+ events = createImgEvents(() => ({}), (value) => this.loaded.set(value));
54
+ resolved = computed(() => imgAttributes({
55
+ src: this.src(),
56
+ alt: this.alt(),
57
+ width: this.width(),
58
+ height: this.height(),
59
+ sizes: this.sizes(),
60
+ densities: this.densities(),
61
+ format: this.format(),
62
+ quality: this.quality(),
63
+ fit: this.fit(),
64
+ modifiers: this.modifiers(),
65
+ provider: this.provider(),
66
+ preset: this.preset(),
67
+ loading: this.loading(),
68
+ decoding: this.decoding(),
69
+ fetchpriority: this.fetchpriority(),
70
+ preload: this.preload(),
71
+ }), /* @ts-ignore */
72
+ ...(ngDevMode ? [{ debugName: "resolved" }] : /* istanbul ignore next */ []));
73
+ attrs = computed(() => strings(this.resolved()), /* @ts-ignore */
74
+ ...(ngDevMode ? [{ debugName: "attrs" }] : /* istanbul ignore next */ []));
75
+ painted = computed(() => {
76
+ const url = this.placeholder();
77
+ return this.loaded() || !url ? {} : placeholderStyle(url);
78
+ }, /* @ts-ignore */
79
+ ...(ngDevMode ? [{ debugName: "painted" }] : /* istanbul ignore next */ []));
80
+ paintedClass = computed(() => (this.loaded() ? "" : (this.placeholderClass() ?? "")), /* @ts-ignore */
81
+ ...(ngDevMode ? [{ debugName: "paintedClass" }] : /* istanbul ignore next */ []));
82
+ constructor() {
83
+ const element = inject(ElementRef).nativeElement;
84
+ const renderer = inject(Renderer2);
85
+ effect(() => {
86
+ const attrs = this.attrs();
87
+ for (const name of ["src", "srcset", "sizes"]) {
88
+ const value = attrs[name] ?? null;
89
+ if (element.getAttribute(name) === value)
90
+ continue;
91
+ if (value === null)
92
+ renderer.removeAttribute(element, name);
93
+ else
94
+ renderer.setAttribute(element, name, value);
95
+ }
96
+ });
97
+ // Read at hydration: a server image the browser already finished fired its load before any listener existed.
98
+ const finished = element.isConnected && element.complete && element.getAttribute("src") !== null;
99
+ const offLoad = renderer.listen(element, "load", (event) => this.events.load(event));
100
+ const offError = renderer.listen(element, "error", (event) => this.events.error(event));
101
+ inject(DestroyRef).onDestroy(() => {
102
+ offLoad();
103
+ offError();
104
+ });
105
+ // Angular's event replay captures a load that fired before hydration and replays it to `(load)` bindings itself;
106
+ // only the directive's own state is caught up here, so no second load is dispatched.
107
+ if (finished)
108
+ afterNextRender(() => this.loaded.set(element.naturalWidth > 0));
109
+ }
110
+ ngOnInit() {
111
+ if (this.preload())
112
+ runInInjectionContext(this.injector, () => injectHead(() => ({ link: [imagePreloadLink(this.resolved())] })));
113
+ }
114
+ static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "22.3.2", ngImport: i0, type: Img, deps: [], target: i0.ɵɵFactoryTarget.Directive });
115
+ static ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "17.1.0", version: "22.3.2", type: Img, isStandalone: true, selector: "img[hamurImg]", inputs: { src: { classPropertyName: "src", publicName: "src", isSignal: true, isRequired: true, transformFunction: null }, alt: { classPropertyName: "alt", publicName: "alt", isSignal: true, isRequired: false, transformFunction: null }, width: { classPropertyName: "width", publicName: "width", isSignal: true, isRequired: false, transformFunction: null }, height: { classPropertyName: "height", publicName: "height", isSignal: true, isRequired: false, transformFunction: null }, sizes: { classPropertyName: "sizes", publicName: "sizes", isSignal: true, isRequired: false, transformFunction: null }, densities: { classPropertyName: "densities", publicName: "densities", isSignal: true, isRequired: false, transformFunction: null }, format: { classPropertyName: "format", publicName: "format", isSignal: true, isRequired: false, transformFunction: null }, quality: { classPropertyName: "quality", publicName: "quality", isSignal: true, isRequired: false, transformFunction: null }, fit: { classPropertyName: "fit", publicName: "fit", isSignal: true, isRequired: false, transformFunction: null }, modifiers: { classPropertyName: "modifiers", publicName: "modifiers", isSignal: true, isRequired: false, transformFunction: null }, provider: { classPropertyName: "provider", publicName: "provider", isSignal: true, isRequired: false, transformFunction: null }, preset: { classPropertyName: "preset", publicName: "preset", isSignal: true, isRequired: false, transformFunction: null }, loading: { classPropertyName: "loading", publicName: "loading", isSignal: true, isRequired: false, transformFunction: null }, decoding: { classPropertyName: "decoding", publicName: "decoding", isSignal: true, isRequired: false, transformFunction: null }, fetchpriority: { classPropertyName: "fetchpriority", publicName: "fetchpriority", isSignal: true, isRequired: false, transformFunction: null }, preload: { classPropertyName: "preload", publicName: "preload", isSignal: true, isRequired: false, transformFunction: null }, placeholder: { classPropertyName: "placeholder", publicName: "placeholder", isSignal: true, isRequired: false, transformFunction: null }, placeholderClass: { classPropertyName: "placeholderClass", publicName: "placeholderClass", isSignal: true, isRequired: false, transformFunction: null } }, host: { properties: { "attr.hamurImg": "null", "attr.densities": "null", "attr.format": "null", "attr.quality": "null", "attr.fit": "null", "attr.modifiers": "null", "attr.provider": "null", "attr.preset": "null", "attr.preload": "null", "attr.placeholder": "null", "attr.placeholderClass": "null", "attr.width": "attrs()['width']", "attr.height": "attrs()['height']", "attr.alt": "attrs()['alt']", "attr.loading": "attrs()['loading']", "attr.decoding": "attrs()['decoding']", "attr.fetchpriority": "attrs()['fetchpriority']", "style": "painted()", "class": "paintedClass()" } }, ngImport: i0 });
116
+ }
117
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.3.2", ngImport: i0, type: Img, decorators: [{
118
+ type: Directive,
119
+ args: [{
120
+ selector: "img[hamurImg]",
121
+ host: {
122
+ "[attr.hamurImg]": "null",
123
+ "[attr.densities]": "null",
124
+ "[attr.format]": "null",
125
+ "[attr.quality]": "null",
126
+ "[attr.fit]": "null",
127
+ "[attr.modifiers]": "null",
128
+ "[attr.provider]": "null",
129
+ "[attr.preset]": "null",
130
+ "[attr.preload]": "null",
131
+ "[attr.placeholder]": "null",
132
+ "[attr.placeholderClass]": "null",
133
+ "[attr.width]": "attrs()['width']",
134
+ "[attr.height]": "attrs()['height']",
135
+ "[attr.alt]": "attrs()['alt']",
136
+ "[attr.loading]": "attrs()['loading']",
137
+ "[attr.decoding]": "attrs()['decoding']",
138
+ "[attr.fetchpriority]": "attrs()['fetchpriority']",
139
+ "[style]": "painted()",
140
+ "[class]": "paintedClass()",
141
+ },
142
+ }]
143
+ }], ctorParameters: () => [], propDecorators: { src: [{ type: i0.Input, args: [{ isSignal: true, alias: "src", required: true }] }], alt: [{ type: i0.Input, args: [{ isSignal: true, alias: "alt", required: false }] }], width: [{ type: i0.Input, args: [{ isSignal: true, alias: "width", required: false }] }], height: [{ type: i0.Input, args: [{ isSignal: true, alias: "height", required: false }] }], sizes: [{ type: i0.Input, args: [{ isSignal: true, alias: "sizes", required: false }] }], densities: [{ type: i0.Input, args: [{ isSignal: true, alias: "densities", required: false }] }], format: [{ type: i0.Input, args: [{ isSignal: true, alias: "format", required: false }] }], quality: [{ type: i0.Input, args: [{ isSignal: true, alias: "quality", required: false }] }], fit: [{ type: i0.Input, args: [{ isSignal: true, alias: "fit", required: false }] }], modifiers: [{ type: i0.Input, args: [{ isSignal: true, alias: "modifiers", required: false }] }], provider: [{ type: i0.Input, args: [{ isSignal: true, alias: "provider", required: false }] }], preset: [{ type: i0.Input, args: [{ isSignal: true, alias: "preset", required: false }] }], loading: [{ type: i0.Input, args: [{ isSignal: true, alias: "loading", required: false }] }], decoding: [{ type: i0.Input, args: [{ isSignal: true, alias: "decoding", required: false }] }], fetchpriority: [{ type: i0.Input, args: [{ isSignal: true, alias: "fetchpriority", required: false }] }], preload: [{ type: i0.Input, args: [{ isSignal: true, alias: "preload", required: false }] }], placeholder: [{ type: i0.Input, args: [{ isSignal: true, alias: "placeholder", required: false }] }], placeholderClass: [{ type: i0.Input, args: [{ isSignal: true, alias: "placeholderClass", required: false }] }] } });
@@ -0,0 +1,172 @@
1
+ import { Component, Injector, Renderer2, afterRenderEffect, booleanAttribute, computed, inject, input, numberAttribute, runInInjectionContext, signal, viewChild, } from "@angular/core";
2
+ import { injectHead } from "@hamur/adapter-angular";
3
+ import { pictureAttributes } from "../runtime/index.ts";
4
+ import { applyPlaceholder, createImgEvents } from "../runtime/img.ts";
5
+ import { picturePreloadLink } from "../runtime/picture.ts";
6
+ import { strings } from "./angular-img.ts";
7
+ import * as i0 from "@angular/core";
8
+ const optionalNumber = (value) => value === undefined ? undefined : numberAttribute(value);
9
+ /**
10
+ * `<hamur-picture src="/hero.jpg" format="webp" width="1200" alt="" />`, one `<source>` per format and the
11
+ * `<img>` in the source's own format, or the `<img>` alone when the provider makes no other format.
12
+ */
13
+ export class Picture {
14
+ src = input.required(/* @ts-ignore */
15
+ ...(ngDevMode ? [{ debugName: "src" }] : /* istanbul ignore next */ []));
16
+ alt = input(/* @ts-ignore */
17
+ ...(ngDevMode ? [undefined, { debugName: "alt" }] : /* istanbul ignore next */ []));
18
+ width = input(/* @ts-ignore */
19
+ ...(ngDevMode ? [undefined, { debugName: "width" }] : /* istanbul ignore next */ []));
20
+ height = input(/* @ts-ignore */
21
+ ...(ngDevMode ? [undefined, { debugName: "height" }] : /* istanbul ignore next */ []));
22
+ sizes = input(/* @ts-ignore */
23
+ ...(ngDevMode ? [undefined, { debugName: "sizes" }] : /* istanbul ignore next */ []));
24
+ densities = input(/* @ts-ignore */
25
+ ...(ngDevMode ? [undefined, { debugName: "densities" }] : /* istanbul ignore next */ []));
26
+ format = input(/* @ts-ignore */
27
+ ...(ngDevMode ? [undefined, { debugName: "format" }] : /* istanbul ignore next */ []));
28
+ legacyFormat = input(/* @ts-ignore */
29
+ ...(ngDevMode ? [undefined, { debugName: "legacyFormat" }] : /* istanbul ignore next */ []));
30
+ quality = input(undefined, { ...(ngDevMode ? { debugName: "quality" } : /* istanbul ignore next */ {}), transform: optionalNumber });
31
+ fit = input(/* @ts-ignore */
32
+ ...(ngDevMode ? [undefined, { debugName: "fit" }] : /* istanbul ignore next */ []));
33
+ modifiers = input(/* @ts-ignore */
34
+ ...(ngDevMode ? [undefined, { debugName: "modifiers" }] : /* istanbul ignore next */ []));
35
+ provider = input(/* @ts-ignore */
36
+ ...(ngDevMode ? [undefined, { debugName: "provider" }] : /* istanbul ignore next */ []));
37
+ preset = input(/* @ts-ignore */
38
+ ...(ngDevMode ? [undefined, { debugName: "preset" }] : /* istanbul ignore next */ []));
39
+ loading = input(/* @ts-ignore */
40
+ ...(ngDevMode ? [undefined, { debugName: "loading" }] : /* istanbul ignore next */ []));
41
+ decoding = input(/* @ts-ignore */
42
+ ...(ngDevMode ? [undefined, { debugName: "decoding" }] : /* istanbul ignore next */ []));
43
+ fetchpriority = input(/* @ts-ignore */
44
+ ...(ngDevMode ? [undefined, { debugName: "fetchpriority" }] : /* istanbul ignore next */ []));
45
+ preload = input(false, { ...(ngDevMode ? { debugName: "preload" } : /* istanbul ignore next */ {}), transform: booleanAttribute });
46
+ placeholder = input(/* @ts-ignore */
47
+ ...(ngDevMode ? [undefined, { debugName: "placeholder" }] : /* istanbul ignore next */ []));
48
+ placeholderClass = input(/* @ts-ignore */
49
+ ...(ngDevMode ? [undefined, { debugName: "placeholderClass" }] : /* istanbul ignore next */ []));
50
+ injector = inject(Injector);
51
+ loaded = signal(false, /* @ts-ignore */
52
+ ...(ngDevMode ? [{ debugName: "loaded" }] : /* istanbul ignore next */ []));
53
+ element = viewChild("img", /* @ts-ignore */
54
+ ...(ngDevMode ? [{ debugName: "element" }] : /* istanbul ignore next */ []));
55
+ events = createImgEvents(() => ({}), (value) => this.loaded.set(value));
56
+ parts = computed(() => pictureAttributes({
57
+ src: this.src(),
58
+ alt: this.alt(),
59
+ width: this.width(),
60
+ height: this.height(),
61
+ sizes: this.sizes(),
62
+ densities: this.densities(),
63
+ format: this.format(),
64
+ legacyFormat: this.legacyFormat(),
65
+ quality: this.quality(),
66
+ fit: this.fit(),
67
+ modifiers: this.modifiers(),
68
+ provider: this.provider(),
69
+ preset: this.preset(),
70
+ loading: this.loading(),
71
+ decoding: this.decoding(),
72
+ fetchpriority: this.fetchpriority(),
73
+ preload: this.preload(),
74
+ }), /* @ts-ignore */
75
+ ...(ngDevMode ? [{ debugName: "parts" }] : /* istanbul ignore next */ []));
76
+ shown = computed(() => strings(applyPlaceholder(this.parts().img, { placeholder: this.placeholder(), placeholderClass: this.placeholderClass() }, this.loaded())), /* @ts-ignore */
77
+ ...(ngDevMode ? [{ debugName: "shown" }] : /* istanbul ignore next */ []));
78
+ constructor() {
79
+ const renderer = inject(Renderer2);
80
+ afterRenderEffect((onCleanup) => {
81
+ const image = this.element()?.nativeElement;
82
+ if (!image)
83
+ return;
84
+ onCleanup(renderer.listen(image, "load", (event) => this.events.load(event)));
85
+ onCleanup(renderer.listen(image, "error", (event) => this.events.error(event)));
86
+ this.events.replay(image);
87
+ });
88
+ }
89
+ ngOnInit() {
90
+ if (this.preload())
91
+ runInInjectionContext(this.injector, () => injectHead(() => ({ link: [picturePreloadLink(this.parts())] })));
92
+ }
93
+ static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "22.3.2", ngImport: i0, type: Picture, deps: [], target: i0.ɵɵFactoryTarget.Component });
94
+ static ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "17.0.0", version: "22.3.2", type: Picture, isStandalone: true, selector: "hamur-picture", inputs: { src: { classPropertyName: "src", publicName: "src", isSignal: true, isRequired: true, transformFunction: null }, alt: { classPropertyName: "alt", publicName: "alt", isSignal: true, isRequired: false, transformFunction: null }, width: { classPropertyName: "width", publicName: "width", isSignal: true, isRequired: false, transformFunction: null }, height: { classPropertyName: "height", publicName: "height", isSignal: true, isRequired: false, transformFunction: null }, sizes: { classPropertyName: "sizes", publicName: "sizes", isSignal: true, isRequired: false, transformFunction: null }, densities: { classPropertyName: "densities", publicName: "densities", isSignal: true, isRequired: false, transformFunction: null }, format: { classPropertyName: "format", publicName: "format", isSignal: true, isRequired: false, transformFunction: null }, legacyFormat: { classPropertyName: "legacyFormat", publicName: "legacyFormat", isSignal: true, isRequired: false, transformFunction: null }, quality: { classPropertyName: "quality", publicName: "quality", isSignal: true, isRequired: false, transformFunction: null }, fit: { classPropertyName: "fit", publicName: "fit", isSignal: true, isRequired: false, transformFunction: null }, modifiers: { classPropertyName: "modifiers", publicName: "modifiers", isSignal: true, isRequired: false, transformFunction: null }, provider: { classPropertyName: "provider", publicName: "provider", isSignal: true, isRequired: false, transformFunction: null }, preset: { classPropertyName: "preset", publicName: "preset", isSignal: true, isRequired: false, transformFunction: null }, loading: { classPropertyName: "loading", publicName: "loading", isSignal: true, isRequired: false, transformFunction: null }, decoding: { classPropertyName: "decoding", publicName: "decoding", isSignal: true, isRequired: false, transformFunction: null }, fetchpriority: { classPropertyName: "fetchpriority", publicName: "fetchpriority", isSignal: true, isRequired: false, transformFunction: null }, preload: { classPropertyName: "preload", publicName: "preload", isSignal: true, isRequired: false, transformFunction: null }, placeholder: { classPropertyName: "placeholder", publicName: "placeholder", isSignal: true, isRequired: false, transformFunction: null }, placeholderClass: { classPropertyName: "placeholderClass", publicName: "placeholderClass", isSignal: true, isRequired: false, transformFunction: null } }, viewQueries: [{ propertyName: "element", first: true, predicate: ["img"], descendants: true, isSignal: true }], ngImport: i0, template: `@if (parts().sources.length > 0) {
95
+ <picture>
96
+ @for (source of parts().sources; track source.type) {
97
+ <source [attr.type]="source.type" [attr.srcset]="source.srcset" [attr.sizes]="source.sizes" />
98
+ }
99
+ <img
100
+ #img
101
+ [attr.src]="shown()['src']"
102
+ [attr.srcset]="shown()['srcset']"
103
+ [attr.sizes]="shown()['sizes']"
104
+ [attr.width]="shown()['width']"
105
+ [attr.height]="shown()['height']"
106
+ [attr.alt]="shown()['alt']"
107
+ [attr.loading]="shown()['loading']"
108
+ [attr.decoding]="shown()['decoding']"
109
+ [attr.fetchpriority]="shown()['fetchpriority']"
110
+ [attr.style]="shown()['style']"
111
+ [attr.class]="shown()['class']"
112
+ />
113
+ </picture>
114
+ } @else {
115
+ <img
116
+ #img
117
+ [attr.src]="shown()['src']"
118
+ [attr.srcset]="shown()['srcset']"
119
+ [attr.sizes]="shown()['sizes']"
120
+ [attr.width]="shown()['width']"
121
+ [attr.height]="shown()['height']"
122
+ [attr.alt]="shown()['alt']"
123
+ [attr.loading]="shown()['loading']"
124
+ [attr.decoding]="shown()['decoding']"
125
+ [attr.fetchpriority]="shown()['fetchpriority']"
126
+ [attr.style]="shown()['style']"
127
+ [attr.class]="shown()['class']"
128
+ />
129
+ }`, isInline: true });
130
+ }
131
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.3.2", ngImport: i0, type: Picture, decorators: [{
132
+ type: Component,
133
+ args: [{
134
+ selector: "hamur-picture",
135
+ template: `@if (parts().sources.length > 0) {
136
+ <picture>
137
+ @for (source of parts().sources; track source.type) {
138
+ <source [attr.type]="source.type" [attr.srcset]="source.srcset" [attr.sizes]="source.sizes" />
139
+ }
140
+ <img
141
+ #img
142
+ [attr.src]="shown()['src']"
143
+ [attr.srcset]="shown()['srcset']"
144
+ [attr.sizes]="shown()['sizes']"
145
+ [attr.width]="shown()['width']"
146
+ [attr.height]="shown()['height']"
147
+ [attr.alt]="shown()['alt']"
148
+ [attr.loading]="shown()['loading']"
149
+ [attr.decoding]="shown()['decoding']"
150
+ [attr.fetchpriority]="shown()['fetchpriority']"
151
+ [attr.style]="shown()['style']"
152
+ [attr.class]="shown()['class']"
153
+ />
154
+ </picture>
155
+ } @else {
156
+ <img
157
+ #img
158
+ [attr.src]="shown()['src']"
159
+ [attr.srcset]="shown()['srcset']"
160
+ [attr.sizes]="shown()['sizes']"
161
+ [attr.width]="shown()['width']"
162
+ [attr.height]="shown()['height']"
163
+ [attr.alt]="shown()['alt']"
164
+ [attr.loading]="shown()['loading']"
165
+ [attr.decoding]="shown()['decoding']"
166
+ [attr.fetchpriority]="shown()['fetchpriority']"
167
+ [attr.style]="shown()['style']"
168
+ [attr.class]="shown()['class']"
169
+ />
170
+ }`,
171
+ }]
172
+ }], ctorParameters: () => [], propDecorators: { src: [{ type: i0.Input, args: [{ isSignal: true, alias: "src", required: true }] }], alt: [{ type: i0.Input, args: [{ isSignal: true, alias: "alt", required: false }] }], width: [{ type: i0.Input, args: [{ isSignal: true, alias: "width", required: false }] }], height: [{ type: i0.Input, args: [{ isSignal: true, alias: "height", required: false }] }], sizes: [{ type: i0.Input, args: [{ isSignal: true, alias: "sizes", required: false }] }], densities: [{ type: i0.Input, args: [{ isSignal: true, alias: "densities", required: false }] }], format: [{ type: i0.Input, args: [{ isSignal: true, alias: "format", required: false }] }], legacyFormat: [{ type: i0.Input, args: [{ isSignal: true, alias: "legacyFormat", required: false }] }], quality: [{ type: i0.Input, args: [{ isSignal: true, alias: "quality", required: false }] }], fit: [{ type: i0.Input, args: [{ isSignal: true, alias: "fit", required: false }] }], modifiers: [{ type: i0.Input, args: [{ isSignal: true, alias: "modifiers", required: false }] }], provider: [{ type: i0.Input, args: [{ isSignal: true, alias: "provider", required: false }] }], preset: [{ type: i0.Input, args: [{ isSignal: true, alias: "preset", required: false }] }], loading: [{ type: i0.Input, args: [{ isSignal: true, alias: "loading", required: false }] }], decoding: [{ type: i0.Input, args: [{ isSignal: true, alias: "decoding", required: false }] }], fetchpriority: [{ type: i0.Input, args: [{ isSignal: true, alias: "fetchpriority", required: false }] }], preload: [{ type: i0.Input, args: [{ isSignal: true, alias: "preload", required: false }] }], placeholder: [{ type: i0.Input, args: [{ isSignal: true, alias: "placeholder", required: false }] }], placeholderClass: [{ type: i0.Input, args: [{ isSignal: true, alias: "placeholderClass", required: false }] }], element: [{ type: i0.ViewChild, args: ["img", { isSignal: true }] }] } });
@@ -0,0 +1,3 @@
1
+ export { getImage, getSizes } from "../runtime/index.ts"
2
+ export { Img } from "./angular-img.ts"
3
+ export { Picture } from "./angular-picture.ts"
@@ -0,0 +1,42 @@
1
+ import { useHead } from "@hamur/adapter-html"
2
+ import {
3
+ applyPlaceholder,
4
+ imagePreloadLink,
5
+ type ImgAttributes,
6
+ type ImgOptions,
7
+ type ImgState as State,
8
+ } from "../runtime/img.ts"
9
+
10
+ /** html has no component events: `isLoaded` is `false` and `imgAttrs` holds strings. */
11
+ export type ImgState = State<Record<string, string>>
12
+
13
+ export type ImgProps = ImgOptions & {
14
+ fetchpriority?: "high" | "low" | "auto"
15
+ custom?: (state: ImgState) => string
16
+ [attribute: string]: unknown
17
+ }
18
+
19
+ const ESCAPES: Record<string, string> = { "&": "&amp;", "<": "&lt;", ">": "&gt;", '"': "&quot;" }
20
+
21
+ export const escape = (value: unknown): string => String(value).replace(/[&<>"]/g, (char) => ESCAPES[char]!)
22
+
23
+ export function createImg(attributes: ImgAttributes): (props: ImgProps) => string {
24
+ return function Img({ custom, ...props }) {
25
+ const attrs = applyPlaceholder(attributes(props), props, false)
26
+ if (props.preload) useHead({ link: [imagePreloadLink(attrs)] })
27
+ if (custom) {
28
+ const imgAttrs: Record<string, string> = {}
29
+ for (const [name, value] of Object.entries(attrs)) {
30
+ if (value === undefined || value === null || value === false) continue
31
+ imgAttrs[name] = value === true ? "" : String(value)
32
+ }
33
+ return custom({ imgAttrs, src: imgAttrs.src ?? "", isLoaded: false })
34
+ }
35
+ let out = "<img"
36
+ for (const [name, value] of Object.entries(attrs)) {
37
+ if (value === undefined || value === null || value === false) continue
38
+ out += value === true ? ` ${name}` : ` ${name}="${escape(value)}"`
39
+ }
40
+ return `${out}>`
41
+ }
42
+ }
@@ -0,0 +1,30 @@
1
+ import { useHead } from "@hamur/adapter-html"
2
+ import { applyPlaceholder } from "../runtime/img.ts"
3
+ import { picturePreloadLink, type PictureAttributes, type PictureOptions } from "../runtime/picture.ts"
4
+ import { escape } from "./html-img.ts"
5
+
6
+ export type PictureProps = PictureOptions & {
7
+ fetchpriority?: "high" | "low" | "auto"
8
+ [attribute: string]: unknown
9
+ }
10
+
11
+ const tag = (name: string, attributes: object): string => {
12
+ let out = `<${name}`
13
+ for (const [key, value] of Object.entries(attributes)) {
14
+ if (value === undefined || value === null || value === false) continue
15
+ out += value === true ? ` ${key}` : ` ${key}="${escape(value)}"`
16
+ }
17
+ return `${out}>`
18
+ }
19
+
20
+ export function createPicture(attributes: PictureAttributes): (props: PictureProps) => string {
21
+ return function Picture(props) {
22
+ const parts = attributes(props)
23
+ const { sources, img } = parts
24
+ if (props.preload) useHead({ link: [picturePreloadLink(parts)] })
25
+ const image = tag("img", applyPlaceholder(img, props, false))
26
+ return sources.length === 0
27
+ ? image
28
+ : `<picture>${sources.map((source) => tag("source", source)).join("")}${image}</picture>`
29
+ }
30
+ }
@@ -0,0 +1,9 @@
1
+ import { getImage, getSizes, imgAttributes, pictureAttributes } from "../runtime/index.ts"
2
+ import { createImg } from "./html-img.ts"
3
+ import { createPicture } from "./html-picture.ts"
4
+
5
+ export type { ImgProps, ImgState } from "./html-img.ts"
6
+ export type { PictureProps } from "./html-picture.ts"
7
+ export { getImage, getSizes }
8
+ export const Img = createImg(imgAttributes)
9
+ export const Picture = createPicture(pictureAttributes)
@@ -0,0 +1,51 @@
1
+ import { h, type ComponentChildren, type JSX } from "preact"
2
+ import { useHead } from "@hamur/adapter-preact"
3
+ import { useEffect, useMemo, useRef, useState } from "preact/hooks"
4
+ import {
5
+ applyPlaceholder,
6
+ createImgEvents,
7
+ imagePreloadLink,
8
+ type ImgAttributes,
9
+ type ImgOptions,
10
+ type ImgState as State,
11
+ } from "../runtime/img.ts"
12
+
13
+ export type ImgState = State
14
+
15
+ export type ImgProps = ImgOptions & {
16
+ fetchpriority?: "high" | "low" | "auto"
17
+ children?: ComponentChildren | ((state: ImgState) => ComponentChildren)
18
+ } & Omit<
19
+ JSX.IntrinsicElements["img"],
20
+ keyof ImgOptions | "srcset" | "srcSet" | "fetchpriority" | "fetchPriority" | "children"
21
+ >
22
+
23
+ export function createImg(attributes: ImgAttributes): (props: ImgProps) => ComponentChildren {
24
+ return function Img({ children, onLoad, onError, ...props }) {
25
+ const [isLoaded, setLoaded] = useState(false)
26
+ const handlers = useRef({ onLoad, onError })
27
+ handlers.current = { onLoad, onError }
28
+ const events = useMemo(
29
+ () =>
30
+ createImgEvents(
31
+ () => ({ load: handlers.current.onLoad as never, error: handlers.current.onError as never }),
32
+ setLoaded,
33
+ ),
34
+ [],
35
+ )
36
+ const image = useRef<HTMLImageElement | null>(null)
37
+ useEffect(() => {
38
+ if (image.current) events.replay(image.current)
39
+ }, [events])
40
+ const attrs = attributes(props)
41
+ if (props.preload) useHead({ link: [imagePreloadLink(attrs)] })
42
+ const imgAttrs = {
43
+ ...applyPlaceholder(attrs, props, isLoaded),
44
+ onLoad: events.load,
45
+ onError: events.error,
46
+ ref: image,
47
+ }
48
+ if (typeof children === "function") return children({ imgAttrs, src: String(attrs.src), isLoaded })
49
+ return h("img", imgAttrs)
50
+ }
51
+ }
@@ -0,0 +1,40 @@
1
+ import { h, type JSX, type VNode } from "preact"
2
+ import { useHead } from "@hamur/adapter-preact"
3
+ import { useEffect, useMemo, useRef, useState } from "preact/hooks"
4
+ import { applyPlaceholder, createImgEvents } from "../runtime/img.ts"
5
+ import { picturePreloadLink, type PictureAttributes, type PictureOptions } from "../runtime/picture.ts"
6
+
7
+ export type PictureProps = PictureOptions & {
8
+ fetchpriority?: "high" | "low" | "auto"
9
+ } & Omit<JSX.IntrinsicElements["img"], keyof PictureOptions | "srcset" | "srcSet" | "fetchpriority" | "fetchPriority">
10
+
11
+ export function createPicture(attributes: PictureAttributes): (props: PictureProps) => VNode {
12
+ return function Picture({ onLoad, onError, ...props }) {
13
+ const [isLoaded, setLoaded] = useState(false)
14
+ const handlers = useRef({ onLoad, onError })
15
+ handlers.current = { onLoad, onError }
16
+ const events = useMemo(
17
+ () =>
18
+ createImgEvents(
19
+ () => ({ load: handlers.current.onLoad as never, error: handlers.current.onError as never }),
20
+ setLoaded,
21
+ ),
22
+ [],
23
+ )
24
+ const node = useRef<HTMLImageElement | null>(null)
25
+ useEffect(() => {
26
+ if (node.current) events.replay(node.current)
27
+ }, [events])
28
+ const parts = attributes(props)
29
+ const { sources, img } = parts
30
+ if (props.preload) useHead({ link: [picturePreloadLink(parts)] })
31
+ const image = h("img", {
32
+ ...applyPlaceholder(img, props, isLoaded),
33
+ onLoad: events.load,
34
+ onError: events.error,
35
+ ref: node,
36
+ }) as VNode
37
+ if (sources.length === 0) return image
38
+ return h("picture", null, ...sources.map((source) => h("source", { key: source.type, ...source })), image)
39
+ }
40
+ }
@@ -0,0 +1,9 @@
1
+ import { getImage, getSizes, imgAttributes, pictureAttributes } from "../runtime/index.ts"
2
+ import { createImg } from "./preact-img.ts"
3
+ import { createPicture } from "./preact-picture.ts"
4
+
5
+ export type { ImgProps, ImgState } from "./preact-img.ts"
6
+ export type { PictureProps } from "./preact-picture.ts"
7
+ export { getImage, getSizes }
8
+ export const Img = createImg(imgAttributes)
9
+ export const Picture = createPicture(pictureAttributes)
@@ -0,0 +1,54 @@
1
+ import { createElement, useEffect, useMemo, useRef, useState, type ImgHTMLAttributes, type ReactNode } from "react"
2
+ import { useHead } from "@hamur/adapter-react"
3
+ import {
4
+ applyPlaceholder,
5
+ createImgEvents,
6
+ imagePreloadLink,
7
+ type ImgAttributes,
8
+ type ImgOptions,
9
+ type ImgState as State,
10
+ } from "../runtime/img.ts"
11
+
12
+ export type ImgState = State
13
+
14
+ export type ImgProps = ImgOptions & {
15
+ fetchPriority?: "high" | "low" | "auto"
16
+ children?: (state: ImgState) => ReactNode
17
+ } & Omit<ImgHTMLAttributes<HTMLImageElement>, keyof ImgOptions | "srcSet" | "fetchPriority" | "children">
18
+
19
+ const NAMES: Record<string, string> = { srcset: "srcSet", fetchpriority: "fetchPriority" }
20
+
21
+ export function createImg(attributes: ImgAttributes): (props: ImgProps) => ReactNode {
22
+ return function Img({ fetchPriority, children, onLoad, onError, ...props }) {
23
+ const [isLoaded, setLoaded] = useState(false)
24
+ const handlers = useRef({ onLoad, onError })
25
+ handlers.current = { onLoad, onError }
26
+ const events = useMemo(
27
+ () =>
28
+ createImgEvents(
29
+ () => ({ load: handlers.current.onLoad as never, error: handlers.current.onError as never }),
30
+ setLoaded,
31
+ ),
32
+ [],
33
+ )
34
+ const image = useRef<HTMLImageElement | null>(null)
35
+ useEffect(() => {
36
+ if (image.current) events.replay(image.current)
37
+ }, [events])
38
+ const attrs = attributes({ ...props, fetchpriority: fetchPriority })
39
+ if (props.preload) useHead({ link: [imagePreloadLink(attrs)] })
40
+ const imgAttrs = applyPlaceholder(
41
+ Object.fromEntries(Object.entries(attrs).map(([name, value]) => [NAMES[name] ?? name, value])),
42
+ props,
43
+ isLoaded,
44
+ { react: true },
45
+ )
46
+ imgAttrs.onLoad = events.load
47
+ imgAttrs.onError = events.error
48
+ imgAttrs.ref = image
49
+ // react-dom writes its own preload link for an eager <img> with a string src; a String object makes it skip, so the head manager holds the only one
50
+ if (props.preload) imgAttrs.src = new String(imgAttrs.src)
51
+ if (children) return children({ imgAttrs, src: String(attrs.src), isLoaded })
52
+ return createElement("img", imgAttrs as ImgHTMLAttributes<HTMLImageElement>)
53
+ }
54
+ }
@@ -0,0 +1,49 @@
1
+ import { createElement, useEffect, useMemo, useRef, useState, type ImgHTMLAttributes, type ReactElement } from "react"
2
+ import { useHead } from "@hamur/adapter-react"
3
+ import { applyPlaceholder, createImgEvents } from "../runtime/img.ts"
4
+ import { picturePreloadLink, type PictureAttributes, type PictureOptions } from "../runtime/picture.ts"
5
+
6
+ export type PictureProps = PictureOptions & {
7
+ fetchPriority?: "high" | "low" | "auto"
8
+ } & Omit<ImgHTMLAttributes<HTMLImageElement>, keyof PictureOptions | "srcSet" | "fetchPriority">
9
+
10
+ const NAMES: Record<string, string> = { srcset: "srcSet", fetchpriority: "fetchPriority" }
11
+
12
+ const named = (attributes: object): Record<string, unknown> =>
13
+ Object.fromEntries(Object.entries(attributes).map(([name, value]) => [NAMES[name] ?? name, value]))
14
+
15
+ export function createPicture(attributes: PictureAttributes): (props: PictureProps) => ReactElement {
16
+ return function Picture({ fetchPriority, onLoad, onError, ...props }) {
17
+ const [isLoaded, setLoaded] = useState(false)
18
+ const handlers = useRef({ onLoad, onError })
19
+ handlers.current = { onLoad, onError }
20
+ const events = useMemo(
21
+ () =>
22
+ createImgEvents(
23
+ () => ({ load: handlers.current.onLoad as never, error: handlers.current.onError as never }),
24
+ setLoaded,
25
+ ),
26
+ [],
27
+ )
28
+ const node = useRef<HTMLImageElement | null>(null)
29
+ useEffect(() => {
30
+ if (node.current) events.replay(node.current)
31
+ }, [events])
32
+ const parts = attributes({ ...props, fetchpriority: fetchPriority })
33
+ const { sources, img } = parts
34
+ if (props.preload) useHead({ link: [picturePreloadLink(parts)] })
35
+ const imgAttrs = applyPlaceholder(named(img), props, isLoaded, { react: true })
36
+ imgAttrs.onLoad = events.load
37
+ imgAttrs.onError = events.error
38
+ imgAttrs.ref = node
39
+ if (props.preload) imgAttrs.src = new String(img.src)
40
+ const image = createElement("img", imgAttrs as ImgHTMLAttributes<HTMLImageElement>)
41
+ if (sources.length === 0) return image
42
+ return createElement(
43
+ "picture",
44
+ null,
45
+ ...sources.map((source) => createElement("source", { key: source.type, ...named(source) })),
46
+ image,
47
+ )
48
+ }
49
+ }
@@ -0,0 +1,9 @@
1
+ import { getImage, getSizes, imgAttributes, pictureAttributes } from "../runtime/index.ts"
2
+ import { createImg } from "./react-img.ts"
3
+ import { createPicture } from "./react-picture.ts"
4
+
5
+ export type { ImgProps, ImgState } from "./react-img.ts"
6
+ export type { PictureProps } from "./react-picture.ts"
7
+ export { getImage, getSizes }
8
+ export const Img = createImg(imgAttributes)
9
+ export const Picture = createPicture(pictureAttributes)