1.1.15 • Published 5 months ago
feather-state v1.1.15
Feather State
✨ A feather light state framework ✨ 468 bytes minified and gzipped - no dependencies
Companion framework:
Live examples:
Getting started
Package
npm i feather-state
...or inline
<head>
<script src="feather-state.min.js"></script>
</head>
<body>
<script>
const { store } = window.__feather__ || {};
</script>
</body>
Index
Usage
Documentation
Examples
Usage
Basic syntax
import { store } from 'feather-state';
const { watch, ...state } = store({
completeCount: 1,
todos: [{
title: 'Todo 1',
done: true
}, {
title: 'Todo 2',
done: false
}]
});
watch(state, 'todos', (next, prev) => {
console.log(next, prev);
});
function addTodo(title: string): void {
const newTodo = {
title: title,
done: false
};
state.todos = [
...todoStore.todos,
newTodo
];
};
Documentation
store()
const { watch, state } = store(null);
const { watch, state } = store(undefined);
const { watch, state } = store(true);
const { watch, state } = store(0);
const { watch, state } = store('Hello, World!');
const { watch, state } = store(['Item 1', 'Item 2']);
const { watch, ...state } = store({ key: 'value' });
Parameters
state
- value
Return values
state
|...state
- valuewatch()
- watch for shallow mutations
store().watch()
const unwatch = watch(parent, 'key', (next, prev) => {
console.log(next, prev);
});
Parameters
parent
- parent object of watched valuekey
- key of watched valuecallback()
- function called when value changes
Return value
unwatch()
- function to unwatch value
Examples
Object values
const { watch, ...state } = store({
isPlaying: true,
playPauseAriaLabel: 'Pause'
});
watch(state, 'isPlaying', (next) => {
if (next) {
videoEl.play();
state.playPauseAriaLabel = 'Pause';
} else {
videoEl.pause();
state.playPauseAriaLabel = 'Play';
}
});
const handleVisibilitychange = () => {
if (document.visibilityState === "visible") {
state.isPlaying = true;
} else {
state.isPlaying = false;
}
};
Primitive values
const { watch, ...isPlaying } = store(true);
const playPauseAriaLabel = store('Pause');
watch(isPlaying, 'state', (next) => {
if (next) {
videoEl.play();
playPauseAriaLabel.state = 'Pause';
} else {
videoEl.pause();
playPauseAriaLabel.state = 'Play';
}
});
const handleVisibilitychange = () => {
if (document.visibilityState === "visible") {
isPlaying.state = true;
} else {
isPlaying.state = false;
}
};
Roadmap 🚀
- Find more performant way of unwatching values
- Cleaner way of referencing values in watcher parameters
- Get even tinier in size
1.1.15
5 months ago
1.1.14
5 months ago
1.1.13
6 months ago
1.1.12
6 months ago
1.1.11
6 months ago
1.1.10
1 year ago
1.1.9
1 year ago
1.1.8
1 year ago
1.1.7
1 year ago
1.1.6
1 year ago
1.1.5
1 year ago
1.1.4
1 year ago
1.1.3
1 year ago
1.1.1
2 years ago
1.1.0
2 years ago
1.1.2
2 years ago
1.0.2
2 years ago
1.0.1
2 years ago
1.0.0
2 years ago
0.1.0
2 years ago