@picked/svelte 0.0.1-dev.1765841386439 → 0.0.1-dev.1765907257168
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.
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import type { HTMLImgAttributes } from 'svelte/elements'
|
|
3
|
+
import { onMount } from 'svelte'
|
|
4
|
+
|
|
5
|
+
type CommonProps = Omit<HTMLImgAttributes, 'src' | 'loading' | 'width' | 'height'> & {
|
|
6
|
+
src: string
|
|
7
|
+
loading?: 'eager' | 'lazy'
|
|
8
|
+
alt: string
|
|
9
|
+
sizes?: string
|
|
10
|
+
objectFit?: string
|
|
11
|
+
loader?: (params: { src: string; width: number }) => string | undefined
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
type FillProps = {
|
|
15
|
+
fill: true
|
|
16
|
+
width?: never
|
|
17
|
+
height?: never
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
type FixedSizeProps = {
|
|
21
|
+
fill?: false
|
|
22
|
+
width: number
|
|
23
|
+
height: number
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
type Props = (CommonProps & FillProps) | (CommonProps & FixedSizeProps)
|
|
27
|
+
|
|
28
|
+
const {
|
|
29
|
+
src,
|
|
30
|
+
width,
|
|
31
|
+
height,
|
|
32
|
+
loading = 'lazy',
|
|
33
|
+
alt,
|
|
34
|
+
sizes = '100vw',
|
|
35
|
+
objectFit = 'cover',
|
|
36
|
+
fill = false,
|
|
37
|
+
loader,
|
|
38
|
+
...restProps
|
|
39
|
+
}: Props = $props()
|
|
40
|
+
|
|
41
|
+
let imgElement = $state<HTMLImageElement | undefined>(undefined)
|
|
42
|
+
let placeholderElement = $state<HTMLDivElement | undefined>(undefined)
|
|
43
|
+
let isLoaded = $state(false)
|
|
44
|
+
let isInView = $state(false)
|
|
45
|
+
let observer: IntersectionObserver | undefined
|
|
46
|
+
|
|
47
|
+
const aspectRatio = $derived.by(() =>
|
|
48
|
+
!fill && typeof width === 'number' && typeof height === 'number' && height !== 0
|
|
49
|
+
? width / height
|
|
50
|
+
: undefined,
|
|
51
|
+
)
|
|
52
|
+
|
|
53
|
+
const SUPPORTED_SIZES = [
|
|
54
|
+
16, 32, 48, 64, 96, 128, 256, 384, 640, 750, 828, 1080, 1200, 1920, 2048, 3840,
|
|
55
|
+
]
|
|
56
|
+
|
|
57
|
+
function findClosestSize(targetSize: number): number {
|
|
58
|
+
const roundedTargetSize = Math.round(targetSize)
|
|
59
|
+
return SUPPORTED_SIZES.reduce(
|
|
60
|
+
(prev, curr) =>
|
|
61
|
+
Math.abs(curr - roundedTargetSize) < Math.abs(prev - roundedTargetSize) ? curr : prev,
|
|
62
|
+
SUPPORTED_SIZES[0],
|
|
63
|
+
)
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function getOptimizedImageUrl(imageSrc: string, targetWidth: number): string {
|
|
67
|
+
const width = findClosestSize(targetWidth)
|
|
68
|
+
const imgSrc = loader ? loader({ src: imageSrc, width }) : undefined
|
|
69
|
+
return imgSrc || getImgUrl(imageSrc)
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function getImgUrl(imageSrc: string): string {
|
|
73
|
+
if (imageSrc.startsWith('data:')) {
|
|
74
|
+
return imageSrc
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const baseUrl = new URL(import.meta.url).origin
|
|
78
|
+
return imageSrc.startsWith('http') ? imageSrc : `${baseUrl}${imageSrc}`
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function handleLoad() {
|
|
82
|
+
isLoaded = true
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
onMount(() => {
|
|
86
|
+
if (loading === 'lazy') {
|
|
87
|
+
const elementToObserve = placeholderElement || imgElement
|
|
88
|
+
if (elementToObserve) {
|
|
89
|
+
observer = new IntersectionObserver(
|
|
90
|
+
(entries) => {
|
|
91
|
+
entries.forEach((entry) => {
|
|
92
|
+
if (entry.isIntersecting) {
|
|
93
|
+
isInView = true
|
|
94
|
+
observer?.unobserve(entry.target)
|
|
95
|
+
}
|
|
96
|
+
})
|
|
97
|
+
},
|
|
98
|
+
{
|
|
99
|
+
rootMargin: '50px',
|
|
100
|
+
},
|
|
101
|
+
)
|
|
102
|
+
observer.observe(elementToObserve)
|
|
103
|
+
|
|
104
|
+
return () => {
|
|
105
|
+
observer?.disconnect()
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
} else if (loading === 'eager') {
|
|
109
|
+
isInView = true
|
|
110
|
+
}
|
|
111
|
+
})
|
|
112
|
+
|
|
113
|
+
const shouldLoadImage = $derived(loading === 'eager' || isInView)
|
|
114
|
+
|
|
115
|
+
const widthCandidates = $derived.by(() => {
|
|
116
|
+
if (typeof width === 'number' && Number.isFinite(width)) {
|
|
117
|
+
const filtered = SUPPORTED_SIZES.filter((w) => w <= width)
|
|
118
|
+
return filtered.length ? filtered : [width]
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
return SUPPORTED_SIZES
|
|
122
|
+
})
|
|
123
|
+
|
|
124
|
+
const srcSet = $derived.by(() =>
|
|
125
|
+
widthCandidates.map((w) => `${getOptimizedImageUrl(src, w)} ${w}w`).join(', '),
|
|
126
|
+
)
|
|
127
|
+
|
|
128
|
+
const fallbackSrc = $derived.by(() => getOptimizedImageUrl(src, widthCandidates[0]))
|
|
129
|
+
|
|
130
|
+
const fadeClasses = $derived.by(() => {
|
|
131
|
+
if (loading === 'eager') return ''
|
|
132
|
+
return `transition-opacity duration-300 ${isLoaded ? 'opacity-100' : 'opacity-0'}`
|
|
133
|
+
})
|
|
134
|
+
</script>
|
|
135
|
+
|
|
136
|
+
{#if shouldLoadImage}
|
|
137
|
+
<img
|
|
138
|
+
{...restProps}
|
|
139
|
+
bind:this={imgElement}
|
|
140
|
+
src={fallbackSrc}
|
|
141
|
+
srcset={srcSet}
|
|
142
|
+
{sizes}
|
|
143
|
+
width={fill ? undefined : width}
|
|
144
|
+
height={fill ? undefined : height}
|
|
145
|
+
{alt}
|
|
146
|
+
style:aspect-ratio={aspectRatio}
|
|
147
|
+
style:object-fit={objectFit}
|
|
148
|
+
style:position={fill ? 'absolute' : undefined}
|
|
149
|
+
style:inset={fill ? 0 : undefined}
|
|
150
|
+
{loading}
|
|
151
|
+
onload={handleLoad}
|
|
152
|
+
class="{fadeClasses} {restProps.class || ''}"
|
|
153
|
+
data-testid="lazy-image-element"
|
|
154
|
+
/>
|
|
155
|
+
{:else}
|
|
156
|
+
<div
|
|
157
|
+
bind:this={placeholderElement}
|
|
158
|
+
style:aspect-ratio={aspectRatio}
|
|
159
|
+
style:position={fill ? 'absolute' : undefined}
|
|
160
|
+
style:inset={fill ? 0 : undefined}
|
|
161
|
+
aria-label={alt}
|
|
162
|
+
data-testid="lazy-image-placeholder"
|
|
163
|
+
></div>
|
|
164
|
+
{/if}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { default as LazyImage } from './LazyImage.svelte';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{default as f}from"./LazyImage.svelte";export{f as LazyImage};
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{adaptSvelteComponent as
|
|
1
|
+
import{adaptSvelteComponent as o,isValidAdaptedComponent as t}from"./component.js";import{default as d}from"./components/LazyImage.svelte";export{d as LazyImage,o as adaptSvelteComponent,t as isValidAdaptedComponent};
|
package/dist/render/browser.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{mount as
|
|
1
|
+
import{mount as a}from"svelte";import{isValidAdaptedComponent as d}from"../component.js";import{CLIENT_CONTEXT_KEY as f,EXPERIENCE_CONTEXT_KEY as C}from"../context.js";import{mergeArrays as u}from"../utils/array.js";function E(r,e){return new Map([[f,r],[C,e]])}const g=async(r,e,n,o,i)=>{var m,p;if(!d(e))throw new Error("The component is invalid.");const c=u((m=e.style)==null?void 0:m.sheets,i.stylesheets),l=u((p=e.style)==null?void 0:p.css,i.css);if(c.length&&c.forEach(s=>{const t=document.createElement("link");t.setAttribute("rel","stylesheet"),t.setAttribute("href",s),t.setAttribute("type","text/css"),t.setAttribute("picked-injected","true"),r.appendChild(t)}),l.length&&l.forEach(s=>{const t=document.createElement("style");t.textContent=s,t.setAttribute("type","text/css"),t.setAttribute("picked-injected","true"),r.appendChild(t)}),e.component instanceof Promise){const{default:s}=await e.component;a(s,{target:r,props:{client:n,experience:o},context:E(n,o)});return}a(e.component,{target:r,props:{client:n,experience:o},context:E(n,o)})},T=r=>{};export{E as createContext,g as renderSvelteComponent,T as unRenderSvelteComponent};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@picked/svelte",
|
|
3
|
-
"version": "0.0.1-dev.
|
|
3
|
+
"version": "0.0.1-dev.1765907257168",
|
|
4
4
|
"dependencies": {
|
|
5
5
|
"d3": "^7.9.0"
|
|
6
6
|
},
|
|
@@ -75,7 +75,7 @@
|
|
|
75
75
|
],
|
|
76
76
|
"module": "./dist/index.js",
|
|
77
77
|
"peerDependencies": {
|
|
78
|
-
"@picked/client": "0.0.1-dev.
|
|
78
|
+
"@picked/client": "0.0.1-dev.1765907257168",
|
|
79
79
|
"effector": "^23.3.0",
|
|
80
80
|
"svelte": "^5.34.3"
|
|
81
81
|
},
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
<script lang="ts">
|
|
2
|
-
import { getPicked } from '../hooks'
|
|
3
|
-
import type { ComponentWithProps } from 'src/component'
|
|
4
|
-
|
|
5
|
-
interface Props {
|
|
6
|
-
component: ComponentWithProps
|
|
7
|
-
componentProps?: Record<string, unknown>
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
const { component: InnerComponent, componentProps }: Props = $props()
|
|
11
|
-
|
|
12
|
-
function handleError(error: unknown) {
|
|
13
|
-
const client = getPicked()
|
|
14
|
-
|
|
15
|
-
client.eventBus({
|
|
16
|
-
type: 'exception',
|
|
17
|
-
level: 'error',
|
|
18
|
-
data: error,
|
|
19
|
-
})
|
|
20
|
-
}
|
|
21
|
-
</script>
|
|
22
|
-
|
|
23
|
-
<svelte:boundary onerror={handleError}><InnerComponent {...componentProps} /></svelte:boundary>
|