Skip to content

Commit

Permalink
feat(colors): use @nivo/colors for inherited colors for all packages
Browse files Browse the repository at this point in the history
  • Loading branch information
Raphaël Benitte authored and Raphaël Benitte committed Apr 17, 2019
1 parent 9cb6b2a commit e895556
Show file tree
Hide file tree
Showing 49 changed files with 160 additions and 558 deletions.
7 changes: 3 additions & 4 deletions packages/bar/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@ import {
Theme,
MotionProps,
ColorProps,
GetColor,
SvgDefsAndFill,
CartesianMarkerProps,
} from '@nivo/core'
import { OrdinalColorsInstruction } from '@nivo/colors'
import { OrdinalColorsInstruction, InheritedColorProp } from '@nivo/colors'
import { LegendProps } from '@nivo/legends'

declare module '@nivo/bar' {
Expand Down Expand Up @@ -102,10 +101,10 @@ declare module '@nivo/bar' {
enableLabel: boolean
label: string | AccessorFunc
labelFormat: string | LabelFormatter
labelLinkColor: string | GetColor<BarDatumWithColor>
labelLinkColor: InheritedColorProp<BarDatumWithColor>
labelSkipWidth: number
labelSkipHeight: number
labelTextColor: string | GetColor<BarDatumWithColor>
labelTextColor: InheritedColorProp<BarDatumWithColor>

colors: OrdinalColorsInstruction
borderRadius: number
Expand Down
6 changes: 3 additions & 3 deletions packages/bar/tests/__snapshots__/Bar.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4354,7 +4354,7 @@ exports[`should render a basic bar chart 1`] = `
alignmentBaseline="central"
style={
Object {
"fill": "#333333",
"fill": "theme",
"fontFamily": "sans-serif",
"fontSize": 11,
"pointerEvents": "none",
Expand Down Expand Up @@ -4387,7 +4387,7 @@ exports[`should render a basic bar chart 1`] = `
alignmentBaseline="central"
style={
Object {
"fill": "#333333",
"fill": "theme",
"fontFamily": "sans-serif",
"fontSize": 11,
"pointerEvents": "none",
Expand Down Expand Up @@ -4420,7 +4420,7 @@ exports[`should render a basic bar chart 1`] = `
alignmentBaseline="central"
style={
Object {
"fill": "#333333",
"fill": "theme",
"fontFamily": "sans-serif",
"fontSize": 11,
"pointerEvents": "none",
Expand Down
4 changes: 2 additions & 2 deletions packages/bullet/src/props.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,10 @@ const commonDefaultProps = {
titleOffsetY: BulletItem.defaultProps.titleOffsetY,
titleRotation: BulletItem.defaultProps.titleRotation,
rangeBorderWidth: 0,
rangeBorderColor: 'inherit',
rangeBorderColor: { from: 'color' },
measureSize: 0.4,
measureBorderWidth: 0,
measureBorderColor: 'inherit',
measureBorderColor: { from: 'color' },
markerSize: 0.6,
markerColors: BulletItem.defaultProps.markerColors,
axisPosition: BulletItem.defaultProps.axisPosition,
Expand Down
9 changes: 9 additions & 0 deletions packages/colors/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,13 @@ declare module '@nivo/colors' {
instruction: OrdinalColorsInstruction,
identity: string | ColorIdentityFunction<D>
): OrdinalColorScale<D>

export type ColorModifierType = 'brighter' | 'darker' | 'opacity'
export type ColorModifier = [ColorModifierType, number]
export type InheritedColorFunction<D = any> = (datum: D) => string
export type InheritedColorProp<D = any> =
| string
| { theme: string }
| { from: string; modifiers?: ColorModifier[] }
| InheritedColorFunction<D>
}
1 change: 0 additions & 1 deletion packages/core/src/lib/colors/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -302,5 +302,4 @@ export const getColorScale = (colors, dataScale) => {
return () => colors
}

export * from './inherit'
export * from './quantize'
64 changes: 0 additions & 64 deletions packages/core/src/lib/colors/inherit.js

This file was deleted.

80 changes: 0 additions & 80 deletions packages/core/tests/lib/colors/colors.test.js

This file was deleted.

1 change: 1 addition & 0 deletions packages/geo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"index.d.ts"
],
"dependencies": {
"@nivo/colors": "0.55.0",
"@nivo/core": "0.55.0",
"@nivo/legends": "0.55.0",
"d3-format": "^1.3.2",
Expand Down
9 changes: 4 additions & 5 deletions packages/geo/src/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ import {
geoNaturalEarth1,
geoGraticule,
} from 'd3-geo'
import { guessQuantizeColorScale } from '@nivo/core'
import { guessQuantizeColorScale, useTheme } from '@nivo/core'
import { useInheritedColor } from '@nivo/colors'
import { useQuantizeColorScaleLegendData } from '@nivo/legends'

export const projectionById = {
Expand Down Expand Up @@ -69,14 +70,12 @@ export const useGeoMap = ({
const path = useMemo(() => geoPath(projection), [projection])
const graticule = useMemo(() => geoGraticule())

const theme = useTheme()
const getBorderWidth = useMemo(
() => (typeof borderWidth === 'function' ? borderWidth : () => borderWidth),
[borderWidth]
)
const getBorderColor = useMemo(
() => (typeof borderColor === 'function' ? borderColor : () => borderColor),
[borderColor]
)
const getBorderColor = useInheritedColor(borderColor, theme)
const getFillColor = useMemo(
() => (typeof fillColor === 'function' ? fillColor : () => fillColor),
[fillColor]
Expand Down
3 changes: 2 additions & 1 deletion packages/geo/src/props.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/
import PropTypes from 'prop-types'
import { quantizeColorScalePropType } from '@nivo/core'
import { inheritedColorPropType } from '@nivo/colors'
import { projectionById } from './hooks'
import ChoroplethTooltip from './ChoroplethTooltip'

Expand All @@ -28,7 +29,7 @@ const commonPropTypes = {

fillColor: PropTypes.oneOfType([PropTypes.string, PropTypes.func]).isRequired,
borderWidth: PropTypes.oneOfType([PropTypes.number, PropTypes.func]).isRequired,
borderColor: PropTypes.oneOfType([PropTypes.string, PropTypes.func]).isRequired,
borderColor: inheritedColorPropType.isRequired,

enableGraticule: PropTypes.bool.isRequired,
graticuleLineWidth: PropTypes.number.isRequired,
Expand Down
7 changes: 4 additions & 3 deletions packages/heatmap/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as React from 'react'
import { Dimensions, Box, MotionProps, ColorProps, GetColor, Theme } from '@nivo/core'
import { Dimensions, Box, MotionProps, ColorProps, Theme } from '@nivo/core'
import { Axis } from '@nivo/axes'
import { InheritedColorProp } from '@nivo/colors'

declare module '@nivo/heatmap' {
export interface HeatMapDatum {
Expand Down Expand Up @@ -33,7 +34,7 @@ declare module '@nivo/heatmap' {
cellShape: 'rect' | 'circle' | React.StatelessComponent<any>
cellOpacity: number
cellBorderWidth: number
cellBorderColor: string | GetColor<HeatMapDatumWithColor>
cellBorderColor: InheritedColorProp<HeatMapDatumWithColor>

axisTop: Axis
axisRight: Axis
Expand All @@ -44,7 +45,7 @@ declare module '@nivo/heatmap' {
enableGridY: boolean

enableLabels: boolean
labelTextColor: string | GetColor<HeatMapDatumWithColor>
labelTextColor: InheritedColorProp<HeatMapDatumWithColor>

isInteractive: boolean
hoverTarget: 'cell' | 'row' | 'column' | 'rowColumn'
Expand Down
19 changes: 12 additions & 7 deletions packages/heatmap/src/enhance.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,14 @@ import withPropsOnChange from 'recompose/withPropsOnChange'
import withState from 'recompose/withState'
import pure from 'recompose/pure'
import { scaleOrdinal, scaleLinear } from 'd3-scale'
import { withTheme, withDimensions, withMotion } from '@nivo/core'
import { getInheritedColorGenerator, guessQuantizeColorScale } from '@nivo/core'
import { getAccessorFor } from '@nivo/core'
import {
withTheme,
withDimensions,
withMotion,
guessQuantizeColorScale,
getAccessorFor,
} from '@nivo/core'
import { getInheritedColorGenerator } from '@nivo/colors'
import { HeatMapDefaultProps } from './props'

const computeX = (column, cellWidth, padding) => {
Expand Down Expand Up @@ -127,11 +132,11 @@ export default Component =>
return { sizeScale }
}
),
withPropsOnChange(['cellBorderColor'], ({ cellBorderColor }) => ({
getCellBorderColor: getInheritedColorGenerator(cellBorderColor),
withPropsOnChange(['cellBorderColor', 'theme'], ({ cellBorderColor, theme }) => ({
getCellBorderColor: getInheritedColorGenerator(cellBorderColor, theme),
})),
withPropsOnChange(['labelTextColor'], ({ labelTextColor }) => ({
getLabelTextColor: getInheritedColorGenerator(labelTextColor),
withPropsOnChange(['labelTextColor', 'theme'], ({ labelTextColor, theme }) => ({
getLabelTextColor: getInheritedColorGenerator(labelTextColor, theme),
})),
pure
)(Component)
9 changes: 5 additions & 4 deletions packages/heatmap/src/props.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/
import PropTypes from 'prop-types'
import { quantizeColorScalePropType, noop } from '@nivo/core'
import { inheritedColorPropType } from '@nivo/colors'
import { axisPropType } from '@nivo/axes'

export const HeatMapPropTypes = {
Expand All @@ -27,7 +28,7 @@ export const HeatMapPropTypes = {
.isRequired,
cellOpacity: PropTypes.number.isRequired,
cellBorderWidth: PropTypes.number.isRequired,
cellBorderColor: PropTypes.oneOfType([PropTypes.string, PropTypes.func]).isRequired,
cellBorderColor: inheritedColorPropType.isRequired,
getCellBorderColor: PropTypes.func.isRequired, // computed

axisTop: axisPropType,
Expand All @@ -39,7 +40,7 @@ export const HeatMapPropTypes = {
enableGridY: PropTypes.bool.isRequired,

enableLabels: PropTypes.bool.isRequired,
labelTextColor: PropTypes.oneOfType([PropTypes.string, PropTypes.func]).isRequired,
labelTextColor: inheritedColorPropType.isRequired,
getLabelTextColor: PropTypes.func.isRequired, // computed

colors: quantizeColorScalePropType.isRequired,
Expand Down Expand Up @@ -71,7 +72,7 @@ export const HeatMapDefaultProps = {
cellShape: 'rect',
cellOpacity: 0.85,
cellBorderWidth: 0,
cellBorderColor: 'inherit',
cellBorderColor: { from: 'color' },

// axes & grid
axisTop: {},
Expand All @@ -81,7 +82,7 @@ export const HeatMapDefaultProps = {

// labels
enableLabels: true,
labelTextColor: 'inherit:darker(1.4)',
labelTextColor: { from: 'color', modifiers: [['darker', 1.4]] },

// theming
colors: 'nivo',
Expand Down
Loading

0 comments on commit e895556

Please sign in to comment.