Skip to content

Commit

Permalink
feat(funnel): add support for custom event handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
plouc committed Jun 17, 2020
1 parent 9fca13c commit bbdbc37
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 13 deletions.
14 changes: 6 additions & 8 deletions packages/funnel/src/Part.js
Expand Up @@ -10,11 +10,12 @@ import React from 'react'
import PropTypes from 'prop-types'
import { useSpring, animated, config } from 'react-spring'

export const Part = ({ part, areaGenerator, borderGenerator, setCurrentPartId }) => {
export const Part = ({ part, areaGenerator, borderGenerator }) => {
const animatedProps = useSpring({
areaPath: areaGenerator(part.areaPoints),
areaColor: part.color,
borderPath: borderGenerator(part.borderPoints),
borderWidth: part.borderWidth,
borderColor: part.borderColor,
config: config.wobbly,
})
Expand All @@ -25,7 +26,7 @@ export const Part = ({ part, areaGenerator, borderGenerator, setCurrentPartId })
<animated.path
d={animatedProps.borderPath}
stroke={animatedProps.borderColor}
strokeWidth={part.borderWidth}
strokeWidth={animatedProps.borderWidth}
strokeOpacity={part.borderOpacity}
fill="none"
/>
Expand All @@ -34,12 +35,9 @@ export const Part = ({ part, areaGenerator, borderGenerator, setCurrentPartId })
d={animatedProps.areaPath}
fill={animatedProps.areaColor}
fillOpacity={part.fillOpacity}
onMouseEnter={() => {
setCurrentPartId(part.data.id)
}}
onMouseLeave={() => {
setCurrentPartId(null)
}}
onMouseEnter={part.onMouseEnter}
onMouseLeave={part.onMouseLeave}
onClick={part.onClick}
/>
</>
)
Expand Down
3 changes: 1 addition & 2 deletions packages/funnel/src/Parts.js
Expand Up @@ -9,13 +9,12 @@
import React from 'react'
import { Part } from './Part'

export const Parts = ({ parts, areaGenerator, borderGenerator, setCurrentPartId }) =>
export const Parts = ({ parts, areaGenerator, borderGenerator }) =>
parts.map(part => (
<Part
key={part.data.id}
part={part}
areaGenerator={areaGenerator}
borderGenerator={borderGenerator}
setCurrentPartId={setCurrentPartId}
/>
))
13 changes: 11 additions & 2 deletions packages/funnel/src/props.js
Expand Up @@ -9,6 +9,7 @@
import PropTypes from 'prop-types'
import { ordinalColorsPropType, inheritedColorPropType } from '@nivo/colors'
import { motionPropTypes } from '@nivo/core'
import { annotationSpecPropType } from '@nivo/annotations'

export const FunnelPropTypes = {
data: PropTypes.arrayOf(
Expand All @@ -20,7 +21,10 @@ export const FunnelPropTypes = {
).isRequired,

layers: PropTypes.arrayOf(
PropTypes.oneOfType([PropTypes.oneOf(['separators', 'parts', 'labels']), PropTypes.func])
PropTypes.oneOfType([
PropTypes.oneOf(['separators', 'parts', 'labels', 'annotations']),
PropTypes.func,
])
).isRequired,

direction: PropTypes.oneOf(['horizontal', 'vertical']).isRequired,
Expand All @@ -45,8 +49,11 @@ export const FunnelPropTypes = {
afterSeparatorLength: PropTypes.number.isRequired,
afterSeparatorOffset: PropTypes.number.isRequired,

annotations: PropTypes.arrayOf(annotationSpecPropType).isRequired,

isInteractive: PropTypes.bool.isRequired,
currentPartSizeExtension: PropTypes.number.isRequired,
currentBorderWidth: PropTypes.number,
onClick: PropTypes.func,
onMouseEnter: PropTypes.func,
onMouseLeave: PropTypes.func,
Expand All @@ -55,7 +62,7 @@ export const FunnelPropTypes = {
}

export const FunnelDefaultProps = {
layers: ['separators', 'parts', 'labels'],
layers: ['separators', 'parts', 'labels', 'annotations'],

direction: 'vertical',
interpolation: 'smooth',
Expand All @@ -79,6 +86,8 @@ export const FunnelDefaultProps = {
afterSeparatorLength: 0,
afterSeparatorOffset: 0,

annotations: [],

isInteractive: true,
currentPartSizeExtension: 0,

Expand Down
58 changes: 58 additions & 0 deletions website/src/data/components/funnel/props.js
Expand Up @@ -295,6 +295,64 @@ const props = [
defaultValue: defaults.isInteractive,
controlType: 'switch',
},
{
key: 'currentPartSizeExtension',
help: `
Expand part size by this amount of pixels on each side
when it's active
`,
required: false,
defaultValue: defaults.currentPartSizeExtension,
type: 'number',
controlType: 'range',
group: 'Interactivity',
controlOptions: {
unit: 'px',
min: 0,
max: 100,
},
},
{
key: 'currentBorderWidth',
help: `Override default border width when a part is active.`,
required: false,
type: 'number',
controlType: 'range',
group: 'Interactivity',
controlOptions: {
unit: 'px',
min: 0,
max: 100,
},
},
{
key: 'onMouseEnter',
group: 'Interactivity',
help: 'onMouseEnter handler.',
type: '(part, event) => void',
required: false,
},
{
key: 'onMouseMove',
group: 'Interactivity',
help: 'onMouseMove handler.',
type: '(part, event) => void',
required: false,
},
{
key: 'onMouseLeave',
group: 'Interactivity',
help: 'onMouseLeave handler.',
type: '(part, event) => void',
required: false,
},
{
key: 'onClick',
group: 'Interactivity',
help: 'onClick handler.',
type: '(part, event) => void',
required: false,
},
]

export const groups = groupProperties(props)
3 changes: 2 additions & 1 deletion website/src/pages/funnel/index.js
Expand Up @@ -47,7 +47,8 @@ const initialProperties = {
afterSeparatorOffset: 20,

isInteractive: true,
currentPartSizeExtension: 30,
currentPartSizeExtension: 10,
currentBorderWidth: 40,

animate: true,
motionStiffness: 90,
Expand Down

0 comments on commit bbdbc37

Please sign in to comment.