Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix getElementsByTagName error #36

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 15 additions & 16 deletions src/PhotoSwipeGallery.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,18 @@ class PhotoSwipeGallery extends React.Component {
}
};

thumbnails = []
state = {
isOpen: this.props.isOpen,
options: this.props.options
options: this.props.options || this.getThumbBoundsFn

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if this.props.options is undefined, state.options will be a function instead of an object?

};

getThumbBoundsFn = (index) => {
const thumbnail = this.thumbnails[index];
const img = thumbnail.getElementsByTagName('img')[0];
const pageYScroll = window.pageYOffset || document.documentElement.scrollTop;
const rect = img.getBoundingClientRect();
return { x: rect.left, y: rect.top + pageYScroll, w: rect.width };
};

componentWillReceiveProps = (nextProps) => {
Expand All @@ -44,22 +53,13 @@ class PhotoSwipeGallery extends React.Component {
}
};

showPhotoSwipe = itemIndex => (e) => {
showPhotoSwipe = index => (e) => {
console.log('click photo')

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove console usage

e.preventDefault();
const getThumbBoundsFn = ((index) => {
const thumbnail = this.thumbnails[index];
const img = thumbnail.getElementsByTagName('img')[0];
const pageYScroll = window.pageYOffset || document.documentElement.scrollTop;
const rect = img.getBoundingClientRect();
return { x: rect.left, y: rect.top + pageYScroll, w: rect.width };
});
const { options } = this.state;
options.index = itemIndex;
options.getThumbBoundsFn = options.getThumbBoundsFn || getThumbBoundsFn;
this.setState({
this.setState(prevState => ({
isOpen: true,
options
});
options: { ...prevState.options, index }
}));
};

handleClose = () => {
Expand All @@ -82,7 +82,6 @@ class PhotoSwipeGallery extends React.Component {
<div
key={index}
ref={(node) => {
this.thumbnails = this.thumbnails || [];
this.thumbnails[index] = node;
}}
className="pswp-thumbnail"
Expand Down