Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Jan 19, 2023. It is now read-only.

React lightbox component

License

NotificationsYou must be signed in to change notification settings

frontend-collective/react-image-lightbox

Repository files navigation

NOTE: This package is not maintained any more and will be archived.

react-photoswipe-gallery is a better-maintained and more feature-rich alternative (as of January 2023).

React Image Lightbox

NPMBuild StatusCoverage Status

RIL Snapshot

A flexible lightbox component for displaying images in a React project.

DEMO

Features

  • Keyboard shortcuts (with rate limiting)
  • Image Zoom
  • Flexible rendering using src values assigned on the fly
  • Image preloading for smoother viewing
  • Mobile friendly, with pinch to zoom and swipe (Thanks,@webcarrot!)

Example

importReact,{Component}from'react';importLightboxfrom'react-image-lightbox';import'react-image-lightbox/style.css';// This only needs to be imported once in your appconstimages=['//placekitten.com/1500/500','//placekitten.com/4000/3000','//placekitten.com/800/1200','//placekitten.com/1500/1500',];exportdefaultclassLightboxExampleextendsComponent{constructor(props){super(props);this.state={photoIndex:0,isOpen:false,};}render(){const{ photoIndex, isOpen}=this.state;return(<div><buttontype="button"onClick={()=>this.setState({isOpen:true})}>          Open Lightbox</button>{isOpen&&(<LightboxmainSrc={images[photoIndex]}nextSrc={images[(photoIndex+1)%images.length]}prevSrc={images[(photoIndex+images.length-1)%images.length]}onCloseRequest={()=>this.setState({isOpen:false})}onMovePrevRequest={()=>this.setState({photoIndex:(photoIndex+images.length-1)%images.length,})}onMoveNextRequest={()=>this.setState({photoIndex:(photoIndex+1)%images.length,})}/>)}</div>);}}

Play with the code on theexample on CodeSandbox

Options

PropertyTypeDescription
mainSrc
(required)
stringMain display image url
prevSrcstringPrevious display image url (displayed to the left). If left undefined,onMovePrevRequest will not be called, and the button not displayed
nextSrcstringNext display image url (displayed to the right). If left undefined,onMoveNextRequest will not be called, and the button not displayed
mainSrcThumbnailstringThumbnail image url corresponding toprops.mainSrc. Displayed as a placeholder while the full-sized image loads.
prevSrcThumbnailstringThumbnail image url corresponding toprops.prevSrc. Displayed as a placeholder while the full-sized image loads.
nextSrcThumbnailstringThumbnail image url corresponding toprops.nextSrc. Displayed as a placeholder while the full-sized image loads.
onCloseRequest
(required)
funcClose window event. Should change the parent state such that the lightbox is not rendered
onMovePrevRequestfuncMove to previous image event. Should change the parent state such thatprops.prevSrc becomesprops.mainSrc,props.mainSrc becomesprops.nextSrc, etc.
onMoveNextRequestfuncMove to next image event. Should change the parent state such thatprops.nextSrc becomesprops.mainSrc,props.mainSrc becomesprops.prevSrc, etc.
onImageLoadfuncCalled when an image loads.
(imageSrc: string, srcType: string, image: object): void
onImageLoadErrorfuncCalled when an image fails to load.
(imageSrc: string, srcType: string, errorEvent: object): void
imageLoadErrorMessagenodeWhat is rendered in place of an image if it fails to load. Centered in the lightbox viewport. Defaults to the string"This image failed to load".
onAfterOpenfuncCalled after the modal has rendered.
discourageDownloadsboolWhentrue, enables download discouragement (preventing [right-click -> Save Image As...]). Defaults tofalse.
animationDisabledboolWhentrue, image sliding animations are disabled. Defaults tofalse.
animationOnKeyInputboolWhentrue, sliding animations are enabled on actions performed with keyboard shortcuts. Defaults tofalse.
animationDurationnumberAnimation duration (ms). Defaults to300.
keyRepeatLimitnumberRequired interval of time (ms) between key actions (prevents excessively fast navigation of images). Defaults to180.
keyRepeatKeyupBonusnumberAmount of time (ms) restored after each keyup (makes rapid key presses slightly faster than holding down the key to navigate images). Defaults to40.
imageTitlenodeImage title (Descriptive element above image)
imageCaptionnodeImage caption (Descriptive element below image)
imageCrossOriginstringcrossorigin attribute to append toimg elements (MDN documentation)
toolbarButtonsnode[]Array of custom toolbar buttons
reactModalStyleObjectSetz-index style, etc., for the parent react-modal (react-modal style format)
reactModalPropsObjectOverride props set on react-modal (https://github.com/reactjs/react-modal)
imagePaddingnumberPadding (px) between the edge of the window and the lightbox. Defaults to10.
clickOutsideToCloseboolWhentrue, clicks outside of the image close the lightbox. Defaults totrue.
enableZoomboolSet tofalse to disable zoom functionality and hide zoom buttons. Defaults totrue.
wrapperClassNamestringClass name which will be applied to root element after React Modal
nextLabelstringaria-label andtitle set on the 'Next' button. Defaults to'Next image'.
prevLabelstringaria-label andtitle set on the 'Previous' button. Defaults to'Previous image'.
zoomInLabelstringaria-label andtitle set on the 'Zoom In' button. Defaults to'Zoom in'.
zoomOutLabelstringaria-label andtitle set on the 'Zoom Out' button. Defaults to'Zoom out'.
closeLabelstringaria-label andtitle set on the 'Close Lightbox' button. Defaults to'Close lightbox'.
loadernodeCustom Loading indicator for loading

Browser Compatibility

BrowserWorks?
ChromeYes
FirefoxYes
SafariYes
IE 11Yes

Contributing

After cloning the repository and runningnpm install inside, you can use the following commands to develop and build the project.

# Starts a webpack dev server that hosts a demo page with the lightbox.# It uses react-hot-loader so changes are reflected on save.npm start# Lints the code with eslint and my custom rules.yarn run lint# Lints and builds the code, placing the result in the dist directory.# This build is necessary to reflect changes if you're#  `npm link`-ed to this repository from another local project.yarn run build

Pull requests are welcome!

License

MIT


[8]ページ先頭

©2009-2025 Movatter.jp