Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(defs): allow gradientTransform to be passed to gradient definitions #1812

Merged
merged 1 commit into from Nov 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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