Unverified Commit 9ef0fae8 authored by Mikkel Laursen's avatar Mikkel Laursen Committed by GitHub

[Examples] Update react-md examples for v2 (#15723)

parent 17716d14
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
# dependencies
/node_modules
/.pnp
.pnp.js
# testing
/coverage
# next.js
/.next/
/out/
# production
/build
# misc
.DS_Store
*.pem
# debug
npm-debug.log*
yarn-debug.log*
yarn-error.log*
# local env files
.env.local
.env.development.local
.env.test.local
.env.production.local
# vercel
.vercel
# ReactMD, Next.js, and Typescript Example
This example sets up a simple [ReactMD](https://github.com/mlaursen/react-md), Next.js, and Typescript
app featuring:
- [\_variables.scss](./styles/_variables.scss) to override the default
`react-md` theme and feature toggles
- [app.scss](./styles/app.scss) global styles that conditionally apply the dark theme
based on the user's OS preferences
- a custom [\_app.tsx](./pages/_app.tsx) that uses a persistent layout
- a reusable [Layout.tsx](./components/Layout/Layout.tsx) that:
- updates all the icons to use `SVGIcon`s instead of `FontIcon`s
- initializes the `Layout` component from `react-md` with navigation items
For more information about ReactMD's features, styling, components, and API, check out
the [main documentation](https://react-md.dev). You can also view the
[documentation site's source code](https://github.com/mlaursen/react-md/tree/master/packages/documentation)
for a more complex example of using ReactMD + Next.js.
## Deploy your own
Deploy the example using [Vercel](https://vercel.com/now):
[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/import/project?template=https://github.com/vercel/next.js/tree/canary/examples/with-react-md-typescript)
## How to use
### Using `create-next-app`
Execute [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app) with [npm](https://docs.npmjs.com/cli/init) or [Yarn](https://yarnpkg.com/lang/en/docs/cli/create/) to bootstrap the example:
```bash
npx create-next-app --example with-react-md-typescript with-react-md-typescript-app
# or
yarn create next-app --example with-react-md-typescript with-react-md-typescript-app
```
Deploy it to the cloud with [Vercel](https://vercel.com/import?filter=next.js&utm_source=github&utm_medium=readme&utm_campaign=next-example) ([Documentation](https://nextjs.org/docs/deployment)).
import React, { ReactElement, ReactNode } from 'react'
import { useRouter } from 'next/router'
import {
Layout as RMDLayout,
Configuration,
ConfigurableIcons,
useLayoutNavigation,
ArrowDropDownSVGIcon,
CheckBoxSVGIcon,
FileDownloadSVGIcon,
KeyboardArrowDownSVGIcon,
KeyboardArrowLeftSVGIcon,
KeyboardArrowRightSVGIcon,
MenuSVGIcon,
NotificationsSVGIcon,
RadioButtonCheckedSVGIcon,
RemoveRedEyeSVGIcon,
ArrowUpwardSVGIcon,
CheckSVGIcon,
} from 'react-md'
import LinkUnstyled from '../LinkUnstyled'
import navItems from './navItems'
const icons: ConfigurableIcons = {
back: <KeyboardArrowLeftSVGIcon />,
checkbox: <CheckBoxSVGIcon />,
dropdown: <ArrowDropDownSVGIcon />,
download: <FileDownloadSVGIcon />,
expander: <KeyboardArrowDownSVGIcon />,
forward: <KeyboardArrowRightSVGIcon />,
menu: <MenuSVGIcon />,
notification: <NotificationsSVGIcon />,
radio: <RadioButtonCheckedSVGIcon />,
password: <RemoveRedEyeSVGIcon />,
selected: <CheckSVGIcon />,
sort: <ArrowUpwardSVGIcon />,
}
interface LayoutProps {
children: ReactNode
}
// Check out the documentation for Configuring your Layout for more information:
// - https://react-md.dev/guides/configuring-your-layout
export default function Layout({ children }: LayoutProps): ReactElement {
const { pathname } = useRouter()
return (
<Configuration icons={icons}>
<RMDLayout
title="Main Title"
navHeaderTitle="Navigation Header Title"
tabletLayout="temporary"
landscapeTabletLayout="temporary"
desktopLayout="temporary"
largeDesktopLayout="temporary"
treeProps={useLayoutNavigation(navItems, pathname, LinkUnstyled)}
>
{children}
</RMDLayout>
</Configuration>
)
}
import React, { ReactNode } from 'react'
import {
LayoutNavigationTree,
LayoutNavigationItem,
HomeSVGIcon,
TvSVGIcon,
} from 'react-md'
/**
* Note: The `parentId` **must** be defaulted to `null` for the navigation tree
* to render correctly since this uses the @react-md/tree package behind the
* scenes. Each item that has a `parentId` set to `null` will appear at the root
* level of your navigation tree.
*/
function createRoute(
pathname: string,
children: string,
leftAddon: ReactNode | undefined,
parentId: string | null = null
): LayoutNavigationItem {
return {
itemId: pathname,
parentId,
href: pathname,
children,
leftAddon,
}
}
const navItems: LayoutNavigationTree = {
'/': createRoute('/', 'Home', <HomeSVGIcon />),
'/route-1': createRoute('/route-1', 'Route 1', <TvSVGIcon />),
}
export default navItems
import React, { ReactElement, AnchorHTMLAttributes } from 'react'
import Link, { LinkProps } from 'next/link'
export interface LinkUnstyledProps
extends Omit<LinkProps, 'children' | 'passHref' | 'onError'>,
Omit<AnchorHTMLAttributes<HTMLAnchorElement>, 'onError' | 'href'> {}
export default function LinkUnstyled({
as,
href,
scroll,
shallow,
replace,
children,
...props
}: LinkUnstyledProps): ReactElement {
if (typeof href === 'string' && href.startsWith('http')) {
// external links
return (
<a {...props} href={href} rel="noopener noreferrer">
{children}
</a>
)
}
return (
<Link
shallow={shallow}
scroll={scroll}
replace={replace}
href={href}
as={as}
>
<a {...props}>{children}</a>
</Link>
)
}
/// <reference types="next" />
/// <reference types="next/types/global" />
{
"name": "with-react-md-typescript",
"version": "0.1.0",
"private": true,
"license": "ISC",
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start"
},
"dependencies": {
"next": "latest",
"react": "16.13.1",
"react-dom": "16.13.1",
"react-md": "^2.1.1"
},
"devDependencies": {
"@types/node": "^14.0.27",
"@types/react": "^16.9.43",
"node-sass": "^4.14.1",
"typescript": "^3.9.7"
}
}
import React, { ReactElement } from 'react'
import Head from 'next/head'
import { AppProps } from 'next/app'
import Layout from '../components/Layout'
import '../styles/app.scss'
export default function App({ Component, pageProps }: AppProps): ReactElement {
return (
<Layout>
<Head>
<title>react-md with next.js</title>
</Head>
<Component {...pageProps} />
</Layout>
)
}
import React, { ReactElement } from 'react'
import { Text, TextContainer } from 'react-md'
export default function Home(): ReactElement {
return (
<TextContainer>
<Text type="headline-4">Hello, world!</Text>
</TextContainer>
)
}
import React, { ReactElement } from 'react'
import { TextContainer, Text } from 'react-md'
export default function Route1(): ReactElement {
return (
<TextContainer>
<Text type="headline-4">Route 1</Text>
</TextContainer>
)
}
@import '~@react-md/theme/dist/color-palette';
// I'm bad with colors... update these variables as needed for
// your app
$rmd-theme-primary: $rmd-purple-500;
$rmd-theme-secondary: $rmd-pink-a-200;
$rmd-theme-light: true;
$rmd-theme-dark-elevation: 'prefers-color-scheme';
$rmd-utils-auto-dense: false;
// any other react-md overrides or "global" variables used
// in your app
// import react-md variable overrides
@import './variables';
// generate all react-md styles with the custom theme
@import 'react-md/dist/styles';
@media (prefers-color-scheme: dark) {
:root {
@include rmd-theme-dark;
}
}
{
"compilerOptions": {
"allowJs": true,
"alwaysStrict": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"isolatedModules": true,
"jsx": "preserve",
"lib": ["dom", "es2017"],
"module": "esnext",
"moduleResolution": "node",
"noEmit": true,
"noFallthroughCasesInSwitch": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"resolveJsonModule": true,
"skipLibCheck": true,
"strict": true,
"target": "esnext"
},
"exclude": ["node_modules"],
"include": ["**/*.ts", "**/*.tsx"]
}
# Example app with react-md # ReactMD and Next.js Example
This example features how you use [react-md](https://react-md.dev/) (React Material Design) with Next.js. This example sets up a simple [ReactMD](https://github.com/mlaursen/react-md) and Next.js
app featuring:
I recommend reading [layout-component](../layout-component) example next to learn how to reuse the layout across the pages.
- [\_variables.scss](./styles/_variables.scss) to override the default
![Screenshot](https://cloud.githubusercontent.com/assets/304265/22472564/b2e04ff0-e7de-11e6-921e-d0c9833ac805.png) `react-md` theme and feature toggles
- [app.scss](./styles/app.scss) global styles that conditionally apply the dark theme
based on the user's OS preferences
- a custom [\_app.jsx](./pages/_app.jsx) that uses a persistent layout
- a reusable [Layout.jsx](./components/Layout/Layout.jsx) that:
- updates all the icons to use `SVGIcon`s instead of `FontIcon`s
- initializes the `Layout` component from `react-md` with navigation items
For more information about ReactMD's features, styling, components, and API, check out
the [main documentation](https://react-md.dev). You can also view the
[documentation site's source code](https://github.com/mlaursen/react-md/tree/master/packages/documentation)
for a more complex example of using ReactMD + Next.js or the [with-react-md-typescript](../with-react-md-typescript)
example for Typescript support.
## Deploy your own ## Deploy your own
Deploy the example using [Vercel](https://vercel.com): Deploy the example using [Vercel](https://vercel.com/now):
[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/import/project?template=https://github.com/vercel/next.js/tree/canary/examples/with-react-md) [![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/import/project?template=https://github.com/vercel/next.js/tree/canary/examples/with-react-md)
...@@ -24,25 +36,4 @@ npx create-next-app --example with-react-md with-react-md-app ...@@ -24,25 +36,4 @@ npx create-next-app --example with-react-md with-react-md-app
yarn create next-app --example with-react-md with-react-md-app yarn create next-app --example with-react-md with-react-md-app
``` ```
### Download manually
Download the example:
```bash
curl https://codeload.github.com/vercel/next.js/tar.gz/canary | tar -xz --strip=2 next.js-canary/examples/with-react-md
cd with-react-md
```
Install it and run:
```bash
npm install
ln -f -s ../node_modules/react-md/dist/react-md.light_blue-yellow.min.css public/react-md.light_blue-yellow.min.css
npm run dev
# or
yarn
ln -f -s ../node_modules/react-md/dist/react-md.light_blue-yellow.min.css public/react-md.light_blue-yellow.min.css
yarn dev
```
Deploy it to the cloud with [Vercel](https://vercel.com/import?filter=next.js&utm_source=github&utm_medium=readme&utm_campaign=next-example) ([Documentation](https://nextjs.org/docs/deployment)). Deploy it to the cloud with [Vercel](https://vercel.com/import?filter=next.js&utm_source=github&utm_medium=readme&utm_campaign=next-example) ([Documentation](https://nextjs.org/docs/deployment)).
import React from 'react'
import { useRouter } from 'next/router'
import {
Layout as RMDLayout,
Configuration,
useLayoutNavigation,
ArrowDropDownSVGIcon,
CheckBoxSVGIcon,
FileDownloadSVGIcon,
KeyboardArrowDownSVGIcon,
KeyboardArrowLeftSVGIcon,
KeyboardArrowRightSVGIcon,
MenuSVGIcon,
NotificationsSVGIcon,
RadioButtonCheckedSVGIcon,
RemoveRedEyeSVGIcon,
ArrowUpwardSVGIcon,
CheckSVGIcon,
} from 'react-md'
import LinkUnstyled from '../LinkUnstyled'
import navItems from './navItems'
const icons = {
back: <KeyboardArrowLeftSVGIcon />,
checkbox: <CheckBoxSVGIcon />,
dropdown: <ArrowDropDownSVGIcon />,
download: <FileDownloadSVGIcon />,
expander: <KeyboardArrowDownSVGIcon />,
forward: <KeyboardArrowRightSVGIcon />,
menu: <MenuSVGIcon />,
notification: <NotificationsSVGIcon />,
radio: <RadioButtonCheckedSVGIcon />,
password: <RemoveRedEyeSVGIcon />,
selected: <CheckSVGIcon />,
sort: <ArrowUpwardSVGIcon />,
}
// Check out the documentation for Configuring your Layout for more information:
// - https://react-md.dev/guides/configuring-your-layout
export default function Layout({ children }) {
const { pathname } = useRouter()
return (
<Configuration icons={icons}>
<RMDLayout
title="Main Title"
navHeaderTitle="Navigation Header Title"
tabletLayout="temporary"
landscapeTabletLayout="temporary"
desktopLayout="temporary"
largeDesktopLayout="temporary"
treeProps={useLayoutNavigation(navItems, pathname, LinkUnstyled)}
>
{children}
</RMDLayout>
</Configuration>
)
}
export { default } from './Layout'
import React from 'react'
import { HomeSVGIcon, TvSVGIcon } from 'react-md'
/**
* Note: The `parentId` **must** be defaulted to `null` for the navigation tree
* to render correctly since this uses the @react-md/tree package behind the
* scenes. Each item that has a `parentId` set to `null` will appear at the root
* level of your navigation tree.
*/
function createRoute(pathname, children, leftAddon, parentId = null) {
return {
itemId: pathname,
parentId,
href: pathname,
children,
leftAddon,
}
}
const navItems = {
'/': createRoute('/', 'Home', <HomeSVGIcon />),
'/route-1': createRoute('/route-1', 'Route 1', <TvSVGIcon />),
}
export default navItems
import React from 'react'
import Link from 'next/link'
export default function LinkUnstyled({
as,
href,
scroll,
shallow,
replace,
children,
...props
}) {
if (typeof href === 'string' && href.startsWith('http')) {
// external links
return (
<a {...props} href={href} rel="noopener noreferrer">
{children}
</a>
)
}
return (
<Link
shallow={shallow}
scroll={scroll}
replace={replace}
href={href}
as={as}
>
<a {...props}>{children}</a>
</Link>
)
}
{ {
"name": "with-react-md", "name": "with-react-md",
"version": "1.0.0", "version": "1.0.0",
"private": true,
"license": "ISC",
"scripts": { "scripts": {
"dev": "next", "dev": "next",
"build": "next build", "build": "next build",
...@@ -8,11 +10,11 @@ ...@@ -8,11 +10,11 @@
}, },
"dependencies": { "dependencies": {
"next": "latest", "next": "latest",
"react": "^16.7.0", "react": "^16.13.1",
"react-addons-css-transition-group": "^16.0.0", "react-dom": "^16.13.1",
"react-addons-transition-group": "^16.0.0", "react-md": "^2.1.1"
"react-dom": "^16.7.0",
"react-md": "^1.0.1"
}, },
"license": "ISC" "devDependencies": {
"node-sass": "^4.14.1"
}
} }
import 'react-md/dist/react-md.light_blue-yellow.min.css'
export default function MyApp({ Component, pageProps }) {
return <Component {...pageProps} />
}
import React from 'react'
import Head from 'next/head'
import Layout from '../components/Layout'
import '../styles/app.scss'
export default function App({ Component, pageProps }) {
return (
<Layout>
<Head>
<title>react-md with next.js</title>
</Head>
<Component {...pageProps} />
</Layout>
)
}
import Head from 'next/head'
import Link from 'next/link'
import { PureComponent } from 'react'
import Avatar from 'react-md/lib/Avatars'
import Button from 'react-md/lib/Buttons/Button'
import FontIcon from 'react-md/lib/FontIcons'
import ListItem from 'react-md/lib/Lists/ListItem'
import NavigationDrawer from 'react-md/lib/NavigationDrawers'
import SelectField from 'react-md/lib/SelectFields'
const avatarSrc =
'https://cloud.githubusercontent.com/assets/13041/19686250/971bf7f8-9ac0-11e6-975c-188defd82df1.png'
const drawerHeaderChildren = [
<Avatar
key={avatarSrc}
src={avatarSrc}
role="presentation"
iconSized
style={{
alignSelf: 'center',
marginLeft: 16,
marginRight: 16,
flexShrink: 0,
}}
/>,
<SelectField
id="account-switcher"
defaultValue="Jonathan"
menuItems={['Jonathan', 'Fred']}
key="account-switcher"
position={SelectField.Positions.BELOW}
className="md-select-field--toolbar"
/>,
]
class NavigationLink extends PureComponent {
// NOTE: Don't try using Stateless (function) component here. `ref` is
// required by React-MD/AccessibleFakeButton, but Stateless components
// don't have one by design:
// https://github.com/facebook/react/issues/4936
render() {
const { href, as, children, ..._props } = this.props
return (
<div {..._props} style={{ padding: 0 }}>
<Link href={href} as={as}>
<a
className="md-list-tile md-list-tile--mini"
style={{ width: '100%', overflow: 'hidden' }}
>
{children}
</a>
</Link>
</div>
)
}
}
export default function Home() {
const closeButton = (
<Button
icon
tooltipLabel="Close the interactive demo"
tooltipDelay={150}
tooltipPosition="left"
>
close
</Button>
)
return (
<div>
<Head>
<link
rel="stylesheet"
href="https://fonts.googleapis.com/css?family=Roboto:300,400,500"
/>
<link
rel="stylesheet"
href="https://fonts.googleapis.com/css?family=Material+Icons"
/>
</Head>
<NavigationDrawer
navItems={[
<ListItem
key="0"
component={NavigationLink}
href="/"
leftIcon={<FontIcon>inbox</FontIcon>}
tileClassName="md-list-tile--mini"
primaryText={'Root'}
/>,
<ListItem
key="1"
component={NavigationLink}
href="/non-existing-page"
leftIcon={<FontIcon>star</FontIcon>}
tileClassName="md-list-tile--mini"
primaryText={'404 page'}
/>,
]}
contentClassName="md-grid"
drawerHeaderChildren={drawerHeaderChildren}
mobileDrawerType={NavigationDrawer.DrawerTypes.TEMPORARY_MINI}
tabletDrawerType={NavigationDrawer.DrawerTypes.PERSISTENT_MINI}
desktopDrawerType={NavigationDrawer.DrawerTypes.PERSISTENT_MINI}
toolbarTitle="Hello, World!"
toolbarActions={closeButton}
>
<h1>Hello Next.js!</h1>
</NavigationDrawer>
</div>
)
}
import React from 'react'
import { Text, TextContainer } from 'react-md'
export default function Home() {
return (
<TextContainer>
<Text type="headline-4">Hello, world!</Text>
</TextContainer>
)
}
import React from 'react'
import { TextContainer, Text } from 'react-md'
export default function Route1() {
return (
<TextContainer>
<Text type="headline-4">Route 1</Text>
</TextContainer>
)
}
@import '~@react-md/theme/dist/color-palette';
// I'm bad with colors... update these variables as needed for
// your app
$rmd-theme-primary: $rmd-purple-500;
$rmd-theme-secondary: $rmd-pink-a-200;
$rmd-theme-light: true;
$rmd-theme-dark-elevation: 'prefers-color-scheme';
$rmd-utils-auto-dense: false;
// any other react-md overrides or "global" variables used
// in your app
// import react-md variable overrides
@import './variables';
// generate all react-md styles with the custom theme
@import 'react-md/dist/styles';
@media (prefers-color-scheme: dark) {
:root {
@include rmd-theme-dark;
}
}
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