Skip to content

Commit

Permalink
feat(marimekko): improve motion configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
plouc committed Nov 16, 2020
1 parent 66d2f4f commit 7df0580
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 8 deletions.
11 changes: 10 additions & 1 deletion packages/core/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,18 @@ declare module '@nivo/core' {

export type ModernMotionProps = Partial<{
animate: boolean
motionConfig: string
motionConfig: string | SpringConfig
}>

export function useMotionConfig(): {
animate: boolean
config: SpringConfig
springConfig: {
stiffness: number
damping: number
}
}

export type SvgFillMatcher<T> = (datum: T) => boolean
export interface SvgDefsAndFill<T> {
defs?: {
Expand Down
9 changes: 6 additions & 3 deletions packages/marimekko/src/Bars.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from 'react'
import { useTransition, config } from 'react-spring'
import { useTransition } from 'react-spring'
import { useMotionConfig } from '@nivo/core'
import { BarDatum, MouseEventHandlers } from './types'
import { Bar } from './Bar'

Expand All @@ -16,6 +17,8 @@ export const Bars = <RawDatum,>({
onMouseMove,
onMouseLeave,
}: BarsProps<RawDatum>) => {
const { animate, config: springConfig } = useMotionConfig()

const transition = useTransition<
BarDatum<RawDatum>,
{
Expand Down Expand Up @@ -73,8 +76,8 @@ export const Bars = <RawDatum,>({
height: bar.height,
color: bar.color,
}),
config: config.wobbly,
//immediate: !animate,
config: springConfig,
immediate: !animate,
})

return (
Expand Down
7 changes: 6 additions & 1 deletion packages/marimekko/src/Marimekko.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,12 @@ export const Marimekko = <RawDatum,>({
animate = defaultProps.animate,
...otherProps
}: SvgProps<RawDatum>) => (
<Container theme={otherProps.theme} isInteractive={isInteractive} animate={animate}>
<Container
theme={otherProps.theme}
isInteractive={isInteractive}
animate={animate}
motionConfig={otherProps.motionConfig}
>
<InnerMarimekko<RawDatum> isInteractive={isInteractive} animate={animate} {...otherProps} />
</Container>
)
1 change: 1 addition & 0 deletions packages/marimekko/src/props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ export const defaultProps = {
isInteractive: true,

animate: true,
motionConfig: 'gentle',
}
6 changes: 3 additions & 3 deletions packages/marimekko/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as React from 'react'
import { Box, Dimensions, Theme, SvgDefsAndFill } from '@nivo/core'
import { Box, Dimensions, Theme, SvgDefsAndFill, ModernMotionProps } from '@nivo/core'
import { OrdinalColorScaleConfig, InheritedColorConfig } from '@nivo/colors'
import { LegendProps } from '@nivo/legends'

Expand Down Expand Up @@ -95,8 +95,6 @@ export type CommonProps<RawDatum> = {

legends: LegendProps[]

animate: boolean

role: string
}

Expand All @@ -115,6 +113,7 @@ export type MouseEventHandlers<RawDatum, ElementType> = Partial<{
export type SvgProps<RawDatum> = DataProps<RawDatum> &
Dimensions &
Partial<CommonProps<RawDatum>> &
ModernMotionProps &
SvgDefsAndFill<ComputedDatum<RawDatum>> &
MouseEventHandlers<RawDatum, SVGRectElement> & {
layers?: Layer<RawDatum>[]
Expand All @@ -123,6 +122,7 @@ export type SvgProps<RawDatum> = DataProps<RawDatum> &
export type CompleteSvgProps<RawDatum> = DataProps<RawDatum> &
Dimensions &
CommonProps<RawDatum> &
ModernMotionProps &
SvgDefsAndFill<ComputedDatum<RawDatum>> &
MouseEventHandlers<RawDatum, SVGRectElement> & {
layers: Layer<RawDatum>[]
Expand Down
2 changes: 2 additions & 0 deletions website/src/data/components/marimekko/props.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
defsProperties,
groupProperties,
getLegendsProps,
motionProperties,
} from '../../../lib/componentProperties'

const props = [
Expand Down Expand Up @@ -264,6 +265,7 @@ const props = [
controlType: 'switch',
group: 'Interactivity',
},
...motionProperties(['svg'], defaults, 'react-spring'),
{
key: 'legends',
flavors: ['svg', 'canvas'],
Expand Down
3 changes: 3 additions & 0 deletions website/src/pages/marimekko/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ const initialProperties = {
defs: [],
fill: [],

animate: defaultProps.animate,
motionConfig: defaultProps.motionConfig,

legends: [
{
anchor: 'bottom',
Expand Down

0 comments on commit 7df0580

Please sign in to comment.