Skip to content

Commit

Permalink
feat(bump): add TypeScript definitions for AreaBump
Browse files Browse the repository at this point in the history
  • Loading branch information
Raphaël Benitte authored and Raphaël Benitte committed May 16, 2019
1 parent edf72ca commit e70c4cd
Show file tree
Hide file tree
Showing 13 changed files with 312 additions and 36 deletions.
111 changes: 111 additions & 0 deletions packages/bump/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
* This file is part of the nivo project.
*
* Copyright 2016-present, Raphaël Benitte.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
import { Component, MouseEvent } from 'react'
import { Dimensions, Box, Theme, MotionProps, CssMixBlendMode } from '@nivo/core'
import { OrdinalColorsInstruction, InheritedColorProp } from '@nivo/colors'

declare module '@nivo/bump' {
export interface AreaBumpInputDatum {
x: string | number
y: number
[key: string]: any
}

export interface AreaBumpInputSerie {
id: string
data: AreaBumpInputDatum[]
[key: string]: any
}

export interface AreaBumpAreaPoint {
x: number
y0: number
y1: number
}

export interface AreaBumpPoint {
x: number
y: number
height: number
data: AreaBumpInputDatum
}

export interface AreaBumpComputedSerie extends AreaBumpInputSerie {
color: string
style: {
fillOpacity: number
borderWidth: number
borderColor: string
borderOpacity: number
}
points: AreaBumpPoint[]
areaPoints: AreaBumpAreaPoint[]
}

export type AreaBumpAlign = 'start' | 'middle' | 'end'
export type AreaBumpInterpolation = 'smooth' | 'linear'

export type AreaBumpLayerType = 'grid' | 'axes' | 'labels' | 'areas'

export type AreaBumpLabelFunction = (serie: AreaBumpComputedSerie) => string
export type AreaBumpLabel = false | string | AreaBumpLabelFunction

export type AreaBumpMouseHandler = (
serie: AreaBumpComputedSerie,
event: MouseEvent<any>
) => void

export type AreaBumpProps = {
data: AreaBumpInputSerie[]

margin?: Box

align?: AreaBumpAlign
interpolation?: AreaBumpInterpolation
spacing?: number
xPadding?: number

theme?: Theme
colors?: OrdinalColorsInstruction
blendMode?: CssMixBlendMode
fillOpacity?: number
activeFillOpacity?: number
inactiveFillOpacity?: number
borderWidth?: number
activeBorderWidth?: number
inactiveBorderWidth?: number
borderColor?: InheritedColorProp
borderOpacity?: number
activeBorderOpacity?: number
inactiveBorderOpacity?: number

startLabel?: AreaBumpLabel
startLabelPadding?: number
startLabelTextColor?: InheritedColorProp
endLabel?: AreaBumpLabel
endLabelPadding?: number
endLabelTextColor?: InheritedColorProp

enableGridX?: boolean
axisTop?: any
axisBottom?: any

isInteractive?: boolean
onMouseEnter?: AreaBumpMouseHandler
onMouseMove?: AreaBumpMouseHandler
onMouseLeave?: AreaBumpMouseHandler
onClick?: AreaBumpMouseHandler
tooltip?: any
}

export type AreaBumpSvgProps = AreaBumpProps & MotionProps

export class AreaBump extends Component<AreaBumpSvgProps & Dimensions> {}
export class ResponsiveAreaBump extends Component<AreaBumpSvgProps> {}
}
1 change: 1 addition & 0 deletions packages/bump/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
],
"main": "./dist/nivo-bump.cjs.js",
"module": "./dist/nivo-bump.esm.js",
"typings": "./index.d.ts",
"files": [
"README.md",
"LICENSE.md",
Expand Down
3 changes: 3 additions & 0 deletions packages/bump/src/area-bump/AnimatedArea.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const AnimatedArea = ({
onMouseEnter,
onMouseMove,
onMouseLeave,
onClick,
}) => {
const { springConfig } = useMotionConfig()

Expand All @@ -42,6 +43,7 @@ const AnimatedArea = ({
onMouseEnter={onMouseEnter}
onMouseMove={onMouseMove}
onMouseLeave={onMouseLeave}
onClick={onClick}
/>
)}
</SmartMotion>
Expand All @@ -64,6 +66,7 @@ AnimatedArea.propTypes = {
onMouseEnter: PropTypes.func,
onMouseMove: PropTypes.func,
onMouseLeave: PropTypes.func,
onClick: PropTypes.func,
}

export default memo(AnimatedArea)
65 changes: 37 additions & 28 deletions packages/bump/src/area-bump/Area.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,35 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
import React, { memo, useCallback } from 'react'
import React, { memo } from 'react'
import PropTypes from 'prop-types'
import { useMotionConfig, blendModePropType } from '@nivo/core'
import { useTooltip } from '@nivo/tooltip'
import AreaTooltip from './AreaTooltip'
import { useSerieHandlers } from './hooks'
import AnimatedArea from './AnimatedArea'
import StaticArea from './StaticArea'

const Area = ({ serie, areaGenerator, blendMode, isInteractive, setCurrentSerie }) => {
const { showTooltipFromEvent, hideTooltip } = useTooltip()
const onMouseEnter = useCallback(
event => {
showTooltipFromEvent(<AreaTooltip serie={serie} />, event)
setCurrentSerie(serie.id)
},
[serie, showTooltipFromEvent, setCurrentSerie]
)
const onMouseMove = useCallback(
event => {
showTooltipFromEvent(<AreaTooltip serie={serie} />, event)
},
[serie, showTooltipFromEvent]
)
const onMouseLeave = useCallback(() => {
hideTooltip()
setCurrentSerie(null)
}, [hideTooltip, setCurrentSerie])
const Area = ({
serie,
areaGenerator,
blendMode,
isInteractive,
onMouseEnter,
onMouseMove,
onMouseLeave,
onClick,
setCurrentSerie,
tooltip,
}) => {
const handlers = useSerieHandlers({
serie,
isInteractive,
onMouseEnter,
onMouseMove,
onMouseLeave,
onClick,
setCurrent: setCurrentSerie,
tooltip,
})

const { animate } = useMotionConfig()
if (animate === true) {
Expand All @@ -41,9 +43,10 @@ const Area = ({ serie, areaGenerator, blendMode, isInteractive, setCurrentSerie
serie={serie}
areaGenerator={areaGenerator}
blendMode={blendMode}
onMouseEnter={isInteractive ? onMouseEnter : undefined}
onMouseMove={isInteractive ? onMouseMove : undefined}
onMouseLeave={isInteractive ? onMouseLeave : undefined}
onMouseEnter={handlers.onMouseEnter}
onMouseMove={handlers.onMouseMove}
onMouseLeave={handlers.onMouseLeave}
onClick={handlers.onClick}
/>
)
}
Expand All @@ -53,9 +56,10 @@ const Area = ({ serie, areaGenerator, blendMode, isInteractive, setCurrentSerie
serie={serie}
areaGenerator={areaGenerator}
blendMode={blendMode}
onMouseEnter={isInteractive ? onMouseEnter : undefined}
onMouseMove={isInteractive ? onMouseMove : undefined}
onMouseLeave={isInteractive ? onMouseLeave : undefined}
onMouseEnter={handlers.onMouseEnter}
onMouseMove={handlers.onMouseMove}
onMouseLeave={handlers.onMouseLeave}
onClick={handlers.onClick}
/>
)
}
Expand All @@ -73,7 +77,12 @@ Area.propTypes = {
areaGenerator: PropTypes.func.isRequired,
blendMode: blendModePropType.isRequired,
isInteractive: PropTypes.bool.isRequired,
onMouseEnter: PropTypes.func,
onMouseMove: PropTypes.func,
onMouseLeave: PropTypes.func,
onClick: PropTypes.func,
setCurrentSerie: PropTypes.func.isRequired,
tooltip: PropTypes.oneOfType([PropTypes.func, PropTypes.object]).isRequired,
}

export default memo(Area)
10 changes: 10 additions & 0 deletions packages/bump/src/area-bump/AreaBump.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ const AreaBump = props => {
axisBottom,

isInteractive,
onMouseEnter,
onMouseMove,
onMouseLeave,
onClick,
tooltip,
} = props

const [currentSerie, setCurrentSerie] = useState(null)
Expand Down Expand Up @@ -112,6 +117,11 @@ const AreaBump = props => {
blendMode={blendMode}
isInteractive={isInteractive}
setCurrentSerie={setCurrentSerie}
onMouseEnter={onMouseEnter}
onMouseMove={onMouseMove}
onMouseLeave={onMouseLeave}
onClick={onClick}
tooltip={tooltip}
/>
))}
</Fragment>
Expand Down
3 changes: 3 additions & 0 deletions packages/bump/src/area-bump/StaticArea.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const StaticArea = ({
onMouseEnter,
onMouseMove,
onMouseLeave,
onClick,
}) => {
return (
<path
Expand All @@ -30,6 +31,7 @@ const StaticArea = ({
onMouseEnter={onMouseEnter}
onMouseMove={onMouseMove}
onMouseLeave={onMouseLeave}
onClick={onClick}
/>
)
}
Expand All @@ -50,6 +52,7 @@ StaticArea.propTypes = {
onMouseEnter: PropTypes.func,
onMouseMove: PropTypes.func,
onMouseLeave: PropTypes.func,
onClick: PropTypes.func,
}

export default memo(StaticArea)
61 changes: 60 additions & 1 deletion packages/bump/src/area-bump/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
import { useMemo } from 'react'
import React, { useMemo, useCallback } from 'react'
import { area as d3Area, curveBasis, curveLinear } from 'd3-shape'
import { useTheme } from '@nivo/core'
import { useOrdinalColorScale, useInheritedColor } from '@nivo/colors'
import { useTooltip } from '@nivo/tooltip'
import { computeSeries } from './compute'

export const useAreaBumpSeries = ({ data, width, height, align, spacing, xPadding }) =>
Expand Down Expand Up @@ -174,3 +175,61 @@ export const useAreaBump = ({
areaGenerator,
}
}

export const useSerieHandlers = ({
serie,
isInteractive,
onMouseEnter,
onMouseMove,
onMouseLeave,
onClick,
setCurrent,
tooltip,
}) => {
const { showTooltipFromEvent, hideTooltip } = useTooltip()

const handleMouseEnter = useCallback(
event => {
showTooltipFromEvent(React.createElement(tooltip, { serie }), event)
setCurrent(serie.id)
onMouseEnter && onMouseEnter(serie, event)
},
[serie, onMouseEnter, showTooltipFromEvent, setCurrent]
)

const handleMouseMove = useCallback(
event => {
showTooltipFromEvent(React.createElement(tooltip, { serie }), event)
onMouseMove && onMouseMove(serie, event)
},
[serie, onMouseMove, showTooltipFromEvent]
)

const handleMouseLeave = useCallback(
event => {
hideTooltip()
setCurrent(null)
onMouseLeave && onMouseLeave(serie, event)
},
[serie, onMouseLeave, hideTooltip, setCurrent]
)

const handleClick = useCallback(
event => {
onClick && onClick(serie, event)
},
[serie, onClick]
)

const handlers = useMemo(
() => ({
onMouseEnter: isInteractive ? handleMouseEnter : undefined,
onMouseMove: isInteractive ? handleMouseMove : undefined,
onMouseLeave: isInteractive ? handleMouseLeave : undefined,
onClick: isInteractive ? handleClick : undefined,
}),
[isInteractive, handleMouseEnter, handleMouseMove, handleMouseLeave, handleClick]
)

return handlers
}
Loading

0 comments on commit e70c4cd

Please sign in to comment.