386 |
![]()
checkImagesLoaded()}
388 | alt={rightImageAlt}
389 | data-testid="right-image"
390 | ref={rightImageRef}
391 | src={rightImage}
392 | style={styles.rightImage}
393 | />
394 |
![]()
checkImagesLoaded()}
396 | alt={leftImageAlt}
397 | data-testid="left-image"
398 | ref={leftImageRef}
399 | src={leftImage}
400 | style={styles.leftImage}
401 | />
402 |
403 |
404 | {handle ? (
405 |
{handle}
406 | ) : (
407 |
411 | )}
412 |
413 |
414 | {/* labels */}
415 | {leftImageLabel && (
416 |
417 |
{leftImageLabel}
418 |
419 | )}
420 | {rightImageLabel && (
421 |
422 |
{rightImageLabel}
423 |
424 | )}
425 |
426 | >
427 | )
428 | }
429 |
430 | export default ReactCompareImage
431 |
--------------------------------------------------------------------------------
/src/global.d.ts:
--------------------------------------------------------------------------------
1 | // Storyでpngファイルをimportするところでエラーが出るので
2 | declare module '*.png' {
3 | const src: string
4 | export default src
5 | }
6 |
--------------------------------------------------------------------------------
/src/hooks/useContainerWidth.ts:
--------------------------------------------------------------------------------
1 | import { type RefObject, useLayoutEffect, useRef } from 'react'
2 | import invariant from 'tiny-invariant'
3 |
4 | type ResizeCallback = (width: number) => void
5 |
6 | /**
7 | * Triggers a callback when an element's width changes.
8 | *
9 | * @param onResize Called when the element's width changes
10 | * @returns ref: A ref to be attached to the element
11 | */
12 | export default function useContainerWidth(
13 | onResize: ResizeCallback,
14 | ): RefObject