-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor Solid component to use LqipHashProvider abstraction
- Loading branch information
1 parent
0429d13
commit b18707e
Showing
4 changed files
with
77 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}")`; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}")`; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export interface LqipHashProvider { | ||
available: boolean; | ||
imageUrl: string | undefined; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters