Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adopt TS in theme-ui/color #672

Merged
merged 14 commits into from
Feb 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions packages/color/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

# @theme-ui/color

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

export default props =>
export default props => (
<div
{...props}
sx={{
color: darken('primary', .25),
bg: lighten('primary', .875),
color: darken('primary', 0.25),
bg: lighten('primary', 0.875),
}}
/>
)
```

## API
Expand Down Expand Up @@ -161,7 +161,15 @@ import { invert } from '@theme-ui/color'
// invert('primary')
```

### `grayscale`

Desaturate the color to grayscale

```js
import { grayscale } from '@theme-ui/color'
// grayscale('primary')
```

### Related

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

2 changes: 2 additions & 0 deletions packages/color/package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
{
"name": "@theme-ui/color",
"version": "0.3.1",
"source": "src/index.ts",
"main": "dist/index.js",
"module": "dist/index.esm.js",
"types": "dist/index.d.ts",
"scripts": {
"prepare": "microbundle --no-compress",
"watch": "microbundle watch --no-compress"
Expand Down
31 changes: 0 additions & 31 deletions packages/color/src/index.js

This file was deleted.

85 changes: 85 additions & 0 deletions packages/color/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import * as P from 'polished'
import { get, Theme } from '@theme-ui/css'

const g = (t: Theme, c: string) =>
get(t, `colors.${c}`, c)
.replace(/^var\(--(\w+)(.*?), /, '')
.replace(/\)/, '')

/**
* Darken a color by an amount 0–1
*/
export const darken = (c: string, n: number) => (t: Theme) =>
P.darken(n, g(t, c))
/**
* Lighten a color by an amount 0–1
*/
export const lighten = (c: string, n: number) => (t: Theme) =>
P.lighten(n, g(t, c))
/**
* Rotate the hue of a color by an amount 0–360
*/
export const rotate = (c: string, d: number) => (t: Theme) =>
P.adjustHue(d, g(t, c))

/**
* Set the hue of a color to a degree 0–360
*/
export const hue = (c: string, h: number) => (t: Theme) => P.setHue(h, g(t, c))
/**
* Set the saturation of a color to an amount 0–1
*/
export const saturation = (c: string, s: number) => (t: Theme) =>
P.setSaturation(s, g(t, c))
/**
* Set the lightness of a color to an amount 0–1
*/
export const lightness = (c: string, l: number) => (t: Theme) =>
P.setLightness(l, g(t, c))
/**
* Desaturate a color by an amount 0–1
*/
export const desaturate = (c: string, n: number) => (t: Theme) =>
P.desaturate(n, g(t, c))
/**
* Saturate a color by an amount 0–1
*/
export const saturate = (c: string, n: number) => (t: Theme) =>
P.saturate(n, g(t, c))

/**
* Shade a color by an amount 0–1
*/
export const shade = (c: string, n: number) => (t: Theme) => P.shade(n, g(t, c))
/**
* Tint a color by an amount 0–1
*/
export const tint = (c: string, n: number) => (t: Theme) => P.tint(n, g(t, c))

export const transparentize = (c: string, n: number) => (t: Theme) =>
P.transparentize(n, g(t, c))
/**
* Set the transparency of a color to an amount 0-1
*/
export const alpha = (c: string, n: number) => (t: Theme) => P.rgba(g(t, c), n)

/**
* Mix two colors by a specific ratio
*/
export const mix = (a: string, b: string, n = 0.5) => (t: Theme) =>
P.mix(n, g(t, a), g(t, b))

/**
* Get the complement of a color
*/
export const complement = (c: string) => (t: Theme) => P.complement(g(t, c))

/**
* Get the inverted color
*/
export const invert = (c: string) => (t: Theme) => P.invert(g(t, c))

/**
* Desaturate the color to grayscale
*/
export const grayscale = (c: string) => desaturate(c, 1)
10 changes: 6 additions & 4 deletions packages/color/test/index.js → packages/color/test/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Theme } from '@theme-ui/css'

import {
darken,
lighten,
Expand All @@ -22,7 +24,7 @@ const theme = {
primary: '#0cf',
secondary: '#639',
},
}
} as Theme

test('desaturate', () => {
const n = desaturate('primary', 0.5)(theme)
Expand Down Expand Up @@ -114,7 +116,7 @@ const themeCustomProps = {
primary: 'var(--theme-ui-colors-primary, #0cf)',
secondary: 'var(--theme-ui-colors-primary, #639)',
},
}
} as Theme

test('desaturateCustomProps', () => {
const n = desaturate('primary', 0.5)(themeCustomProps)
Expand Down Expand Up @@ -195,7 +197,7 @@ const themeTomato = {
colors: {
primary: 'tomato',
},
}
} as Theme

test('darkenTomato', () => {
const n = darken('primary', 0.25)(themeTomato)
Expand All @@ -206,7 +208,7 @@ const themeTomatoCustomProps = {
colors: {
primary: 'var(--theme-ui-colors-primary, tomato)',
},
}
} as Theme

test('darkenTomatoCustomProps', () => {
const n = darken('primary', 0.25)(themeTomatoCustomProps)
Expand Down
10 changes: 10 additions & 0 deletions packages/color/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"compilerOptions": {
"strict": true,

"resolveJsonModule": true,
"esModuleInterop": true,
"moduleResolution": "node"
},
"include": ["src/**/*.ts", "src/**/*.tsx", "src/**/*.d.ts"]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you run into issues without that explicit include ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, tests get built.

dist
  src
  test
    index.test.d.ts

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense! Might want to add that to other files in a follow-up PR then

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is a monorepo, we could add tsconfig.json to the root with all base compiler options (like moduleResolution node and no resolveJsonModule) and then use "extends" to inherit them. The tsconfig in root would be useful when you open the entire repo in an IDE.

I'm not sure for 100%, but I think "include" is inherited literally (relative to the new config, not to the parent), so most tsconfigs in packages could look like

{ 
  "extends": "../core/tsconfig.json",
  "compilerOptions": {
    "strict": true // if possible since core is unstrict
  }
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes we should totally do this! 👍

}
2 changes: 0 additions & 2 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
"dependencies": {
"@emotion/core": "^10.0.0",
"@theme-ui/css": "^0.3.1",
"@types/styled-system": "^5.1.6",
"@types/styled-system__css": "^5.0.4",
"deepmerge": "^4.2.2"
},
"peerDependencies": {
Expand Down
5 changes: 2 additions & 3 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@ import {
InterpolationWithTheme,
} from '@emotion/core'
// @ts-ignore
import { css } from '@theme-ui/css'
import { css, Theme } from '@theme-ui/css'
import React from 'react'
import deepmerge from 'deepmerge'
import { version as __EMOTION_VERSION__ } from '@emotion/core/package.json'
import { Theme } from './theme'

import './react-jsx'

export * from './theme'
export * from './types'

const getCSS = props => {
if (!props.sx && !props.css) return undefined
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/react-jsx.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SxProps } from './theme'
import { SxProps } from './types'

declare module 'react' {
// tslint:disable-next-line: no-empty-interface
Expand Down
Loading