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

Fixing AMP rendering #115

Merged
merged 1 commit into from
Jun 22, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
7 changes: 2 additions & 5 deletions src/LazyHydrate.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ if (isBrowser()) {
will be removed.
*/
export function LazyStyles() {
const amp = useAmp()
if (amp) return null
if (useAmp()) return null

let styles = null
try {
Expand Down Expand Up @@ -91,11 +90,8 @@ Router.events.on('routeChangeStart', () => {
})

function LazyHydrateInstance({ id, className, ssrOnly, children, on, ...props }) {
const amp = useAmp()

function isHydrated() {
if (isBrowser()) {
if (amp) return true
// If rendering after client side navigation
if (window.__lazyHydrateNavigated) return true
// return true
Expand Down Expand Up @@ -216,6 +212,7 @@ function LazyHydrateInstance({ id, className, ssrOnly, children, on, ...props })
*/

function LazyHydrate({ children, ...props }) {
if (useAmp()) return children
return (
<LazyHydrateInstance {...props}>
{/* LazyStylesProvider should not be used in the browser. Once components
Expand Down
14 changes: 12 additions & 2 deletions src/carousel/CarouselThumbnails.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ function CarouselThumbnails({
classes,
className,
thumbnailPosition,
ImageComponent,
}) {
const styles = useStyles({ classes })
const theme = useTheme()
Expand All @@ -152,7 +153,7 @@ function CarouselThumbnails({
}}
>
{thumbnails.map(({ src, alt }, i) => {
const icon = <Image contain className={styles.thumb} src={src} alt={alt} />
const icon = <ImageComponent contain className={styles.thumb} src={src} alt={alt} />
return (
<Tab
classes={{
Expand Down Expand Up @@ -205,6 +206,15 @@ CarouselThumbnails.propTypes = {
* Position of the thumbnails, relative to the main carousel image.
*/
thumbnailPosition: PropTypes.oneOf(['bottom', 'top', 'left', 'right']),

/**
* The component type to use to display images.
*/
ImageComponent: PropTypes.elementType,
}

CarouselThumbnails.defaultProps = {
ImageComponent: Image,
}

export default React.memo(CarouselThumbnails)
export default CarouselThumbnails
25 changes: 22 additions & 3 deletions src/carousel/Media.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const useStyles = makeStyles(theme => ({
* An element that determines the proper tag to use for a media node within a
* [`Carousel`](/apiReference/carousel/Carousel).
*/
export default function Media({
function Media({
magnifyProps,
imageProps,
videoProps,
Expand All @@ -29,6 +29,8 @@ export default function Media({
magnify,
sources,
type = 'image',
ImageComponent,
ImageMagnifyComponent,
}) {
const classes = useStyles()

Expand Down Expand Up @@ -69,7 +71,7 @@ export default function Media({
}
} else if (magnify) {
return (
<ReactImageMagnify
<ImageMagnifyComponent
enlargedImagePosition="over"
{...adjustMagnifyProps()}
smallImage={{
Expand All @@ -81,7 +83,7 @@ export default function Media({
/>
)
} else {
return <Image key={src} src={src} alt={alt} fill {...imageProps} />
return <ImageComponent key={src} src={src} alt={alt} fill {...imageProps} />
}
}

Expand Down Expand Up @@ -127,4 +129,21 @@ Media.propTypes = {
* If `false`, the media is not able to be magnified.
*/
magnify: PropTypes.oneOfType([PropTypes.bool, PropTypes.object]),

/**
* The component type to use to display images.
*/
ImageComponent: PropTypes.elementType,

/**
* The component type to use to display magnified images.
*/
ImageMagnifyComponent: PropTypes.elementType,
}

Media.defaultProps = {
ImageComponent: Image,
ImageMagnifyComponent: ReactImageMagnify,
}

export default Media
13 changes: 9 additions & 4 deletions src/carousel/MediaCarousel.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { light } from '@material-ui/core/styles/createPalette'
import PropTypes from 'prop-types'
import React, { useCallback, useEffect, useState, useRef } from 'react'
import clsx from 'clsx'
import { makeStyles, useTheme } from '@material-ui/core/styles'
import useMediaQuery from '@material-ui/core/useMediaQuery'
import Fill from '../Fill'
import Carousel from './Carousel'
import Image from '../Image'
import Lightbox from './Lightbox'
Expand Down Expand Up @@ -142,6 +140,7 @@ function MediaCarousel(props) {
magnifyProps,
id,
CarouselComponent,
ImageComponent,
MediaComponent,
CarouselThumbnailsComponent,
...others
Expand Down Expand Up @@ -212,7 +211,7 @@ function MediaCarousel(props) {

if (thumbnail && !imagesLoaded) {
belowAdornments.push(
<Image
<ImageComponent
id={THUMBNAIL_IMAGE_ID}
key="thumbnail"
className={styles.thumbnail}
Expand Down Expand Up @@ -416,6 +415,11 @@ MediaCarousel.propTypes = {
* A component type to use for the main carousel component.
*/
CarouselComponent: PropTypes.elementType,

/**
* The component type to use to display images.
*/
ImageComponent: PropTypes.elementType,
}

MediaCarousel.defaultProps = {
Expand All @@ -424,6 +428,7 @@ MediaCarousel.defaultProps = {
thumbnails: true,
thumbnailPosition: 'bottom',
MediaComponent: Media,
ImageComponent: Image,
CarouselComponent: Carousel,
CarouselThumbnailsComponent: CarouselThumbnails,
videoProps: {
Expand All @@ -432,4 +437,4 @@ MediaCarousel.defaultProps = {
},
}

export default React.memo(MediaCarousel)
export default MediaCarousel