Browse Source

Make SideBar items active on route. Fix history api with webpack.

master
meriadec 7 years ago
parent
commit
cd8f1c9877
  1. 19
      src/components/SideBar/Item.js
  2. 3
      webpack.renderer.js

19
src/components/SideBar/Item.js

@ -2,10 +2,13 @@
import React from 'react' import React from 'react'
import styled from 'styled-components' import styled from 'styled-components'
import { compose } from 'redux'
import { withRouter } from 'react-router'
import { push } from 'react-router-redux' import { push } from 'react-router-redux'
import { connect } from 'react-redux' import { connect } from 'react-redux'
import type { Element } from 'react' import type { Element } from 'react'
import type { Location } from 'react-router'
import Box from 'components/base/Box' import Box from 'components/base/Box'
import Text from 'components/base/Text' import Text from 'components/base/Text'
@ -15,6 +18,7 @@ type Props = {
linkTo?: string | null, linkTo?: string | null,
desc?: string | null, desc?: string | null,
icon?: Element<*> | null, icon?: Element<*> | null,
location: Location,
push: Function, push: Function,
} }
@ -29,6 +33,7 @@ const Container = styled(Box).attrs({
p: 2, p: 2,
})` })`
cursor: pointer; cursor: pointer;
color: ${p => (p.active ? p.theme.colors.white : '')};
&:hover { &:hover {
background: rgba(255, 255, 255, 0.05); background: rgba(255, 255, 255, 0.05);
@ -41,9 +46,10 @@ const IconWrapper = styled(Box)`
border: 2px solid rgba(255, 255, 255, 0.1); border: 2px solid rgba(255, 255, 255, 0.1);
` `
function Item({ children, desc, icon, linkTo, push }: Props) { function Item({ children, desc, icon, linkTo, push, location }: Props) {
const { pathname } = location
return ( return (
<Container onClick={linkTo ? () => push(linkTo) : void 0}> <Container onClick={linkTo ? () => push(linkTo) : void 0} active={pathname === linkTo}>
<IconWrapper mr={2}>{icon || null}</IconWrapper> <IconWrapper mr={2}>{icon || null}</IconWrapper>
<div> <div>
<Text fontWeight="bold" fontSize={1}> <Text fontWeight="bold" fontSize={1}>
@ -65,4 +71,11 @@ Item.defaultProps = {
linkTo: null, linkTo: null,
} }
export default connect(null, mapDispatchToProps)(Item) export default compose(
withRouter,
// connect router here only to make components re-render
// see https://github.com/ReactTraining/react-router/issues/4671
connect(({ router }) => ({ router }), mapDispatchToProps, null, {
pure: false,
}),
)(Item)

3
webpack.renderer.js

@ -3,6 +3,9 @@ const webpack = require('webpack')
require('./src/globals') require('./src/globals')
module.exports = { module.exports = {
output: {
publicPath: '/',
},
plugins: [ plugins: [
new webpack.DefinePlugin({ new webpack.DefinePlugin({
__DEV__, __DEV__,

Loading…
Cancel
Save