@marigold/system 0.1.0 → 0.2.0
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/dist/Element.d.ts +8 -0
- package/dist/index.d.ts +1 -0
- package/dist/normalize.d.ts +110 -0
- package/dist/system.cjs.development.js +246 -45
- 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 +239 -39
- package/dist/system.esm.js.map +1 -1
- package/package.json +5 -6
- package/src/Colors.stories.mdx +614 -446
- package/src/Element.test.tsx +203 -0
- package/src/Element.tsx +59 -0
- package/src/concepts-principles.mdx +1 -1
- package/src/index.ts +1 -0
- package/src/normalize.test.tsx +42 -0
- package/src/normalize.ts +131 -0
- package/src/reset.ts +3 -3
- package/src/useStyles.stories.mdx +24 -0
- package/src/writeComponent.stories.mdx +0 -126
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { render, screen } from '@testing-library/react';
|
|
3
|
+
import { useStyles } from './useStyles';
|
|
4
|
+
import { ThemeProvider } from './useTheme';
|
|
5
|
+
import { Element } from './Element';
|
|
6
|
+
|
|
7
|
+
const theme = {
|
|
8
|
+
text: {
|
|
9
|
+
body: {
|
|
10
|
+
fontSize: 1,
|
|
11
|
+
color: 'black',
|
|
12
|
+
marginTop: '2px',
|
|
13
|
+
},
|
|
14
|
+
heading: {
|
|
15
|
+
fontSize: 3,
|
|
16
|
+
color: 'primary',
|
|
17
|
+
},
|
|
18
|
+
padding: {
|
|
19
|
+
paddingTop: '2px',
|
|
20
|
+
},
|
|
21
|
+
},
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
test('renders a <div> by default', () => {
|
|
25
|
+
render(<Element>Text</Element>);
|
|
26
|
+
const testelem = screen.getByText('Text');
|
|
27
|
+
|
|
28
|
+
expect(testelem instanceof HTMLDivElement).toBeTruthy();
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
test('supports as prop', () => {
|
|
32
|
+
render(<Element as="p">Text</Element>);
|
|
33
|
+
const testelem = screen.getByText('Text');
|
|
34
|
+
|
|
35
|
+
expect(testelem instanceof HTMLParagraphElement).toBeTruthy();
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
test('supports HTML className attribute', () => {
|
|
39
|
+
render(<Element className="my-custom-class">Text</Element>);
|
|
40
|
+
const element = screen.getByText('Text');
|
|
41
|
+
|
|
42
|
+
expect(element.getAttribute('class')).toMatch('my-custom-class');
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
test('passes down HTML attributes', () => {
|
|
46
|
+
render(
|
|
47
|
+
<Element className="my-custom-class" id="element-id" disabled>
|
|
48
|
+
Text
|
|
49
|
+
</Element>
|
|
50
|
+
);
|
|
51
|
+
const element = screen.getByText('Text');
|
|
52
|
+
|
|
53
|
+
expect(element.getAttribute('id')).toEqual('element-id');
|
|
54
|
+
expect(element.getAttribute('disabled')).toMatch('');
|
|
55
|
+
expect(element.getAttribute('class')).toMatch('my-custom-class');
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
test('forwards ref', () => {
|
|
59
|
+
const ref = React.createRef<HTMLButtonElement>();
|
|
60
|
+
render(
|
|
61
|
+
<Element as="button" ref={ref}>
|
|
62
|
+
button
|
|
63
|
+
</Element>
|
|
64
|
+
);
|
|
65
|
+
|
|
66
|
+
expect(ref.current instanceof HTMLButtonElement).toBeTruthy();
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
test('base styles first', () => {
|
|
70
|
+
const { getByText } = render(<Element as="p">Text</Element>);
|
|
71
|
+
const testelem = getByText('Text');
|
|
72
|
+
const style = getComputedStyle(testelem);
|
|
73
|
+
|
|
74
|
+
expect(style.marginTop).toEqual('0px'); // 0px come from base
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
test('variant styles second', () => {
|
|
78
|
+
const TestComponent: React.FC<{ variant?: 'body' }> = ({
|
|
79
|
+
variant = 'body',
|
|
80
|
+
children,
|
|
81
|
+
...props
|
|
82
|
+
}) => {
|
|
83
|
+
return (
|
|
84
|
+
<Element as="p" variant={`text.${variant}`} {...props}>
|
|
85
|
+
{children}
|
|
86
|
+
</Element>
|
|
87
|
+
);
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
const { getByText } = render(
|
|
91
|
+
<ThemeProvider theme={theme}>
|
|
92
|
+
<TestComponent>Text</TestComponent>
|
|
93
|
+
</ThemeProvider>
|
|
94
|
+
);
|
|
95
|
+
const testelem = getByText('Text');
|
|
96
|
+
const style = getComputedStyle(testelem);
|
|
97
|
+
|
|
98
|
+
expect(style.marginTop).not.toEqual('0px'); // 0px come from base
|
|
99
|
+
expect(style.marginBottom).toEqual('0px'); // 0px still come from base
|
|
100
|
+
expect(style.marginTop).toEqual('2px'); // 2px come from variant
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
test('array of variant styles', () => {
|
|
104
|
+
const TestComponent: React.FC<{ variant?: 'body' }> = ({
|
|
105
|
+
variant = 'body',
|
|
106
|
+
children,
|
|
107
|
+
...props
|
|
108
|
+
}) => {
|
|
109
|
+
return (
|
|
110
|
+
<Element as="p" variant={[`text.${variant}`, `text.padding`]} {...props}>
|
|
111
|
+
{children}
|
|
112
|
+
</Element>
|
|
113
|
+
);
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
const { getByText } = render(
|
|
117
|
+
<ThemeProvider theme={theme}>
|
|
118
|
+
<TestComponent>Text</TestComponent>
|
|
119
|
+
</ThemeProvider>
|
|
120
|
+
);
|
|
121
|
+
const testelem = getByText('Text');
|
|
122
|
+
const style = getComputedStyle(testelem);
|
|
123
|
+
|
|
124
|
+
expect(style.marginTop).not.toEqual('0px'); // 0px come from base
|
|
125
|
+
expect(style.marginBottom).toEqual('0px'); // 0px still come from base
|
|
126
|
+
expect(style.marginTop).toEqual('2px'); // 2px marginTop come from variant
|
|
127
|
+
expect(style.paddingTop).toEqual('2px'); // 2px paddingTop come from variant
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
test('custom styles with css prop third', () => {
|
|
131
|
+
const TestComponent: React.FC<{ variant?: 'body' }> = ({
|
|
132
|
+
variant = 'body',
|
|
133
|
+
children,
|
|
134
|
+
...props
|
|
135
|
+
}) => {
|
|
136
|
+
return (
|
|
137
|
+
<Element
|
|
138
|
+
as="p"
|
|
139
|
+
variant={`text.${variant}`}
|
|
140
|
+
css={{ marginTop: '4px' }}
|
|
141
|
+
{...props}
|
|
142
|
+
>
|
|
143
|
+
{children}
|
|
144
|
+
</Element>
|
|
145
|
+
);
|
|
146
|
+
};
|
|
147
|
+
|
|
148
|
+
const { getByText } = render(
|
|
149
|
+
<ThemeProvider theme={theme}>
|
|
150
|
+
<TestComponent>Text</TestComponent>
|
|
151
|
+
</ThemeProvider>
|
|
152
|
+
);
|
|
153
|
+
const testelem = getByText('Text');
|
|
154
|
+
const style = getComputedStyle(testelem);
|
|
155
|
+
|
|
156
|
+
expect(style.marginTop).not.toEqual('0px'); // do not apply 0px from base
|
|
157
|
+
expect(style.marginTop).not.toEqual('2px'); // do not apply 2px from variant
|
|
158
|
+
expect(style.marginTop).toEqual('4px'); // apply 4px from custom styles
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
test("don't apply the same reset multiple times", () => {
|
|
162
|
+
const Button = ({ className }: { className?: string }) => {
|
|
163
|
+
const classNames = useStyles({ element: 'button', className });
|
|
164
|
+
return (
|
|
165
|
+
<Element as="button" title="button" className={classNames}>
|
|
166
|
+
Click me!
|
|
167
|
+
</Element>
|
|
168
|
+
);
|
|
169
|
+
};
|
|
170
|
+
const Wrapper = () => <Button />;
|
|
171
|
+
|
|
172
|
+
render(<Wrapper />);
|
|
173
|
+
const button = screen.getByTitle('button');
|
|
174
|
+
const classNames = button.className.split(' ').filter(i => i.length);
|
|
175
|
+
|
|
176
|
+
// Test if applied classnames are unique
|
|
177
|
+
expect(classNames.length).toEqual([...new Set(classNames)].length);
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
test('normalize tag name <a>', () => {
|
|
181
|
+
const TestComponent: React.FC<{ variant?: 'body' }> = ({
|
|
182
|
+
variant = 'body',
|
|
183
|
+
children,
|
|
184
|
+
...props
|
|
185
|
+
}) => {
|
|
186
|
+
return (
|
|
187
|
+
<Element as="a" variant={`text.${variant}`} {...props}>
|
|
188
|
+
{children}
|
|
189
|
+
</Element>
|
|
190
|
+
);
|
|
191
|
+
};
|
|
192
|
+
|
|
193
|
+
const { getByText } = render(
|
|
194
|
+
<ThemeProvider theme={theme}>
|
|
195
|
+
<TestComponent>Link</TestComponent>
|
|
196
|
+
</ThemeProvider>
|
|
197
|
+
);
|
|
198
|
+
const testelem = getByText('Link');
|
|
199
|
+
const style = getComputedStyle(testelem);
|
|
200
|
+
|
|
201
|
+
expect(style.boxSizing).toEqual('border-box'); // from base
|
|
202
|
+
expect(style.textDecoration).toEqual('none'); // from a
|
|
203
|
+
});
|
package/src/Element.tsx
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { jsx } from '@emotion/react';
|
|
2
|
+
import { forwardRef } from 'react';
|
|
3
|
+
import {
|
|
4
|
+
PolymorphicPropsWithRef,
|
|
5
|
+
PolymorphicComponentWithRef,
|
|
6
|
+
} from '@marigold/types';
|
|
7
|
+
|
|
8
|
+
import { getNormalizedStyles } from './normalize';
|
|
9
|
+
import { CSSObject } from './types';
|
|
10
|
+
import { useTheme } from './useTheme';
|
|
11
|
+
|
|
12
|
+
export type ElementOwnProps = {
|
|
13
|
+
css?: CSSObject;
|
|
14
|
+
variant?: string | string[];
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export type ElementProps = PolymorphicPropsWithRef<ElementOwnProps, 'div'>;
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Function expression to check if there is any falsy value or empty object
|
|
21
|
+
*/
|
|
22
|
+
const isNotEmpty = (val: any) =>
|
|
23
|
+
!(val && Object.keys(val).length === 0 && val.constructor === Object);
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Get the normalized base styles
|
|
27
|
+
*/
|
|
28
|
+
const baseStyles = getNormalizedStyles('base');
|
|
29
|
+
|
|
30
|
+
export const Element: PolymorphicComponentWithRef<ElementOwnProps, 'div'> =
|
|
31
|
+
forwardRef(
|
|
32
|
+
({ as = 'div', css: styles = {}, variant, children, ...props }, ref) => {
|
|
33
|
+
const { css } = useTheme();
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Transform variant input for `@theme-ui/css`
|
|
37
|
+
*/
|
|
38
|
+
const variants = Array.isArray(variant)
|
|
39
|
+
? variant.map(v => ({ variant: v }))
|
|
40
|
+
: [{ variant }];
|
|
41
|
+
|
|
42
|
+
return jsx(
|
|
43
|
+
as,
|
|
44
|
+
{
|
|
45
|
+
...props,
|
|
46
|
+
...{
|
|
47
|
+
css: [
|
|
48
|
+
baseStyles,
|
|
49
|
+
getNormalizedStyles(as),
|
|
50
|
+
...variants.map(v => css(v)),
|
|
51
|
+
css(styles),
|
|
52
|
+
].filter(isNotEmpty),
|
|
53
|
+
},
|
|
54
|
+
ref,
|
|
55
|
+
},
|
|
56
|
+
children
|
|
57
|
+
);
|
|
58
|
+
}
|
|
59
|
+
);
|
package/src/index.ts
CHANGED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { getNormalizedStyles } from './normalize';
|
|
2
|
+
|
|
3
|
+
test('get base styles', () => {
|
|
4
|
+
const baseStyles = getNormalizedStyles('base');
|
|
5
|
+
expect(baseStyles).toEqual({
|
|
6
|
+
boxSizing: 'border-box',
|
|
7
|
+
margin: 0,
|
|
8
|
+
padding: 0,
|
|
9
|
+
minWidth: 0,
|
|
10
|
+
fontSize: '100%',
|
|
11
|
+
fontFamily: 'inherit',
|
|
12
|
+
verticalAlign: 'baseline',
|
|
13
|
+
WebkitTapHighlightColor: 'transparent',
|
|
14
|
+
});
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
test('get reset style by element', () => {
|
|
18
|
+
const baseStyles = getNormalizedStyles('a');
|
|
19
|
+
expect(baseStyles).toEqual({
|
|
20
|
+
textDecoration: 'none',
|
|
21
|
+
touchAction: 'manipulation',
|
|
22
|
+
});
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
test('getNormalizedStyles returns base if input is not a string', () => {
|
|
26
|
+
const baseStyles = getNormalizedStyles(undefined);
|
|
27
|
+
expect(baseStyles).toEqual({
|
|
28
|
+
boxSizing: 'border-box',
|
|
29
|
+
margin: 0,
|
|
30
|
+
padding: 0,
|
|
31
|
+
minWidth: 0,
|
|
32
|
+
fontSize: '100%',
|
|
33
|
+
fontFamily: 'inherit',
|
|
34
|
+
verticalAlign: 'baseline',
|
|
35
|
+
WebkitTapHighlightColor: 'transparent',
|
|
36
|
+
});
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
test('getNormalizedStyles returns empty object if input is unknown', () => {
|
|
40
|
+
const baseStyles = getNormalizedStyles('p');
|
|
41
|
+
expect(baseStyles).toEqual({});
|
|
42
|
+
});
|
package/src/normalize.ts
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import { ElementType } from 'react';
|
|
2
|
+
|
|
3
|
+
const base = {
|
|
4
|
+
boxSizing: 'border-box',
|
|
5
|
+
margin: 0,
|
|
6
|
+
padding: 0,
|
|
7
|
+
minWidth: 0,
|
|
8
|
+
fontSize: '100%',
|
|
9
|
+
fontFamily: 'inherit',
|
|
10
|
+
verticalAlign: 'baseline',
|
|
11
|
+
WebkitTapHighlightColor: 'transparent',
|
|
12
|
+
} as const;
|
|
13
|
+
|
|
14
|
+
// Content
|
|
15
|
+
// ---------------
|
|
16
|
+
const block = {
|
|
17
|
+
display: 'block',
|
|
18
|
+
} as const;
|
|
19
|
+
|
|
20
|
+
const list = {
|
|
21
|
+
// empty
|
|
22
|
+
} as const;
|
|
23
|
+
|
|
24
|
+
const table = {
|
|
25
|
+
borderCollapse: 'collapse',
|
|
26
|
+
borderSpacing: 0,
|
|
27
|
+
} as const;
|
|
28
|
+
|
|
29
|
+
// Typography
|
|
30
|
+
// ---------------
|
|
31
|
+
const a = {
|
|
32
|
+
textDecoration: 'none',
|
|
33
|
+
touchAction: 'manipulation',
|
|
34
|
+
} as const;
|
|
35
|
+
|
|
36
|
+
const quote = {
|
|
37
|
+
quotes: 'none',
|
|
38
|
+
selectors: {
|
|
39
|
+
'&:before, &:after': {
|
|
40
|
+
content: "''",
|
|
41
|
+
},
|
|
42
|
+
},
|
|
43
|
+
} as const;
|
|
44
|
+
|
|
45
|
+
// Form Elements
|
|
46
|
+
// ---------------
|
|
47
|
+
const button = {
|
|
48
|
+
display: 'block',
|
|
49
|
+
appearance: 'none',
|
|
50
|
+
background: 'transparent',
|
|
51
|
+
textAlign: 'center',
|
|
52
|
+
touchAction: 'manipulation',
|
|
53
|
+
} as const;
|
|
54
|
+
|
|
55
|
+
const input = {
|
|
56
|
+
display: 'block',
|
|
57
|
+
appearance: 'none',
|
|
58
|
+
selectors: {
|
|
59
|
+
'&::-ms-clear': {
|
|
60
|
+
display: 'none',
|
|
61
|
+
},
|
|
62
|
+
'&::-webkit-search-cancel-button': {
|
|
63
|
+
WebkitAppearance: 'none',
|
|
64
|
+
},
|
|
65
|
+
},
|
|
66
|
+
} as const;
|
|
67
|
+
|
|
68
|
+
const select = {
|
|
69
|
+
display: 'block',
|
|
70
|
+
appearance: 'none',
|
|
71
|
+
selectors: {
|
|
72
|
+
'&::-ms-expand': {
|
|
73
|
+
display: 'none',
|
|
74
|
+
},
|
|
75
|
+
},
|
|
76
|
+
} as const;
|
|
77
|
+
|
|
78
|
+
const textarea = {
|
|
79
|
+
display: 'block',
|
|
80
|
+
appearance: 'none',
|
|
81
|
+
} as const;
|
|
82
|
+
|
|
83
|
+
// Reset
|
|
84
|
+
// ---------------
|
|
85
|
+
const reset = {
|
|
86
|
+
article: block,
|
|
87
|
+
aside: block,
|
|
88
|
+
details: block,
|
|
89
|
+
figcaption: block,
|
|
90
|
+
figure: block,
|
|
91
|
+
footer: block,
|
|
92
|
+
header: block,
|
|
93
|
+
hgroup: block,
|
|
94
|
+
menu: block,
|
|
95
|
+
nav: block,
|
|
96
|
+
section: block,
|
|
97
|
+
ul: list,
|
|
98
|
+
ol: list,
|
|
99
|
+
blockquote: quote,
|
|
100
|
+
q: quote,
|
|
101
|
+
a,
|
|
102
|
+
base,
|
|
103
|
+
table,
|
|
104
|
+
select,
|
|
105
|
+
button,
|
|
106
|
+
textarea,
|
|
107
|
+
input,
|
|
108
|
+
} as const;
|
|
109
|
+
|
|
110
|
+
export type NormalizedElement = keyof typeof reset;
|
|
111
|
+
const isKnownElement = (input: string): input is NormalizedElement =>
|
|
112
|
+
input in reset;
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Helper to conveniently get reset styles.
|
|
116
|
+
*/
|
|
117
|
+
export const getNormalizedStyles = (input?: ElementType): object => {
|
|
118
|
+
/**
|
|
119
|
+
* If a React component is given, we don't apply any reset styles
|
|
120
|
+
* and return the base reset.
|
|
121
|
+
*/
|
|
122
|
+
if (typeof input !== 'string') {
|
|
123
|
+
return reset.base;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Try to find the reset style for a HTML element. If the element
|
|
128
|
+
* is not included return empty styles.
|
|
129
|
+
*/
|
|
130
|
+
return isKnownElement(input) ? reset[input] : {};
|
|
131
|
+
};
|
package/src/reset.ts
CHANGED
|
@@ -18,7 +18,7 @@ const block = css({
|
|
|
18
18
|
});
|
|
19
19
|
|
|
20
20
|
const list = css({
|
|
21
|
-
|
|
21
|
+
// empty
|
|
22
22
|
});
|
|
23
23
|
|
|
24
24
|
const table = css({
|
|
@@ -30,7 +30,7 @@ const table = css({
|
|
|
30
30
|
// ---------------
|
|
31
31
|
const a = css({
|
|
32
32
|
textDecoration: 'none',
|
|
33
|
-
touchAction: 'manipulation'
|
|
33
|
+
touchAction: 'manipulation',
|
|
34
34
|
});
|
|
35
35
|
|
|
36
36
|
const quote = css({
|
|
@@ -49,7 +49,7 @@ const button = css({
|
|
|
49
49
|
appearance: 'none',
|
|
50
50
|
background: 'transparent',
|
|
51
51
|
textAlign: 'center',
|
|
52
|
-
touchAction: 'manipulation'
|
|
52
|
+
touchAction: 'manipulation',
|
|
53
53
|
});
|
|
54
54
|
|
|
55
55
|
const input = css({
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { Meta, Story, Canvas } from '@storybook/addon-docs';
|
|
2
|
+
import { useStyles } from './useStyles';
|
|
3
|
+
import { Text } from '@marigold/components';
|
|
4
|
+
|
|
5
|
+
<Meta title="Hooks/useStyles" />
|
|
6
|
+
|
|
7
|
+
# useStyles
|
|
8
|
+
|
|
9
|
+
<Canvas>
|
|
10
|
+
<Story name="usestyles">
|
|
11
|
+
<Text>
|
|
12
|
+
The useStyles hook generates classnames that style your component:
|
|
13
|
+
</Text>
|
|
14
|
+
</Story>
|
|
15
|
+
</Canvas>
|
|
16
|
+
|
|
17
|
+
```tsx
|
|
18
|
+
useStyles({
|
|
19
|
+
element: ElementType; // reset styles for the given HTML element
|
|
20
|
+
css: CSSObject; // custom styles
|
|
21
|
+
variant: string|string[]; // theme variant(s)
|
|
22
|
+
className: string; // additional classnames
|
|
23
|
+
})
|
|
24
|
+
```
|
|
@@ -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
|
-
```
|