Skip to content

Commit

Permalink
Refactor Solid component to use LqipHashProvider abstraction
Browse files Browse the repository at this point in the history
  • Loading branch information
simonihmig committed Jan 2, 2025
1 parent 0429d13 commit b18707e
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 55 deletions.
26 changes: 26 additions & 0 deletions packages/solid/src/lqip-hash/blurhash.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import type { LqipBlurhash } from '@responsive-image/core';
import type { LqipHashProvider } from './types';
import { createResource } from 'solid-js';

export class LqipBlurhashProvider implements LqipHashProvider {
private script;
constructor(private lqip: LqipBlurhash) {
// Allow lazy loading of dependency to make it pay as you go
const [script] = createResource(
() => import('@responsive-image/core/blurhash/decode'),
);

this.script = script;
}

get available() {
return !this.script.loading && !this.script.error;
}

get imageUrl() {
const { hash, width, height } = this.lqip;
const uri = this.script()?.decode2url(hash, width, height);

return `url("${uri}")`;
}
}
26 changes: 26 additions & 0 deletions packages/solid/src/lqip-hash/thumbhash.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import type { LqipThumbhash } from '@responsive-image/core';
import type { LqipHashProvider } from './types';
import { createResource } from 'solid-js';

export class LqipThumbhashProvider implements LqipHashProvider {
private script;
constructor(private lqip: LqipThumbhash) {
// Allow lazy loading of dependency to make it pay as you go
const [script] = createResource(
() => import('@responsive-image/core/thumbhash/decode'),
);

this.script = script;
}

get available() {
return !this.script.loading && !this.script.error;
}

get imageUrl() {
const { hash } = this.lqip;
const uri = this.script()?.decode2url(hash);

return `url("${uri}")`;
}
}
4 changes: 4 additions & 0 deletions packages/solid/src/lqip-hash/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export interface LqipHashProvider {
available: boolean;
imageUrl: string | undefined;
}
76 changes: 21 additions & 55 deletions packages/solid/src/responsive-image.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,12 @@ import {
isLqipBlurhash,
isLqipThumbhash,
} from '@responsive-image/core';
import {
Component,
createResource,
createSignal,
type JSX,
splitProps,
} from 'solid-js';
import { Component, createSignal, type JSX, splitProps } from 'solid-js';
import { isServer } from 'solid-js/web';

import './responsive-image.css';
import { LqipBlurhashProvider } from './lqip-hash/blurhash.ts';
import { LqipThumbhashProvider } from './lqip-hash/thumbhash.ts';

export interface ResponsiveImageArgs {
src: ImageData;
Expand Down Expand Up @@ -163,61 +159,31 @@ export const ResponsiveImage: Component<ResponsiveImageProps> = (props) => {
const blurhashMeta = isLqipBlurhash(args.src.lqip)
? args.src.lqip
: undefined;
let blurhashStyles: (() => JSX.CSSProperties | undefined) | undefined =
undefined;

if (!isServer && blurhashMeta) {
const [blurhashLib] = createResource(() => {
return import('@responsive-image/core/blurhash/decode');
});

blurhashStyles = () => {
if (isLoaded()) {
return undefined;
}

const { hash, width, height } = blurhashMeta;
const uri = blurhashLib()?.decode2url(hash, width, height);

if (!uri) {
return undefined;
}

return {
'background-image': `url("${uri}")`,
'background-size': 'cover',
};
};
}

const thumbhashMeta = isLqipThumbhash(args.src.lqip)
? args.src.lqip
: undefined;
let thumbhashStyles: (() => JSX.CSSProperties | undefined) | undefined =
undefined;

if (!isServer && thumbhashMeta) {
const [blurhashLib] = createResource(() => {
return import('@responsive-image/core/thumbhash/decode');
});

thumbhashStyles = () => {
if (isLoaded()) {
return undefined;
}
let hashStyles: (() => JSX.CSSProperties | undefined) | undefined = undefined;

const { hash } = thumbhashMeta;
const uri = blurhashLib()?.decode2url(hash);
if (!isServer) {
const hashProvider = blurhashMeta
? new LqipBlurhashProvider(blurhashMeta)
: thumbhashMeta
? new LqipThumbhashProvider(thumbhashMeta)
: undefined;

if (!uri) {
return undefined;
}
if (hashProvider) {
hashStyles = () => {
if (isLoaded() || !hashProvider.available) {
return undefined;
}

return {
'background-image': `url("${uri}")`,
'background-size': 'cover',
return {
'background-image': `${hashProvider.imageUrl}`,
'background-size': 'cover',
};
};
};
}
}

return (
Expand All @@ -237,7 +203,7 @@ export const ResponsiveImage: Component<ResponsiveImageProps> = (props) => {
data-ri-bh-w={blurhashMeta?.width}
data-ri-bh-h={blurhashMeta?.height}
data-ri-th={thumbhashMeta?.hash}
style={blurhashStyles?.() ?? thumbhashStyles?.()}
style={hashStyles?.()}
on:load={() => setLoaded(true)}
/>
</picture>
Expand Down

0 comments on commit b18707e

Please sign in to comment.