@seed-design/react-image 0.1.0 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -127,7 +127,12 @@ const ImageContent = /*#__PURE__*/ react.forwardRef((props, ref)=>{
|
|
|
127
127
|
});
|
|
128
128
|
return /*#__PURE__*/ jsxRuntime.jsx(reactPrimitive.Primitive.img, {
|
|
129
129
|
ref: reactComposeRefs.composeRefs(refs.image, ref),
|
|
130
|
-
...domUtils.mergeProps(contentProps, otherProps
|
|
130
|
+
...domUtils.mergeProps(contentProps, otherProps, {
|
|
131
|
+
// if loading is lazy, we should not hide the image even if it's not loaded yet,
|
|
132
|
+
// because the browser should be able to check if it's in the viewport.
|
|
133
|
+
// TODO: it should be better than this; why doesn't useImage properly handle this case?
|
|
134
|
+
hidden: otherProps.loading === "lazy" ? false : contentProps.hidden
|
|
135
|
+
}),
|
|
131
136
|
onLoad: (e)=>{
|
|
132
137
|
handleLoad();
|
|
133
138
|
onLoad?.(e);
|
|
@@ -127,7 +127,12 @@ const ImageContent = /*#__PURE__*/ forwardRef((props, ref)=>{
|
|
|
127
127
|
});
|
|
128
128
|
return /*#__PURE__*/ jsx(Primitive.img, {
|
|
129
129
|
ref: composeRefs(refs.image, ref),
|
|
130
|
-
...mergeProps(contentProps, otherProps
|
|
130
|
+
...mergeProps(contentProps, otherProps, {
|
|
131
|
+
// if loading is lazy, we should not hide the image even if it's not loaded yet,
|
|
132
|
+
// because the browser should be able to check if it's in the viewport.
|
|
133
|
+
// TODO: it should be better than this; why doesn't useImage properly handle this case?
|
|
134
|
+
hidden: otherProps.loading === "lazy" ? false : contentProps.hidden
|
|
135
|
+
}),
|
|
131
136
|
onLoad: (e)=>{
|
|
132
137
|
handleLoad();
|
|
133
138
|
onLoad?.(e);
|
package/lib/index.cjs
CHANGED
package/lib/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { I as ImageContent, a as ImageFallback, b as ImageRoot } from './Image-12s-
|
|
2
|
-
export { c as useImage, u as useImageContext } from './Image-12s-
|
|
1
|
+
import { I as ImageContent, a as ImageFallback, b as ImageRoot } from './Image-12s-GoG3c-IV.js';
|
|
2
|
+
export { c as useImage, u as useImageContext } from './Image-12s-GoG3c-IV.js';
|
|
3
3
|
|
|
4
4
|
var Image_namespace = {
|
|
5
5
|
__proto__: null,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@seed-design/react-image",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "1.0.0",
|
|
4
4
|
"repository": {
|
|
5
5
|
"type": "git",
|
|
6
6
|
"url": "git+https://github.com/daangn/seed-design.git",
|
|
@@ -30,8 +30,8 @@
|
|
|
30
30
|
"@radix-ui/react-compose-refs": "^1.1.2",
|
|
31
31
|
"@radix-ui/react-use-callback-ref": "^1.1.1",
|
|
32
32
|
"@radix-ui/react-use-layout-effect": "^1.1.1",
|
|
33
|
-
"@seed-design/dom-utils": "
|
|
34
|
-
"@seed-design/react-primitive": "
|
|
33
|
+
"@seed-design/dom-utils": "^2.0.0",
|
|
34
|
+
"@seed-design/react-primitive": "^2.0.0"
|
|
35
35
|
},
|
|
36
36
|
"devDependencies": {
|
|
37
37
|
"@types/react": "^19.1.6",
|
package/src/Image.tsx
CHANGED
|
@@ -4,7 +4,7 @@ import { composeRefs } from "@radix-ui/react-compose-refs";
|
|
|
4
4
|
import { useLayoutEffect } from "@radix-ui/react-use-layout-effect";
|
|
5
5
|
import { mergeProps } from "@seed-design/dom-utils";
|
|
6
6
|
import { Primitive, type PrimitiveProps } from "@seed-design/react-primitive";
|
|
7
|
-
import * as React from "react";
|
|
7
|
+
import type * as React from "react";
|
|
8
8
|
import { forwardRef } from "react";
|
|
9
9
|
import { useImage, type UseImageProps } from "./useImage";
|
|
10
10
|
import { ImageProvider, useImageContext } from "./useImageContext";
|
|
@@ -43,7 +43,12 @@ export const ImageContent = forwardRef<HTMLImageElement, ImageContentProps>((pro
|
|
|
43
43
|
return (
|
|
44
44
|
<Primitive.img
|
|
45
45
|
ref={composeRefs(refs.image, ref)}
|
|
46
|
-
{...mergeProps(contentProps, otherProps
|
|
46
|
+
{...mergeProps(contentProps, otherProps, {
|
|
47
|
+
// if loading is lazy, we should not hide the image even if it's not loaded yet,
|
|
48
|
+
// because the browser should be able to check if it's in the viewport.
|
|
49
|
+
// TODO: it should be better than this; why doesn't useImage properly handle this case?
|
|
50
|
+
hidden: otherProps.loading === "lazy" ? false : contentProps.hidden,
|
|
51
|
+
})}
|
|
47
52
|
onLoad={(e) => {
|
|
48
53
|
handleLoad();
|
|
49
54
|
onLoad?.(e);
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { render } from "@testing-library/react";
|
|
2
|
+
import userEvent from "@testing-library/user-event";
|
|
3
|
+
import { describe, expect, it } from "bun:test";
|
|
4
|
+
|
|
5
|
+
import type { ReactElement } from "react";
|
|
6
|
+
|
|
7
|
+
import { useImage, type UseImageProps } from "./useImage";
|
|
8
|
+
|
|
9
|
+
const ROOT_TEST_ID = "image-root";
|
|
10
|
+
const FALLBACK_TEXT = "AB";
|
|
11
|
+
const IMAGE_ALT_TEXT = "Fake Image";
|
|
12
|
+
|
|
13
|
+
function setUp(jsx: ReactElement) {
|
|
14
|
+
return {
|
|
15
|
+
user: userEvent.setup(),
|
|
16
|
+
...render(jsx),
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function Image(props: UseImageProps) {
|
|
21
|
+
const { rootProps, getContentProps, fallbackProps } = useImage(props);
|
|
22
|
+
return (
|
|
23
|
+
<div data-testid={ROOT_TEST_ID} {...rootProps}>
|
|
24
|
+
<img {...getContentProps({})} alt={IMAGE_ALT_TEXT} />
|
|
25
|
+
<span {...fallbackProps}>{FALLBACK_TEXT}</span>
|
|
26
|
+
</div>
|
|
27
|
+
);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
describe("useImage", () => {
|
|
31
|
+
it("initial state is loading", () => {
|
|
32
|
+
const { getByAltText } = setUp(<Image />);
|
|
33
|
+
const image = getByAltText(IMAGE_ALT_TEXT);
|
|
34
|
+
expect(image).toHaveAttribute("data-loading-state", "loading");
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it("should not render the image initially", () => {
|
|
38
|
+
const { getByAltText } = setUp(<Image />);
|
|
39
|
+
const image = getByAltText(IMAGE_ALT_TEXT);
|
|
40
|
+
expect(image).not.toBeVisible();
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it("should render the fallback initially", () => {
|
|
44
|
+
const { queryByText } = setUp(<Image />);
|
|
45
|
+
const fallback = queryByText(FALLBACK_TEXT);
|
|
46
|
+
expect(fallback).toBeVisible();
|
|
47
|
+
});
|
|
48
|
+
});
|