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

Improve type error messages and tooltips #910

Merged
merged 12 commits into from
May 28, 2020
2 changes: 1 addition & 1 deletion examples/codesandbox-starter/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"dependencies": {
"@emotion/core": "^10.0.17",
"@mdx-js/react": "^1.4.5",
"@theme-ui/presets": "^0.4.0-alpha.2",
"@theme-ui/presets": "^0.4.0-alpha.3",
"react": "^16.12.0",
"react-dom": "^16.9.0",
"theme-ui": "^0.4.0-alpha.3"
Expand Down
2 changes: 1 addition & 1 deletion packages/chrome/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"webpack-cli": "^3.3.4"
},
"devDependencies": {
"@theme-ui/css": "^0.4.0-alpha.2"
"@theme-ui/css": "^0.4.0-alpha.3"
},
"gitHead": "bfd026cae085f377ca537de897dc43233d50f5d5"
}
2 changes: 1 addition & 1 deletion packages/color-modes/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"dependencies": {
"@emotion/core": "^10.0.0",
"@theme-ui/core": "^0.4.0-alpha.3",
"@theme-ui/css": "^0.4.0-alpha.2",
"@theme-ui/css": "^0.4.0-alpha.3",
"deepmerge": "^4.2.2"
},
"peerDependencies": {
Expand Down
4 changes: 2 additions & 2 deletions packages/color/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@theme-ui/color",
"version": "0.4.0-alpha.2",
"version": "0.4.0-alpha.3",
"source": "src/index.ts",
"main": "dist/index.js",
"module": "dist/index.esm.js",
Expand All @@ -10,7 +10,7 @@
"watch": "microbundle watch --no-compress"
},
"dependencies": {
"@theme-ui/css": "^0.4.0-alpha.2",
"@theme-ui/css": "^0.4.0-alpha.3",
"polished": "^3.4.1"
},
"publishConfig": {
Expand Down
2 changes: 1 addition & 1 deletion packages/components/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"@styled-system/color": "^5.1.2",
"@styled-system/should-forward-prop": "^5.1.2",
"@styled-system/space": "^5.1.2",
"@theme-ui/css": "^0.4.0-alpha.2"
"@theme-ui/css": "^0.4.0-alpha.3"
},
"peerDependencies": {
"react": "^16.8.0"
Expand Down
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
},
"dependencies": {
"@emotion/core": "^10.0.0",
"@theme-ui/css": "^0.4.0-alpha.2",
"@theme-ui/css": "^0.4.0-alpha.3",
"deepmerge": "^4.2.2"
},
"peerDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ const BaseProvider: React.FC<BaseProviderProps> = ({ context, children }) =>
)

export interface ThemeProviderProps {
theme: Partial<Theme> | ((outerTheme: Theme) => Theme)
theme: Theme | ((outerTheme: Theme) => Theme)
children?: React.ReactNode
}

Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { SystemStyleObject } from '@theme-ui/css'
import { ThemeUIStyleObject } from '@theme-ui/css'
Copy link
Member

Choose a reason for hiding this comment

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

Not to bikeshed, but this is a much better name :)


/**
* The `sx` prop accepts a `SxStyleProp` object and properties that are part of
* the `Theme` will be transformed to their corresponding values. Other valid
* CSS properties are also allowed.
*/
export type SxStyleProp = SystemStyleObject
export type SxStyleProp = ThemeUIStyleObject

export interface SxProps {
/**
Expand Down
2 changes: 1 addition & 1 deletion packages/css/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@theme-ui/css",
"version": "0.4.0-alpha.2",
"version": "0.4.0-alpha.3",
"source": "src/index.ts",
"main": "dist/index.js",
"module": "dist/index.esm.js",
Expand Down
8 changes: 4 additions & 4 deletions packages/css/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CSSObject, SystemStyleObject, UseThemeFunction, Theme } from './types'
import { CSSObject, ThemeUIStyleObject, UseThemeFunction, Theme } from './types'

export * from './types'

Expand Down Expand Up @@ -215,10 +215,10 @@ const transforms = [
{}
)

const responsive = (styles: Exclude<SystemStyleObject, UseThemeFunction>) => (
const responsive = (styles: Exclude<ThemeUIStyleObject, UseThemeFunction>) => (
theme?: Theme
) => {
const next: Exclude<SystemStyleObject, UseThemeFunction> = {}
const next: Exclude<ThemeUIStyleObject, UseThemeFunction> = {}
const breakpoints =
(theme && (theme.breakpoints as string[])) || defaultBreakpoints
const mediaQueries = [
Expand Down Expand Up @@ -252,7 +252,7 @@ const responsive = (styles: Exclude<SystemStyleObject, UseThemeFunction>) => (

type CssPropsArgument = { theme: Theme } | Theme

export const css = (args: SystemStyleObject = {}) => (
export const css = (args: ThemeUIStyleObject = {}) => (
props: CssPropsArgument = {}
): CSSObject => {
const theme: Theme = {
Expand Down
150 changes: 73 additions & 77 deletions packages/css/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import * as CSS from 'csstype'

type StandardCSSProperties = CSS.PropertiesFallback<number | string>
type StandardCSSProperties = CSS.Properties<number | string>

/**
* The `css` function accepts arrays as values for mobile-first responsive styles.
Expand All @@ -22,12 +22,13 @@ export type ResponsiveStyleValue<T> = T | Array<T | null>
*/
export interface CSSProperties
extends CSS.StandardProperties<number | string>,
CSS.SvgProperties<number | string> {}
CSS.SvgProperties<number | string>,
CSS.VendorProperties<number | string> {}

/**
* Map of all CSS pseudo selectors (`:hover`, `:focus`, ...)
*/
export type CSSPseudoSelectorProps = { [K in CSS.Pseudos]?: SystemStyleObject }
export type CSSPseudoSelectorProps = { [K in CSS.Pseudos]?: ThemeUIStyleObject }

/**
* CSS as POJO that is compatible with CSS-in-JS libaries.
Expand All @@ -51,7 +52,7 @@ interface CSSOthersObjectForCSSObject {
* Map all nested selectors
*/
export interface CSSSelectorObject {
[cssSelector: string]: SystemStyleObject
[cssSelector: string]: ThemeUIStyleObject
}

interface AliasesCSSProperties {
Expand Down Expand Up @@ -427,20 +428,19 @@ interface OverwriteCSSProperties {
}

/**
* Map of all available CSS properties (including aliases) and their raw value.
* Only used internally to map CCS properties to input types (responsive value,
* theme function or nested) in `SystemCssProperties`.
* Map of all available CSS properties (including aliases and overwrites)
* and their raw value.
*/
interface AllSystemCSSProperties
export interface ThemeUIExtendedCSSProperties
extends Omit<CSSProperties, keyof OverwriteCSSProperties>,
AliasesCSSProperties,
OverwriteCSSProperties {}

export type SystemCssProperties = {
[K in keyof AllSystemCSSProperties]:
| ResponsiveStyleValue<AllSystemCSSProperties[K]>
| ((theme: any) => ResponsiveStyleValue<AllSystemCSSProperties[K]>)
| SystemStyleObject
export type ThemeUICSSProperties = {
[K in keyof ThemeUIExtendedCSSProperties]:
| ResponsiveStyleValue<ThemeUIExtendedCSSProperties[K]>
| ((theme: Theme) => ResponsiveStyleValue<ThemeUIExtendedCSSProperties[K]>)
| ThemeUIStyleObject
}

export interface VariantProperty {
Expand Down Expand Up @@ -469,16 +469,16 @@ export interface VariantProperty {
}

export interface UseThemeFunction {
(theme: any): Exclude<SystemStyleObject, UseThemeFunction>
(theme: any): Exclude<ThemeUIStyleObject, UseThemeFunction>
}

/**
* The `SystemStyleObject` extends [style props](https://emotion.sh/docs/object-styles)
* The `ThemeUIStyleObject` extends [style props](https://emotion.sh/docs/object-styles)
* such that properties that are part of the `Theme` will be transformed to
* their corresponding values. Other valid CSS properties are also allowed.
*/
export type SystemStyleObject =
| SystemCssProperties
export type ThemeUIStyleObject =
| ThemeUICSSProperties
| CSSPseudoSelectorProps
| CSSSelectorObject
| VariantProperty
Expand All @@ -489,27 +489,19 @@ type ObjectOrArray<T> = T[] | { [K: string]: T | ObjectOrArray<T> }
export type TLengthStyledSystem = string | 0 | number

/**
* To use Theme UI color modes, color scales should include at least a text
* and background color. These values are used in the ColorMode component to
* set body foreground and background colors. Color modes should be defined as
* nested objects within a theme.colors.modes object. Each key in this object
* should correspond to a color mode name, where the name can be anything, but
* typically light and dark are used for applications with a dark mode. The
* initialColorModeName key is required to enable color modes and will be used as
* the name for the root color palette.
* Color modes can be used to create a user-configurable dark mode
* or any number of other color modes.
*/
export type ColorMode = {
[k: string]: CSS.ColorProperty | ObjectOrArray<CSS.ColorProperty>
} & {
export interface ColorMode {
/**
* Body background color
*/
background: CSS.ColorProperty
background?: CSS.ColorProperty

/**
* Body foreground color
*/
text: CSS.ColorProperty
text?: CSS.ColorProperty

/**
* Primary brand color for links, buttons, etc.
Expand All @@ -531,6 +523,51 @@ export type ColorMode = {
* A contrast color for emphasizing UI
*/
accent?: CSS.ColorProperty

[k: string]: CSS.ColorProperty | ObjectOrArray<CSS.ColorProperty>
}

interface ColorModesScale extends ColorMode {
/**
* Nested color modes can provide overrides when used in conjunction with
* `Theme.initialColorModeName and `useColorMode()`
*/
modes?: {
[k: string]: ColorMode
}
}

export interface ThemeStyles {
tr?: ThemeUIStyleObject
th?: ThemeUIStyleObject
td?: ThemeUIStyleObject
em?: ThemeUIStyleObject
strong?: ThemeUIStyleObject
div?: ThemeUIStyleObject
p?: ThemeUIStyleObject
b?: ThemeUIStyleObject
i?: ThemeUIStyleObject
a?: ThemeUIStyleObject
h1?: ThemeUIStyleObject
h2?: ThemeUIStyleObject
h3?: ThemeUIStyleObject
h4?: ThemeUIStyleObject
h5?: ThemeUIStyleObject
h6?: ThemeUIStyleObject
img?: ThemeUIStyleObject
pre?: ThemeUIStyleObject
code?: ThemeUIStyleObject
ol?: ThemeUIStyleObject
ul?: ThemeUIStyleObject
li?: ThemeUIStyleObject
blockquote?: ThemeUIStyleObject
hr?: ThemeUIStyleObject
table?: ThemeUIStyleObject
delete?: ThemeUIStyleObject
inlineCode?: ThemeUIStyleObject
thematicBreak?: ThemeUIStyleObject
root?: ThemeUIStyleObject
[key: string]: ThemeUIStyleObject
}

export interface Theme {
Expand All @@ -549,10 +586,10 @@ export interface Theme {
radii?: ObjectOrArray<CSS.BorderRadiusProperty<TLengthStyledSystem>>
shadows?: ObjectOrArray<CSS.BoxShadowProperty>
zIndices?: ObjectOrArray<CSS.ZIndexProperty>
buttons?: ObjectOrArray<SystemCssProperties>
colorStyles?: ObjectOrArray<SystemCssProperties>
textStyles?: ObjectOrArray<SystemCssProperties>
text?: ObjectOrArray<SystemCssProperties>
buttons?: ObjectOrArray<ThemeUICSSProperties>
colorStyles?: ObjectOrArray<ThemeUICSSProperties>
textStyles?: ObjectOrArray<ThemeUICSSProperties>
text?: ObjectOrArray<ThemeUICSSProperties>
opacities?: ObjectOrArray<CSS.OpacityProperty>
/**
* Enable/disable custom CSS properties/variables if lower browser
Expand Down Expand Up @@ -590,15 +627,7 @@ export interface Theme {
/**
* Define the colors that are available through this theme
*/
colors?: ColorMode & {
/**
* Nested color modes can provide overrides when used in conjunction with
* `Theme.initialColorModeName and `useColorMode()`
*/
modes?: {
[k: string]: ColorMode
}
}
colors?: ColorModesScale

/**
* Styles for elements rendered in MDX can be added to the theme.styles
Expand All @@ -607,38 +636,5 @@ export interface Theme {
* with @styled-system/css and have access to base theme values like colors,
* fonts, etc.
*/
styles?: {
[P in StyledTags]?: SystemStyleObject
}
styles?: ThemeStyles
}

type StyledTags =
| 'tr'
| 'th'
| 'td'
| 'em'
| 'strong'
| 'div'
| 'p'
| 'b'
| 'i'
| 'a'
| 'h1'
| 'h2'
| 'h3'
| 'h4'
| 'h5'
| 'h6'
| 'img'
| 'pre'
| 'code'
| 'ol'
| 'ul'
| 'li'
| 'blockquote'
| 'hr'
| 'table'
| 'delete'
| 'inlineCode'
| 'thematicBreak'
| 'root'
Loading