-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #60 from js-tool-pack/image
Image
- Loading branch information
Showing
35 changed files
with
930 additions
and
8 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
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
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,139 @@ | ||
import React, { useContext, useEffect, useState, useRef } from 'react'; | ||
import { ImagePreviewGroupContext } from '~/image/components'; | ||
import { useForceUpdate, getClasses } from '@pkg/shared'; | ||
import type { RequiredPart } from '@tool-pack/types'; | ||
import { getClassNames } from '@tool-pack/basic'; | ||
import type { ImageProps } from './image.types'; | ||
import { ImagePreview } from '@pkg/components'; | ||
|
||
const cls = getClasses('image', ['img', 'fallback'], ['preview']); | ||
const defaultProps = { | ||
fallback: | ||
'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAMIAAADDCAYAAADQvc6UAAABRWlDQ1BJQ0MgUHJvZmlsZQAAKJFjYGASSSwoyGFhYGDIzSspCnJ3UoiIjFJgf8LAwSDCIMogwMCcmFxc4BgQ4ANUwgCjUcG3awyMIPqyLsis7PPOq3QdDFcvjV3jOD1boQVTPQrgSkktTgbSf4A4LbmgqISBgTEFyFYuLykAsTuAbJEioKOA7DkgdjqEvQHEToKwj4DVhAQ5A9k3gGyB5IxEoBmML4BsnSQk8XQkNtReEOBxcfXxUQg1Mjc0dyHgXNJBSWpFCYh2zi+oLMpMzyhRcASGUqqCZ16yno6CkYGRAQMDKMwhqj/fAIcloxgHQqxAjIHBEugw5sUIsSQpBobtQPdLciLEVJYzMPBHMDBsayhILEqEO4DxG0txmrERhM29nYGBddr//5/DGRjYNRkY/l7////39v///y4Dmn+LgeHANwDrkl1AuO+pmgAAADhlWElmTU0AKgAAAAgAAYdpAAQAAAABAAAAGgAAAAAAAqACAAQAAAABAAAAwqADAAQAAAABAAAAwwAAAAD9b/HnAAAHlklEQVR4Ae3dP3PTWBSGcbGzM6GCKqlIBRV0dHRJFarQ0eUT8LH4BnRU0NHR0UEFVdIlFRV7TzRksomPY8uykTk/zewQfKw/9znv4yvJynLv4uLiV2dBoDiBf4qP3/ARuCRABEFAoBEgghggQAQZQKAnYEaQBAQaASKIAQJEkAEEegJmBElAoBEgghggQAQZQKAnYEaQBAQaASKIAQJEkAEEegJmBElAoBEgghggQAQZQKAnYEaQBAQaASKIAQJEkAEEegJmBElAoBEgghggQAQZQKAnYEaQBAQaASKIAQJEkAEEegJmBElAoBEgghggQAQZQKAnYEaQBAQaASKIAQJEkAEEegJmBElAoBEgghggQAQZQKAnYEaQBAQaASKIAQJEkAEEegJmBElAoBEgghggQAQZQKAnYEaQBAQaASKIAQJEkAEEegJmBElAoBEgghggQAQZQKAnYEaQBAQaASKIAQJEkAEEegJmBElAoBEgghggQAQZQKAnYEaQBAQaASKIAQJEkAEEegJmBElAoBEgghggQAQZQKAnYEaQBAQaASKIAQJEkAEEegJmBElAoBEgghggQAQZQKAnYEaQBAQaASKIAQJEkAEEegJmBElAoBEgghggQAQZQKAnYEaQBAQaASKIAQJEkAEEegJmBElAoBEgghgg0Aj8i0JO4OzsrPv69Wv+hi2qPHr0qNvf39+iI97soRIh4f3z58/u7du3SXX7Xt7Z2enevHmzfQe+oSN2apSAPj09TSrb+XKI/f379+08+A0cNRE2ANkupk+ACNPvkSPcAAEibACyXUyfABGm3yNHuAECRNgAZLuYPgEirKlHu7u7XdyytGwHAd8jjNyng4OD7vnz51dbPT8/7z58+NB9+/bt6jU/TI+AGWHEnrx48eJ/EsSmHzx40L18+fLyzxF3ZVMjEyDCi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WejUogAEQo121BzAkTI2agUIkCEQs021JwAEXI2KoUIEKFQsw01J0CEnI1KIQJEKNRsQ80JECFno1KIABEKNdtQcwJEyNmoFCJAhELNNtScABFyNiqFCBChULMNNSdAhJyNSiECRCjUbEPNCRAhZ6NSiAARCjXbUHMCRMjZqBQiQIRCzTbUnAARcjYqhQgQoVCzDTUnQIScjUohAkQo1GxDzQkQIWejUogAEQo121BzAkTI2agUIkCEQs021JwAEXI2KoUIEKFQsw01J0CEnI1KIQJEKNRsQ80JECFno1KIABEKNdtQcwJEyNmoFCJAhELNNtScABFyNiqFCBChULMNNSdAhJyNSiECRCjUbEPNCRAhZ6NSiAARCjXbUHMCRMjZqBQiQIRCzTbUnAARcjYqhQgQoVCzDTUnQIScjUohAkQo1GxDzQkQIWejUogAEQo121BzAkTI2agUIkCEQs021JwAEXI2KoUIEKFQsw01J0CEnI1KIQJEKNRsQ80JECFno1KIABEKNdtQcwJEyNmoFCJAhELNNtScABFyNiqFCBChULMNNSdAhJyNSiEC/wGgKKC4YMA4TAAAAABJRU5ErkJggg==', | ||
preview: true, | ||
} satisfies Partial<ImageProps>; | ||
|
||
export const Image: React.FC<ImageProps> = React.forwardRef< | ||
HTMLDivElement, | ||
ImageProps | ||
>((props, ref) => { | ||
const { | ||
imgAttrs = {}, | ||
attrs = {}, | ||
fallback, | ||
preview, | ||
height, | ||
width, | ||
lazy, | ||
src, | ||
fit, | ||
alt, | ||
} = props as RequiredPart<ImageProps, keyof typeof defaultProps>; | ||
|
||
const context = useContext(ImagePreviewGroupContext); | ||
|
||
useEffect(() => { | ||
if (!context || !preview) return; | ||
const url = src || imgAttrs.src || ''; | ||
if (context.includes(url)) return; | ||
context.push(url); | ||
return () => { | ||
const index = context.indexOf(url); | ||
if (index === -1) return; | ||
context.splice(index, 1); | ||
}; | ||
}, [preview]); | ||
|
||
const [imgVisible, setImgVisible] = useState(true); | ||
const [previewVisible, setPreviewVisible] = useState(false); | ||
const imgRef = useRef<HTMLImageElement>(null); | ||
const forceUpdate = useForceUpdate(); | ||
const lazyingRef = useRef<boolean | void>(undefined); | ||
|
||
if (lazyingRef.current === undefined && lazy) { | ||
lazyingRef.current = true; | ||
} | ||
|
||
useEffect(() => { | ||
const el = imgRef.current; | ||
if (!lazy) return; | ||
if (!el || !lazyingRef.current) return; | ||
|
||
const handler = (entries: IntersectionObserverEntry[]) => { | ||
if (entries[0]!.intersectionRatio <= 0) return; | ||
lazyingRef.current = false; | ||
forceUpdate(); | ||
}; | ||
const observer = new IntersectionObserver(handler); | ||
observer.observe(el); | ||
return () => observer.disconnect(); | ||
}, [lazy]); | ||
|
||
const Img = ( | ||
// eslint-disable-next-line jsx-a11y/click-events-have-key-events,jsx-a11y/no-noninteractive-element-interactions | ||
<img | ||
onClick={(e) => { | ||
if (lazyingRef.current) return; | ||
imgAttrs.onClick?.(e); | ||
setPreviewVisible(true); | ||
}} | ||
style={{ | ||
...imgAttrs.style, | ||
objectFit: fit || imgAttrs.style?.objectFit, | ||
}} | ||
src={lazyingRef.current ? fallback : src || imgAttrs.src} | ||
height={height || imgAttrs.height} | ||
width={width || imgAttrs.width} | ||
alt={alt || imgAttrs.alt} | ||
className={cls.__.img} | ||
onError={hideImg} | ||
onAbort={hideImg} | ||
ref={imgRef} | ||
key={1} | ||
/> | ||
); | ||
|
||
const Fallback = ( | ||
<img | ||
style={{ | ||
...imgAttrs.style, | ||
objectFit: fit || imgAttrs.style?.objectFit, | ||
}} | ||
height={height || imgAttrs.height} | ||
width={width || imgAttrs.width} | ||
className={cls.__.fallback} | ||
alt={alt || imgAttrs.alt} | ||
src={fallback} | ||
/> | ||
); | ||
|
||
return ( | ||
<div | ||
{...attrs} | ||
className={getClassNames(cls.root, attrs.className, { | ||
[cls['--'].preview]: preview, | ||
})} | ||
ref={ref} | ||
> | ||
{imgVisible | ||
? [ | ||
Img, | ||
preview && previewVisible && ( | ||
<ImagePreview | ||
onHide={() => setPreviewVisible(false)} | ||
images={[src]} | ||
key={2} | ||
/> | ||
), | ||
] | ||
: Fallback} | ||
</div> | ||
); | ||
|
||
function hideImg(): void { | ||
setImgVisible(false); | ||
} | ||
}); | ||
|
||
Image.defaultProps = defaultProps; | ||
Image.displayName = 'Image'; |
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,43 @@ | ||
import { fireEvent, render } from '@testing-library/react'; | ||
import { ImagePreviewGroup, Image } from '~/image'; | ||
import { testAttrs } from '~/testAttrs'; | ||
|
||
describe('Image', () => { | ||
testAttrs(Image); | ||
|
||
test('basic', () => { | ||
render(<Image />); | ||
expect(document.body).toMatchSnapshot(); | ||
}); | ||
|
||
describe('preview', () => { | ||
test('disabled', () => { | ||
render(<Image preview={false} />); | ||
expect(document.body).toMatchSnapshot(); | ||
}); | ||
|
||
test('enabled', () => { | ||
render(<Image />); | ||
expect(document.body).toMatchSnapshot(); | ||
}); | ||
}); | ||
|
||
test('group', () => { | ||
render( | ||
<ImagePreviewGroup> | ||
<Image src="https://test.com/a.png" /> | ||
<Image src="https://test.com/b.png" /> | ||
<Image src="https://test.com/c.png" /> | ||
</ImagePreviewGroup>, | ||
); | ||
|
||
fireEvent.click(document.querySelectorAll('.t-image__img')![1]!); | ||
|
||
expect( | ||
document.querySelector<HTMLImageElement>('.t-image-preview img')!.src, | ||
).toBe('https://test.com/b.png'); | ||
expect( | ||
document.querySelector('.t-image-preview__progress'), | ||
).toHaveTextContent('2 / 3'); | ||
}); | ||
}); |
43 changes: 43 additions & 0 deletions
43
packages/components/src/image/__tests__/__snapshots__/Image.test.tsx.snap
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,43 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`Image basic 1`] = ` | ||
<body> | ||
<div> | ||
<div | ||
class="t-image t-image--preview" | ||
> | ||
<img | ||
class="t-image__img" | ||
/> | ||
</div> | ||
</div> | ||
</body> | ||
`; | ||
|
||
exports[`Image preview disabled 1`] = ` | ||
<body> | ||
<div> | ||
<div | ||
class="t-image" | ||
> | ||
<img | ||
class="t-image__img" | ||
/> | ||
</div> | ||
</div> | ||
</body> | ||
`; | ||
|
||
exports[`Image preview enabled 1`] = ` | ||
<body> | ||
<div> | ||
<div | ||
class="t-image t-image--preview" | ||
> | ||
<img | ||
class="t-image__img" | ||
/> | ||
</div> | ||
</div> | ||
</body> | ||
`; |
14 changes: 14 additions & 0 deletions
14
packages/components/src/image/components/Preview.Group.tsx
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,14 @@ | ||
import React, { createContext } from 'react'; | ||
|
||
export const ImagePreviewGroupContext = createContext<string[] | null>(null); | ||
export const ImagePreviewGroup: React.FC<{ children?: React.ReactNode }> = ( | ||
props, | ||
) => { | ||
return ( | ||
<ImagePreviewGroupContext.Provider value={[]}> | ||
{props.children} | ||
</ImagePreviewGroupContext.Provider> | ||
); | ||
}; | ||
|
||
ImagePreviewGroup.displayName = 'ImagePreviewGroup'; |
67 changes: 67 additions & 0 deletions
67
packages/components/src/image/components/Preview.Toolbar.tsx
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,67 @@ | ||
import { | ||
RotateRight, | ||
RotateLeft, | ||
Download, | ||
ZoomOut, | ||
ZoomIn, | ||
Right, | ||
Close, | ||
Reset, | ||
FlipV, | ||
FlipH, | ||
Left, | ||
} from '@pkg/icons'; | ||
import { getClasses } from '@pkg/shared'; | ||
import { Icon } from '~/icon'; | ||
import React from 'react'; | ||
|
||
export const ImagePreviewToolbarActions = [ | ||
'prev', | ||
'next', | ||
'rotate-left', | ||
'rotate-right', | ||
'zoom-out', | ||
'zoom-in', | ||
'reset', | ||
'flip-horizontal', | ||
'flip-vertically', | ||
'download', | ||
'close', | ||
] as const; | ||
|
||
const Actions = ImagePreviewToolbarActions; | ||
|
||
const IconMap: Record<(typeof Actions)[number], React.ReactNode> = { | ||
'rotate-right': <RotateRight />, | ||
'rotate-left': <RotateLeft />, | ||
'flip-horizontal': <FlipH />, | ||
'flip-vertically': <FlipV />, | ||
'zoom-out': <ZoomOut />, | ||
download: <Download />, | ||
'zoom-in': <ZoomIn />, | ||
close: <Close />, | ||
reset: <Reset />, | ||
next: <Right />, | ||
prev: <Left />, | ||
}; | ||
|
||
const cls = getClasses('image-preview-toolbar', Actions, ['visible']); | ||
|
||
interface Props { | ||
onTrigger(action: (typeof Actions)[number]): void; | ||
} | ||
|
||
export const ImagePreviewToolbar: React.FC<Props> = (props) => { | ||
return ( | ||
<div className={cls.root}> | ||
{Actions.map((a) => { | ||
return ( | ||
// eslint-disable-next-line jsx-a11y/click-events-have-key-events,jsx-a11y/no-static-element-interactions | ||
<div onClick={() => props.onTrigger(a)} className={cls.__[a]} key={a}> | ||
<Icon size={22}>{IconMap[a]}</Icon> | ||
</div> | ||
); | ||
})} | ||
</div> | ||
); | ||
}; |
Oops, something went wrong.