French for Myopia
Noun
- the quality of being short-sighted
- lack of foresight.
I thought would be a perfect name for a skinned, simplified, alternative to Vue (with some code ripped from ReefJS.
- Component-based rendering: Build dynamic UIs with a minimalistic approach.
- Data binding: Two-way binding for form inputs and data.
- Efficient DOM diffing: Only updates changed parts of the DOM.
- Lifecycle hooks: Customize initialization and rendering.
- Lightweight: Minimal overhead for small to medium-sized projects.
In browser:
<script type="module">
import Myopie from 'https://www.unpkg.com/myopie.js/myopie.min.js';
...
</script>
In node:
import Myopie from 'myopie.js';
...
You can create a Myopie instance by calling its constructor:
const myopie = new Myopie(
'#app', // Target selector
(data) => `<div>Hello, ${data.name}!</div>`, // Render function
{ name: 'World' }, // Initial state
[['input', 'name']], // Input bindings
500 // Debounce time in milliseconds
);
Manually triggers the rendering of the DOM.
myopie.render();
Retrieves a value from the state using a slash-separated path.
const name = myopie.get('name');
Updates the state at the given path and optionally triggers a re-render.
myopie.set('name', 'Myopie');
<div id="app"></div>
<script type="module">
import Myopie from 'https://www.unpkg.com/myopie.js/myopie.min.js';
const myopie = new Myopie(
'#app',
(data) => `<div>
<input type="text" value="${data.name}" />
<p>Hello, ${data.name}!</p>
</div>`,
{ name: 'World' },
[['input', 'name']]
);
// Update the name dynamically
setTimeout(() => myopie.set('name', 'User'), 2000);
</script>
Creates a new Myopie instance.
target
(string): Selector for the root element.renderFunction
(function): Function returning the HTML string to render.initialState
(object): Initial state data.bindings
(array): List of input bindings in the format[selector, path]
.debounce
(number): Debounce time in milliseconds for updates.
Gets the value of a property from the state.
path
(string): Slash-separated path to the property.
Sets a property in the state and optionally triggers a re-render.
path
(string): Slash-separated path to the property.value
: New value to set.render
(boolean): Whether to trigger a render after setting.
Manually triggers a re-render of the DOM.
Myopie provides several lifecycle hooks that allow you to inject custom behavior at specific points during the component's initialization and rendering process. These hooks are executed with specific parameters based on the type of hook.
-
Initialization Hooks
HooksInitAddPre
: Executed before the component is initialized. Receives the current state data as a parameter.myopie.HooksInitAddPre((dataCurrent) => { console.log('This runs before initialization.', dataCurrent); });
HooksInitAddPost
: Executed after the component is initialized. Receives the current state data as a parameter.myopie.HooksInitAddPost((dataCurrent) => { console.log('This runs after initialization.', dataCurrent); });
-
Rendering Hooks
HooksRenderAddPre
: Executed before the component renders. Receives the current and previous state data arrays as parameters.myopie.HooksRenderAddPre((dataCurrent, dataPrevious) => { console.log('This runs before rendering.', dataCurrent, dataPrevious); });
HooksRenderAddPost
: Executed after the component renders. Receives the current and previous state data arrays as parameters.myopie.HooksRenderAddPost((dataCurrent, dataPrevious) => { console.log('This runs after rendering.', dataCurrent, dataPrevious); });
Contributions are welcome! Please submit issues or pull requests on the GitHub repository.
Myopie is released under the BSD-3-Clause License.