@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.
Files changed (2) hide show
  1. package/README.md +91 -115
  2. 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 a number of other novel ideas. Focused heavily on DX and performance, in that order.
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
- - ~~🚀 44% faster state updates than Redux~~ Potentially O(1) state updates!
11
- - ~~📉 12.5% lower memory usage~~ Large memory usage savings (perf pending...)
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 (~1KB)
16
- - ~~💪 Full TypeScript support~~ Sorry, JS-only for a while...
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
- ```typescript
32
- import { UIState } from '@uistate/core';
27
+ ```javascript
28
+ import { cssState, eventState } from '@uistate/core';
33
29
 
34
30
  // Initialize state
35
- UIState.init();
31
+ cssState.init();
36
32
 
37
- // Set state
38
- UIState.setState('count', 0);
33
+ // Set state via CSS variables
34
+ cssState.set('--counter-value', '0');
39
35
 
40
36
  // Get state
41
- const count = UIState.getState('count');
37
+ const count = parseInt(cssState.get('--counter-value'));
42
38
 
43
39
  // Subscribe to changes
44
- const unsubscribe = UIState.observe('count', (newValue) => {
45
- console.log('Count changed:', newValue);
40
+ const unsubscribe = eventState.on('counter:change', (newValue) => {
41
+ console.log('Counter changed:', newValue);
46
42
  });
47
43
 
48
- // React Hook
49
- import { useUIState } from '@uistate/core/react';
44
+ // Set state with an attribute
45
+ document.documentElement.dataset.counterValue = count + 1;
46
+ ```
47
+
48
+ ### HTML Usage Example
50
49
 
51
- function Counter() {
52
- const [count, setCount] = useUIState('count', 0);
53
- return (
54
- <button onClick={() => setCount(count + 1)}>
55
- Count: {count}
56
- </button>
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
- - **44% Faster Updates**: Leverages browser's CSS engine for optimal performance
66
- - **12.5% Lower Memory**: Efficient state storage using CSS custom properties
67
- - **Minimal Overhead**: No virtual DOM diffing for state updates
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**: Just `setState`, `getState`, and `observe`
72
- - **TypeScript Support**: Full type safety and autocompletion
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
- ## Performance Monitoring
77
-
78
- The `@uistate/performance` package provides detailed performance metrics for your application:
85
+ ### Core Concepts
79
86
 
80
- ```typescript
81
- import { PerformanceTracker } from '@uistate/performance';
82
- import { PerformanceDisplay } from '@uistate/performance';
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.ts # Main entry
112
- │ ├── UIState.ts # Core implementation
113
- └── react/ # React bindings
114
- └── index.ts # React hooks
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
- ├── traditional/ # Traditional Redux app
119
- └── uistate/ # UIState implementation
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
- # Explanation of the Core Ideas Behind the UI State Management System
112
+ # Core Ideas Behind UIstate
130
113
 
131
- A TypeScript-based UI state management system that leverages CSS custom properties (CSS variables) as the storage mechanism.
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
- ### Type Definitions
118
+ ### cssState
119
+
120
+ The `cssState` module provides methods to manage state through CSS custom properties:
136
121
 
137
- ```typescript
138
- type StateObserver<T> = (value: T) => void;
139
- interface UIStateType { ... }
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
- - Defines a type for state change observers (callbacks)
143
- - Defines the interface for the state management system
133
+ ### eventState
144
134
 
145
- ## Core Functionality
135
+ The `eventState` module provides an event system for state transitions:
146
136
 
147
- The `UIState` object provides several key methods:
137
+ ```javascript
138
+ // Listen for state changes
139
+ eventState.on('theme:change', (newTheme) => {
140
+ console.log('Theme changed to:', newTheme);
141
+ });
148
142
 
149
- ### `init()`
150
- - Creates a style element in the document head
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
- ### `setState<T>(key: string, value: T)`
155
- - Stores values as CSS custom properties (--key)
156
- - Converts non-string values to JSON strings
157
- - Notifies observers when values change
146
+ // Clean up listeners
147
+ eventState.off('theme:change');
148
+ ```
158
149
 
159
- ### `getState<T>(key: string)`
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
- ### `observe<T>(key: string, callback)`
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
- ## Internal Mechanisms
154
+ ```javascript
155
+ // Initialize components from templates
156
+ templateManager.init();
170
157
 
171
- ```typescript
172
- _sheet: CSSStyleSheet | null // Stores the stylesheet reference
173
- _observers: Map<string, Set<StateObserver>> // Stores observers per key
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 type safety through TypeScript generics
180
- 3. Implements the observer pattern for reactive updates
181
- 4. Handles serialization/deserialization of complex data types through JSON
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 managing global UI state like themes, layout preferences, or any other state that might affect multiple components simultaneously.
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@uistate/core",
3
- "version": "2.0.0",
3
+ "version": "2.0.1",
4
4
  "description": "High-performance UI state management using CSS custom properties and ADSI (Attribute-Driven State Inheritance)",
5
5
  "main": "src/index.js",
6
6
  "module": "src/index.js",