Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
dai-shi committed Apr 17, 2024
1 parent 167f085 commit d1623b3
Show file tree
Hide file tree
Showing 20 changed files with 9,615 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .codesandbox/ci.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"buildCommand": "compile",
"sandboxes": ["new", "react-typescript-react-ts"],
"node": "18"
}
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/dist
/src/vendor
64 changes: 64 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
{
"parser": "@typescript-eslint/parser",
"plugins": [
"@typescript-eslint",
"prettier"
],
"extends": [
"plugin:@typescript-eslint/recommended",
"airbnb",
"plugin:prettier/recommended"
],
"env": {
"browser": true
},
"globals": {
"JSX": "readonly"
},
"settings": {
"import/resolver": {
"node": {
"extensions": [".js", ".ts", ".tsx"]
}
}
},
"rules": {
"react/jsx-uses-react": "off",
"react/react-in-jsx-scope": "off",
"@typescript-eslint/explicit-function-return-type": "off",
"@typescript-eslint/explicit-module-boundary-types": "off",
"@typescript-eslint/no-explicit-any": "off",
"react/jsx-filename-extension": ["error", { "extensions": [".js", ".tsx"] }],
"react/prop-types": "off",
"react/jsx-one-expression-per-line": "off",
"import/extensions": ["error", "never"],
"import/prefer-default-export": "off",
"import/no-unresolved": ["error", { "ignore": ["zustand-slices"] }],
"no-param-reassign": "off",
"no-plusplus": "off",
"no-bitwise": "off",
"symbol-description": "off",
"prefer-object-spread": "off",
"no-use-before-define": "off",
"no-unused-vars": "off",
"@typescript-eslint/no-unused-vars": ["error", {
"varsIgnorePattern": "^(createElement|Fragment)$"
}],
"no-redeclare": "off",
"react/function-component-definition": ["error", { "namedComponents": "arrow-function" }]
},
"overrides": [{
"files": ["__tests__/**/*"],
"env": {
"jest": true
},
"rules": {
"import/no-extraneous-dependencies": ["error", { "devDependencies": true }]
}
}, {
"files": ["examples/**/*"],
"rules": {
"import/no-extraneous-dependencies": "off"
}
}]
}
27 changes: 27 additions & 0 deletions .github/workflows/cd.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: CD

on:
push:
tags:
- v*

jobs:
publish:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: pnpm/action-setup@v2
with:
version: 8.2.0
- uses: actions/setup-node@v3
with:
node-version: 18
registry-url: 'https://registry.npmjs.org'
cache: 'pnpm'
cache-dependency-path: '**/pnpm-lock.yaml'
- run: pnpm install --frozen-lockfile
- run: npm test
- run: npm run compile
- run: npm publish
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
21 changes: 21 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: CI

on:
push:
pull_request:

jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: pnpm/action-setup@v2
with:
version: 8.2.0
- uses: actions/setup-node@v3
with:
node-version: 18
cache: 'pnpm'
cache-dependency-path: '**/pnpm-lock.yaml'
- run: pnpm install --frozen-lockfile
- run: npm test
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
*~
*.swp
node_modules
/dist
4 changes: 4 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"singleQuote": true,
"trailingComma": "all"
}
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Change Log

## [Unreleased]

## [0.0.1] - 2024-04-18
### Added
- Initial release
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2024 Daishi Kato

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
75 changes: 75 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,77 @@
# zustand-slices

[![CI](https://img.shields.io/github/actions/workflow/status/zustandjs/zustand-slices/ci.yml?branch=main)](https://github.com/zustandjs/zustand-slices/actions?query=workflow%3ACI)
[![npm](https://img.shields.io/npm/v/zustand-slices)](https://www.npmjs.com/package/zustand-slices)
[![size](https://img.shields.io/bundlephobia/minzip/zustand-slices)](https://bundlephobia.com/result?p=zustand-slices)
[![discord](https://img.shields.io/discord/627656437971288081)](https://discord.gg/MrQdmzd)

A slice utility for Zustand

## Install

```bash
npm install zustand zustand-slices
```

## Usage

```jsx
import { create } from 'zustand';
import { createSlice, withSlices } from 'zustand-slices';

const countSlice = createSlice({
name: 'count',
value: 0,
actions: {
inc: () => (prev) => prev + 1,
addBy: (n: number) => (prev) => prev + n,
},
});

const textSlice = createSlice({
name: 'text',
value: 'Hello',
actions: {
updateText: (newText: string) => () => newText,
},
});

const useCountStore = create(withSlices(countSlice, textSlice));

const Counter = () => {
const count = useCountStore((state) => state.count);
const inc = useCountStore((state) => state.inc);
const addBy = useCountStore((state) => state.addBy);
const text = useCountStore((state) => state.text);
const updateText = useCountStore((state) => state.updateText);
return (
<>
<div>Count: {count}</div>
<button type="button" onClick={inc}>
+1
</button>
<button type="button" onClick={() => addBy(10)}>
+10
</button>
<input value={text} onChange={(e) => updateText(e.target.value)} />
</>
);
};
```

## Examples

The [examples](examples) folder contains working examples.
You can run one of them with

```bash
PORT=8080 yarn run examples:01_counter
```

and open <http://localhost:8080> in your web browser.

You can also try them in codesandbox.io:
[01](https://codesandbox.io/s/github/zustandjs/zustand-slices/tree/main/examples/01_counter)

## Tweets

8 changes: 8 additions & 0 deletions __tests__/01_basic_spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { createSlice, withSlices } from '../src/index';

describe('basic spec', () => {
it('should export functions', () => {
expect(createSlice).toBeDefined();
expect(withSlices).toBeDefined();
});
});
27 changes: 27 additions & 0 deletions examples/01_counter/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "zustand-slices-example",
"version": "0.1.0",
"private": true,
"dependencies": {
"@types/react": "latest",
"@types/react-dom": "latest",
"react": "latest",
"react-dom": "latest",
"react-scripts": "latest",
"typescript": "latest",
"zustand": "latest",
"zustand-slices": "latest"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"browserslist": [
">0.2%",
"not dead",
"not ie <= 11",
"not op_mini all"
]
}
8 changes: 8 additions & 0 deletions examples/01_counter/public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<html>
<head>
<title>zustand-slices example</title>
</head>
<body>
<div id="app"></div>
</body>
</html>
49 changes: 49 additions & 0 deletions examples/01_counter/src/App.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { create } from 'zustand';
import { createSlice, withSlices } from 'zustand-slices';

const countSlice = createSlice({
name: 'count',
value: 0,
actions: {
inc: () => (prev) => prev + 1,
addBy: (n: number) => (prev) => prev + n,
},
});

const textSlice = createSlice({
name: 'text',
value: 'Hello',
actions: {
updateText: (newText: string) => () => newText,
},
});

const useCountStore = create(withSlices(countSlice, textSlice));

const Counter = () => {
const count = useCountStore((state) => state.count);
const inc = useCountStore((state) => state.inc);
const addBy = useCountStore((state) => state.addBy);
const text = useCountStore((state) => state.text);
const updateText = useCountStore((state) => state.updateText);
return (
<>
<div>Count: {count}</div>
<button type="button" onClick={inc}>
+1
</button>
<button type="button" onClick={() => addBy(10)}>
+10
</button>
<input value={text} onChange={(e) => updateText(e.target.value)} />
</>
);
};

const App = () => (
<div>
<Counter />
</div>
);

export default App;
13 changes: 13 additions & 0 deletions examples/01_counter/src/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { StrictMode } from 'react';
import { createRoot } from 'react-dom/client';

import App from './App';

const ele = document.getElementById('app');
if (ele) {
createRoot(ele).render(
<StrictMode>
<App />
</StrictMode>,
);
}
Loading

0 comments on commit d1623b3

Please sign in to comment.