Skip to content

Commit

Permalink
feat: add @unlazy/solid package
Browse files Browse the repository at this point in the history
  • Loading branch information
johannschopplich committed Apr 20, 2023
1 parent 0e65a85 commit 750723f
Show file tree
Hide file tree
Showing 17 changed files with 374 additions and 15 deletions.
18 changes: 7 additions & 11 deletions packages/react/playground/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,13 @@ import { LazyImage } from '../src'

function App() {
return (
<>
<div>
<LazyImage
blurhash="LKO2:N%2Tw=w]~RBVZRi};RPxuwH"
autoSizes={true}
data-srcset="image-320w.jpg 320w, image-640w.jpg 640w"
width="640"
height="640"
/>
</div>
</>
<LazyImage
blurhash="LKO2:N%2Tw=w]~RBVZRi};RPxuwH"
autoSizes={true}
data-srcset="image-320w.jpg 320w, image-640w.jpg 640w"
width="640"
height="640"
/>
)
}

Expand Down
2 changes: 1 addition & 1 deletion packages/react/playground/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<title>@unlazy/react</title>
</head>
<body>
<div id="root"></div>
<div id="app"></div>
<script type="module" src="/main.tsx"></script>
</body>
</html>
4 changes: 3 additions & 1 deletion packages/react/playground/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import React from 'react'
import ReactDOM from 'react-dom/client'
import App from './App.tsx'

ReactDOM.createRoot(document.getElementById('root') as HTMLElement).render(
const app = document.getElementById('app')!

ReactDOM.createRoot(app).render(
<React.StrictMode>
<App />
</React.StrictMode>,
Expand Down
1 change: 0 additions & 1 deletion packages/react/playground/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{
"type": "module",
"version": "0.4.4",
"private": true,
"scripts": {
"dev": "vite",
Expand Down
15 changes: 15 additions & 0 deletions packages/solid/build.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { defineBuildConfig } from 'unbuild'

export default defineBuildConfig({
entries: ['src/index'],
clean: true,
declaration: true,
externals: ['react', 'react-dom'],
rollup: {
emitCJS: true,
inlineDependencies: true,
resolve: {
extensions: ['.ts', '.tsx'],
},
},
})
62 changes: 62 additions & 0 deletions packages/solid/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
{
"name": "@unlazy/solid",
"version": "0.4.4",
"packageManager": "[email protected]",
"description": "Solid lazy loading library for placeholder images",
"author": "Johann Schopplich <[email protected]>",
"license": "MIT",
"homepage": "https://unlazy.byjohann.dev",
"repository": {
"type": "git",
"url": "git+https://github.com/johannschopplich/unlazy.git",
"directory": "packages/solid"
},
"bugs": {
"url": "https://github.com/johannschopplich/unlazy/issues"
},
"keywords": [
"async",
"image",
"intersection",
"lazy",
"loading",
"lozad",
"native",
"observer",
"responsive",
"react"
],
"sideEffects": false,
"exports": {
".": {
"types": "./dist/index.d.ts",
"require": "./dist/index.cjs",
"import": "./dist/index.mjs"
}
},
"main": "./dist/index.cjs",
"module": "./dist/index.mjs",
"types": "./dist/index.d.ts",
"files": [
"dist"
],
"scripts": {
"build": "unbuild",
"dev": "pnpm -C playground run dev",
"dev:build": "pnpm -C playground run build",
"test:types": "tsc --noEmit"
},
"peerDependencies": {
"fast-blurhash": "^1.1.2",
"solid-js": "^1.7.3"
},
"dependencies": {
"unlazy": "workspace:*"
},
"devDependencies": {
"@types/node": "^18.15.12",
"fast-blurhash": "^1.1.2",
"solid-js": "^1.7.3",
"vite-plugin-solid": "^2.7.0"
}
}
16 changes: 16 additions & 0 deletions packages/solid/playground/App.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/* @refresh granular */
import { LazyImage } from '../src'

function App() {
return (
<LazyImage
blurhash="LKO2:N%2Tw=w]~RBVZRi};RPxuwH"
autoSizes={true}
data-srcset="image-320w.jpg 320w, image-640w.jpg 640w"
width="640"
height="640"
/>
)
}

export default App
12 changes: 12 additions & 0 deletions packages/solid/playground/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>@unlazy/solid</title>
</head>
<body>
<div id="app"></div>
<script type="module" src="/main.tsx"></script>
</body>
</html>
7 changes: 7 additions & 0 deletions packages/solid/playground/main.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { render } from 'solid-js/web'
import App from './App'

const app = document.getElementById('app')

if (app)
render(() => <App />, app)
9 changes: 9 additions & 0 deletions packages/solid/playground/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"type": "module",
"private": true,
"scripts": {
"dev": "vite",
"build": "vite build",
"preview": "vite preview"
}
}
1 change: 1 addition & 0 deletions packages/solid/playground/vite-env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/// <reference types="vite/client" />
6 changes: 6 additions & 0 deletions packages/solid/playground/vite.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { defineConfig } from 'vite'
import solid from 'vite-plugin-solid'

export default defineConfig({
plugins: [solid()],
})
38 changes: 38 additions & 0 deletions packages/solid/src/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { createEffect, createSignal, onCleanup } from 'solid-js'
import { lazyLoad } from 'unlazy'
import type { UnLazyLoadOptions } from 'unlazy'
import type { JSX } from 'solid-js'

interface Props
extends JSX.ImgHTMLAttributes<HTMLImageElement>,
Pick<UnLazyLoadOptions, 'blurhashSize'> {
autoSizes?: boolean
blurhash?: string
}

export function LazyImage(props: Props) {
const [target, setTarget] = createSignal<HTMLImageElement | null>(null)

createEffect(() => {
const currentTarget = target()
if (currentTarget) {
const cleanup = lazyLoad(currentTarget, {
blurhash: props.blurhash,
blurhashSize: props.blurhashSize,
})

onCleanup(() => {
cleanup()
})
}
})

return (
<img
ref={setTarget}
data-sizes={props.autoSizes ? 'auto' : undefined}
loading="lazy"
{...props}
/>
)
}
11 changes: 11 additions & 0 deletions packages/solid/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
/* Limit global types to those in the current package */
"typeRoots": ["./node_modules/@types"],
"jsx": "preserve",
"jsxImportSource": "solid-js"
},
"include": ["src", "playground"],
"exclude": ["**/dist"]
}
3 changes: 2 additions & 1 deletion packages/unlazy/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"homepage": "https://unlazy.byjohann.dev",
"repository": {
"type": "git",
"url": "git+https://github.com/johannschopplich/unlazy.git"
"url": "git+https://github.com/johannschopplich/unlazy.git",
"directory": "packages/unlazy"
},
"bugs": {
"url": "https://github.com/johannschopplich/unlazy/issues"
Expand Down
Loading

0 comments on commit 750723f

Please sign in to comment.