@marigold/system 0.2.0 → 0.3.3

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.
Files changed (50) hide show
  1. package/CHANGELOG.md +57 -0
  2. package/dist/Box.d.ts +14 -0
  3. package/dist/Global.d.ts +2 -0
  4. package/dist/SVG.d.ts +6 -0
  5. package/dist/SVG.stories.d.ts +5 -0
  6. package/dist/index.d.ts +6 -4
  7. package/dist/normalize.d.ts +101 -67
  8. package/dist/system.cjs.development.js +299 -294
  9. package/dist/system.cjs.development.js.map +1 -1
  10. package/dist/system.cjs.production.min.js +1 -1
  11. package/dist/system.cjs.production.min.js.map +1 -1
  12. package/dist/system.esm.js +291 -289
  13. package/dist/system.esm.js.map +1 -1
  14. package/dist/theme.d.ts +136 -0
  15. package/dist/types.d.ts +1 -2
  16. package/dist/useTheme.d.ts +11 -5
  17. package/dist/variant.d.ts +29 -0
  18. package/package.json +5 -6
  19. package/src/Box.test.tsx +308 -0
  20. package/src/Box.tsx +199 -0
  21. package/src/Global.test.tsx +57 -0
  22. package/src/Global.tsx +34 -0
  23. package/src/SVG.stories.tsx +48 -0
  24. package/src/SVG.test.tsx +82 -0
  25. package/src/SVG.tsx +24 -0
  26. package/src/index.ts +6 -4
  27. package/src/normalize.test.tsx +9 -36
  28. package/src/normalize.ts +51 -82
  29. package/src/theme.ts +157 -0
  30. package/src/types.ts +0 -2
  31. package/src/useTheme.test.tsx +22 -14
  32. package/src/useTheme.tsx +37 -9
  33. package/src/variant.test.ts +93 -0
  34. package/src/variant.ts +54 -0
  35. package/dist/Element.d.ts +0 -8
  36. package/dist/cache.d.ts +0 -4
  37. package/dist/reset.d.ts +0 -24
  38. package/dist/useClassname.d.ts +0 -2
  39. package/dist/useStyles.d.ts +0 -15
  40. package/src/Colors.stories.mdx +0 -623
  41. package/src/Element.test.tsx +0 -203
  42. package/src/Element.tsx +0 -59
  43. package/src/cache.ts +0 -4
  44. package/src/concepts-principles.mdx +0 -84
  45. package/src/reset.ts +0 -108
  46. package/src/useClassname.test.tsx +0 -70
  47. package/src/useClassname.ts +0 -23
  48. package/src/useStyles.stories.mdx +0 -24
  49. package/src/useStyles.test.tsx +0 -286
  50. package/src/useStyles.ts +0 -63
@@ -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
- };