Skip to content

Commit 325e449

Browse files
authored
Merge pull request #672 from hasparus/ts-color
Adopt TS in theme-ui/color
2 parents 2810436 + 7a5104b commit 325e449

File tree

21 files changed

+849
-246
lines changed

21 files changed

+849
-246
lines changed

packages/color/README.md

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
# @theme-ui/color
32

43
Color manipulation utilities for Theme UI
@@ -14,14 +13,15 @@ Import utilities from the `@theme-ui/color` package and use them with colors in
1413
import { jsx } from 'theme-ui'
1514
import { darken, lighten } from '@theme-ui/color'
1615

17-
export default props =>
16+
export default props => (
1817
<div
1918
{...props}
2019
sx={{
21-
color: darken('primary', .25),
22-
bg: lighten('primary', .875),
20+
color: darken('primary', 0.25),
21+
bg: lighten('primary', 0.875),
2322
}}
2423
/>
24+
)
2525
```
2626

2727
## API
@@ -161,7 +161,15 @@ import { invert } from '@theme-ui/color'
161161
// invert('primary')
162162
```
163163

164+
### `grayscale`
165+
166+
Desaturate the color to grayscale
167+
168+
```js
169+
import { grayscale } from '@theme-ui/color'
170+
// grayscale('primary')
171+
```
172+
164173
### Related
165174

166175
- [Polished](https://polished.js.org)
167-

packages/color/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
{
22
"name": "@theme-ui/color",
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
"scripts": {
79
"prepare": "microbundle --no-compress",
810
"watch": "microbundle watch --no-compress"

packages/color/src/index.js

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

packages/color/src/index.ts

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import * as P from 'polished'
2+
import { get, Theme } from '@theme-ui/css'
3+
4+
const g = (t: Theme, c: string) =>
5+
get(t, `colors.${c}`, c)
6+
.replace(/^var\(--(\w+)(.*?), /, '')
7+
.replace(/\)/, '')
8+
9+
/**
10+
* Darken a color by an amount 0–1
11+
*/
12+
export const darken = (c: string, n: number) => (t: Theme) =>
13+
P.darken(n, g(t, c))
14+
/**
15+
* Lighten a color by an amount 0–1
16+
*/
17+
export const lighten = (c: string, n: number) => (t: Theme) =>
18+
P.lighten(n, g(t, c))
19+
/**
20+
* Rotate the hue of a color by an amount 0–360
21+
*/
22+
export const rotate = (c: string, d: number) => (t: Theme) =>
23+
P.adjustHue(d, g(t, c))
24+
25+
/**
26+
* Set the hue of a color to a degree 0–360
27+
*/
28+
export const hue = (c: string, h: number) => (t: Theme) => P.setHue(h, g(t, c))
29+
/**
30+
* Set the saturation of a color to an amount 0–1
31+
*/
32+
export const saturation = (c: string, s: number) => (t: Theme) =>
33+
P.setSaturation(s, g(t, c))
34+
/**
35+
* Set the lightness of a color to an amount 0–1
36+
*/
37+
export const lightness = (c: string, l: number) => (t: Theme) =>
38+
P.setLightness(l, g(t, c))
39+
/**
40+
* Desaturate a color by an amount 0–1
41+
*/
42+
export const desaturate = (c: string, n: number) => (t: Theme) =>
43+
P.desaturate(n, g(t, c))
44+
/**
45+
* Saturate a color by an amount 0–1
46+
*/
47+
export const saturate = (c: string, n: number) => (t: Theme) =>
48+
P.saturate(n, g(t, c))
49+
50+
/**
51+
* Shade a color by an amount 0–1
52+
*/
53+
export const shade = (c: string, n: number) => (t: Theme) => P.shade(n, g(t, c))
54+
/**
55+
* Tint a color by an amount 0–1
56+
*/
57+
export const tint = (c: string, n: number) => (t: Theme) => P.tint(n, g(t, c))
58+
59+
export const transparentize = (c: string, n: number) => (t: Theme) =>
60+
P.transparentize(n, g(t, c))
61+
/**
62+
* Set the transparency of a color to an amount 0-1
63+
*/
64+
export const alpha = (c: string, n: number) => (t: Theme) => P.rgba(g(t, c), n)
65+
66+
/**
67+
* Mix two colors by a specific ratio
68+
*/
69+
export const mix = (a: string, b: string, n = 0.5) => (t: Theme) =>
70+
P.mix(n, g(t, a), g(t, b))
71+
72+
/**
73+
* Get the complement of a color
74+
*/
75+
export const complement = (c: string) => (t: Theme) => P.complement(g(t, c))
76+
77+
/**
78+
* Get the inverted color
79+
*/
80+
export const invert = (c: string) => (t: Theme) => P.invert(g(t, c))
81+
82+
/**
83+
* Desaturate the color to grayscale
84+
*/
85+
export const grayscale = (c: string) => desaturate(c, 1)

packages/color/test/index.js renamed to packages/color/test/index.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { Theme } from '@theme-ui/css'
2+
13
import {
24
darken,
35
lighten,
@@ -22,7 +24,7 @@ const theme = {
2224
primary: '#0cf',
2325
secondary: '#639',
2426
},
25-
}
27+
} as Theme
2628

2729
test('desaturate', () => {
2830
const n = desaturate('primary', 0.5)(theme)
@@ -114,7 +116,7 @@ const themeCustomProps = {
114116
primary: 'var(--theme-ui-colors-primary, #0cf)',
115117
secondary: 'var(--theme-ui-colors-primary, #639)',
116118
},
117-
}
119+
} as Theme
118120

119121
test('desaturateCustomProps', () => {
120122
const n = desaturate('primary', 0.5)(themeCustomProps)
@@ -195,7 +197,7 @@ const themeTomato = {
195197
colors: {
196198
primary: 'tomato',
197199
},
198-
}
200+
} as Theme
199201

200202
test('darkenTomato', () => {
201203
const n = darken('primary', 0.25)(themeTomato)
@@ -206,7 +208,7 @@ const themeTomatoCustomProps = {
206208
colors: {
207209
primary: 'var(--theme-ui-colors-primary, tomato)',
208210
},
209-
}
211+
} as Theme
210212

211213
test('darkenTomatoCustomProps', () => {
212214
const n = darken('primary', 0.25)(themeTomatoCustomProps)

packages/color/tsconfig.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"compilerOptions": {
3+
"strict": true,
4+
5+
"resolveJsonModule": true,
6+
"esModuleInterop": true,
7+
"moduleResolution": "node"
8+
},
9+
"include": ["src/**/*.ts", "src/**/*.tsx", "src/**/*.d.ts"]
10+
}

packages/core/package.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919
"dependencies": {
2020
"@emotion/core": "^10.0.0",
2121
"@theme-ui/css": "^0.3.1",
22-
"@types/styled-system": "^5.1.6",
23-
"@types/styled-system__css": "^5.0.4",
2422
"deepmerge": "^4.2.2"
2523
},
2624
"peerDependencies": {

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/react-jsx.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { SxProps } from './theme'
1+
import { SxProps } from './types'
22

33
declare module 'react' {
44
// tslint:disable-next-line: no-empty-interface

0 commit comments

Comments
 (0)