Skip to content

Commit ca9c226

Browse files
committed
refactor: move Theme to @theme-ui/css
1 parent 8328017 commit ca9c226

File tree

11 files changed

+732
-198
lines changed

11 files changed

+732
-198
lines changed

packages/color/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
"watch": "microbundle watch --no-compress"
1111
},
1212
"dependencies": {
13-
"@theme-ui/core": "^0.3.1",
1413
"@theme-ui/css": "^0.3.1",
1514
"polished": "^3.4.1"
1615
},

packages/color/src/declarations.d.ts

Lines changed: 0 additions & 11 deletions
This file was deleted.

packages/color/src/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import * as P from 'polished'
2-
import { get } from '@theme-ui/css'
3-
import { Theme } from '@theme-ui/core'
2+
import { get, Theme } from '@theme-ui/css'
43

54
const g = (t: Theme, c: string) =>
65
get(t, `colors.${c}`, c)

packages/core/src/index.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,14 @@ import {
44
InterpolationWithTheme,
55
} from '@emotion/core'
66
// @ts-ignore
7-
import { css } from '@theme-ui/css'
7+
import { css, Theme } from '@theme-ui/css'
88
import React from 'react'
99
import deepmerge from 'deepmerge'
1010
import { version as __EMOTION_VERSION__ } from '@emotion/core/package.json'
11-
import { Theme } from './theme'
1211

1312
import './react-jsx'
1413

15-
export * from './theme'
14+
export * from './types'
1615

1716
const getCSS = props => {
1817
if (!props.sx && !props.css) return undefined

packages/core/src/theme.ts

Lines changed: 0 additions & 158 deletions
This file was deleted.

packages/core/src/types.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import { SystemStyleObject } from '@styled-system/css'
2+
3+
/**
4+
* The `sx` prop accepts a `SxStyleProp` object and properties that are part of
5+
* the `Theme` will be transformed to their corresponding values. Other valid
6+
* CSS properties are also allowed.
7+
*/
8+
export type SxStyleProp = SystemStyleObject
9+
10+
export interface SxProps {
11+
/**
12+
* The sx prop lets you style elements inline, using values from your
13+
* theme. To use the sx prop, add the custom pragma as a comment to the
14+
* top of your module and import the jsx function.
15+
*
16+
* ```ts
17+
* // @jsx jsx
18+
*
19+
* import { jsx } from 'theme-ui'
20+
* ```
21+
*/
22+
sx?: SxStyleProp
23+
}
24+
25+
export interface IntrinsicSxElements {
26+
p: JSX.IntrinsicElements['p'] & SxProps
27+
b: JSX.IntrinsicElements['b'] & SxProps
28+
i: JSX.IntrinsicElements['i'] & SxProps
29+
a: JSX.IntrinsicElements['a'] & SxProps
30+
h1: JSX.IntrinsicElements['h1'] & SxProps
31+
h2: JSX.IntrinsicElements['h2'] & SxProps
32+
h3: JSX.IntrinsicElements['h3'] & SxProps
33+
h4: JSX.IntrinsicElements['h4'] & SxProps
34+
h5: JSX.IntrinsicElements['h5'] & SxProps
35+
h6: JSX.IntrinsicElements['h6'] & SxProps
36+
img: JSX.IntrinsicElements['img'] & SxProps
37+
pre: JSX.IntrinsicElements['pre'] & SxProps
38+
code: JSX.IntrinsicElements['code'] & SxProps
39+
ol: JSX.IntrinsicElements['ol'] & SxProps
40+
ul: JSX.IntrinsicElements['ul'] & SxProps
41+
li: JSX.IntrinsicElements['li'] & SxProps
42+
blockquote: JSX.IntrinsicElements['blockquote'] & SxProps
43+
hr: JSX.IntrinsicElements['hr'] & SxProps
44+
table: JSX.IntrinsicElements['table'] & SxProps
45+
tr: JSX.IntrinsicElements['tr'] & SxProps
46+
th: JSX.IntrinsicElements['th'] & SxProps
47+
td: JSX.IntrinsicElements['td'] & SxProps
48+
em: JSX.IntrinsicElements['em'] & SxProps
49+
strong: JSX.IntrinsicElements['strong'] & SxProps
50+
div: JSX.IntrinsicElements['div'] & SxProps
51+
delete: JSX.IntrinsicElements['div'] & SxProps
52+
inlineCode: JSX.IntrinsicElements['div'] & SxProps
53+
thematicBreak: JSX.IntrinsicElements['div'] & SxProps
54+
root: JSX.IntrinsicElements['div'] & SxProps
55+
}

packages/css/package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
{
22
"name": "@theme-ui/css",
33
"version": "0.3.1",
4+
"source": "src/index.ts",
45
"main": "dist/index.js",
56
"module": "dist/index.esm.js",
7+
"types": "dist/index.d.ts",
68
"sideEffects": false,
79
"scripts": {
810
"prepare": "microbundle --no-compress",
@@ -12,5 +14,8 @@
1214
"license": "MIT",
1315
"publishConfig": {
1416
"access": "public"
17+
},
18+
"dependencies": {
19+
"csstype": "^2.5.7"
1520
}
1621
}

packages/css/src/index.js renamed to packages/css/src/index.ts

Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,19 @@
1-
export const get = (obj, key, def, p, undef) => {
2-
key = key && key.split ? key.split('.') : [key]
3-
for (p = 0; p < key.length; p++) {
4-
obj = obj ? obj[key[p]] : undef
1+
import * as CSS from 'csstype'
2+
3+
import { SystemStyleObject, UseThemeFunction, Theme } from './types'
4+
5+
export * from './types'
6+
7+
export function get(
8+
obj: object,
9+
key: string | number,
10+
def?: unknown,
11+
p?: number,
12+
undef?: unknown
13+
): any {
14+
const path = key && typeof key === 'string' ? key.split('.') : [key]
15+
for (p = 0; p < path.length; p++) {
16+
obj = obj ? (obj as any)[path[p]] : undef
517
}
618
return obj === undef ? def : obj
719
}
@@ -29,7 +41,8 @@ const aliases = {
2941
pl: 'paddingLeft',
3042
px: 'paddingX',
3143
py: 'paddingY',
32-
}
44+
} as const
45+
type Aliases = typeof aliases
3346

3447
export const multiples = {
3548
marginX: ['marginLeft', 'marginRight'],
@@ -111,16 +124,17 @@ export const scales = {
111124
// svg
112125
fill: 'colors',
113126
stroke: 'colors',
114-
}
127+
} as const
128+
type Scales = typeof scales
115129

116-
const positiveOrNegative = (scale, value) => {
130+
const positiveOrNegative = (scale: object, value: string | number) => {
117131
if (typeof value !== 'number' || value >= 0) {
118132
return get(scale, value, value)
119133
}
120134
const absolute = Math.abs(value)
121135
const n = get(scale, absolute, absolute)
122136
if (typeof n === 'string') return '-' + n
123-
return n * -1
137+
return Number(n) * -1
124138
}
125139

126140
const transforms = [
@@ -143,9 +157,12 @@ const transforms = [
143157
{}
144158
)
145159

146-
const responsive = styles => theme => {
147-
const next = {}
148-
const breakpoints = get(theme, 'breakpoints', defaultBreakpoints)
160+
const responsive = (styles: Exclude<SystemStyleObject, UseThemeFunction>) => (
161+
theme?: Theme
162+
) => {
163+
const next: Exclude<SystemStyleObject, UseThemeFunction> = {}
164+
const breakpoints =
165+
(theme && (theme.breakpoints as string[])) || defaultBreakpoints
149166
const mediaQueries = [
150167
null,
151168
...breakpoints.map(n => `@media screen and (min-width: ${n})`),
@@ -175,8 +192,15 @@ const responsive = styles => theme => {
175192
return next
176193
}
177194

178-
export const css = args => (props = {}) => {
179-
const theme = { ...defaultTheme, ...(props.theme || props) }
195+
type CssPropsArgument = { theme: Theme } | Theme
196+
197+
export const css = (args: SystemStyleObject = {}) => (
198+
props: CssPropsArgument = {}
199+
): CSS.Properties => {
200+
const theme: Theme = {
201+
...defaultTheme,
202+
...('theme' in props ? props.theme : props),
203+
}
180204
let result = {}
181205
const obj = typeof args === 'function' ? args(theme) : args
182206
const styles = responsive(obj)(theme)
@@ -196,10 +220,10 @@ export const css = args => (props = {}) => {
196220
continue
197221
}
198222

199-
const prop = get(aliases, key, key)
200-
const scaleName = get(scales, prop)
201-
const scale = get(theme, scaleName, get(theme, prop, {}))
202-
const transform = get(transforms, prop, get)
223+
const prop = key in aliases ? aliases[key as keyof Aliases] : key
224+
const scaleName = prop in scales ? scales[prop as keyof Scales] : undefined
225+
const scale = get(theme, scaleName as any, get(theme, prop, {}))
226+
const transform: any = get(transforms, prop, get)
203227
const value = transform(scale, val, val)
204228

205229
if (multiples[prop]) {

0 commit comments

Comments
 (0)