Skip to content

Commit

Permalink
feat(defs): allow gradientTransform to be passed to gradient definiti…
Browse files Browse the repository at this point in the history
…ons, fixes #1098 (#1812)
  • Loading branch information
benjamintd committed Nov 8, 2021
1 parent 0b3bdb0 commit 76062b5
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 8 deletions.
1 change: 1 addition & 0 deletions packages/core/index.d.ts
Expand Up @@ -255,6 +255,7 @@ declare module '@nivo/core' {
color: string
opacity?: number
}[]
gradientTransform?: string
}

export type PatternDotsDef = {
Expand Down
5 changes: 3 additions & 2 deletions packages/core/src/components/defs/gradients/LinearGradient.js
Expand Up @@ -8,8 +8,8 @@
*/
import PropTypes from 'prop-types'

export const LinearGradient = ({ id, colors }) => (
<linearGradient id={id} x1={0} x2={0} y1={0} y2={1}>
export const LinearGradient = ({ id, colors, ...rest }) => (
<linearGradient id={id} x1={0} x2={0} y1={0} y2={1} {...rest}>
{colors.map(({ offset, color, opacity }) => (
<stop
key={offset}
Expand All @@ -30,6 +30,7 @@ LinearGradient.propTypes = {
opacity: PropTypes.number,
})
).isRequired,
gradientTransform: PropTypes.string,
}

export const linearGradientDef = (id, colors, options = {}) => ({
Expand Down
18 changes: 18 additions & 0 deletions packages/core/tests/lib/defs.test.js
Expand Up @@ -163,6 +163,24 @@ describe('bindDefs()', () => {
])
})

it('should allow gradientTransform to be used', () => {
const defs = [
{
id: 'gradient',
type: 'linearGradient',
colors: [],
gradientTransform: 'rotate(90 0.5 0.5)',
},
]
const nodes = [{}, {}]

const boundDefs = bindDefs(defs, nodes, [{ match: '*', id: 'gradient' }], {
targetKey: 'style.fill',
})

expect(boundDefs).toEqual(defs)
})

it('should support inheritance', () => {
const defs = [
{
Expand Down
14 changes: 9 additions & 5 deletions packages/treemap/stories/treemap.stories.js
Expand Up @@ -46,11 +46,15 @@ stories.add('patterns & gradients', () => (
modifiers: [['darker', 3]],
}}
defs={[
linearGradientDef('gradient', [
{ offset: 0, color: '#ffffff' },
{ offset: 15, color: 'inherit' },
{ offset: 100, color: 'inherit' },
]),
linearGradientDef(
'gradient',
[
{ offset: 0, color: '#ffffff' },
{ offset: 15, color: 'inherit' },
{ offset: 100, color: 'inherit' },
],
{ gradientTransform: 'rotate(-90 0.5 0.5)' }
),
patternDotsDef('pattern', {
background: 'inherit',
color: '#ffffff',
Expand Down
8 changes: 7 additions & 1 deletion website/src/components/guides/gradients/GradientsExample.js
Expand Up @@ -20,7 +20,13 @@ const MyChart = () => (
linearGradientDef('gradientB', [
{ offset: 0, color: '#000' },
{ offset: 100, color: 'inherit' },
]),
],
// you may specify transforms for your gradients, e.g. rotations and skews,
// following the transform attribute format.
// For instance here we rotate 90 degrees relative to the center of the object.
{
gradientTransform: 'rotate(90 0.5 0.5)'
}),
// using plain object
{
id: 'gradientC',
Expand Down

0 comments on commit 76062b5

Please sign in to comment.