Skip to content

Commit 44f31b6

Browse files
committed
feat: various refactoring & typing improvements
BREAKING CHANGE: Box has been removed, "cssProperty" has been renamed "css", "getBreakpoints" has been renamed "getScreens", "useBreakpoints" has been renamed "useScreens", "th.timingFunctions" has been renamed "th.timingFunction".
1 parent 9003deb commit 44f31b6

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

87 files changed

+2564
-4022
lines changed

CONTRIBUTING.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,11 @@ _Before_ submitting a pull request, please make sure the following is done…
4040
yarn --version
4141
```
4242

43-
4. If you've added code that should be tested, add tests. You can use watch mode that continuously transforms changed files to make your life easier.
43+
4. If you've added code that should be tested, add tests.
4444

4545
```sh
46-
# build packages continuously
47-
yarn run build:watch
46+
# build packages
47+
yarn run build
4848
# run docs
4949
yarn run dev
5050
```

benchmarks/package.json

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,16 @@
1010
"start": "webpack-dev-server"
1111
},
1212
"devDependencies": {
13-
"@babel/core": "^7.13.10",
14-
"@babel/preset-env": "^7.13.10",
15-
"@babel/preset-react": "^7.12.13",
13+
"@babel/core": "^7.13.14",
14+
"@babel/preset-env": "^7.13.12",
15+
"@babel/preset-react": "^7.13.13",
1616
"babel-loader": "^8.2.2",
1717
"benchmark": "^2.1.4",
1818
"html-webpack-plugin": "^5.3.1",
19-
"react": "^17.0.1",
20-
"react-dom": "^17.0.1",
21-
"webpack": "^5.27.1",
22-
"webpack-cli": "^4.5.0",
19+
"react": "^17.0.2",
20+
"react-dom": "^17.0.2",
21+
"webpack": "^5.30.0",
22+
"webpack-cli": "^4.6.0",
2323
"webpack-dev-server": "^3.11.2"
2424
},
2525
"dependencies": {
@@ -29,7 +29,7 @@
2929
"@xstyled/emotion": "^2.5.0",
3030
"@xstyled/styled-components": "^2.5.0",
3131
"emotion-theming": "^11.0.0",
32-
"styled-components": "^5.2.1",
32+
"styled-components": "^5.2.3",
3333
"styled-system": "^5.1.5"
3434
}
3535
}

build/rollup.config.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import path from 'path'
2+
import dts from 'rollup-plugin-dts'
3+
import esbuild from 'rollup-plugin-esbuild'
4+
5+
// eslint-disable-next-line @typescript-eslint/no-var-requires
6+
const name = require(path.resolve(
7+
process.cwd(),
8+
'./package.json',
9+
)).main.replace(/\.js$/, '')
10+
11+
const bundle = (config) => ({
12+
...config,
13+
input: 'src/index.ts',
14+
external: (id) => !/^[./]/.test(id),
15+
})
16+
17+
export default [
18+
bundle({
19+
plugins: [esbuild()],
20+
output: [
21+
{
22+
file: `${name}.js`,
23+
format: 'cjs',
24+
sourcemap: true,
25+
},
26+
{
27+
file: `${name}.mjs`,
28+
format: 'es',
29+
sourcemap: true,
30+
},
31+
],
32+
}),
33+
bundle({
34+
plugins: [dts()],
35+
output: {
36+
file: `${name}.d.ts`,
37+
format: 'es',
38+
},
39+
}),
40+
]

package.json

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,48 +22,50 @@
2222
"scripts": {
2323
"build": "lerna run build --ignore @xstyled/website --ignore @xstyled/benchmarks",
2424
"reset": "lerna run reset --ignore @xstyled/website --ignore @xstyled/benchmarks",
25-
"watch": "lerna run watch --parallel",
2625
"format": "prettier --write \"**/*.{js,ts,tsx,json,md,mdx}\"",
2726
"lint": "eslint .",
2827
"release": "lerna publish --conventional-commits && conventional-github-releaser --preset angular",
2928
"test": "jest"
3029
},
3130
"devDependencies": {
32-
"@babel/core": "^7.13.10",
33-
"@babel/preset-env": "^7.13.10",
34-
"@babel/preset-react": "^7.12.13",
31+
"@babel/core": "^7.13.14",
32+
"@babel/preset-env": "^7.13.12",
33+
"@babel/preset-react": "^7.13.13",
3534
"@babel/preset-typescript": "^7.13.0",
3635
"@emotion/jest": "^11.2.1",
3736
"@emotion/react": "^11.1.5",
3837
"@emotion/serialize": "^1.0.1",
3938
"@emotion/styled": "^11.1.5",
40-
"@testing-library/jest-dom": "^5.11.9",
41-
"@testing-library/react": "^11.2.5",
39+
"@testing-library/jest-dom": "^5.11.10",
40+
"@testing-library/react": "^11.2.6",
4241
"@types/react": "^17.0.3",
4342
"@types/styled-components": "^5.1.9",
44-
"@typescript-eslint/eslint-plugin": "^4.18.0",
45-
"@typescript-eslint/parser": "^4.18.0",
43+
"@typescript-eslint/eslint-plugin": "^4.20.0",
44+
"@typescript-eslint/parser": "^4.20.0",
4645
"babel-jest": "^26.6.3",
4746
"browser-resolve": "^2.0.0",
4847
"bundlesize": "^0.18.1",
4948
"codecov": "^3.8.1",
5049
"conventional-github-releaser": "^3.1.5",
51-
"eslint": "^7.22.0",
52-
"eslint-plugin-react": "^7.22.0",
50+
"esbuild": "^0.11.5",
51+
"eslint": "^7.23.0",
52+
"eslint-plugin-react": "^7.23.1",
5353
"eslint-plugin-react-hooks": "^4.2.0",
5454
"jest": "^26.6.3",
5555
"jest-styled-components": "^7.0.3",
5656
"lerna": "^4.0.0",
5757
"prettier": "^2.2.1",
5858
"prop-types": "^15.7.2",
59-
"react": "^17.0.1",
60-
"react-dom": "^17.0.1",
61-
"shx": "^0.3.3",
62-
"styled-components": "5.2.1",
63-
"tsdx": "^0.14.1",
59+
"react": "^17.0.2",
60+
"react-dom": "^17.0.2",
61+
"rollup": "^2.44.0",
62+
"rollup-plugin-dts": "^3.0.1",
63+
"rollup-plugin-esbuild": "^3.0.2",
64+
"styled-components": "5.2.3",
6465
"typescript": "^4.2.3"
6566
},
6667
"resolutions": {
67-
"tsdx/typescript": "^4.1.3"
68+
"tsdx/typescript": "^4.2.3",
69+
"tsdx/tslib": "^2.0.0"
6870
}
6971
}

packages/babel-preset-emotion-css-prop/package.json

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,13 @@
44
"version": "2.3.0",
55
"sideEffects": false,
66
"main": "dist/index.js",
7-
"module": "dist/babel-preset-emotion-css-prop.esm.js",
7+
"module": "dist/index.mjs",
88
"typings": "dist/index.d.js",
99
"author": "Greg Bergé <berge.greg@gmail.com>",
1010
"license": "MIT",
1111
"scripts": {
12-
"reset": "rm -rf dist && rm -rf node_modules/.cache",
13-
"watch": "tsdx watch",
14-
"build": "tsdx build",
12+
"reset": "rm -rf dist",
13+
"build": "../../node_modules/.bin/rollup -c ../../build/rollup.config.js",
1514
"prepublishOnly": "yarn reset && yarn build"
1615
},
1716
"homepage": "https://xstyled.dev",
@@ -24,7 +23,7 @@
2423
"access": "public"
2524
},
2625
"dependencies": {
27-
"@babel/plugin-transform-react-jsx": "^7.12.17",
26+
"@babel/plugin-transform-react-jsx": "^7.13.12",
2827
"@emotion/babel-plugin": "^11.2.0",
2928
"@emotion/babel-plugin-jsx-pragmatic": "^0.1.5"
3029
},

packages/babel-preset-emotion-css-prop/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"noImplicitReturns": true,
88
"noFallthroughCasesInSwitch": true,
99
"module": "esnext",
10-
"target": "ES5",
10+
"target": "esnext",
1111
"esModuleInterop": true,
1212
"moduleResolution": "node",
1313
"jsx": "react",

packages/core/package.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,13 @@
44
"version": "2.5.0",
55
"sideEffects": false,
66
"main": "dist/index.js",
7-
"module": "dist/core.esm.js",
7+
"module": "dist/index.mjs",
88
"typings": "dist/index.d.ts",
99
"author": "Greg Bergé <berge.greg@gmail.com>",
1010
"license": "MIT",
1111
"scripts": {
12-
"reset": "rm -rf dist && rm -rf node_modules/.cache",
13-
"watch": "tsdx watch",
14-
"build": "tsdx build",
12+
"reset": "rm -rf dist",
13+
"build": "../../node_modules/.bin/rollup -c ../../build/rollup.config.js",
1514
"prepublishOnly": "yarn reset && yarn build"
1615
},
1716
"homepage": "https://xstyled.dev",

packages/core/src/breakpoints.ts

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
/* eslint-disable no-undef */
22
import * as React from 'react'
3-
import { getBreakpoints, Theme } from '@xstyled/system'
3+
import { getScreens, ITheme, Screens } from '@xstyled/system'
44

5-
export function useThemeBreakpoints(theme: Theme) {
6-
return getBreakpoints({ theme })
5+
export function useThemeScreens(theme: ITheme): Screens {
6+
return getScreens({ theme })
77
}
88

99
/**
1010
* Minimum breakpoint width.
1111
* Null for the smallest breakpoint.
1212
*/
13-
function useThemeMinValue(theme: Theme, key: string | number) {
14-
const breakpoints = useThemeBreakpoints(theme)
15-
const value = breakpoints[key]
13+
function useThemeMinValue(theme: ITheme, key: string | number): number | null {
14+
const screens = useThemeScreens(theme)
15+
const value = screens[key]
1616
return value === 0 ? null : value
1717
}
1818

@@ -24,13 +24,13 @@ function useThemeMinValue(theme: Theme, key: string | number) {
2424
* Uses 0.02px rather than 0.01px to work around a current rounding bug in Safari.
2525
* See https://bugs.webkit.org/show_bug.cgi?id=178261
2626
*/
27-
function useThemeMaxValue(theme: Theme, key: string | number) {
28-
const breakpoints = useThemeBreakpoints(theme)
29-
const breakPoint = breakpoints[key]
27+
function useThemeMaxValue(theme: ITheme, key: string | number): number | null {
28+
const screens = useThemeScreens(theme)
29+
const breakPoint = screens[key]
3030
return breakPoint === 0 ? null : breakPoint - 0.02
3131
}
3232

33-
export function useViewportWidth() {
33+
export function useViewportWidth(): number | null {
3434
const [width, setWidth] = React.useState(
3535
typeof window === 'undefined' ? null : window.innerWidth,
3636
)
@@ -50,27 +50,25 @@ export function useViewportWidth() {
5050
return width
5151
}
5252

53-
export function useThemeBreakpoint(theme: Theme) {
54-
const breakpoints = useThemeBreakpoints(theme)
53+
export function useThemeBreakpoint(theme: ITheme): string | null {
54+
const screns = useThemeScreens(theme)
5555
const width = useViewportWidth()
5656
return React.useMemo(() => {
5757
return (
58-
Object.keys(breakpoints)
58+
Object.keys(screns)
5959
.reverse()
60-
.find(
61-
(breakpoint) => width !== null && width > breakpoints[breakpoint],
62-
) || null
60+
.find((screen) => width !== null && width > screns[screen]) || null
6361
)
64-
}, [breakpoints, width])
62+
}, [screns, width])
6563
}
6664

67-
export function useThemeUp(theme: Theme, key: string | number) {
65+
export function useThemeUp(theme: ITheme, key: string | number): boolean {
6866
const value = useThemeMinValue(theme, key)
6967
const width = useViewportWidth()
7068
return width !== null && value !== null && width >= value
7169
}
7270

73-
export function useThemeDown(theme: Theme, key: string | number) {
71+
export function useThemeDown(theme: ITheme, key: string | number): boolean {
7472
const value = useThemeMaxValue(theme, key)
7573
const width = useViewportWidth()
7674
return width !== null && value !== null && width < value

packages/core/src/colorModes.tsx

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,30 @@
22
/* eslint-disable react/no-danger */
33
/* eslint-env browser */
44
import * as React from 'react'
5+
import { Colors } from '@xstyled/system'
56
import {
67
toCustomPropertiesDeclarations,
78
toCustomPropertiesReferences,
89
} from './customProperties'
910

1011
type ColorModeState = [string | null, (mode: string | null) => void]
11-
type Color = string | ((props: Record<string, unknown>) => Color)
12-
type Colors = Record<string, Color>
12+
13+
interface ColorModes {
14+
[key: string]: Colors
15+
}
1316

1417
interface ITheme {
1518
useCustomProperties?: boolean
1619
useColorSchemeMediaQuery?: boolean
1720
initialColorModeName?: string
1821
defaultColorModeName?: string
1922
colors?: Colors & {
20-
modes?: Record<string, Colors>
23+
modes?: ColorModes
2124
}
2225
}
2326

2427
interface IColorModeTheme extends ITheme {
25-
colors: Colors & { modes: Record<string, Colors> }
28+
colors: Colors & { modes: ColorModes }
2629
}
2730

2831
const STORAGE_KEY = 'xstyled-color-mode'
@@ -105,7 +108,7 @@ function getDefaultColorModeName(theme: ITheme): string {
105108
return theme.defaultColorModeName || getInitialColorModeName(theme)
106109
}
107110

108-
function getUsedColorKeys(modes: Record<string, Record<string, Color>>) {
111+
function getUsedColorKeys(modes: ColorModes) {
109112
let keys: string[] = []
110113
for (const key in modes) {
111114
keys = [...keys, ...Object.keys(modes[key])]

packages/core/src/createBox.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { system } from '@xstyled/system'
22

3-
export function createBox() {
3+
export function createBox(): (string | typeof system)[] {
44
return [`&&{`, system, `}`]
55
}
66
createBox.meta = system.meta

0 commit comments

Comments
 (0)