Commit 3e7f836c authored by yi's avatar yi Committed by Tim Neutkens

Added example with react-multi-carousel for rendering items on the server-side (#6820)

* Simplified format function in with-redux (#6518)

* Update README.md (#6749)

- fix runtime configuration example to use `customKey`
- standardize warning callouts to use a block quote and bold "**Warning:**" text. Previously there were different ways a warning was mentioned on the docs page:
  - A warning emoji. The warning emoji didn't render in the actual site. 
    <img width="770" alt="Screen Shot 2019-03-21 at 9 22 21 AM" src="https://user-images.githubusercontent.com/1103708/54759302-c655f380-4bbb-11e9-9116-b0fdc2467bc3.png">
  - Italicized "Warning" text. Imo, does not stand out on the page.
    <img width="663" alt="Screen Shot 2019-03-21 at 9 24 43 AM" src="https://user-images.githubusercontent.com/1103708/54759376-f604fb80-4bbb-11e9-85c6-6589b7738dee.png">

* Adde example on how to rendering muiltiple items carousel on server-side with nextjs
parent 49fd5990
# react-multi-carousel example
Source code is hosted on the [react-multi-carorusel](https://github.com/YIZHUANG/react-multi-carousel/tree/master/examples/ssr) repository.
[![Demo](https://react-multi-carousel.now.sh/)
### Usage
Install and run:
```bash
npm install
npm run dev
```
## The idea behind the example
[react-multi-carousel](https://www.npmjs.com/package/react-multi-carousel) is a React component that provides a Carousel that renders on the server-side that supports multiple items with no external dependency.
The reason for that is i needed to implement a Carousel component for my own project, but couldn't find any that is lightweight and supports both ssr and allows me to customized.
## How does it work with ssr?
* On the server-side, we detect the user's device to decide how many items we are showing and then using flex-basis to assign * width to the carousel item.
* On the client-side, old fashion getting width of the container and assign the average of it to each carousel item.
The UI part of this example is copy paste from for the sake of simplicity. [with-material-ui](https://github.com/zeit/next.js/tree/canary/examples/with-material-ui)
import React from 'react'
const Image = ({ url, alt }) => (
<img draggable={false} style={{ width: '100%', height: '100%', position: 'relative' }} src={url} alt={alt} />
)
export default Image
const withPlugins = require('next-compose-plugins')
const withCSS = require('@zeit/next-css')
const withFonts = require('next-fonts')
module.exports = withPlugins([withCSS, withFonts], { target: 'serverless' })
{
"version": 2,
"name": "nextjs",
"builds": [
{ "src": "package.json", "use": "@now/next" }
]
}
{
"name": "ssr",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"dev": "next",
"build": "next build",
"start": "next start"
},
"author": "",
"license": "ISC",
"dependencies": {
"@material-ui/core": "^3.9.2",
"@material-ui/icons": "^3.0.2",
"@zeit/next-css": "^1.0.1",
"jss": "^9.8.7",
"mobile-detect": "^1.4.3",
"next": "^8.0.3",
"react": "^16.8.3",
"react-dom": "^16.8.3",
"react-jss": "^8.6.1",
"react-multi-carousel": "^1.2.5"
},
"devDependencies": {
"file-loader": "^3.0.1",
"next-compose-plugins": "^2.1.1",
"next-fonts": "^0.17.0",
"url-loader": "^1.1.2"
}
}
/*
This is copy paste from the with-material-ui example.
*/
import React from 'react'
import App, { Container } from 'next/app'
import Head from 'next/head'
import { MuiThemeProvider } from '@material-ui/core/styles'
import CssBaseline from '@material-ui/core/CssBaseline'
import JssProvider from 'react-jss/lib/JssProvider'
import getPageContext from '../src/getPageContext'
class MyApp extends App {
constructor () {
super()
this.pageContext = getPageContext()
}
render () {
const { Component, pageProps } = this.props
return (
<Container>
<Head>
<title>react-multi-carousel</title>
</Head>
<JssProvider
registry={this.pageContext.sheetsRegistry}
generateClassName={this.pageContext.generateClassName}
>
<MuiThemeProvider
theme={this.pageContext.theme}
sheetsManager={this.pageContext.sheetsManager}
>
<CssBaseline />
<Component pageContext={this.pageContext} {...pageProps} />
</MuiThemeProvider>
</JssProvider>
</Container>
)
}
}
export default MyApp
import React from 'react'
import MobileDetect from 'mobile-detect'
import { withStyles } from '@material-ui/core/styles'
import Carousel from 'react-multi-carousel'
import Image from '../components/image'
import '../style.css'
import 'react-multi-carousel/lib/styles.css'
const styles = theme => ({
root: {
textAlign: 'center'
},
title: {
maxWidth: 400,
margin: 'auto',
marginTop: 10
}
})
class Index extends React.Component {
static getInitialProps ({ req, isServer }) {
let userAgent
let deviceType
if (req) {
userAgent = req.headers['user-agent']
} else {
userAgent = navigator.userAgent
}
const md = new MobileDetect(userAgent)
if (md.tablet()) {
deviceType = 'tablet'
} else if (md.mobile()) {
deviceType = 'mobile'
} else {
deviceType = 'desktop'
}
return { deviceType }
}
render () {
const { classes } = this.props
const images = [
'https://images.unsplash.com/photo-1549989476-69a92fa57c36?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=800&q=60',
'https://images.unsplash.com/photo-1549396535-c11d5c55b9df?ixlib=rb-1.2.1&auto=format&fit=crop&w=800&q=60',
'https://images.unsplash.com/photo-1550133730-695473e544be?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=800&q=60',
'https://images.unsplash.com/photo-1550167164-1b67c2be3973?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=800&q=60',
'https://images.unsplash.com/photo-1550338861-b7cfeaf8ffd8?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=800&q=60',
'https://images.unsplash.com/photo-1550223640-23097fc71cb2?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=800&q=60',
'https://images.unsplash.com/photo-1550353175-a3611868086b?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=800&q=60',
'https://images.unsplash.com/photo-1550330039-a54e15ed9d33?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=800&q=60',
'https://images.unsplash.com/photo-1549737328-8b9f3252b927?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=800&q=60',
'https://images.unsplash.com/photo-1549833284-6a7df91c1f65?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=800&q=60',
'https://images.unsplash.com/photo-1549985908-597a09ef0a7c?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=800&q=60',
'https://images.unsplash.com/photo-1550064824-8f993041ffd3?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=800&q=60'
]
const responsive = {
desktop: {
breakpoint: { max: 3000, min: 1024 },
items: 3
},
tablet: {
breakpoint: { max: 1024, min: 464 },
items: 2
},
mobile: {
breakpoint: { max: 464, min: 0 },
items: 1
}
}
return (
<div className={classes.root}>
<Carousel
responsive={responsive}
ssr
showDots={false}
slidesToSlide={1}
infinite
containerClass='container-with-dots'
itemClass='image-item'
deviceType={this.props.deviceType}
>
{images.map(image => {
return <Image url={image} alt={image} />
})}
</Carousel>
</div>
)
}
}
export default withStyles(styles)(Index)
import { SheetsRegistry } from 'jss'
import { createMuiTheme, createGenerateClassName } from '@material-ui/core/styles'
import purple from '@material-ui/core/colors/purple'
import green from '@material-ui/core/colors/green'
// A theme with custom primary and secondary color.
// It's optional.
const theme = createMuiTheme({
palette: {
primary: {
light: purple[300],
main: purple[500],
dark: purple[700]
},
secondary: {
light: green[300],
main: green[500],
dark: green[700]
}
},
typography: {
useNextVariants: true
}
})
function createPageContext () {
return {
theme,
// This is needed in order to deduplicate the injection of CSS in the page.
sheetsManager: new Map(),
// This is needed in order to inject the critical CSS.
sheetsRegistry: new SheetsRegistry(),
// The standard class name generator.
generateClassName: createGenerateClassName()
}
}
let pageContext
export default function getPageContext () {
// Make sure to create a new context for every server-side request so that data
// isn't shared between connections (which would be bad).
if (!process.browser) {
return createPageContext()
}
// Reuse context on the client-side.
if (!pageContext) {
pageContext = createPageContext()
}
return pageContext
}
.image-item {
padding: 10px;
}
.container-with-dots {
margin-top: 20px;
padding-bottom: 10px;
}
#__next {
overflow: scroll;
margin-bottom: 40px;
}
...@@ -19,7 +19,4 @@ export default ({ lastUpdate, light }) => { ...@@ -19,7 +19,4 @@ export default ({ lastUpdate, light }) => {
) )
} }
const format = t => const format = t => t.toJSON().slice(11, 19) // cut off except hh:mm:ss
`${pad(t.getUTCHours())}:${pad(t.getUTCMinutes())}:${pad(t.getUTCSeconds())}`
const pad = n => (n < 10 ? `0${n}` : n)
...@@ -1704,7 +1704,7 @@ export default Index ...@@ -1704,7 +1704,7 @@ export default Index
#### Runtime configuration #### Runtime configuration
> :warning: Note that this option is not available when using `target: 'serverless'` > **Warning:** Note that this option is not available when using `target: 'serverless'`
> :warning: Generally you want to use build-time configuration to provide your configuration. > :warning: Generally you want to use build-time configuration to provide your configuration.
> The reason for this is that runtime configuration adds a small rendering / initialization overhead. > The reason for this is that runtime configuration adds a small rendering / initialization overhead.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment