Skip to content

Commit ebdc3b8

Browse files
committed
docs
1 parent c4addb5 commit ebdc3b8

File tree

4 files changed

+110
-5
lines changed

4 files changed

+110
-5
lines changed

CHANGELOG.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
# 0.1.0
1+
# 0.1.2
22

33
- Initial release

README.md

+105
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,108 @@ Filejet SDK
77
```
88
yarn add @filejet/sdk
99
```
10+
11+
## Utils
12+
13+
**Generate `<img / >` attributes:**
14+
15+
To generate the correct `src` and `srcSet` attributes for the `<img />` tag, you can use the `filejetImg` function.
16+
17+
It will return urls with the correct mutations to fit your requirements.
18+
19+
```ts
20+
import { filejetImg } from '@filejet/sdk/utils';
21+
22+
const attributes = filejetImg({
23+
src: 'https://myapp.com/image.jpg',
24+
width: 128,
25+
height: 128,
26+
dpiScale: [1, 1.5, 2],
27+
fit: 'cover',
28+
backgroundColor: 'transparent',
29+
filejetDomain: 'https://cdn.myapp.com',
30+
});
31+
32+
// {
33+
// src: '...',
34+
// srcSet: '...',
35+
// width: 128,
36+
// height: 128
37+
// }
38+
}
39+
console.log(attributes);
40+
```
41+
42+
## React
43+
44+
To use the Filejet integration, you need to initialize the Filejet and
45+
provide it trough the `<FilejetProvider />`.
46+
47+
```tsx
48+
import { Filejet, FilejetProvider, LruCache } from '@filejet/sdk/react';
49+
50+
const filejet = new Filejet({
51+
domain: 'cdn.app.com',
52+
Img: {
53+
dpiScale: [1, 1.5, 2],
54+
placeholderNode: <div style={{ background: '#eeeded', width: '100%', height: '100%' }}></div>,
55+
errorNode: (
56+
<div
57+
style={{
58+
width: '100%',
59+
height: '100%',
60+
display: 'flex',
61+
justifyContent: 'center',
62+
alignItems: 'center',
63+
flexDirection: 'column',
64+
backgroundColor: `rgba(255,255,255, 0.4)`
65+
}}
66+
>
67+
<WarningIcon color="primary" />
68+
Failed to load
69+
</div>
70+
)
71+
},
72+
ThumbhashImg: {
73+
cache: new LruCache({ maxSize: 512 }),
74+
intersectRootMargin: 100
75+
}
76+
});
77+
78+
ReactDOM.render(
79+
<FilejetProvider filejet={filejet}>
80+
<App />
81+
</FilejetProvider>,
82+
document.getElementById('root')
83+
);
84+
```
85+
86+
### Img component
87+
88+
`<Img>` component will render the image in the most optimized way.
89+
90+
`src` property can be either a filejet ID or HTTPs URL to external file.
91+
92+
```tsx
93+
import { Img } from '@filejet/sdk/react';
94+
95+
<Img
96+
src="KRhBC0tycdeENyP1PQkgBA"
97+
thumbhash="HBkSHYSIeHiPiHh8eJd4eTN0EEQG"
98+
height={168}
99+
fit="cover"
100+
alt="Photo"
101+
/>;
102+
```
103+
104+
**Recommended usage:**
105+
106+
It is recommended to alway use `thumbhash` in a combination with `height` to ensure dimensions are known before the image is loaded to prevent layout shift.
107+
108+
Thumbhash encodes both the blurred placeholder and its ratio, so client can avoid layout shifts even when the ratio is not known as it can be calculated from the thumbhash.
109+
110+
**Rendering process:**
111+
Image starts with the single-color placeholder calculated from the average color of thumbhash. Then, when the image is in the viewport, the original image is fetched (scaled based on DPI). If the image is in browser's cache, it is rendered immediately. If not, the blurred placeholder is rendered until the image is loaded.
112+
113+
**Rendering priority:**
114+
`priority` prop can be used to prioritize the image fetching, decoding and blurred placeholder rendering. Recommended value is `auto` (default). Using `low` will lead into longer time between the single-color placeholder and the real image.

package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
{
22
"name": "@filejet/sdk",
3-
"version": "0.1.0",
3+
"version": "0.1.2",
44
"description": "Filejet SDK.",
55
"license": "MIT",
66
"homepage": "https://github.com/filejet/filejet-sdk-js",
77
"exports": {
8-
"react": {
8+
"./react": {
99
"types": "./dist/esm/react.d.ts",
1010
"import": "./dist/esm/react.js",
1111
"require": "./dist/cjs/react.js"
1212
},
13-
"utils": {
13+
"./utils": {
1414
"types": "./dist/esm/utils.d.ts",
1515
"import": "./dist/esm/utils.js",
1616
"require": "./dist/cjs/utils.js"

src/filejetImg.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export interface FilejetImgProps {
4545
*
4646
* Image is ALWAYS auto-resized to the specified width and height by default.
4747
*/
48-
readonly mutation: string | undefined;
48+
readonly mutation?: string;
4949

5050
/**
5151
* Filejet domain.

0 commit comments

Comments
 (0)