Skip to content

Commit

Permalink
Specify Enlarged Image Container Dimensions
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanselzer committed Feb 20, 2018
1 parent 89c4696 commit 9bcda75
Show file tree
Hide file tree
Showing 19 changed files with 423 additions and 94 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ Features tinted guide lens, interaction hint, hover intent, long-press gesture,

[Integration with react-slick carousel](https://ethanselzer.github.io/react-image-magnify/#/react-slick)

[Specify Enlarged Image Container Dimensions](https://ethanselzer.github.io/react-image-magnify/#/dimensions)

Experiment with react-image-magnify [live on CodePen](https://codepen.io/ethanselzer/full/oePMNY/).
Use the Change View button to select editing mode or for different layout options.
Use the Fork button to save your changes.
Expand Down Expand Up @@ -91,7 +93,8 @@ If you would like more information on responsive images, please try these resour
| `hoverDelayInMs` | Number | No | 250 | Milliseconds to delay hover trigger. |
| `hoverOffDelayInMs` | Number | No | 150 | Milliseconds to delay hover-off trigger. |
| `lensStyle` | Object | No | | Style applied to tinted lens. |
| `enlargedImagePosition` | String | No | beside | Enlarged image position. Can be 'beside' or 'over'. |
| `enlargedImagePosition` | String | No | beside | Enlarged image position. Can be 'beside' or 'over'. |
| `enlargedImageContainerDimensions` | Object | No | {width: '100%', height: '100%'} | Specify enlarged image container dimensions as an object with `width` and `height` properties. Values may be expressed as a percentage (e.g. '150%') or a number (e.g. 200). Percentage is based on small image dimension. Number is pixels. Not applied when `enlargedImagePosition` is set to 'over'. |

### Touch Specific
| Prop | Type | Required | Default | Description |
Expand Down
2 changes: 2 additions & 0 deletions example/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ If your default browser does not start automatically, open a new browser window

[Integration with react-slick](http://localhost:3000/#/react-slick).

[Specify Enlarged Image Container Dimensions](http://localhost:3000/#/dimensions).

## Reference
If you would like more information on responsive images, please try these resources:
[https://cloudfour.com/thinks/responsive-images-101-definitions/](https://cloudfour.com/thinks/responsive-images-101-definitions/)
Expand Down
89 changes: 89 additions & 0 deletions example/src/pages/EnlargedImageContainerDimensions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import React, { Component } from 'react';
import ReactImageMagnify from '../pkg-lnk/ReactImageMagnify';
import SpacedSpan from '../components/SpacedSpan';

import './app.css';

import watchImg355 from '../images/wristwatch_355.jpg';
import watchImg481 from '../images/wristwatch_481.jpg';
import watchImg584 from '../images/wristwatch_584.jpg';
import watchImg687 from '../images/wristwatch_687.jpg';
import watchImg770 from '../images/wristwatch_770.jpg';
import watchImg861 from '../images/wristwatch_861.jpg';
import watchImg955 from '../images/wristwatch_955.jpg';
import watchImg1033 from '../images/wristwatch_1033.jpg';
import watchImg1112 from '../images/wristwatch_1112.jpg';
import watchImg1192 from '../images/wristwatch_1192.jpg';
import watchImg1200 from '../images/wristwatch_1200.jpg';

export default class EnlargedImageContainerDimensions extends Component {
get srcSet() {
return [
`${watchImg355} 355w`,
`${watchImg481} 481w`,
`${watchImg584} 584w`,
`${watchImg687} 687w`,
`${watchImg770} 770w`,
`${watchImg861} 861w`,
`${watchImg955} 955w`,
`${watchImg1033} 1033w`,
`${watchImg1112} 1112w`,
`${watchImg1192} 1192w`,
`${watchImg1200} 1200w`,
].join(', ');
}

render() {
return (
<div className="fluid">
<div className="fluid__image-container">
<ReactImageMagnify {...{
largeImage: {
alt: '',
src: watchImg1200,
width: 1200,
height: 1800
},
smallImage: {
isFluidWidth: true,
alt: 'Wristwatch by Ted Baker London',
src: watchImg1200,
srcSet: this.srcSet,
sizes: '(max-width: 480px) 100vw, (max-width: 1200px) 30vw, 360px'
},
isHintEnabled: true,
enlargedImageContainerDimensions: {
width: '200%',
height: '100%'
}
}} />
</div>
<div className="fluid__instructions">
<h3>Enlarged Image Container Dimensions Example</h3>
<p>
Specify dimensions as percentage of small image or number of pixels.
</p>
<p>
May be percentage for one dimension and number for the other.
</p>
<p>
Exmample specifies width of
<SpacedSpan className="code">200%</SpacedSpan>
and height of
<SpacedSpan className="code">100%.</SpacedSpan>
</p>
<p>
Please see
<SpacedSpan>
<a href="https://github.com/ethanselzer/react-image-magnify/blob/master/example/src/pages/EnlargedImageContainerDimensions.js">
source code
</a>
</SpacedSpan>
for details.
</p>
</div>
<div style={{height: '500px'}} />
</div>
);
}
}
38 changes: 38 additions & 0 deletions example/src/pages/FixedWidthSmallImage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import React, { Component } from 'react';
import ReactImageMagnify from '../pkg-lnk/ReactImageMagnify';

import './app.css';

import watchImg from '../images/wristwatch_1200.jpg';

export default class extends Component {
render() {
return (
<div className="fluid">
<div className="fluid__image-container">
<ReactImageMagnify {...{
largeImage: {
alt: '',
src: watchImg,
width: 1200,
height: 1800
},
smallImage: {
alt: 'Wristwatch by Ted Baker London',
src: watchImg,
width: 300,
height: 450
},
isHintEnabled: true
}} />
</div>
<div className="fixed__instructions">
<h3>Fixed Width Small Image Example</h3>
<p>Specify small image width and height as numbers</p>
<p>Small image is not fluid width.</p>
</div>
<div style={{height: '1000px'}} />
</div>
);
}
}
18 changes: 9 additions & 9 deletions example/src/pages/ReactSlick.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import back_1020 from '../images/versace-blue/back-1020.jpg';
import back_1200 from '../images/versace-blue/back-1200.jpg';
import back_1426 from '../images/versace-blue/back-1426.jpg';

import './App.css';
import './app.css';
import './react-slick.css';

const frontSrcSet = [
Expand Down Expand Up @@ -87,25 +87,25 @@ export default class ReactSlickExample extends Component {
))}
</ReactSlick>
</div>
<div id="hello" className="fluid__instructions">
<div className="fluid__instructions">
<h3>Carousel Example</h3>
<p className="App-intro">
<p>
Integration with&nbsp;
<a href="https://www.npmjs.com/package/react-slick">
react-slick
</a>
.
</p>
<p className="App-intro">
<p>
In-place enlargement for mouse and touch input.
</p>
<p className="App-intro">
<p>
Side-by-side enlargement not yet compatible with react-slick.
</p>
<p className="App-intro">
<p>
Responsive and fluid between breakpoints.
</p>
<p className="App-intro">
<p>
Initial file size optimized via
<SpacedSpan className="code">
srcSet
Expand All @@ -116,10 +116,10 @@ export default class ReactSlickExample extends Component {
</SpacedSpan>
attributes.
</p>
<p className="App-intro">
<p>
Please see
<SpacedSpan>
<a href="https://github.com/ethanselzer/react-image-magnify/tree/master/example">
<a href="https://github.com/ethanselzer/react-image-magnify/blob/master/example/src/pages/ReactSlick.js">
source code
</a>
</SpacedSpan>
Expand Down
14 changes: 7 additions & 7 deletions example/src/pages/SideBySide.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React, { Component } from 'react';
import ReactImageMagnify from '../pkg-lnk/ReactImageMagnify';
import SpacedSpan from '../components/SpacedSpan';

import './App.css';
import './app.css';

import watchImg355 from '../images/wristwatch_355.jpg';
import watchImg481 from '../images/wristwatch_481.jpg';
Expand Down Expand Up @@ -56,16 +56,16 @@ export default class BasicExample extends Component {
</div>
<div className="fluid__instructions">
<h3>Basic Example</h3>
<p className="App-intro">
<p>
Side by Side enlargement for mouse input.
</p>
<p className="App-intro">
<p>
In place enlargement for touch input.
</p>
<p className="App-intro">
<p>
Fluid between breakpoints.
</p>
<p className="App-intro">
<p>
Initial file size optimized via
<SpacedSpan className="code">
srcSet
Expand All @@ -76,10 +76,10 @@ export default class BasicExample extends Component {
</SpacedSpan>
attributes.
</p>
<p className="App-intro">
<p>
Please see
<SpacedSpan>
<a href="https://github.com/ethanselzer/react-image-magnify/tree/master/example">
<a href="https://github.com/ethanselzer/react-image-magnify/blob/master/example/src/pages/SideBySide.js">
source code
</a>
</SpacedSpan>
Expand Down
10 changes: 10 additions & 0 deletions example/src/pages/App.css → example/src/pages/app.css
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ body {
margin: 0 20px;
}

.fixed__instructions {
flex: 1;
margin: 0 20px;
}

a {
color: black;
}
Expand All @@ -40,4 +45,9 @@ a:hover {
flex: 0 0 50%;
padding-top: 30px;
}

.fixed__instructions {
padding-top: 30px;
margin: 0 10px;
}
}
2 changes: 1 addition & 1 deletion example/src/pages/react-slick.css
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
@import "~slick-carousel/slick/slick.css";
@import "~slick-carousel/slick/slick-theme.css";

* {
.react-slick * {
min-height: 0;
min-width: 0;
}
Expand Down
4 changes: 4 additions & 0 deletions example/src/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@ import { Router, Route } from 'react-router';

import SideBySide from './pages/SideBySide';
import ReactSlick from './pages/ReactSlick';
import EnlargedImageContainerDimensions from './pages/EnlargedImageContainerDimensions';
import FixedWidthSmallImage from './pages/FixedWidthSmallImage';

const Routes = (props) => (
<Router {...props}>
<Route path="/" component={SideBySide} />
<Route path="/react-slick" component={ReactSlick} />
<Route path="/dimensions" component={EnlargedImageContainerDimensions} />
<Route path="/fixed" component={FixedWidthSmallImage} />
</Router>
);

Expand Down
31 changes: 18 additions & 13 deletions src/EnlargedImage.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ export default class extends React.Component {
isActive: PropTypes.bool,
isLazyLoaded: PropTypes.bool,
largeImage: ImageShape,
smallImage: ImageShape,
containerDimensions: PropTypes.shape({
width: PropTypes.number,
height: PropTypes.number
}),
imagePosition: PropTypes.oneOf([
ENLARGED_IMAGE_POSITION.beside,
ENLARGED_IMAGE_POSITION.over
Expand Down Expand Up @@ -128,26 +131,28 @@ export default class extends React.Component {
imagePosition,
cursorOffset,
largeImage,
smallImage,
position
containerDimensions,
position,
smallImage
} = this.props;
const { over: OVER } = ENLARGED_IMAGE_POSITION;
const isInPlaceMode = imagePosition === OVER;

if (isInPlaceMode) {
return getInPlaceEnlargedImageCoordinates(
smallImage,
return getInPlaceEnlargedImageCoordinates({
containerDimensions,
largeImage,
position
);
});
}

return getLensModeEnlargedImageCoordinates(
smallImage,
return getLensModeEnlargedImageCoordinates({
containerDimensions,
cursorOffset,
largeImage,
position,
cursorOffset
);
smallImage
});
}

render() {
Expand All @@ -163,7 +168,7 @@ export default class extends React.Component {
onLoad = noop,
onError = noop
},
smallImage,
containerDimensions,
} = this.props;

const {
Expand All @@ -175,8 +180,8 @@ export default class extends React.Component {

const defaultContainerStyle = this.getDefaultContainerStyle();
const computedContainerStyle = {
width: smallImage.width,
height: smallImage.height,
width: containerDimensions.width,
height: containerDimensions.height,
opacity: this.state.isTransitionActive ? 1 : 0,
transition: `opacity ${fadeDurationInMs}ms ease-in`,
pointerEvents: 'none'
Expand Down
Loading

0 comments on commit 9bcda75

Please sign in to comment.