@marigold/system 0.0.3 → 0.3.1

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 (47) 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 -3
  7. package/dist/normalize.d.ts +144 -0
  8. package/dist/system.cjs.development.js +348 -132
  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 +341 -129
  13. package/dist/system.esm.js.map +1 -1
  14. package/dist/theme.d.ts +136 -0
  15. package/dist/types.d.ts +2 -3
  16. package/dist/useTheme.d.ts +15 -5
  17. package/dist/variant.d.ts +29 -0
  18. package/package.json +4 -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 -3
  27. package/src/normalize.test.tsx +15 -0
  28. package/src/normalize.ts +100 -0
  29. package/src/theme.ts +157 -0
  30. package/src/types.ts +2 -3
  31. package/src/useTheme.test.tsx +43 -18
  32. package/src/useTheme.tsx +45 -7
  33. package/src/variant.test.ts +93 -0
  34. package/src/variant.ts +54 -0
  35. package/dist/cache.d.ts +0 -4
  36. package/dist/reset.d.ts +0 -24
  37. package/dist/useClassname.d.ts +0 -2
  38. package/dist/useStyles.d.ts +0 -15
  39. package/src/Colors.stories.mdx +0 -455
  40. package/src/cache.ts +0 -4
  41. package/src/concepts-principles.mdx +0 -84
  42. package/src/reset.ts +0 -106
  43. package/src/useClassname.test.tsx +0 -70
  44. package/src/useClassname.ts +0 -24
  45. package/src/useStyles.test.tsx +0 -286
  46. package/src/useStyles.ts +0 -55
  47. package/src/writeComponent.stories.mdx +0 -126
package/src/theme.ts ADDED
@@ -0,0 +1,157 @@
1
+ import * as CSS from 'csstype';
2
+ import { NestedScaleDict } from '@theme-ui/css';
3
+
4
+ /**
5
+ * Value used to define a scale.
6
+ *
7
+ * Can be nested to support a default value.
8
+ *
9
+ * @example
10
+ * Given a theme
11
+ * ```
12
+ * {
13
+ * colors: {
14
+ * primary: { __default: '#00f', light: '#33f' }
15
+ * }
16
+ * }
17
+ * ```
18
+ * `css{{ color: 'primary' }}` resolves to `color: #00f`.
19
+ */
20
+ export type ScaleValue<T> = T | T[] | NestedScaleDict<T> | undefined;
21
+
22
+ /**
23
+ * Scales are a set of named, pre-defined CSS values which are used
24
+ * to create consitency in sizing across visual elements. They give
25
+ * plain values semantics meaning.
26
+ *
27
+ * Marigold uses a plain object to define scales, where the key should be a
28
+ * descriptive name for the scale (e.g. `small`/`medium`/.. or `body`/`heading`/...),
29
+ * and the value is the CSS value.
30
+ */
31
+ export type Scale<T> = {
32
+ [key: string]: ScaleValue<T>;
33
+ };
34
+
35
+ /**
36
+ * Predefined {@link Scale} scale which uses size values.
37
+ */
38
+ export type SizeScale<T> = {
39
+ xxsmall?: ScaleValue<T>;
40
+ xsmall?: ScaleValue<T>;
41
+ small?: ScaleValue<T>;
42
+ medium?: ScaleValue<T>;
43
+ large?: ScaleValue<T>;
44
+ xlarge?: ScaleValue<T>;
45
+ xxlarge?: ScaleValue<T>;
46
+ };
47
+
48
+ /**
49
+ * A {@link SizeScale} that also includes a required `none` value, which is
50
+ * usually used to define the blank value (e.g `0`).
51
+ */
52
+ export type ZeroScale<T> = {
53
+ none: ScaleValue<T>;
54
+ } & Scale<T>;
55
+
56
+ /**
57
+ * Base theme with typings for available scales properties.
58
+ */
59
+ export interface Theme {
60
+ /**
61
+ * To configure the default breakpoints used in responsive array values,
62
+ * add a breakpoints array to your theme.
63
+ *
64
+ * Each breakpoint should be a string with a CSS length unit included or a
65
+ * string including a CSS media query. String values with a CSS length unit
66
+ * will be used to generate a mobile-first (i.e. min-width) media query.
67
+ *
68
+ * @example
69
+ * ```ts
70
+ * {
71
+ * breakpoints: [
72
+ * '40em', '@media (min-width: 56em) and (orientation: landscape)', '64em',
73
+ * ],
74
+ * }
75
+ * ```
76
+ */
77
+ breakpoints?: Array<string>;
78
+
79
+ colors?: Scale<CSS.Property.Color | NestedScaleDict<CSS.Property.Color>>;
80
+
81
+ /**
82
+ * Used to define a scale for whitspace values,
83
+ * like `padding`, `margin`, `gap`, etc.
84
+ */
85
+ space?: ZeroScale<CSS.Property.Margin<number | string>>;
86
+
87
+ /**
88
+ * Used to define a `font-size` scale.
89
+ */
90
+ fontSizes?: Scale<CSS.Property.FontSize<number>>;
91
+
92
+ /**
93
+ * Used to define a `font-family` scale.
94
+ */
95
+ fonts?: Scale<CSS.Property.FontFamily>;
96
+
97
+ /**
98
+ * Used to define a `font-weight` scale.
99
+ */
100
+ fontWeights?: Scale<CSS.Property.FontWeight>;
101
+
102
+ /**
103
+ * Used to define a `line-height` scale.
104
+ */
105
+ lineHeights?: Scale<CSS.Property.LineHeight<string | 0 | number>>;
106
+
107
+ /**
108
+ * Used to define a `letter-spacing` scale.
109
+ */
110
+ letterSpacings?: ZeroScale<CSS.Property.LetterSpacing<string | 0 | number>>;
111
+
112
+ /**
113
+ * Used to define a scale for size values,
114
+ * like `height`, `width`, `flexBasis`, etc.
115
+ */
116
+ sizes?: ZeroScale<CSS.Property.Height<{}> | CSS.Property.Width<{}>>;
117
+
118
+ /**
119
+ * Used to define different `border` styles.
120
+ */
121
+ borders?: ZeroScale<CSS.Property.Border<{}>>;
122
+
123
+ /**
124
+ * Used to define `border-style` styles.
125
+ */
126
+ borderStyles?: Scale<CSS.Property.Border<{}>>;
127
+
128
+ /**
129
+ * Used to define `border-width` styles.
130
+ */
131
+ borderWidths?: ZeroScale<CSS.Property.BorderWidth<string | 0 | number>>;
132
+
133
+ /**
134
+ * Used to define `border-radius` styles.
135
+ */
136
+ radii?: ZeroScale<CSS.Property.BorderRadius<string | 0 | number>>;
137
+
138
+ /**
139
+ * Used to define `Shadow` styles.
140
+ */
141
+ shadows?: ZeroScale<CSS.Property.BoxShadow>;
142
+
143
+ /**
144
+ * Used to define a `z-index` scake.
145
+ */
146
+ zIndices?: Scale<CSS.Property.ZIndex>;
147
+
148
+ /**
149
+ * Used to define a `opacity` scale.
150
+ */
151
+ opacities?: Scale<CSS.Property.Opacity>;
152
+
153
+ /**
154
+ * Used to define a `transition` styles.
155
+ */
156
+ transitions?: ZeroScale<CSS.Property.Transition>;
157
+ }
package/src/types.ts CHANGED
@@ -2,14 +2,13 @@
2
2
  * Create type aliases for `theme-ui` so that it doesn't leak too much into our code.
3
3
  */
4
4
  import {
5
- Theme as ThemeUITheme,
6
5
  ThemeUIStyleObject,
7
6
  ThemeUICSSObject,
8
7
  ThemeUICSSProperties,
8
+ ResponsiveStyleValue as RSV,
9
9
  } from '@theme-ui/css';
10
- export { ResponsiveStyleValue } from '@theme-ui/css';
11
10
 
11
+ export type ResponsiveStyleValue<T> = RSV<T>;
12
12
  export type StyleObject = ThemeUIStyleObject;
13
13
  export type CSSObject = ThemeUICSSObject;
14
14
  export type CSSProperties = ThemeUICSSProperties;
15
- export type Theme = ThemeUITheme;
@@ -1,4 +1,5 @@
1
1
  import React from 'react';
2
+ import { jsx } from '@emotion/react';
2
3
  import { render, screen } from '@testing-library/react';
3
4
  import { renderHook } from '@testing-library/react-hooks';
4
5
 
@@ -11,6 +12,12 @@ const theme = {
11
12
  primary: 'hotpink',
12
13
  black: '#000',
13
14
  },
15
+ space: {
16
+ none: 0,
17
+ small: 16,
18
+ medium: 24,
19
+ large: 32,
20
+ },
14
21
  text: {
15
22
  body: {
16
23
  fontSize: 1,
@@ -27,24 +34,35 @@ const wrapper: React.FC = ({ children }) => (
27
34
  <ThemeProvider theme={theme}>{children}</ThemeProvider>
28
35
  );
29
36
 
30
- test('return theme', () => {
37
+ test('returns the theme', () => {
38
+ const { result } = renderHook(() => useTheme(), { wrapper });
39
+ expect(result.current.theme).toEqual(theme);
40
+ });
41
+
42
+ test('returns a "css" function', () => {
31
43
  const { result } = renderHook(() => useTheme(), { wrapper });
32
- expect(result.current).toMatchInlineSnapshot(`
33
- Object {
34
- "colors": Object {
35
- "black": "#000",
36
- "primary": "hotpink",
37
- },
38
- "text": Object {
39
- "body": Object {
40
- "color": "black",
41
- "fontSize": 1,
42
- },
43
- "heading": Object {
44
- "color": "primary",
45
- "fontSize": 3,
46
- },
47
- },
44
+ expect(result.current.css).toEqual(expect.any(Function));
45
+ });
46
+
47
+ test('transpile style object to css object', () => {
48
+ const { result } = renderHook(() => useTheme(), { wrapper });
49
+ const css = result.current.css;
50
+
51
+ expect(css({ p: 'small' })).toMatchInlineSnapshot(`
52
+ {
53
+ "padding": 16,
54
+ }
55
+ `);
56
+ expect(css({ color: 'primary', p: 'large' })).toMatchInlineSnapshot(`
57
+ {
58
+ "color": "hotpink",
59
+ "padding": 32,
60
+ }
61
+ `);
62
+ expect(css({ variant: 'text.body' })).toMatchInlineSnapshot(`
63
+ {
64
+ "color": "#000",
65
+ "fontSize": 14,
48
66
  }
49
67
  `);
50
68
  });
@@ -63,7 +81,7 @@ test('themes can be cascaded', () => {
63
81
  };
64
82
 
65
83
  const Theme = ({ testId }: { testId: string }) => {
66
- const theme = useTheme();
84
+ const { theme } = useTheme();
67
85
  return <div data-testid={testId}>{JSON.stringify(theme, null, 2)}</div>;
68
86
  };
69
87
 
@@ -96,3 +114,10 @@ test('themes can be cascaded', () => {
96
114
  }"
97
115
  `);
98
116
  });
117
+
118
+ test('theme is passed down to emotion', () => {
119
+ const css = jest.fn().mockReturnValue({});
120
+ render(<ThemeProvider theme={theme}>{jsx('div', { css })}</ThemeProvider>);
121
+
122
+ expect(css).toHaveBeenCalledWith(theme);
123
+ });
package/src/useTheme.tsx CHANGED
@@ -1,12 +1,50 @@
1
- import React, { createContext, useContext } from 'react';
2
- import { Theme } from '@marigold/system';
1
+ import React, {
2
+ createContext,
3
+ ReactNode,
4
+ useCallback,
5
+ useContext,
6
+ } from 'react';
7
+ import { css as transformStyleObject } from '@theme-ui/css';
8
+ import { ThemeProvider as EmotionProvider } from '@emotion/react';
3
9
 
4
- const Context = createContext<Theme>({});
10
+ import { Theme } from './theme';
11
+ import { StyleObject } from './types';
5
12
 
6
- export const useTheme = () => useContext(Context);
13
+ /**
14
+ * @internal
15
+ */
16
+ export const __defaultTheme: Theme = {};
7
17
 
8
- export type ThemeProviderProps = { theme: any };
9
- export const ThemeProvider: React.FC<ThemeProviderProps> = ({
18
+ const InternalContext = createContext<Theme>(__defaultTheme);
19
+
20
+ export const useTheme = () => {
21
+ const theme = useContext(InternalContext);
22
+ /**
23
+ * We cast the theme here to `any` since our subset is not
24
+ * compatible with the typings of `theme-ui`, since they
25
+ * also support arrays as scale values, while we don't.
26
+ */
27
+ const css = useCallback(
28
+ (style: StyleObject) => transformStyleObject(style)(theme as any),
29
+ [theme]
30
+ );
31
+ return { theme, css };
32
+ };
33
+
34
+ export type ThemeProviderProps<T extends Theme> = {
35
+ theme: T;
36
+ children: ReactNode;
37
+ };
38
+
39
+ export function ThemeProvider<T extends Theme>({
10
40
  theme,
11
41
  children,
12
- }) => <Context.Provider value={theme}>{children}</Context.Provider>;
42
+ }: ThemeProviderProps<T>) {
43
+ return (
44
+ <EmotionProvider theme={theme}>
45
+ <InternalContext.Provider value={theme}>
46
+ {children}
47
+ </InternalContext.Provider>
48
+ </EmotionProvider>
49
+ );
50
+ }
@@ -0,0 +1,93 @@
1
+ import {
2
+ appendVariantState,
3
+ conditional,
4
+ ensureArray,
5
+ ensureArrayVariant,
6
+ ensureVariantDefault,
7
+ } from './variant';
8
+
9
+ test('ensure array', () => {
10
+ expect(ensureArray('foo')).toEqual(['foo']);
11
+ expect(ensureArray(['foo'])).toEqual(['foo']);
12
+ expect(ensureArray(['foo', 'bar'])).toEqual(['foo', 'bar']);
13
+ expect(ensureArray(null)).toEqual([]);
14
+ expect(ensureArray(undefined)).toEqual([]);
15
+ });
16
+
17
+ test('ensure variant default', () => {
18
+ expect(ensureVariantDefault('foo.')).toEqual('foo');
19
+ expect(ensureVariantDefault('foo')).toEqual('foo');
20
+ });
21
+
22
+ test('ensure array of variants', () => {
23
+ expect(ensureArrayVariant('foo')).toEqual(['foo']);
24
+ expect(ensureArrayVariant(['foo'])).toEqual(['foo']);
25
+ expect(ensureArrayVariant(['foo', 'bar'])).toEqual(['foo', 'bar']);
26
+ expect(ensureArrayVariant(undefined)).toEqual([]);
27
+ });
28
+
29
+ test('append variant state', () => {
30
+ expect(appendVariantState('foo', 'checked')).toEqual('foo.:checked');
31
+ expect(appendVariantState('foo', 'focus')).toEqual('foo.:focus');
32
+ expect(appendVariantState('foo', 'disabled')).toEqual('foo.:disabled');
33
+ });
34
+
35
+ test('conditional variants', () => {
36
+ expect(
37
+ conditional('foo', {
38
+ checked: true,
39
+ })
40
+ ).toMatchInlineSnapshot(`
41
+ [
42
+ "foo",
43
+ "foo.:checked",
44
+ ]
45
+ `);
46
+
47
+ expect(
48
+ conditional('foo', {
49
+ checked: true,
50
+ focus: true,
51
+ disabled: true,
52
+ })
53
+ ).toMatchInlineSnapshot(`
54
+ [
55
+ "foo",
56
+ "foo.:checked",
57
+ "foo.:focus",
58
+ "foo.:disabled",
59
+ ]
60
+ `);
61
+
62
+ expect(
63
+ conditional('foo', {
64
+ checked: true,
65
+ focus: false,
66
+ disabled: true,
67
+ })
68
+ ).toMatchInlineSnapshot(`
69
+ [
70
+ "foo",
71
+ "foo.:checked",
72
+ "foo.:disabled",
73
+ ]
74
+ `);
75
+ });
76
+
77
+ test('disabled is always last state variant', () => {
78
+ const one = conditional('foo', {
79
+ disabled: true,
80
+ checked: true,
81
+ focus: true,
82
+ });
83
+
84
+ expect(one[one.length - 1]).toMatchInlineSnapshot(`"foo.:disabled"`);
85
+
86
+ const two = conditional('foo', {
87
+ checked: true,
88
+ disabled: true,
89
+ focus: true,
90
+ });
91
+
92
+ expect(two[two.length - 1]).toMatchInlineSnapshot(`"foo.:disabled"`);
93
+ });
package/src/variant.ts ADDED
@@ -0,0 +1,54 @@
1
+ const isNil = (value: any): value is null | undefined =>
2
+ value === null || value === undefined;
3
+
4
+ /**
5
+ * Ensures that the `val` is an array. Will return an empty array if `val` is falsy.
6
+ */
7
+ export const ensureArray = <T>(val?: T | T[]) =>
8
+ isNil(val) ? [] : Array.isArray(val) ? val : [val];
9
+
10
+ /**
11
+ * Removes trailing dot from variant, if necessary. This is necessary to support
12
+ * `__default` styles. See https://github.com/system-ui/theme-ui/pull/951
13
+ */
14
+ export const ensureVariantDefault = (val: string) => val.replace(/\.$/, '');
15
+
16
+ /**
17
+ * Ensures that the `variant` is an array and supports the `__default` key.
18
+ */
19
+ export const ensureArrayVariant = <T extends string>(variant?: T | T[]) =>
20
+ ensureArray(variant).map(ensureVariantDefault);
21
+
22
+ export type State = {
23
+ checked?: boolean;
24
+ focus?: boolean;
25
+ hover?: boolean;
26
+ disabled?: boolean;
27
+ error?: boolean;
28
+ };
29
+
30
+ /**
31
+ * Appends given `state` to a `variant`.
32
+ */
33
+ export const appendVariantState = (variant: string, state: keyof State) => {
34
+ return `${ensureVariantDefault(variant)}.:${state}`;
35
+ };
36
+
37
+ /**
38
+ * Create a variant array from a `variant` and `state` containing
39
+ * passed states, if they are truthy.
40
+ */
41
+ export const conditional = (
42
+ variant: string,
43
+ { disabled = false, ...states }: State
44
+ ) => {
45
+ const entries = [...Object.entries(states), ['disabled', disabled]] as [
46
+ keyof State,
47
+ boolean
48
+ ][];
49
+ const stateVariants = entries
50
+ .filter(([, val]) => Boolean(val))
51
+ .map(([key]) => appendVariantState(variant, key));
52
+
53
+ return [variant, ...stateVariants];
54
+ };
package/dist/cache.d.ts DELETED
@@ -1,4 +0,0 @@
1
- /**
2
- * Expose cache from emotion so we can use the same cache when doing SSR.
3
- */
4
- export { cache } from '@emotion/css';
package/dist/reset.d.ts DELETED
@@ -1,24 +0,0 @@
1
- export declare const reset: {
2
- readonly article: string;
3
- readonly aside: string;
4
- readonly details: string;
5
- readonly figcaption: string;
6
- readonly figure: string;
7
- readonly footer: string;
8
- readonly header: string;
9
- readonly hgroup: string;
10
- readonly menu: string;
11
- readonly nav: string;
12
- readonly section: string;
13
- readonly ul: string;
14
- readonly ol: string;
15
- readonly blockquote: string;
16
- readonly q: string;
17
- readonly a: string;
18
- readonly base: string;
19
- readonly table: string;
20
- readonly select: string;
21
- readonly button: string;
22
- readonly textarea: string;
23
- readonly input: string;
24
- };
@@ -1,2 +0,0 @@
1
- import { StyleObject } from './types';
2
- export declare const useClassname: (...styles: StyleObject[]) => string;
@@ -1,15 +0,0 @@
1
- import { ElementType } from 'react';
2
- import { CSSObject } from './types';
3
- export declare type UseStyleInput = {
4
- element?: ElementType;
5
- css?: Omit<CSSObject, 'variant' | 'element'> & {
6
- variant?: never;
7
- element?: never;
8
- };
9
- variant?: string | string[];
10
- className?: string;
11
- };
12
- /**
13
- * Hook that can adds base styles, reset for certain elements, variants and custom styles
14
- */
15
- export declare const useStyles: ({ element, css: styles, variant, className }: UseStyleInput) => string;