Skip to content

Commit e62910b

Browse files
authored
Hide MagnifyHint when image is already fully magnified, and use magnified image in Lightbox (#50)
* Hide MagnifyHint when image is already fully magnified, and use magnified image in Lightbox * Reset window width for future tests * Add explanation for media width check
1 parent 3296b14 commit e62910b

File tree

3 files changed

+54
-10
lines changed

3 files changed

+54
-10
lines changed

src/carousel/Carousel.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ const Carousel = React.forwardRef((props, ref) => {
104104
slideStyle={slideStyle}
105105
autoplay={autoplay}
106106
interval={interval}
107+
containerStyle={{ alignItems: 'center' }}
107108
>
108109
{children}
109110
</AutoPlaySwipeableViews>

src/carousel/MediaCarousel.js

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ function MediaCarousel(props) {
118118
const [lightboxActive, setLightboxActive] = useState()
119119
const theme = useTheme()
120120
const isSmall = useMediaQuery(theme.breakpoints.down('xs'))
121+
const isTouchScreen = useMediaQuery('(hover:none)')
121122

122123
useEffect(() => {
123124
// Reset selection index when media changes
@@ -167,14 +168,28 @@ function MediaCarousel(props) {
167168
}
168169

169170
if (media && media.full && media.full.some(item => item.magnify)) {
170-
belowAdornments.push(
171-
<MagnifyHint
172-
key="magnify-hint"
173-
over={over}
174-
disableExpand={lightboxActive}
175-
className={magnifyHintClassName}
176-
/>,
177-
)
171+
// we use the media's magnify.width prop to test if the image is larger than the screen size, and
172+
// hide the magnify hint if so. this is a magic large number just used to ensure that the hint is
173+
// shown if the width property is not defined for the given media
174+
const MAX_WIDTH = 10000
175+
let showHint = true
176+
if (typeof window !== 'undefined') {
177+
const { innerWidth } = window
178+
const mediaWidth = get(media.full[selected], 'magnify.width', MAX_WIDTH)
179+
if (mediaWidth <= innerWidth) {
180+
showHint = false
181+
}
182+
}
183+
if (showHint) {
184+
belowAdornments.push(
185+
<MagnifyHint
186+
key="magnify-hint"
187+
over={over}
188+
disableExpand={lightboxActive}
189+
className={magnifyHintClassName}
190+
/>,
191+
)
192+
}
178193
}
179194

180195
if (lightboxActive) {
@@ -212,9 +227,10 @@ function MediaCarousel(props) {
212227
onLoad={i === 0 ? onFullSizeImagesLoaded : null}
213228
magnifyProps={magnifyProps}
214229
{...item}
215-
magnify={isSmall ? undefined : item.magnify}
230+
magnify={isTouchScreen ? undefined : item.magnify}
231+
src={get(item, 'magnify.src', item.src)}
216232
imageProps={
217-
lightboxActive && !isSmall
233+
lightboxActive && !isTouchScreen
218234
? {
219235
fill: false,
220236
contain: true,

test/carousel/MediaCarousel.test.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ describe('MediaCarousel', () => {
1717

1818
const mockMediaQuery = jest.spyOn(useMediaQuery, 'default')
1919

20+
beforeEach(() => {
21+
window.innerWidth = 1024
22+
window.dispatchEvent(new Event('resize'))
23+
})
24+
2025
afterEach(() => {
2126
wrapper.unmount()
2227
})
@@ -114,6 +119,13 @@ describe('MediaCarousel', () => {
114119
contain: true,
115120
src: media.full[0].magnify.src,
116121
})
122+
123+
expect(
124+
wrapper
125+
.find(Media)
126+
.first()
127+
.prop('src'),
128+
).toEqual(media.full[0].magnify.src)
117129
})
118130

119131
it('should pass height 100% to Carousel if window size is small and lightbox is opened', () => {
@@ -130,6 +142,21 @@ describe('MediaCarousel', () => {
130142
).toBe('100%')
131143
})
132144

145+
it('should hide the magnify hint if the lightbox is opened and the window is wider than the image', () => {
146+
wrapper = mount(<MediaCarousel media={media} />)
147+
wrapper.find(Carousel).simulate('click')
148+
console.log(wrapper.find(MagnifyHint))
149+
expect(wrapper.find(MagnifyHint)).toExist()
150+
151+
window.innerWidth = 1300
152+
window.dispatchEvent(new Event('resize'))
153+
154+
wrapper = mount(<MediaCarousel media={media} />)
155+
wrapper.find(Carousel).simulate('click')
156+
console.log(wrapper.find(MagnifyHint))
157+
expect(wrapper.find(MagnifyHint)).not.toExist()
158+
})
159+
133160
it('should pass thumbnail to render below the Carousel if image is not loaded and thumbnail is passed as prop ', () => {
134161
wrapper = mount(<MediaCarousel media={media} thumbnail={{ test: 'test' }} />)
135162

0 commit comments

Comments
 (0)