Skip to content

[DO NOT MERGE] Switch to @reach/router #90

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion demo/about.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react'
import { Link } from 'react-router-dom'
import { Link } from '@reach/router'

export default props => (
<div>
Expand Down
2 changes: 1 addition & 1 deletion demo/count.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react'
import { Link } from 'react-router-dom'
import { Link } from '@reach/router'

export default class extends React.Component {
static getInitialProps = async () => {
Expand Down
2 changes: 1 addition & 1 deletion docs/_app.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react'
import * as scope from 'rebass'
import { Link } from 'react-router-dom'
import { Link } from '@reach/router'
import { ScopeProvider, SidebarLayout } from '../components'
import {
Provider as RebassProvider,
Expand Down
2 changes: 1 addition & 1 deletion docs/_layout.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
Caps,
BlockLink,
} from 'rebass'
import { Link } from 'react-router-dom'
import { Link } from '@reach/router'
import { Logo } from '@compositor/logo'

export default ({ children }) =>
Expand Down
2 changes: 1 addition & 1 deletion docs/custom-app.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ This file can be used for custom layouts, including headers, footers, and naviga
```jsx
// _app.js
import React from 'react'
import { Link } from 'react-router-dom'
import { Link } from '@reach/router'
import {
Container,
Toolbar,
Expand Down
2 changes: 1 addition & 1 deletion docs/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react'
import { Link } from 'react-router-dom'
import { Link } from '@reach/router'
import styled from 'styled-components'
import {
Container,
Expand Down
2 changes: 1 addition & 1 deletion lib/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const { generateJSReferences } = require('mini-html-webpack-plugin')
const merge = require('webpack-merge')
const React = require('react')
const { renderToString, renderToStaticMarkup } = require('react-dom/server')
const { StaticRouter } = require('react-router-dom')
const { ServerLocation } = require('@reach/router')
const semver = require('semver')
const rimraf = require('rimraf')
const util = require('util')
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"@mdx-js/loader": "^0.11.0",
"@mdx-js/mdx": "^0.10.1",
"@mdx-js/tag": "^0.11.0",
"@reach/router": "^1.1.1",
"@rebass/markdown": "^1.0.0-1",
"babel-core": "^6.26.3",
"babel-loader": "^7.1.4",
Expand Down Expand Up @@ -63,8 +64,6 @@
"react-dev-utils": "^5.0.1",
"react-dom": "^16.4.1",
"react-live": "^1.10.1",
"react-router": "^4.3.1",
"react-router-dom": "^4.3.1",
"react-scope-provider": "^1.0.0-1",
"read-pkg-up": "^3.0.0",
"rebass": "^2.0.0-6",
Expand Down
2 changes: 1 addition & 1 deletion src/FileList.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react'
import { Link } from 'react-router-dom'
import { Link } from '@reach/router'

export default ({ routes = [] }) => (
<React.Fragment>
Expand Down
2 changes: 1 addition & 1 deletion src/Library.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react'
import { Link } from 'react-router-dom'
import { Link } from '@reach/router'
import styled from 'styled-components'
import {
style,
Expand Down
15 changes: 11 additions & 4 deletions src/ScrollTop.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
import React from 'react'
import { withRouter } from 'react-router-dom'
import { Location } from '@reach/Router'

export default withRouter(class extends React.Component {
const withLocation = Comp => props => (
<Location>
{({ location }) => <Comp location={location} {...props} />}
</Location>
)

export default withLocation(class extends React.Component {
componentDidUpdate (prev) {
const { pathname, hash } = this.props.location
if (prev.location.pathname !== pathname) {
const { hash } = this.props.location

if (prev.location !== this.props.location) {
window.scrollTo(0, 0)
}

Expand Down
27 changes: 23 additions & 4 deletions src/SidebarLayout.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import React from 'react'
import PropTypes from 'prop-types'
import {
Link as RouterLink,
NavLink as RouterNavLink
} from 'react-router-dom'
import { Link as RouterLink } from '@reach/router'
import styled from 'styled-components'
import {
Provider as RebassProvider,
Expand All @@ -23,6 +20,28 @@ import {
} from 'rebass'
import { borderColor, themeGet } from 'styled-system'

// const RouterNavLink = RouterLink
const RouterNavLink = ({
activeClassName="active",
activeStyle,
style,
className,
exact,
...props
}) => (
<RouterLink getProps={({ isCurrent, isPartiallyCurrent }) => {
const isActive = (exact && isCurrent) || isPartiallyCurrent
if (isActive) {
return {
className: [ className, activeClassName ].join(' '),
style: { ...style, ...activeStyle }
}
} else {
return { className, style }
}
}} {...props}/>
)

const breakpoint = `@media screen and (min-width: 48em)`

export const Root = styled(Flex)([], {
Expand Down
42 changes: 22 additions & 20 deletions src/entry.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@ import path from 'path'
import React from 'react'
import { render, hydrate } from 'react-dom'
import {
StaticRouter,
BrowserRouter,
Switch,
Route,
ServerLocation,
Router,
Link,
withRouter
} from 'react-router-dom'
Location
} from '@reach/router'
import { Provider as RebassProvider } from 'rebass'
import minimatch from 'minimatch'
import sortBy from 'lodash.sortby'
Expand Down Expand Up @@ -41,7 +39,7 @@ const initialComponents = getComponents(req)

const DefaultApp = props => props.children

const Router = IS_CLIENT ? BrowserRouter : StaticRouter
const Wrapper = IS_CLIENT ? React.Fragment : ServerLocation
const appPath = req.keys().find(key => key === './_app.js')
const App = appPath ? (req(appPath).default || req(appPath)) : DefaultApp

Expand Down Expand Up @@ -96,11 +94,17 @@ export const getRoutes = async (components = initialComponents) => {
return sorted
}

const RouterState = withRouter(({ render, ...props }) => {
const { pathname } = props.location
const route = props.routes.find(r => r.path === pathname || r.href === pathname) || { props: {} }
return render({ ...props, route })
})
const RouterState = ({ render, routes }) => (
<Location>
{(props) => {
const { pathname } = props.location
const route = routes.find(r => r.path === pathname || r.href === pathname) || { props: {} }
return render({ ...props, route })
}}
</Location>
)

const Route = ({ render, ...props }) => render(props)

export default class Root extends React.Component {
static defaultProps = {
Expand All @@ -124,10 +128,10 @@ export default class Root extends React.Component {
: FileList

const render = appProps => (
<Switch>
<Router basepath={basename}>
{routes.map(({ Component, ...route }) => (
<Route
{...route}
path={route.path}
render={props => (
<Catch>
<CenteredLayout
Expand All @@ -143,16 +147,14 @@ export default class Root extends React.Component {
/>
))}
<Route
default
render={props => <NotFound {...props} routes={routes} />}
/>
</Switch>
</Router>
)

return (
<Router
context={{}}
basename={basename}
location={path}>
<Wrapper url={path}>
<React.Fragment>
<RebassProvider>
<ScopeProvider>
Expand All @@ -174,7 +176,7 @@ export default class Root extends React.Component {
</RebassProvider>
{!disableScroll && <ScrollTop />}
</React.Fragment>
</Router>
</Wrapper>
)
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export { Link, NavLink } from 'react-router-dom'
export { Link } from '@reach/router'
export { Link as NavLink } from '@reach/router'
export { default as Catch } from './Catch'
export { default as CenteredLayout } from './CenteredLayout'
export { default as FileList } from './FileList'
Expand Down
11 changes: 8 additions & 3 deletions src/scope.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react'
import { withRouter, Link } from 'react-router-dom'
import { Location, Link } from '@reach/router'
import rebassMarkdown from '@rebass/markdown'
import { Pre } from 'rebass'
import { MDXTag } from '@mdx-js/tag'
Expand All @@ -11,9 +11,14 @@ const cleanHREF = href => href
.replace(/\.mdx?$/, '')
.replace(/\.jsx?$/, '')

export const link = withRouter(({
const withLocation = (Comp) => props => (
<Location>
{({ location }) => <Comp location={location} {...props} />}
</Location>
)

export const link = withLocation(({
href = '',
match,
location,
children,
className,
Expand Down