@uistate/core 2.0.0 → 2.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +91 -115
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,122 +1,105 @@
|
|
|
1
1
|
# @uistate/core
|
|
2
2
|
|
|
3
|
-
**author**: Ajdin Imsirovic <ajdika@live.com> (GitHub)
|
|
3
|
+
**author**: Ajdin Imsirovic <ajdika@live.com> (GitHub)
|
|
4
4
|
**maintainer**: uistate <ajdika.i@gmail.com> (npm)
|
|
5
5
|
|
|
6
|
-
High-performance UI state management using CSS custom properties and
|
|
6
|
+
High-performance UI state management using CSS custom properties and ADSI (Attribute-Driven State Inheritance). Focused heavily on DX and performance.
|
|
7
7
|
|
|
8
8
|
## Features
|
|
9
9
|
|
|
10
|
-
-
|
|
11
|
-
-
|
|
10
|
+
- 🚀 Potentially O(1) state updates using CSS custom properties
|
|
11
|
+
- 📉 Significant memory savings compared to virtual DOM approaches
|
|
12
12
|
- 🎯 Zero configuration
|
|
13
|
-
- 🔄 Automatic reactivity
|
|
13
|
+
- 🔄 Automatic reactivity through CSS cascade
|
|
14
14
|
- 🎨 Framework agnostic
|
|
15
|
-
- 📦 Tiny bundle size (~
|
|
16
|
-
-
|
|
17
|
-
- ~~📊 Optional performance monitoring~~ In progress (perf pending...)
|
|
15
|
+
- 📦 Tiny bundle size (~2KB)
|
|
16
|
+
- 🧩 Modular architecture with dedicated modules for CSS state and events
|
|
18
17
|
|
|
19
18
|
## Installation
|
|
20
19
|
|
|
21
20
|
```bash
|
|
22
21
|
# Install the core package
|
|
23
22
|
npm install @uistate/core
|
|
24
|
-
|
|
25
|
-
# Optional: Install performance monitoring
|
|
26
|
-
npm install @uistate/performance
|
|
27
23
|
```
|
|
28
24
|
|
|
29
25
|
## Quick Start
|
|
30
26
|
|
|
31
|
-
```
|
|
32
|
-
import {
|
|
27
|
+
```javascript
|
|
28
|
+
import { cssState, eventState } from '@uistate/core';
|
|
33
29
|
|
|
34
30
|
// Initialize state
|
|
35
|
-
|
|
31
|
+
cssState.init();
|
|
36
32
|
|
|
37
|
-
// Set state
|
|
38
|
-
|
|
33
|
+
// Set state via CSS variables
|
|
34
|
+
cssState.set('--counter-value', '0');
|
|
39
35
|
|
|
40
36
|
// Get state
|
|
41
|
-
const count =
|
|
37
|
+
const count = parseInt(cssState.get('--counter-value'));
|
|
42
38
|
|
|
43
39
|
// Subscribe to changes
|
|
44
|
-
const unsubscribe =
|
|
45
|
-
console.log('
|
|
40
|
+
const unsubscribe = eventState.on('counter:change', (newValue) => {
|
|
41
|
+
console.log('Counter changed:', newValue);
|
|
46
42
|
});
|
|
47
43
|
|
|
48
|
-
//
|
|
49
|
-
|
|
44
|
+
// Set state with an attribute
|
|
45
|
+
document.documentElement.dataset.counterValue = count + 1;
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
### HTML Usage Example
|
|
50
49
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
50
|
+
```html
|
|
51
|
+
<button data-counter-value="0" id="counter-btn">Count: 0</button>
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
```css
|
|
55
|
+
[data-counter-value] {
|
|
56
|
+
/* Style based on state */
|
|
58
57
|
}
|
|
59
58
|
```
|
|
60
59
|
|
|
60
|
+
```javascript
|
|
61
|
+
document.getElementById('counter-btn').addEventListener('click', () => {
|
|
62
|
+
const btn = document.getElementById('counter-btn');
|
|
63
|
+
const currentValue = parseInt(btn.dataset.counterValue);
|
|
64
|
+
btn.dataset.counterValue = currentValue + 1;
|
|
65
|
+
btn.textContent = `Count: ${currentValue + 1}`;
|
|
66
|
+
eventState.emit('counter:change', currentValue + 1);
|
|
67
|
+
});
|
|
68
|
+
```
|
|
69
|
+
|
|
61
70
|
## Why @uistate/core?
|
|
62
71
|
|
|
63
72
|
### Performance
|
|
64
73
|
|
|
65
|
-
- **
|
|
66
|
-
- **
|
|
67
|
-
- **Minimal Overhead**: No virtual DOM diffing
|
|
74
|
+
- **CSS-Driven Updates**: Leverages browser's CSS engine for optimal performance with O(1) complexity
|
|
75
|
+
- **DOM as Source of Truth**: Efficient state storage using CSS custom properties and data attributes
|
|
76
|
+
- **Minimal Overhead**: No virtual DOM diffing or shadow DOM needed
|
|
68
77
|
|
|
69
78
|
### Developer Experience
|
|
70
79
|
|
|
71
|
-
- **Simple API**:
|
|
72
|
-
- **
|
|
73
|
-
- **Framework Agnostic**: Works with any framework
|
|
80
|
+
- **Simple API**: Modular `cssState` and `eventState` for clear separation of concerns
|
|
81
|
+
- **Framework Agnostic**: Works with any framework or vanilla JavaScript
|
|
74
82
|
- **Zero Config**: No store setup, no reducers, no actions
|
|
83
|
+
- **CSS-Native**: Leverages the power of CSS selectors and the cascade
|
|
75
84
|
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
The `@uistate/performance` package provides detailed performance metrics for your application:
|
|
85
|
+
### Core Concepts
|
|
79
86
|
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
// Start tracking performance
|
|
85
|
-
const tracker = PerformanceTracker.getInstance();
|
|
86
|
-
tracker.start();
|
|
87
|
-
|
|
88
|
-
// Optional: Add the performance display component to your React app
|
|
89
|
-
function App() {
|
|
90
|
-
return (
|
|
91
|
-
<div>
|
|
92
|
-
<YourApp />
|
|
93
|
-
<PerformanceDisplay />
|
|
94
|
-
</div>
|
|
95
|
-
);
|
|
96
|
-
}
|
|
97
|
-
```
|
|
98
|
-
|
|
99
|
-
The performance tracker monitors:
|
|
100
|
-
- State update duration
|
|
101
|
-
- Component render time
|
|
102
|
-
- FPS (Frames Per Second)
|
|
103
|
-
- Memory usage
|
|
104
|
-
- Long task duration
|
|
87
|
+
- **Attribute-Driven State Inheritance (ADSI)**: State represented both as CSS variables and data attributes
|
|
88
|
+
- **Hierarchical State Machines**: Model complex UI states with nested state machines
|
|
89
|
+
- **CSS-Driven State Derivation**: Derive complex states using CSS without JavaScript
|
|
105
90
|
|
|
106
91
|
## Project Structure
|
|
107
92
|
|
|
108
93
|
```
|
|
109
94
|
@uistate/core/
|
|
110
95
|
├── src/ # Core library
|
|
111
|
-
│ ├── index.
|
|
112
|
-
│ ├──
|
|
113
|
-
│
|
|
114
|
-
│
|
|
115
|
-
├── packages/
|
|
116
|
-
│ └── performance/ # Performance monitoring package
|
|
96
|
+
│ ├── index.js # Main entry
|
|
97
|
+
│ ├── cssState.js # CSS variables management
|
|
98
|
+
│ ├── eventState.js # Event-based state transitions
|
|
99
|
+
│ └── templateManager.js # Component management
|
|
117
100
|
└── examples/ # Example applications
|
|
118
|
-
├──
|
|
119
|
-
└──
|
|
101
|
+
├── basic/ # Simple examples (range sliders, toggles, etc)
|
|
102
|
+
└── advanced/ # Advanced patterns and techniques
|
|
120
103
|
```
|
|
121
104
|
|
|
122
105
|
## Browser Support
|
|
@@ -126,72 +109,65 @@ The performance tracker monitors:
|
|
|
126
109
|
- Safari 10.1+
|
|
127
110
|
- Edge 79+
|
|
128
111
|
|
|
129
|
-
#
|
|
112
|
+
# Core Ideas Behind UIstate
|
|
130
113
|
|
|
131
|
-
|
|
114
|
+
UIstate is a JavaScript-based UI state management system that leverages CSS custom properties and data attributes as the storage mechanism, paired with event-based state transitions.
|
|
132
115
|
|
|
133
116
|
## Key Components
|
|
134
117
|
|
|
135
|
-
###
|
|
118
|
+
### cssState
|
|
119
|
+
|
|
120
|
+
The `cssState` module provides methods to manage state through CSS custom properties:
|
|
136
121
|
|
|
137
|
-
```
|
|
138
|
-
|
|
139
|
-
|
|
122
|
+
```javascript
|
|
123
|
+
// Initialize CSS state management
|
|
124
|
+
cssState.init();
|
|
125
|
+
|
|
126
|
+
// Set a CSS custom property
|
|
127
|
+
cssState.set('--theme-mode', 'dark');
|
|
128
|
+
|
|
129
|
+
// Get a CSS custom property value
|
|
130
|
+
const theme = cssState.get('--theme-mode');
|
|
140
131
|
```
|
|
141
132
|
|
|
142
|
-
|
|
143
|
-
- Defines the interface for the state management system
|
|
133
|
+
### eventState
|
|
144
134
|
|
|
145
|
-
|
|
135
|
+
The `eventState` module provides an event system for state transitions:
|
|
146
136
|
|
|
147
|
-
|
|
137
|
+
```javascript
|
|
138
|
+
// Listen for state changes
|
|
139
|
+
eventState.on('theme:change', (newTheme) => {
|
|
140
|
+
console.log('Theme changed to:', newTheme);
|
|
141
|
+
});
|
|
148
142
|
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
- Initializes a CSS stylesheet for managing custom properties
|
|
152
|
-
- Returns the UIState instance for chaining
|
|
143
|
+
// Trigger state changes
|
|
144
|
+
eventState.emit('theme:change', 'light');
|
|
153
145
|
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
- Notifies observers when values change
|
|
146
|
+
// Clean up listeners
|
|
147
|
+
eventState.off('theme:change');
|
|
148
|
+
```
|
|
158
149
|
|
|
159
|
-
###
|
|
160
|
-
- Retrieves values from CSS custom properties
|
|
161
|
-
- Attempts to parse JSON values back to their original type
|
|
162
|
-
- Falls back to raw string if parsing fails
|
|
150
|
+
### templateManager
|
|
163
151
|
|
|
164
|
-
|
|
165
|
-
- Implements an observer pattern for state changes
|
|
166
|
-
- Returns a cleanup function to remove the observer
|
|
167
|
-
- Allows multiple observers per state key
|
|
152
|
+
The `templateManager` module helps with component initialization and templating:
|
|
168
153
|
|
|
169
|
-
|
|
154
|
+
```javascript
|
|
155
|
+
// Initialize components from templates
|
|
156
|
+
templateManager.init();
|
|
170
157
|
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
158
|
+
// Create a component from a template
|
|
159
|
+
const button = templateManager.createFromTemplate('button-template');
|
|
160
|
+
document.body.appendChild(button);
|
|
174
161
|
```
|
|
175
162
|
|
|
176
163
|
## Key Features
|
|
177
164
|
|
|
178
165
|
1. Uses CSS custom properties as a storage mechanism, making state changes automatically trigger UI updates
|
|
179
|
-
2. Provides
|
|
180
|
-
3. Implements
|
|
181
|
-
4.
|
|
182
|
-
|
|
183
|
-
## Example Usage
|
|
184
|
-
|
|
185
|
-
```typescript
|
|
186
|
-
UIState.init();
|
|
187
|
-
UIState.setState('theme', 'dark');
|
|
188
|
-
UIState.observe('theme', (newValue) => {
|
|
189
|
-
console.log('Theme changed to:', newValue);
|
|
190
|
-
});
|
|
191
|
-
const currentTheme = UIState.getState<string>('theme');
|
|
192
|
-
```
|
|
166
|
+
2. Provides a clear separation between state storage (CSS) and behavior (JavaScript)
|
|
167
|
+
3. Implements a pub/sub pattern for reactive updates
|
|
168
|
+
4. Leverages the CSS cascade for hierarchical state inheritance
|
|
193
169
|
|
|
194
|
-
This implementation is particularly useful for
|
|
170
|
+
This implementation is particularly useful for building UI components with clean separation of concerns and optimal performance.
|
|
195
171
|
|
|
196
172
|
## Contributing
|
|
197
173
|
|
package/package.json
CHANGED