/
PatternsDotsDemo.tsx
105 lines (99 loc) · 2.55 KB
/
PatternsDotsDemo.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import React from 'react'
import {
Defs,
// @ts-ignore
patternDotsDef,
// @ts-ignore
PatternDots,
} from '@nivo/core'
import { ChartProperty } from '../../../types'
import { GuideDemoBlock } from '../GuideDemoBlock'
const defaults = (PatternDots as unknown as any).defaultProps as Settings
const SAMPLE_SIZE = 120
const patternId = 'dots-pattern'
interface Settings {
size: number
padding: number
stagger: boolean
background: string
color: string
}
const initialSettings: Settings = {
size: defaults.size,
padding: defaults.padding,
stagger: defaults.stagger,
background: defaults.background,
color: defaults.color,
}
const controls: ChartProperty[] = [
{
name: 'size',
type: 'number',
help: 'dots size.',
defaultValue: defaults.size,
control: {
type: 'range',
unit: 'px',
min: 1,
max: 24,
},
},
{
name: 'padding',
type: 'number',
help: 'padding between dots.',
defaultValue: defaults.padding,
control: {
type: 'range',
unit: 'px',
min: 0,
max: 36,
},
},
{
name: 'stagger',
type: 'boolean',
help: 'staggered dots.',
defaultValue: defaults.stagger,
control: { type: 'switch' },
},
{
name: 'background',
type: 'string',
help: 'pattern background color.',
defaultValue: defaults.background,
control: { type: 'colorPicker' },
},
{
name: 'color',
type: 'string',
help: 'dots color.',
defaultValue: defaults.color,
control: { type: 'colorPicker' },
},
]
const generateCode = (settings: Settings) =>
`
// helper
import { patternDotsDef } from '@nivo/core'
patternDotsDef('${patternId}', ${JSON.stringify(settings, null, ' ')})
// plain object
${JSON.stringify(patternDotsDef(patternId, settings), null, ' ')}
`.trim()
export const PatternsDotsDemo = () => {
return (
<GuideDemoBlock<Settings>
title="Dots"
controls={controls}
initialSettings={initialSettings}
generateCode={generateCode}
>
{settings => (
<svg width={SAMPLE_SIZE} height={SAMPLE_SIZE}>
<Defs defs={[patternDotsDef(patternId, settings)]} />
<rect width={SAMPLE_SIZE} height={SAMPLE_SIZE} fill={`url(#${patternId})`} />
</svg>
)}
</GuideDemoBlock>
)
}