Skip to content

Commit affb8ff

Browse files
committed
initial commit
0 parents  commit affb8ff

File tree

14 files changed

+1108
-0
lines changed

14 files changed

+1108
-0
lines changed

.github/workflows/publish-npm.yaml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
name: Publish Package to npmjs
2+
on:
3+
release:
4+
types: [published]
5+
jobs:
6+
build:
7+
runs-on: ubuntu-latest
8+
steps:
9+
- uses: actions/checkout@v3
10+
# Setup .npmrc file to publish to npm
11+
- uses: actions/setup-node@v3
12+
with:
13+
node-version: '16.x'
14+
registry-url: 'https://registry.npmjs.org'
15+
- run: npm ci
16+
- run: npm run build
17+
- run: npm publish
18+
env:
19+
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

.gitignore

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
yarn-debug.log*
6+
yarn-error.log*
7+
pnpm-debug.log*
8+
lerna-debug.log*
9+
10+
node_modules
11+
dist
12+
dist-ssr
13+
distTypes
14+
*.local
15+
16+
# Editor directories and files
17+
.vscode/*
18+
!.vscode/extensions.json
19+
.idea
20+
.DS_Store
21+
*.suo
22+
*.ntvs*
23+
*.njsproj
24+
*.sln
25+
*.sw?

LICENSE

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
MIT License
2+
3+
Copyright (c) 2023 paripsky
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of
6+
this software and associated documentation files (the "Software"), to deal in
7+
the Software without restriction, including without limitation the rights to
8+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9+
the Software, and to permit persons to whom the Software is furnished to do so,
10+
subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
# tinystate - Tiny React State Management Library
2+
3+
**tinystate** is a super small state management solution for React applications.
4+
It provides a simple and efficient way to manage and share state across your
5+
components without the complexity of larger state management libraries. With
6+
just a few lines of code, you can integrate **tinystate** into your React
7+
project and start managing your application's state effortlessly.
8+
9+
## Installation
10+
11+
You can install **tinystate** using npm or yarn:
12+
13+
```bash
14+
npm install tinystate
15+
# or
16+
yarn add tinystate
17+
```
18+
19+
## Usage
20+
21+
### Creating a Store
22+
23+
To create a store, use the `createStore` function from the **tinystate**
24+
library. You'll need to provide an initial state for your store:
25+
26+
```javascript
27+
import { createStore } from "tinystate";
28+
29+
const countStore = createStore({ initialState: 0 });
30+
```
31+
32+
### Using the Store
33+
34+
After creating a store, you can create a custom hook to access and manage the
35+
state within your React components:
36+
37+
```javascript
38+
import { createUseStore } from "tinystate";
39+
40+
export const useCountStore = createUseStore(countStore);
41+
```
42+
43+
Now, you can use the `useCountStore` hook in your components to access and
44+
update the state:
45+
46+
```javascript
47+
import { useCountStore } from "./path-to-your-store";
48+
49+
function Counter() {
50+
const [count, setCount] = useCountStore();
51+
52+
const increment = () => {
53+
setCount(count + 1);
54+
};
55+
56+
const decrement = () => {
57+
setCount(count - 1);
58+
};
59+
60+
return (
61+
<div>
62+
<p>Count: {count}</p>
63+
<button onClick={increment}>Increment</button>
64+
<button onClick={decrement}>Decrement</button>
65+
</div>
66+
);
67+
}
68+
```
69+
70+
### API Reference
71+
72+
#### `createStore(options: CreateStoreOptions)`
73+
74+
Creates a store instance with the provided initial state.
75+
76+
- `options` (object):
77+
- `initialState` (any): The initial state for the store.
78+
79+
Returns a store object with the following methods:
80+
81+
- `subscribe(fn: Listener): () => void`: Subscribes a listener function to state
82+
changes and returns an unsubscribe function.
83+
- `getSnapshot(): T`: Returns the current snapshot of the state.
84+
- `setState(newState: T): void`: Updates the state and notifies subscribers of
85+
the change.
86+
87+
#### `createUseStore(store: Store)`
88+
89+
Creates a custom hook that encapsulates the store's subscription and state
90+
management.
91+
92+
- `store` (Store): The store instance created using `createStore`.
93+
94+
Returns a function that, when called, returns a tuple containing:
95+
96+
- A getter function for the current state snapshot.
97+
- A setter function to update the state.
98+
99+
## License
100+
101+
This project is licensed under the [MIT License](LICENSE).

docs/index.html

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
4+
<head>
5+
<meta charset="UTF-8">
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7+
<link rel="stylesheet" href="styles.css">
8+
<title>tinystate Documentation</title>
9+
</head>
10+
11+
<body>
12+
<header>
13+
<h1>tinystate</h1>
14+
</header>
15+
<nav>
16+
<ul class="container">
17+
<li><a href="#installation">Installation</a></li>
18+
<li><a href="#usage">Usage</a></li>
19+
<li><a href="#api-reference">API Reference</a></li>
20+
<li><a href="#license">License</a></li>
21+
</ul>
22+
</nav>
23+
<main class="container">
24+
<section id="installation">
25+
<h2>Installation</h2>
26+
<p>You can install <strong>tinystate</strong> using npm or yarn:</p>
27+
<pre><code>npm install tinystate
28+
# or
29+
yarn add tinystate</code></pre>
30+
</section>
31+
32+
<section id="usage">
33+
<h2>Usage</h2>
34+
<p>To create a store, use the <code>createStore</code> function from the <strong>tinystate</strong> library.
35+
You'll need to provide an initial state for your store:</p>
36+
<pre><code>// Import necessary functions
37+
import { createStore, createUseStore } from 'tinystate';
38+
39+
// Create a store
40+
const countStore = createStore({ initialState: 0 });
41+
42+
// Create a custom hook to use the store
43+
export const useCountStore = createUseStore(countStore);</code></pre>
44+
<p>After creating a store, you can use the <code>useCountStore</code> hook in your components to access and update
45+
the state.</p>
46+
</section>
47+
48+
<section id="api-reference">
49+
<h2>API Reference</h2>
50+
<p>The following methods are available for managing the state in your store:</p>
51+
<pre><code>// Subscribe a listener to state changes
52+
subscribe(fn: Listener): () =&gt; void;
53+
54+
// Get the current snapshot of the state
55+
getSnapshot(): T;
56+
57+
// Update the state and notify subscribers
58+
setState(newState: T): void;</code></pre>
59+
</section>
60+
61+
<section id="license">
62+
<h2>License</h2>
63+
<p>This project is licensed under the <a href="LICENSE">MIT License</a>.</p>
64+
</section>
65+
</main>
66+
</body>
67+
68+
</html>

docs/styles.css

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/* Reset some default styles */
2+
body,
3+
h1,
4+
h2,
5+
h3,
6+
ul,
7+
li {
8+
margin: 0;
9+
padding: 0;
10+
}
11+
12+
.container {
13+
max-width: 1200px;
14+
margin: .5rem auto;
15+
padding: 0 20px;
16+
}
17+
18+
body {
19+
font-family: Arial, sans-serif;
20+
background-color: #282c36;
21+
color: #c0c5ce;
22+
line-height: 1.6;
23+
}
24+
25+
header {
26+
background-color: #1d2128;
27+
padding: 1rem;
28+
text-align: center;
29+
}
30+
31+
header h1 {
32+
font-size: 1.5rem;
33+
color: #61dafb;
34+
}
35+
36+
nav {
37+
background-color: #1d2128;
38+
padding: 1rem;
39+
}
40+
41+
nav ul {
42+
list-style: none;
43+
display: flex;
44+
gap: .5rem;
45+
}
46+
47+
nav li {
48+
margin-bottom: 0.5rem;
49+
}
50+
51+
nav a {
52+
color: #c0c5ce;
53+
text-decoration: none;
54+
}
55+
56+
nav a:hover {
57+
color: #61dafb;
58+
}
59+
60+
main {
61+
padding: 2rem;
62+
}
63+
64+
section {
65+
margin-bottom: 2rem;
66+
}
67+
68+
h2 {
69+
font-size: 1.2rem;
70+
margin-bottom: 1rem;
71+
}
72+
73+
h3 {
74+
font-size: 1rem;
75+
margin-bottom: 0.5rem;
76+
}
77+
78+
p {
79+
margin-bottom: 1rem;
80+
}
81+
82+
pre {
83+
background-color: #1d2128;
84+
color: #c0c5ce;
85+
padding: 1rem;
86+
border-radius: 4px;
87+
overflow-x: auto;
88+
}
89+
90+
code {
91+
font-family: 'Courier New', monospace;
92+
}
93+
94+
button {
95+
background-color: #61dafb;
96+
color: #282c36;
97+
border: none;
98+
padding: 0.5rem 1rem;
99+
border-radius: 4px;
100+
cursor: pointer;
101+
}
102+
103+
button:hover {
104+
background-color: #3f8fba;
105+
}

index.html

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
7+
<title>Vite + TS</title>
8+
</head>
9+
<body>
10+
<div id="app"></div>
11+
<script type="module" src="/src/main.ts"></script>
12+
</body>
13+
</html>

0 commit comments

Comments
 (0)