|
| 1 | +# `@solidjs/image` |
| 2 | + |
| 3 | +## Install |
| 4 | + |
| 5 | +```bash |
| 6 | +npm i @solidjs/image |
| 7 | +``` |
| 8 | + |
| 9 | +```bash |
| 10 | +pnpm add @solidjs/image |
| 11 | +``` |
| 12 | + |
| 13 | +## Setup |
| 14 | + |
| 15 | +### Vite |
| 16 | + |
| 17 | +```ts |
| 18 | +import { imagePlugin } from "@solidjs/image/vite"; |
| 19 | + |
| 20 | +export default defineConfig({ |
| 21 | + plugins: [ |
| 22 | + imagePlugin({ |
| 23 | + /** |
| 24 | + * Used to process local image imports |
| 25 | + * |
| 26 | + * example: |
| 27 | + * import myImage from './path/to/my-image.jpg?image'; |
| 28 | + */ |
| 29 | + local: { |
| 30 | + /** |
| 31 | + * Image formats that can be processed |
| 32 | + */ |
| 33 | + input: ['jpeg', 'png'], |
| 34 | + /** |
| 35 | + * Image format for the output images. |
| 36 | + * |
| 37 | + * Take note that each input image will |
| 38 | + * produce a new image for each format |
| 39 | + */ |
| 40 | + output: ['jpeg', 'png'], |
| 41 | + /** |
| 42 | + * Sizes of the output images, based on width |
| 43 | + * while retaining the aspect ratio. |
| 44 | + * |
| 45 | + * This option also produces an image for |
| 46 | + * each width and for each output format. |
| 47 | + */ |
| 48 | + sizes: [480, 600], |
| 49 | + /** |
| 50 | + * Quality of the processed images |
| 51 | + */ |
| 52 | + quality: 80, |
| 53 | + /** |
| 54 | + * Where the processed images as emitted |
| 55 | + */ |
| 56 | + publicPath: "public", |
| 57 | + }, |
| 58 | + /** |
| 59 | + * Used for remote images |
| 60 | + * |
| 61 | + * example: |
| 62 | + * import myImage from 'image:my-value'; |
| 63 | + */ |
| 64 | + remote: { |
| 65 | + /** |
| 66 | + * Transforms the right-hand part of the `image:*` string |
| 67 | + */ |
| 68 | + transformURL(url) { |
| 69 | + return { |
| 70 | + /** |
| 71 | + * The default image for the given url |
| 72 | + */ |
| 73 | + src: { |
| 74 | + source: `https://picsum.photos/seed/${url}/1200/900.webp`, |
| 75 | + width: 1080, |
| 76 | + height: 760, |
| 77 | + }, |
| 78 | + /** |
| 79 | + * Variants of the image (format, size) for responsiveness |
| 80 | + */ |
| 81 | + variants: [ |
| 82 | + { |
| 83 | + path: `https://picsum.photos/seed/${url}/800/600.jpg`, |
| 84 | + width: 800, |
| 85 | + type: "image/jpeg", |
| 86 | + }, |
| 87 | + { |
| 88 | + path: `https://picsum.photos/seed/${url}/400/300.jpg`, |
| 89 | + width: 400, |
| 90 | + type: "image/jpeg", |
| 91 | + }, |
| 92 | + { |
| 93 | + path: `https://picsum.photos/seed/${url}/800/600.png`, |
| 94 | + width: 800, |
| 95 | + type: "image/png", |
| 96 | + }, |
| 97 | + { |
| 98 | + path: `https://picsum.photos/seed/${url}/400/300.png`, |
| 99 | + width: 400, |
| 100 | + type: "image/png", |
| 101 | + }, |
| 102 | + ], |
| 103 | + }; |
| 104 | + }, |
| 105 | + }, |
| 106 | + }), |
| 107 | + ], |
| 108 | +}); |
| 109 | +``` |
| 110 | + |
| 111 | +## Usage |
| 112 | + |
| 113 | +### Local image |
| 114 | + |
| 115 | +```tsx |
| 116 | +import { SolidImage as Image } from "@solidjs/image"; |
| 117 | +import { type JSX, onMount, Show } from "solid-js"; |
| 118 | + |
| 119 | +import exampleImage from "../images/example.jpg?image"; |
| 120 | + |
| 121 | +interface PlaceholderProps { |
| 122 | + show: () => void; |
| 123 | +} |
| 124 | + |
| 125 | +function Placeholder(props: PlaceholderProps): JSX.Element { |
| 126 | + onMount(() => { |
| 127 | + props.show(); |
| 128 | + }); |
| 129 | + |
| 130 | + return <div>Loading...</div>; |
| 131 | +} |
| 132 | + |
| 133 | +export default function App(): JSX.Element { |
| 134 | + return ( |
| 135 | + <div style={{ width: "50vw" }}> |
| 136 | + <Image |
| 137 | + {...exampleImage} |
| 138 | + alt="example" |
| 139 | + fallback={(visible, show) => ( |
| 140 | + <Show when={visible()}> |
| 141 | + <Placeholder show={show} /> |
| 142 | + </Show> |
| 143 | + )} |
| 144 | + /> |
| 145 | + </div> |
| 146 | + ); |
| 147 | +} |
| 148 | +``` |
| 149 | + |
| 150 | +### Remote image |
| 151 | + |
| 152 | +```tsx |
| 153 | +import { SolidImage as Image } from "@solidjs/image"; |
| 154 | +import { type JSX, onMount, Show } from "solid-js"; |
| 155 | + |
| 156 | +import exampleImage from "image:foobar"; |
| 157 | + |
| 158 | +interface PlaceholderProps { |
| 159 | + show: () => void; |
| 160 | +} |
| 161 | + |
| 162 | +function Placeholder(props: PlaceholderProps): JSX.Element { |
| 163 | + onMount(() => { |
| 164 | + props.show(); |
| 165 | + }); |
| 166 | + |
| 167 | + return <div>Loading...</div>; |
| 168 | +} |
| 169 | + |
| 170 | +export default function App(): JSX.Element { |
| 171 | + return ( |
| 172 | + <div style={{ width: "50vw" }}> |
| 173 | + <Image |
| 174 | + {...exampleImage} |
| 175 | + alt="example" |
| 176 | + fallback={(visible, show) => ( |
| 177 | + <Show when={visible()}> |
| 178 | + <Placeholder show={show} /> |
| 179 | + </Show> |
| 180 | + )} |
| 181 | + /> |
| 182 | + </div> |
| 183 | + ); |
| 184 | +} |
| 185 | +``` |
0 commit comments