Skip to content

Commit 178845c

Browse files
committed
Merge branch 'master' into convert-editor-to-typescript
2 parents efda5f6 + 0a298d3 commit 178845c

File tree

25 files changed

+832
-280
lines changed

25 files changed

+832
-280
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
"@babel/runtime": "^7.7.7",
2828
"@testing-library/react": "^9.1.3",
2929
"@types/jest": "^25.1.2",
30+
"@types/lodash.debounce": "^4.0.6",
31+
"@types/lodash.merge": "^4.6.6",
3032
"@types/react-test-renderer": "^16.9.2",
3133
"babel-jest": "^25.3.0",
3234
"husky": ">=4.0.7",

packages/chrome/README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
Chrome devtools extension for editing Theme UI styles in the browser.
44

5+
## Status: Deprecated.
6+
7+
This package is not compatible with current Theme UI version.
8+
9+
See [Blocks UI](https://github.com/blocks/blocks) for a theme editor and JSX page builder.
10+
511
## Installation
612

713
1. [Download extension](https://github.com/system-ui/theme-ui/tree/master/packages/chrome/public)
@@ -12,5 +18,5 @@ Chrome devtools extension for editing Theme UI styles in the browser.
1218

1319
## Local Development
1420

15-
1. Run `yarn prepare` (or `yarn watch`)
21+
1. Run `yarn __prepare` (or `yarn watch`)
1622
1. Load unpacked extension from the `public/` directory

packages/chrome/babel.config.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
1-
module.exports = {
2-
presets: ['@babel/preset-env', '@babel/preset-react'],
3-
}
1+
module.exports = require('../../babel.config')

packages/chrome/global.d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export {}
2+
3+
declare global {
4+
interface Window {
5+
chrome: any
6+
}
7+
}

packages/chrome/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,8 @@
3131
"webpack": "^4.33.0",
3232
"webpack-cli": "^3.3.4"
3333
},
34+
"devDependencies": {
35+
"@theme-ui/css": "^0.4.0-alpha.1"
36+
},
3437
"gitHead": "bfd026cae085f377ca537de897dc43233d50f5d5"
3538
}

packages/chrome/src/index.js renamed to packages/chrome/src/index.tsx

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
/** @jsx jsx */
22
import { jsx } from 'theme-ui'
3-
import React, { useReducer, useEffect, useRef, useState } from 'react'
3+
import {
4+
useReducer,
5+
useEffect,
6+
useRef,
7+
useState,
8+
FunctionComponent,
9+
} from 'react'
410
import { render } from 'react-dom'
511
import debounce from 'lodash.debounce'
612
import merge from 'lodash.merge'
@@ -15,13 +21,24 @@ import {
1521
LineHeights,
1622
FontSizes,
1723
Space,
24+
// @ts-ignore
1825
} from '@theme-ui/editor'
26+
import { Theme } from '@theme-ui/css'
27+
28+
interface DevToolsExceptionInfo {
29+
isError: boolean
30+
code: string
31+
description: string
32+
details: any[]
33+
isException: boolean
34+
value: string
35+
}
1936

20-
const runScript = script =>
37+
const runScript = (script: string) =>
2138
new Promise((resolve, reject) => {
2239
debounce(window.chrome.devtools.inspectedWindow.eval, 100)(
2340
script,
24-
(result, err) => {
41+
(result: object, err: DevToolsExceptionInfo) => {
2542
if (err) {
2643
console.error(err)
2744
reject(err)
@@ -31,17 +48,19 @@ const runScript = script =>
3148
)
3249
})
3350

34-
const mergeState = (state, next) => merge({}, state, next)
51+
type StateWithColorMode = { colorMode?: string; theme?: Theme }
52+
const mergeState = (state: StateWithColorMode, next: StateWithColorMode) =>
53+
merge({}, state, next)
3554

36-
const CopyTheme = ({ theme }) => {
55+
const CopyTheme = ({ theme }: { theme: Theme }) => {
3756
const [copied, setCopied] = useState(false)
38-
const timer = useRef(false)
57+
const timer = useRef(0)
3958

4059
const handleClick = () => {
4160
setCopied(true)
4261
copyToClipboard(JSON.stringify(theme))
4362
clearInterval(timer.current)
44-
timer.current = setInterval(() => setCopied(false), 1000)
63+
timer.current = window.setInterval(() => setCopied(false), 1000)
4564
}
4665

4766
return (
@@ -51,33 +70,32 @@ const CopyTheme = ({ theme }) => {
5170

5271
const Spacer = () => <div sx={{ my: 2 }} />
5372

54-
const App = () => {
73+
const App: FunctionComponent = () => {
5574
const dark = window.chrome.devtools.panels.themeName === 'dark'
5675

57-
const [state, setState] = useReducer(mergeState, {
58-
theme: null,
59-
colorMode: null,
60-
})
76+
const [state, setState] = useReducer(mergeState, {})
6177

6278
const getTheme = () => {
63-
runScript(`window.__THEME_UI__.theme`).then(theme => {
79+
runScript(`window.__THEME_UI__.theme`).then(resolvedTheme => {
80+
const theme = resolvedTheme as StateWithColorMode['theme']
6481
setState({ theme })
6582
})
6683
}
6784

6885
const getColorMode = () => {
69-
runScript(`window.__THEME_UI__.colorMode`).then(colorMode => {
86+
runScript(`window.__THEME_UI__.colorMode`).then(resolvedColorMode => {
87+
const colorMode = resolvedColorMode as StateWithColorMode['colorMode']
7088
setState({ colorMode })
7189
})
7290
}
7391

74-
const setTheme = nextTheme => {
92+
const setTheme = (nextTheme: Theme) => {
7593
const json = JSON.stringify(nextTheme)
7694
runScript(`window.__THEME_UI__.setTheme(${json})`)
7795
setState({ theme: nextTheme })
7896
}
7997

80-
const setColorMode = nextMode => {
98+
const setColorMode = (nextMode: Theme['initialColorModeName']) => {
8199
setState({ colorMode: nextMode })
82100
runScript(`window.__THEME_UI__.setColorMode('${nextMode}')`)
83101
}
@@ -96,7 +114,7 @@ const App = () => {
96114
setColorMode,
97115
}
98116

99-
if (!context.theme) return false
117+
if (!context.theme) return null
100118

101119
return (
102120
<Editor
@@ -135,7 +153,7 @@ const App = () => {
135153
<Space />
136154
</Row>
137155
<Spacer />
138-
<CopyTheme theme={state.theme} />
156+
<CopyTheme theme={context.theme} />
139157
</Editor>
140158
)
141159
}

packages/chrome/src/theme.js renamed to packages/chrome/src/theme.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
export default {
2-
initialColorMode: 'light',
1+
import { Theme } from '@theme-ui/css'
2+
3+
const theme: Theme = {
4+
initialColorModeName: 'light',
35
colors: {
46
text: '#000',
57
background: '#fff',
@@ -30,11 +32,13 @@ export default {
3032
monospace: 'Menlo, monospace',
3133
},
3234
fontWeights: {
33-
body: '400',
34-
heading: '700',
35+
body: 400,
36+
heading: 700,
3537
},
3638
lineHeights: {
3739
body: 1.5,
3840
heading: 1.25,
3941
},
4042
}
43+
44+
export default theme

packages/chrome/tsconfig.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"extends": "../../tsconfig.json",
3+
"compilerOptions": {
4+
"lib": ["ES2015", "DOM"],
5+
"jsx": "react"
6+
},
7+
"include": ["src/**/*.ts", "src/**/*.tsx", "src/**/*.d.ts", "global.d.ts"]
8+
}

packages/chrome/webpack.config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
const path = require('path')
22

33
module.exports = {
4+
entry: ['./src/index.tsx'],
45
output: {
56
path: path.resolve(__dirname, 'public', 'bundle'),
67
filename: 'index.js',
78
},
89
module: {
910
rules: [
1011
{
11-
test: /\.js$/,
12+
test: /\.tsx?$/,
1213
use: 'babel-loader',
1314
},
1415
],

0 commit comments

Comments
 (0)