Skip to content

Commit b83fa00

Browse files
author
jedmao
committed
feat: initial release
BREAKING CHANGE: initial release
1 parent 4387f8b commit b83fa00

16 files changed

+13649
-60
lines changed

.editorconfig

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
root = true
2+
3+
[*]
4+
indent_style = tab
5+
indent_size = 3
6+
charset = utf-8
7+
trim_trailing_whitespace = true
8+
insert_final_newline = true
9+
end_of_line = lf
10+
11+
[*.md]
12+
trim_trailing_whitespace = false
13+
14+
[{*.json,*.md,*.yml}]
15+
indent_style = space
16+
indent_size = 2
17+
18+
[src/__generated__/**/*.ts]
19+
indent_style = space
20+
indent_size = 4

.eslintrc.json

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
{
2+
"env": {
3+
"browser": true,
4+
"es6": true,
5+
"jest": true,
6+
"node": true
7+
},
8+
"parser": "@typescript-eslint/parser",
9+
"parserOptions": {
10+
"ecmaFeatures": {
11+
"impliedStrict": true
12+
},
13+
"ecmaVersion": 6,
14+
"project": "./tsconfig.json",
15+
"sourceType": "module",
16+
"tsconfigRootDir": "."
17+
},
18+
"plugins": ["@typescript-eslint"],
19+
"extends": [
20+
"eslint:recommended",
21+
"prettier",
22+
"plugin:@typescript-eslint/recommended"
23+
],
24+
"rules": {
25+
// Possible Errors
26+
"no-async-promise-executor": "error",
27+
"no-await-in-loop": "error",
28+
"no-misleading-character-class": "error",
29+
"no-template-curly-in-string": "error",
30+
31+
// TypeScript-ESLint (Prettier)
32+
"@typescript-eslint/member-delimiter-style": "off",
33+
34+
// TypeScript-ESLint
35+
"@typescript-eslint/explicit-function-return-type": "off",
36+
"@typescript-eslint/indent": "off",
37+
"@typescript-eslint/no-explicit-any": "error",
38+
"@typescript-eslint/no-object-literal-type-assertion": "off",
39+
"@typescript-eslint/no-this-alias": [
40+
"error",
41+
{
42+
"allowDestructuring": true
43+
}
44+
],
45+
"@typescript-eslint/no-use-before-define": "off"
46+
}
47+
}

.gitignore

Lines changed: 9 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,12 @@
1-
# Logs
2-
logs
3-
*.log
4-
npm-debug.log*
5-
yarn-debug.log*
6-
yarn-error.log*
7-
8-
# Runtime data
9-
pids
10-
*.pid
11-
*.seed
12-
*.pid.lock
13-
14-
# Directory for instrumented libs generated by jscoverage/JSCover
15-
lib-cov
16-
17-
# Coverage directory used by tools like istanbul
18-
coverage
19-
20-
# nyc test coverage
21-
.nyc_output
22-
23-
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
24-
.grunt
25-
26-
# Bower dependency directory (https://bower.io/)
27-
bower_components
28-
29-
# node-waf configuration
30-
.lock-wscript
1+
# dependencies
2+
node_modules
313

32-
# Compiled binary addons (https://nodejs.org/api/addons.html)
33-
build/Release
4+
# testing
5+
/coverage
346

35-
# Dependency directories
36-
node_modules/
37-
jspm_packages/
7+
# production
8+
/dist
389

39-
# TypeScript v1 declaration files
40-
typings/
41-
42-
# Optional npm cache directory
43-
.npm
44-
45-
# Optional eslint cache
46-
.eslintcache
47-
48-
# Optional REPL history
49-
.node_repl_history
50-
51-
# Output of 'npm pack'
52-
*.tgz
53-
54-
# Yarn Integrity file
55-
.yarn-integrity
56-
57-
# dotenv environment variables file
58-
.env
59-
60-
# next.js build output
61-
.next
10+
npm-debug.log*
11+
yarn-debug.log*
12+
yarn-error.log*

.markdownlint.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"list-marker-space": {
3+
"ol_multi": 2,
4+
"ol_single": 2,
5+
"ul_multi": 2,
6+
"ul_single": 2
7+
}
8+
}

.travis.yml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
language: node_js
2+
3+
node_js:
4+
- 'lts/*'
5+
6+
cache: npm
7+
8+
branches:
9+
only:
10+
- master
11+
12+
script:
13+
- npx npm-install-peers
14+
- npm run cover
15+
16+
after_success:
17+
- npx codecov -f coverage/lcov.info
18+
- echo "//registry.npmjs.org/:_authToken=${NPM_TOKEN}" > ~/.npmrc
19+
20+
deploy:
21+
- provider: script
22+
script:
23+
npx semantic-release -r
24+
https://${GH_TOKEN}@github.com/${TRAVIS_REPO_SLUG}.git
25+
skip_cleanup: true
26+
keep_history: true
27+
on:
28+
branch: master

.vscode/extensions.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"recommendations": [
3+
"dbaeumer.vscode-eslint",
4+
"editorconfig.editorconfig",
5+
"esbenp.prettier-vscode",
6+
"orta.vscode-jest"
7+
]
8+
}

.vscode/settings.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"editor.formatOnSave": true,
3+
"files.associations": {
4+
".eslintrc.json": "jsonc"
5+
},
6+
"search.exclude": {
7+
"**/coverge": true,
8+
"**/dist": true,
9+
"**/node_modules": true
10+
}
11+
}

.vscode/tasks.json

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"version": "2.0.0",
3+
"tasks": [
4+
{
5+
"type": "npm",
6+
"script": "build",
7+
"group": {
8+
"kind": "build",
9+
"isDefault": true
10+
}
11+
},
12+
{
13+
"type": "npm",
14+
"script": "test",
15+
"group": {
16+
"kind": "test",
17+
"isDefault": true
18+
}
19+
},
20+
{
21+
"type": "npm",
22+
"script": "check-types",
23+
"problemMatcher": ["$tsc"]
24+
}
25+
]
26+
}

README.md

Lines changed: 138 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,138 @@
1-
# react-theme-utils
2-
Utilities for theming and theme switching via React 16.8 hooks and CSS custom properties.
1+
# react-theme-context
2+
3+
[![Travis Build Status](https://img.shields.io/travis/com/jedmao/react-theme-context.svg?style=popout-square&logo=travis)](https://travis-ci.com/jedmao/react-theme-context)
4+
[![codecov](https://img.shields.io/codecov/c/gh/jedmao/react-theme-context.svg?style=popout-square&token=202a1ea4-a048-44a2-8bd8-527f6effec6a)](https://codecov.io/gh/jedmao/react-theme-context)
5+
[![npm version](https://img.shields.io/npm/v/react-theme-context/latest.svg?style=popout-square&logo=npm)](https://www.npmjs.com/package/react-theme-context)
6+
7+
Provides theme [context](https://reactjs.org/docs/context.html) and
8+
[hooks](https://reactjs.org/docs/hooks-reference.html). Supports theme switching
9+
via
10+
[CSS custom properties](https://developer.mozilla.org/en-US/docs/Web/CSS/--*).
11+
12+
## Usage
13+
14+
_The following example uses [TypeScript](http://www.typescriptlang.org/)._
15+
16+
- You only want to create a single theme context and reuse it throughout your
17+
application.
18+
- You can create as many themes as you want.
19+
- A `Theme` is expected to be of type:
20+
`Record<string, string | number | boolean>`.
21+
22+
### themeContext.tsx
23+
24+
```tsx
25+
import ThemeContext from 'react-theme-context'
26+
27+
const defaultTheme = { primaryColor: 'red' }
28+
29+
export default new ThemeContext(defaultTheme)
30+
```
31+
32+
### App.tsx
33+
34+
```tsx
35+
import React, { FC } from 'react'
36+
37+
import themeContext from './themeContext'
38+
39+
const App: FC = () => {
40+
themeContext.useLayoutEffect()
41+
const [theme, setTheme] = themeContext.use()
42+
return (
43+
<div style={`background: ${themeContext.prop('primaryColor')}`}>
44+
<button
45+
onClick={() => {
46+
setTheme({ primaryColor: 'blue' })
47+
}}
48+
>
49+
{theme.primaryColor}
50+
</button>
51+
</div>
52+
)
53+
}
54+
55+
export default App
56+
```
57+
58+
### index.tsx
59+
60+
```tsx
61+
import React from 'react'
62+
import ReactDOM from 'react-dom'
63+
64+
import themeContext from './themeContext'
65+
import App from './App'
66+
67+
const renderApp = () =>
68+
ReactDOM.render(
69+
<themeContext.Provider>
70+
<App />
71+
</themeContext.Provider>,
72+
document.getElementById('root'),
73+
)
74+
75+
renderApp()
76+
```
77+
78+
## ThemeContext API
79+
80+
### `#Provider`
81+
82+
_See:
83+
[React Docs | Context Provider](https://reactjs.org/docs/context.html#contextprovider)_
84+
85+
### `#prop(property: keyof Theme): string`
86+
87+
Converts `property` into
88+
[CSS custom property](https://developer.mozilla.org/en-US/docs/Web/CSS/--*)
89+
syntax. In TypeScript, it also prevents you from using a theme property that is
90+
not defined.
91+
92+
#### Example
93+
94+
```ts
95+
themeContext.prop('primaryColor')
96+
// 'var(--primary-color)'
97+
```
98+
99+
### `#useLayoutEffect(element = document.documentElement)`
100+
101+
_Returns: `[T, Dispatch<SetStateAction<T>>]`_
102+
103+
Sets theme properties as
104+
[CSS custom properties](https://developer.mozilla.org/en-US/docs/Web/CSS/--*) on
105+
the provided `element` or the root `documentElement` by default. If the theme is
106+
updated, these props are updated too. This enables live theme switching!
107+
108+
_See:
109+
[React Hooks API Reference | useLayoutEffect](https://reactjs.org/docs/hooks-reference.html#uselayouteffect)_
110+
111+
### `#use()`
112+
113+
_Returns: the result of
114+
[`React.useContext`](https://reactjs.org/docs/hooks-reference.html#usecontext)_
115+
116+
## Available Scripts
117+
118+
In the project directory, you can run:
119+
120+
### `npm test`
121+
122+
Launches the [Jest][https://jestjs.io/] test runner in the interactive
123+
[watch](https://jestjs.io/docs/en/cli#watch) mode.
124+
125+
For a coverage report, run `npm test -- --coverage`.
126+
127+
### `npm run lint`
128+
129+
Runs [ESLint](https://eslint.org/).
130+
131+
### `npm run commit`
132+
133+
Runs [commitizen](https://www.npmjs.com/package/commitizen), prompting you to
134+
fill out any required commit fields at commit time.
135+
136+
### `npm run build`
137+
138+
Builds the library for [npm](https://www.npmjs.com/) into the `dist` folder.

0 commit comments

Comments
 (0)