@marigold/system 0.1.0 → 0.3.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +57 -0
- package/dist/Box.d.ts +14 -0
- package/dist/Global.d.ts +2 -0
- package/dist/SVG.d.ts +6 -0
- package/dist/SVG.stories.d.ts +5 -0
- package/dist/index.d.ts +6 -3
- package/dist/normalize.d.ts +144 -0
- package/dist/system.cjs.development.js +350 -144
- package/dist/system.cjs.development.js.map +1 -1
- package/dist/system.cjs.production.min.js +1 -1
- package/dist/system.cjs.production.min.js.map +1 -1
- package/dist/system.esm.js +342 -140
- package/dist/system.esm.js.map +1 -1
- package/dist/theme.d.ts +136 -0
- package/dist/types.d.ts +1 -2
- package/dist/useTheme.d.ts +11 -5
- package/dist/variant.d.ts +29 -0
- package/package.json +4 -6
- package/src/Box.test.tsx +308 -0
- package/src/Box.tsx +199 -0
- package/src/Global.test.tsx +57 -0
- package/src/Global.tsx +34 -0
- package/src/SVG.stories.tsx +48 -0
- package/src/SVG.test.tsx +82 -0
- package/src/SVG.tsx +24 -0
- package/src/index.ts +6 -3
- package/src/normalize.test.tsx +15 -0
- package/src/normalize.ts +100 -0
- package/src/theme.ts +157 -0
- package/src/types.ts +0 -2
- package/src/useTheme.test.tsx +22 -14
- package/src/useTheme.tsx +37 -9
- package/src/variant.test.ts +93 -0
- package/src/variant.ts +54 -0
- package/dist/cache.d.ts +0 -4
- package/dist/reset.d.ts +0 -24
- package/dist/useClassname.d.ts +0 -2
- package/dist/useStyles.d.ts +0 -15
- package/src/Colors.stories.mdx +0 -455
- package/src/cache.ts +0 -4
- package/src/concepts-principles.mdx +0 -84
- package/src/reset.ts +0 -108
- package/src/useClassname.test.tsx +0 -70
- package/src/useClassname.ts +0 -23
- package/src/useStyles.test.tsx +0 -286
- package/src/useStyles.ts +0 -63
- package/src/writeComponent.stories.mdx +0 -126
|
@@ -1,70 +0,0 @@
|
|
|
1
|
-
import React from 'react';
|
|
2
|
-
import { renderHook } from '@testing-library/react-hooks';
|
|
3
|
-
import { useClassname } from './useClassname';
|
|
4
|
-
import { ThemeProvider } from './useTheme';
|
|
5
|
-
|
|
6
|
-
// Setup
|
|
7
|
-
// ---------------
|
|
8
|
-
const theme = {
|
|
9
|
-
colors: {
|
|
10
|
-
primary: 'hotpink',
|
|
11
|
-
black: '#000',
|
|
12
|
-
},
|
|
13
|
-
sizes: [0, 1, 2],
|
|
14
|
-
text: {
|
|
15
|
-
body: {
|
|
16
|
-
fontSize: 1,
|
|
17
|
-
color: 'black',
|
|
18
|
-
},
|
|
19
|
-
heading: {
|
|
20
|
-
fontSize: 3,
|
|
21
|
-
color: 'primary',
|
|
22
|
-
},
|
|
23
|
-
},
|
|
24
|
-
};
|
|
25
|
-
|
|
26
|
-
const wrapper: React.FC = ({ children }) => (
|
|
27
|
-
<ThemeProvider theme={theme}>{children}</ThemeProvider>
|
|
28
|
-
);
|
|
29
|
-
|
|
30
|
-
test('create a string classname', () => {
|
|
31
|
-
const { result } = renderHook(() => useClassname({ color: 'primary' }), {
|
|
32
|
-
wrapper,
|
|
33
|
-
});
|
|
34
|
-
expect(result.current).toEqual(expect.any(String));
|
|
35
|
-
});
|
|
36
|
-
|
|
37
|
-
test('create classnames from multiple intpus', () => {
|
|
38
|
-
const { result } = renderHook(
|
|
39
|
-
() => useClassname({ color: 'primary' }, { p: 2 }),
|
|
40
|
-
{
|
|
41
|
-
wrapper,
|
|
42
|
-
}
|
|
43
|
-
);
|
|
44
|
-
expect(result.current).toEqual(expect.any(String));
|
|
45
|
-
});
|
|
46
|
-
|
|
47
|
-
test('create a unique classnames', () => {
|
|
48
|
-
const { result: first } = renderHook(
|
|
49
|
-
() => useClassname({ color: 'primary' }),
|
|
50
|
-
{
|
|
51
|
-
wrapper,
|
|
52
|
-
}
|
|
53
|
-
);
|
|
54
|
-
const { result: second } = renderHook(
|
|
55
|
-
() => useClassname({ color: 'black' }),
|
|
56
|
-
{
|
|
57
|
-
wrapper,
|
|
58
|
-
}
|
|
59
|
-
);
|
|
60
|
-
expect(first.current).not.toEqual(second.current);
|
|
61
|
-
});
|
|
62
|
-
|
|
63
|
-
test('handle empty styles', () => {
|
|
64
|
-
/**
|
|
65
|
-
* emotion will add a lot of `css-0` classes especially
|
|
66
|
-
* when a "variant" is not present in the theme.
|
|
67
|
-
*/
|
|
68
|
-
const { result } = renderHook(() => useClassname({}));
|
|
69
|
-
expect(result.current).toMatchInlineSnapshot(`""`);
|
|
70
|
-
});
|
package/src/useClassname.ts
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import { css as emotion } from '@emotion/css';
|
|
2
|
-
import { StyleObject } from './types';
|
|
3
|
-
import { useTheme } from './useTheme';
|
|
4
|
-
|
|
5
|
-
// 🤫 https://stackoverflow.com/questions/679915/how-do-i-test-for-an-empty-javascript-object
|
|
6
|
-
// lodash.isEmpty is tooo much KBs!
|
|
7
|
-
const isEmpty = (val: any) =>
|
|
8
|
-
val && Object.keys(val).length === 0 && val.constructor === Object;
|
|
9
|
-
|
|
10
|
-
export const useClassname = (...styles: StyleObject[]) => {
|
|
11
|
-
const { css } = useTheme();
|
|
12
|
-
return styles
|
|
13
|
-
.map(style => {
|
|
14
|
-
/**
|
|
15
|
-
* emotion will create a `css-0` class whenever an empty object is
|
|
16
|
-
* passed. Since this makes debugging harder we'll do not pass empty
|
|
17
|
-
* objects to emotion.
|
|
18
|
-
*/
|
|
19
|
-
const themedStyle = css(style);
|
|
20
|
-
return isEmpty(themedStyle) ? '' : emotion(themedStyle);
|
|
21
|
-
})
|
|
22
|
-
.join(' ');
|
|
23
|
-
};
|
package/src/useStyles.test.tsx
DELETED
|
@@ -1,286 +0,0 @@
|
|
|
1
|
-
import React, { ElementType } from 'react';
|
|
2
|
-
import { renderHook } from '@testing-library/react-hooks';
|
|
3
|
-
import { render, screen } from '@testing-library/react';
|
|
4
|
-
import { useStyles } from './useStyles';
|
|
5
|
-
import { ThemeProvider } from './useTheme';
|
|
6
|
-
|
|
7
|
-
// Setup
|
|
8
|
-
// ---------------
|
|
9
|
-
const theme = {
|
|
10
|
-
text: {
|
|
11
|
-
body: {
|
|
12
|
-
fontSize: 1,
|
|
13
|
-
color: 'black',
|
|
14
|
-
marginTop: '2px',
|
|
15
|
-
},
|
|
16
|
-
heading: {
|
|
17
|
-
fontSize: 3,
|
|
18
|
-
color: 'primary',
|
|
19
|
-
},
|
|
20
|
-
padding: {
|
|
21
|
-
paddingTop: '2px',
|
|
22
|
-
},
|
|
23
|
-
},
|
|
24
|
-
};
|
|
25
|
-
|
|
26
|
-
const wrapper: React.FC = ({ children }) => (
|
|
27
|
-
<ThemeProvider theme={theme}>{children}</ThemeProvider>
|
|
28
|
-
);
|
|
29
|
-
|
|
30
|
-
test('create a string classname', () => {
|
|
31
|
-
const { result } = renderHook(
|
|
32
|
-
() => useStyles({ element: 'a', css: { color: 'primary' } }),
|
|
33
|
-
{
|
|
34
|
-
wrapper,
|
|
35
|
-
}
|
|
36
|
-
);
|
|
37
|
-
expect(result.current).toEqual(expect.any(String));
|
|
38
|
-
});
|
|
39
|
-
|
|
40
|
-
// Tests
|
|
41
|
-
// ---------------
|
|
42
|
-
|
|
43
|
-
test('base styles first', () => {
|
|
44
|
-
const TestComponent: React.FC<{}> = ({ children, ...props }) => {
|
|
45
|
-
const classNames = useStyles({ element: 'p' });
|
|
46
|
-
return (
|
|
47
|
-
<p className={classNames} {...props}>
|
|
48
|
-
{children}
|
|
49
|
-
</p>
|
|
50
|
-
);
|
|
51
|
-
};
|
|
52
|
-
|
|
53
|
-
const { getByText } = render(
|
|
54
|
-
<ThemeProvider theme={theme}>
|
|
55
|
-
<TestComponent>Text</TestComponent>
|
|
56
|
-
</ThemeProvider>
|
|
57
|
-
);
|
|
58
|
-
const testelem = getByText('Text');
|
|
59
|
-
const style = getComputedStyle(testelem);
|
|
60
|
-
|
|
61
|
-
expect(style.marginTop).toEqual('0px'); // 0px come from base
|
|
62
|
-
});
|
|
63
|
-
|
|
64
|
-
test('variant styles second', () => {
|
|
65
|
-
const TestComponent: React.FC<{ variant?: 'body' }> = ({
|
|
66
|
-
variant = 'body',
|
|
67
|
-
children,
|
|
68
|
-
...props
|
|
69
|
-
}) => {
|
|
70
|
-
const classNames = useStyles({
|
|
71
|
-
element: 'p',
|
|
72
|
-
variant: `text.${variant}`,
|
|
73
|
-
});
|
|
74
|
-
return (
|
|
75
|
-
<p className={classNames} {...props}>
|
|
76
|
-
{children}
|
|
77
|
-
</p>
|
|
78
|
-
);
|
|
79
|
-
};
|
|
80
|
-
|
|
81
|
-
const { getByText } = render(
|
|
82
|
-
<ThemeProvider theme={theme}>
|
|
83
|
-
<TestComponent>Text</TestComponent>
|
|
84
|
-
</ThemeProvider>
|
|
85
|
-
);
|
|
86
|
-
const testelem = getByText('Text');
|
|
87
|
-
const style = getComputedStyle(testelem);
|
|
88
|
-
|
|
89
|
-
expect(style.marginTop).not.toEqual('0px'); // 0px come from base
|
|
90
|
-
expect(style.marginBottom).toEqual('0px'); // 0px still come from base
|
|
91
|
-
expect(style.marginTop).toEqual('2px'); // 2px come from variant
|
|
92
|
-
});
|
|
93
|
-
|
|
94
|
-
test('array of variant styles', () => {
|
|
95
|
-
const TestComponent: React.FC<{ variant?: 'body' }> = ({
|
|
96
|
-
variant = 'body',
|
|
97
|
-
children,
|
|
98
|
-
...props
|
|
99
|
-
}) => {
|
|
100
|
-
const classNames = useStyles({
|
|
101
|
-
element: 'p',
|
|
102
|
-
variant: [`text.${variant}`, `text.padding`],
|
|
103
|
-
});
|
|
104
|
-
return (
|
|
105
|
-
<p className={classNames} {...props}>
|
|
106
|
-
{children}
|
|
107
|
-
</p>
|
|
108
|
-
);
|
|
109
|
-
};
|
|
110
|
-
|
|
111
|
-
const { getByText } = render(
|
|
112
|
-
<ThemeProvider theme={theme}>
|
|
113
|
-
<TestComponent>Text</TestComponent>
|
|
114
|
-
</ThemeProvider>
|
|
115
|
-
);
|
|
116
|
-
const testelem = getByText('Text');
|
|
117
|
-
const style = getComputedStyle(testelem);
|
|
118
|
-
|
|
119
|
-
expect(style.marginTop).not.toEqual('0px'); // 0px come from base
|
|
120
|
-
expect(style.marginBottom).toEqual('0px'); // 0px still come from base
|
|
121
|
-
expect(style.marginTop).toEqual('2px'); // 2px marginTop come from variant
|
|
122
|
-
expect(style.paddingTop).toEqual('2px'); // 2px paddingTop come from variant
|
|
123
|
-
});
|
|
124
|
-
|
|
125
|
-
test('custom styles third', () => {
|
|
126
|
-
const TestComponent: React.FC<{ variant?: 'body' }> = ({
|
|
127
|
-
variant = 'body',
|
|
128
|
-
children,
|
|
129
|
-
...props
|
|
130
|
-
}) => {
|
|
131
|
-
const classNames = useStyles({
|
|
132
|
-
element: 'p',
|
|
133
|
-
variant: `text.${variant}`,
|
|
134
|
-
css: {
|
|
135
|
-
marginTop: '4px',
|
|
136
|
-
},
|
|
137
|
-
});
|
|
138
|
-
return (
|
|
139
|
-
<p className={classNames} {...props}>
|
|
140
|
-
{children}
|
|
141
|
-
</p>
|
|
142
|
-
);
|
|
143
|
-
};
|
|
144
|
-
|
|
145
|
-
const { getByText } = render(
|
|
146
|
-
<ThemeProvider theme={theme}>
|
|
147
|
-
<TestComponent>Text</TestComponent>
|
|
148
|
-
</ThemeProvider>
|
|
149
|
-
);
|
|
150
|
-
const testelem = getByText('Text');
|
|
151
|
-
const style = getComputedStyle(testelem);
|
|
152
|
-
|
|
153
|
-
expect(style.marginTop).not.toEqual('0px'); // do not apply 0px from base
|
|
154
|
-
expect(style.marginTop).not.toEqual('2px'); // do not apply 2px from variant
|
|
155
|
-
expect(style.marginTop).toEqual('4px'); // apply 4px from custom styles
|
|
156
|
-
});
|
|
157
|
-
|
|
158
|
-
test('customClassName styles fourth', () => {
|
|
159
|
-
const TestComponent: React.FC<{ variant?: 'body' }> = ({
|
|
160
|
-
variant = 'body',
|
|
161
|
-
children,
|
|
162
|
-
...props
|
|
163
|
-
}) => {
|
|
164
|
-
const custom = useStyles({ element: 'p', css: { marginTop: '8px' } });
|
|
165
|
-
const classNames = useStyles({
|
|
166
|
-
element: 'p',
|
|
167
|
-
variant: `text.${variant}`,
|
|
168
|
-
css: {
|
|
169
|
-
marginTop: '4px',
|
|
170
|
-
},
|
|
171
|
-
className: custom,
|
|
172
|
-
});
|
|
173
|
-
return (
|
|
174
|
-
<p className={classNames} {...props}>
|
|
175
|
-
{children}
|
|
176
|
-
</p>
|
|
177
|
-
);
|
|
178
|
-
};
|
|
179
|
-
|
|
180
|
-
const { getByText } = render(
|
|
181
|
-
<ThemeProvider theme={theme}>
|
|
182
|
-
<TestComponent>Text</TestComponent>
|
|
183
|
-
</ThemeProvider>
|
|
184
|
-
);
|
|
185
|
-
const testelem = getByText('Text');
|
|
186
|
-
const style = getComputedStyle(testelem);
|
|
187
|
-
|
|
188
|
-
expect(style.marginTop).not.toEqual('0px'); // do not apply 0px from base
|
|
189
|
-
expect(style.marginTop).not.toEqual('2px'); // do not apply 2px from variant
|
|
190
|
-
expect(style.marginTop).not.toEqual('4px'); // do not apply 4px from custom styles
|
|
191
|
-
expect(style.marginTop).toEqual('8px'); // apply 8px from customClassNames styles
|
|
192
|
-
});
|
|
193
|
-
|
|
194
|
-
test("don't apply the same reset multiple times", () => {
|
|
195
|
-
const Button = ({ className }: { className?: string }) => {
|
|
196
|
-
const classNames = useStyles({ element: 'button', className });
|
|
197
|
-
return (
|
|
198
|
-
<button title="button" className={classNames}>
|
|
199
|
-
Click me!
|
|
200
|
-
</button>
|
|
201
|
-
);
|
|
202
|
-
};
|
|
203
|
-
const Wrapper = () => <Button className={useStyles({ element: 'button' })} />;
|
|
204
|
-
|
|
205
|
-
render(<Wrapper />);
|
|
206
|
-
const button = screen.getByTitle('button');
|
|
207
|
-
const classNames = button.className.split(' ').filter(i => i.length);
|
|
208
|
-
|
|
209
|
-
// Test if applied classnames are unique
|
|
210
|
-
expect(classNames.length).toEqual([...new Set(classNames)].length);
|
|
211
|
-
});
|
|
212
|
-
|
|
213
|
-
test('element resets are applied dynamically', () => {
|
|
214
|
-
const Component = ({ element }: { element?: ElementType }) => (
|
|
215
|
-
<div title="element" className={useStyles({ element })}>
|
|
216
|
-
div
|
|
217
|
-
</div>
|
|
218
|
-
);
|
|
219
|
-
|
|
220
|
-
const { rerender } = render(<Component element="input" />);
|
|
221
|
-
const inputClassName = screen.getByTitle('element').className;
|
|
222
|
-
|
|
223
|
-
rerender(<Component element="table" />);
|
|
224
|
-
const tableClassName = screen.getByTitle('element').className;
|
|
225
|
-
|
|
226
|
-
expect(inputClassName).not.toEqual(tableClassName);
|
|
227
|
-
});
|
|
228
|
-
|
|
229
|
-
test('normalize base without element prop', () => {
|
|
230
|
-
const TestComponent: React.FC<{ variant?: 'body' }> = ({
|
|
231
|
-
variant = 'normal',
|
|
232
|
-
children,
|
|
233
|
-
...props
|
|
234
|
-
}) => {
|
|
235
|
-
const classNames = useStyles({
|
|
236
|
-
variant: `text.${variant}`,
|
|
237
|
-
});
|
|
238
|
-
return (
|
|
239
|
-
<a className={classNames} {...props}>
|
|
240
|
-
{children}
|
|
241
|
-
</a>
|
|
242
|
-
);
|
|
243
|
-
};
|
|
244
|
-
|
|
245
|
-
const { getByText } = render(
|
|
246
|
-
<ThemeProvider theme={theme}>
|
|
247
|
-
<TestComponent>Link</TestComponent>
|
|
248
|
-
</ThemeProvider>
|
|
249
|
-
);
|
|
250
|
-
const testelem = getByText('Link');
|
|
251
|
-
const style = getComputedStyle(testelem);
|
|
252
|
-
|
|
253
|
-
expect(style.boxSizing).toEqual('border-box');
|
|
254
|
-
expect(style.margin).toEqual('0px');
|
|
255
|
-
expect(style.padding).toEqual('0px');
|
|
256
|
-
expect(style.minWidth).toEqual('0');
|
|
257
|
-
});
|
|
258
|
-
|
|
259
|
-
test('normalize tag name <a>', () => {
|
|
260
|
-
const TestComponent: React.FC<{ variant?: 'body' }> = ({
|
|
261
|
-
variant = 'body',
|
|
262
|
-
children,
|
|
263
|
-
...props
|
|
264
|
-
}) => {
|
|
265
|
-
const classNames = useStyles({
|
|
266
|
-
element: 'a',
|
|
267
|
-
variant: `text.${variant}`,
|
|
268
|
-
});
|
|
269
|
-
return (
|
|
270
|
-
<a className={classNames} {...props}>
|
|
271
|
-
{children}
|
|
272
|
-
</a>
|
|
273
|
-
);
|
|
274
|
-
};
|
|
275
|
-
|
|
276
|
-
const { getByText } = render(
|
|
277
|
-
<ThemeProvider theme={theme}>
|
|
278
|
-
<TestComponent>Link</TestComponent>
|
|
279
|
-
</ThemeProvider>
|
|
280
|
-
);
|
|
281
|
-
const testelem = getByText('Link');
|
|
282
|
-
const style = getComputedStyle(testelem);
|
|
283
|
-
|
|
284
|
-
expect(style.boxSizing).toEqual('border-box'); // from base
|
|
285
|
-
expect(style.textDecoration).toEqual('none'); // from a
|
|
286
|
-
});
|
package/src/useStyles.ts
DELETED
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
import { ElementType } from 'react';
|
|
2
|
-
import { reset } from './reset';
|
|
3
|
-
import { CSSObject } from './types';
|
|
4
|
-
import { useClassname } from './useClassname';
|
|
5
|
-
|
|
6
|
-
export type UseStyleInput = {
|
|
7
|
-
element?: ElementType;
|
|
8
|
-
css?: Omit<CSSObject, 'variant' | 'element'> & {
|
|
9
|
-
variant?: never;
|
|
10
|
-
element?: never;
|
|
11
|
-
};
|
|
12
|
-
variant?: string | string[];
|
|
13
|
-
className?: string;
|
|
14
|
-
};
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* Hook that can adds base styles, reset for certain elements, variants and custom styles
|
|
18
|
-
*/
|
|
19
|
-
export const useStyles = ({
|
|
20
|
-
element,
|
|
21
|
-
css: styles = {},
|
|
22
|
-
variant,
|
|
23
|
-
className = '',
|
|
24
|
-
}: UseStyleInput) => {
|
|
25
|
-
/**
|
|
26
|
-
* Get reset styles. Base is always applied. An additional reset maybe applied
|
|
27
|
-
* based on the passed element.
|
|
28
|
-
*
|
|
29
|
-
* We check the passed className if it already includes the reset styles so no
|
|
30
|
-
* duplicates are applied.
|
|
31
|
-
*/
|
|
32
|
-
const baseClassName = className.includes(reset.base) ? '' : reset.base;
|
|
33
|
-
const resetClassName =
|
|
34
|
-
typeof element === 'string'
|
|
35
|
-
? className.includes((reset as { [key: string]: string })[element])
|
|
36
|
-
? ''
|
|
37
|
-
: (reset as { [key: string]: string })[element]
|
|
38
|
-
: '';
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* Get variant styles (from theme).
|
|
42
|
-
*/
|
|
43
|
-
const variants = Array.isArray(variant)
|
|
44
|
-
? variant.map(v => ({ variant: v }))
|
|
45
|
-
: [{ variant }];
|
|
46
|
-
const variantsClassName = useClassname(...variants);
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* Custom styles are applied "on runtime". They are usually controlled via component
|
|
50
|
-
* props and can change between component instances.
|
|
51
|
-
*/
|
|
52
|
-
const customClassName = useClassname(styles);
|
|
53
|
-
|
|
54
|
-
return [
|
|
55
|
-
baseClassName,
|
|
56
|
-
resetClassName,
|
|
57
|
-
variantsClassName,
|
|
58
|
-
customClassName,
|
|
59
|
-
className,
|
|
60
|
-
]
|
|
61
|
-
.filter(Boolean)
|
|
62
|
-
.join(' ');
|
|
63
|
-
};
|
|
@@ -1,126 +0,0 @@
|
|
|
1
|
-
import { Meta } from '@storybook/addon-docs/blocks';
|
|
2
|
-
|
|
3
|
-
<Meta title="Guides/How to Write and Use Components" />
|
|
4
|
-
|
|
5
|
-
# How to Write and Use Components
|
|
6
|
-
|
|
7
|
-
Small guidance for creating components in Marigold.
|
|
8
|
-
|
|
9
|
-
## API
|
|
10
|
-
|
|
11
|
-
### Use the base HTML element
|
|
12
|
-
|
|
13
|
-
Create your basic component by using the HTML tagname with its own props. Specify the HTML element you want to render.
|
|
14
|
-
The `system` helper infers a forwardRef (and will soon be abolished).
|
|
15
|
-
|
|
16
|
-
```tsx
|
|
17
|
-
export const Button = system<ButtonProps, 'button'>(({ ...props }) => {
|
|
18
|
-
return <button {...props}>{children}</button>;
|
|
19
|
-
});
|
|
20
|
-
```
|
|
21
|
-
|
|
22
|
-
### Add Styling via Classnames
|
|
23
|
-
|
|
24
|
-
In order to add styles to your component, you can work with classNames. ClassNames are automatically generated via the `useStyles` hook.
|
|
25
|
-
There are three levels of styles to be applied: `useStyles` takes the HTML element type used in the component to normalize the styles according to those HTML elements.
|
|
26
|
-
A base normalization is automatically added to this list and always applies in order to have the same defaults cross-browser.
|
|
27
|
-
Next, `useStyles` takes a `variant` which is retrieved from the theme (which is given in the `<ThemeProvider>`). Additionally, you can insert custom styles
|
|
28
|
-
which are applied on runtime and override normalization and theme styles. Lastly, you can use custom classnames and pass them to your component in the `useStyles` hook.
|
|
29
|
-
|
|
30
|
-
```tsx
|
|
31
|
-
const classNames = useStyles(
|
|
32
|
-
{
|
|
33
|
-
element: 'button',
|
|
34
|
-
variant: `button.${variant}`,
|
|
35
|
-
},
|
|
36
|
-
className
|
|
37
|
-
);
|
|
38
|
-
```
|
|
39
|
-
|
|
40
|
-
### Put it together
|
|
41
|
-
|
|
42
|
-
Don't forget imports, defaults and (optional) logic needed for your component as seen in the following example.
|
|
43
|
-
|
|
44
|
-
```tsx
|
|
45
|
-
import React from 'react';
|
|
46
|
-
import { ThemeProvider, system } from '@marigold/system';
|
|
47
|
-
import { render } from '@testing-library/react';
|
|
48
|
-
|
|
49
|
-
const theme = {
|
|
50
|
-
...anyTheme,
|
|
51
|
-
button: {
|
|
52
|
-
outlined: { // variant
|
|
53
|
-
border: '1px solid orange',
|
|
54
|
-
mx: 2,
|
|
55
|
-
},
|
|
56
|
-
},
|
|
57
|
-
};
|
|
58
|
-
// Define custom properties for your component
|
|
59
|
-
type ButtonProps = {
|
|
60
|
-
kind?: 'basic' | 'other';
|
|
61
|
-
size?: 'small' | 'medium' | 'large';
|
|
62
|
-
};
|
|
63
|
-
|
|
64
|
-
// Specify the HTML element you want to render and merge its properties with the custom component props
|
|
65
|
-
const Button = system<ButtonProps, 'button'>(
|
|
66
|
-
// Set default values for extra props
|
|
67
|
-
({
|
|
68
|
-
kind = 'basic',
|
|
69
|
-
size = 'medium',
|
|
70
|
-
className
|
|
71
|
-
children,
|
|
72
|
-
...props
|
|
73
|
-
}) => {
|
|
74
|
-
const classNames = useStyles(
|
|
75
|
-
// add normalization and variant
|
|
76
|
-
{
|
|
77
|
-
element: 'button',
|
|
78
|
-
variant: `button.${variant}`,
|
|
79
|
-
},
|
|
80
|
-
className
|
|
81
|
-
)
|
|
82
|
-
// Place logic here like toggle states, calculations, mappings etc.
|
|
83
|
-
return (
|
|
84
|
-
<button
|
|
85
|
-
className={classNames}
|
|
86
|
-
{...props}
|
|
87
|
-
>
|
|
88
|
-
<span
|
|
89
|
-
className={useStyles({ // use custom styles only
|
|
90
|
-
display: 'inline-flex',
|
|
91
|
-
alignItems: 'center',
|
|
92
|
-
})}
|
|
93
|
-
>
|
|
94
|
-
{children}
|
|
95
|
-
</span>
|
|
96
|
-
</button>
|
|
97
|
-
);
|
|
98
|
-
}
|
|
99
|
-
);
|
|
100
|
-
// Use the component; don't forget the imports
|
|
101
|
-
render(
|
|
102
|
-
<ThemeProvider theme={theme}>
|
|
103
|
-
<Button /> // Default Button component instance
|
|
104
|
-
</ThemeProvider>
|
|
105
|
-
);
|
|
106
|
-
```
|
|
107
|
-
|
|
108
|
-
### Folder Structure
|
|
109
|
-
|
|
110
|
-
As we want to keep an organized file structure, we use a script to create new component templates. Navigate to the root folder and use it with `yarn create:component COMPONENTNAME`. The new component
|
|
111
|
-
is generated with all files needed and already added to the component exports.
|
|
112
|
-
|
|
113
|
-
```
|
|
114
|
-
packages
|
|
115
|
-
└─── system
|
|
116
|
-
|
|
|
117
|
-
└─── components
|
|
118
|
-
| index.ts
|
|
119
|
-
| theme.ts
|
|
120
|
-
└─── src
|
|
121
|
-
└─── Button
|
|
122
|
-
| index.ts
|
|
123
|
-
| Button.tsx
|
|
124
|
-
| Button.test.tsx
|
|
125
|
-
| Button.stories.mdx
|
|
126
|
-
```
|