Skip to content

Commit

Permalink
feat(marimekko): use react-spring@next and add animation support to bars
Browse files Browse the repository at this point in the history
  • Loading branch information
plouc committed Nov 16, 2020
1 parent 5f18b05 commit 001b671
Show file tree
Hide file tree
Showing 18 changed files with 247 additions and 47 deletions.
2 changes: 1 addition & 1 deletion packages/annotations/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"dependencies": {
"@nivo/colors": "0.64.0",
"lodash": "^4.17.11",
"react-spring": "^8.0.27"
"react-spring": "next"
},
"peerDependencies": {
"@nivo/core": "0.63.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/axes/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"d3-format": "^1.4.4",
"d3-time": "^1.0.11",
"d3-time-format": "^2.1.3",
"react-spring": "^8.0.27"
"react-spring": "next"
},
"peerDependencies": {
"@nivo/core": "0.63.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/bullet/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"@nivo/legends": "0.64.0",
"@nivo/tooltip": "0.64.0",
"d3-scale": "^3.0.0",
"react-spring": "^8.0.27"
"react-spring": "next"
},
"devDependencies": {
"@nivo/core": "*",
Expand Down
2 changes: 1 addition & 1 deletion packages/bump/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"@nivo/legends": "0.64.0",
"@nivo/tooltip": "0.64.0",
"d3-shape": "^1.3.5",
"react-spring": "^8.0.27"
"react-spring": "next"
},
"peerDependencies": {
"@nivo/core": "0.63.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"d3-shape": "^1.3.5",
"d3-time-format": "^2.1.3",
"lodash": "^4.17.11",
"react-spring": "^8.0.27",
"react-spring": "next",
"recompose": "^0.30.0",
"resize-observer-polyfill": "^1.5.1"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/funnel/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"dependencies": {
"d3-scale": "^3.0.0",
"d3-shape": "^1.3.5",
"react-spring": "^8.0.27"
"react-spring": "next"
},
"peerDependencies": {
"@nivo/annotations": "0.63.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/heatmap/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"@nivo/colors": "0.64.0",
"@nivo/tooltip": "0.64.0",
"d3-scale": "^3.0.0",
"react-spring": "^8.0.27"
"react-spring": "next"
},
"peerDependencies": {
"@nivo/core": "0.63.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/line/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"@nivo/tooltip": "0.64.0",
"@nivo/voronoi": "0.64.0",
"d3-shape": "^1.3.5",
"react-spring": "^8.0.27"
"react-spring": "next"
},
"peerDependencies": {
"@nivo/core": "0.63.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/marimekko/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"d3-scale": "^3.0.0",
"d3-shape": "^1.3.5",
"lodash": "^4.17.11",
"react-spring": "^8.0.27"
"react-spring": "next"
},
"devDependencies": {
"@nivo/core": "*",
Expand Down
46 changes: 46 additions & 0 deletions packages/marimekko/src/Bar.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import React, { createElement, MouseEvent } from 'react'
import { animated, SpringValues } from 'react-spring'
import { useTooltip } from '@nivo/tooltip'
import { DimensionDatum } from './types'
import { BarTooltip } from './BarTooltip'

interface BarProps<RawDatum> {
datum: DimensionDatum<RawDatum>
borderWidth: number
borderColor: string
animatedProps: SpringValues<{
x: number
y: number
width: number
height: number
opacity: number
color: string
borderColor: string
}>
}

export const Bar = <RawDatum,>({ datum, borderWidth, animatedProps }: BarProps<RawDatum>) => {
const { showTooltipFromEvent, hideTooltip } = useTooltip()

const handle = (event: MouseEvent) => {
showTooltipFromEvent(
createElement<{ datum: DimensionDatum<RawDatum> }>(BarTooltip, { datum }),
event
)
}

return (
<animated.rect
x={animatedProps.x}
y={animatedProps.y}
width={animatedProps.width}
height={animatedProps.height}
fill={animatedProps.color}
stroke={animatedProps.borderColor}
strokeWidth={borderWidth}
onMouseEnter={handle}
onMouseMove={handle}
onMouseLeave={hideTooltip}
/>
)
}
98 changes: 71 additions & 27 deletions packages/marimekko/src/Bars.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import React, { createElement, useMemo, MouseEvent } from 'react'
// import { useTransition } from 'react-spring'
import React, { useMemo } from 'react'
import { useTransition, config } from 'react-spring'
// @ts-ignore
import { useTheme } from '@nivo/core'
import { InheritedColorConfig, useInheritedColor } from '@nivo/colors'
import { useTooltip } from '@nivo/tooltip'
import { ComputedDatum, DimensionDatum } from './types'
import { BarTooltip } from './BarTooltip'
import { Bar } from './Bar'

interface NodesProps<RawDatum> {
interface BarsProps<RawDatum> {
data: ComputedDatum<RawDatum>[]
borderWidth: number
borderColor: InheritedColorConfig<DimensionDatum<RawDatum>>
Expand All @@ -18,7 +17,7 @@ interface BarData<RawDatum> extends DimensionDatum<RawDatum> {
borderColor: string
}

export const Bars = <RawDatum,>({ data, borderWidth, borderColor }: NodesProps<RawDatum>) => {
export const Bars = <RawDatum,>({ data, borderWidth, borderColor }: BarsProps<RawDatum>) => {
const theme = useTheme()
const getBorderColor = useInheritedColor<DimensionDatum<RawDatum>>(borderColor, theme)

Expand All @@ -37,32 +36,77 @@ export const Bars = <RawDatum,>({ data, borderWidth, borderColor }: NodesProps<R
return all
}, [data, borderWidth, getBorderColor])

const { showTooltipFromEvent, hideTooltip } = useTooltip()

const handle = (datum: DimensionDatum<RawDatum>, event: MouseEvent) => {
showTooltipFromEvent(
createElement<{ datum: DimensionDatum<RawDatum> }>(BarTooltip, { datum }),
event
)
}
const transition = useTransition<
BarData<RawDatum>,
{
x: number
y: number
width: number
height: number
color: string
opacity: number
borderColor: string
}
>(allBars, {
key: bar => bar.key,
initial: bar => ({
x: bar.x,
y: bar.y,
width: bar.width,
height: bar.height,
color: bar.color,
opacity: 1,
borderColor: bar.borderColor,
}),
from: bar => ({
x: bar.x,
y: bar.y,
width: bar.width,
height: bar.height,
color: bar.color,
opacity: 0,
borderColor: bar.borderColor,
}),
enter: bar => ({
x: bar.x,
y: bar.y,
width: bar.width,
height: bar.height,
color: bar.color,
opacity: 1,
borderColor: bar.borderColor,
}),
update: bar => ({
x: bar.x,
y: bar.y,
width: bar.width,
height: bar.height,
color: bar.color,
opacity: 1,
borderColor: bar.borderColor,
}),
leave: bar => ({
opacity: 0,
x: bar.x,
y: bar.y,
width: bar.width,
height: bar.height,
color: bar.color,
}),
config: config.wobbly,
//immediate: !animate,
})

return (
<>
{allBars.map(bar => {
{transition((style, bar) => {
return (
<rect
<Bar<RawDatum>
key={bar.key}
id={bar.key}
x={bar.x}
y={bar.y}
width={bar.width}
height={bar.height}
fill={bar.color}
stroke={bar.borderColor}
strokeWidth={borderWidth}
onMouseEnter={event => handle(bar, event)}
onMouseMove={event => handle(bar, event)}
onMouseLeave={hideTooltip}
datum={bar}
borderWidth={borderWidth}
borderColor={bar.borderColor}
animatedProps={style}
/>
)
})}
Expand Down
2 changes: 1 addition & 1 deletion packages/parallel-coordinates/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"@nivo/tooltip": "0.64.0",
"d3-scale": "^3.0.0",
"d3-shape": "^1.3.5",
"react-spring": "^8.0.27"
"react-spring": "next"
},
"peerDependencies": {
"@nivo/core": "0.63.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/radar/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"@nivo/tooltip": "0.64.0",
"d3-scale": "^3.0.0",
"d3-shape": "^1.3.5",
"react-spring": "^8.0.27"
"react-spring": "next"
},
"peerDependencies": {
"@nivo/core": "0.63.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/sankey/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"d3-sankey": "^0.12.1",
"d3-shape": "^1.3.5",
"lodash": "^4.17.11",
"react-spring": "^8.0.27"
"react-spring": "next"
},
"peerDependencies": {
"@nivo/core": "0.63.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/stream/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"@nivo/tooltip": "0.64.0",
"d3-scale": "^3.0.0",
"d3-shape": "^1.3.5",
"react-spring": "^8.0.27"
"react-spring": "next"
},
"peerDependencies": {
"@nivo/core": "0.63.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/tooltip/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"dist/"
],
"dependencies": {
"react-spring": "^8.0.27"
"react-spring": "next"
},
"devDependencies": {
"@nivo/core": "*"
Expand Down
2 changes: 1 addition & 1 deletion packages/treemap/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"@nivo/tooltip": "0.64.0",
"d3-hierarchy": "^1.1.8",
"lodash": "^4.17.11",
"react-spring": "^8.0.27"
"react-spring": "next"
},
"peerDependencies": {
"@nivo/core": "0.63.1",
Expand Down
Loading

0 comments on commit 001b671

Please sign in to comment.