-
Notifications
You must be signed in to change notification settings - Fork 0
Home
hyper-ui
is a lightweight front-end UI lib.
This package(@hyper-ui/core
) is the core of hyper-ui
which mainly deals with basic UI things.
This lib is rather lightweight. The size of this core package which you need to include is less than 10KB!
To create interactive UIs, just define simple components for each state in your app, and changes will be found as well as updated efficiently.
You can just easily include it and start writing the code of your app because it can be just plain JavaScript.
-
Use npm to install it as a dependency:
npm install @hyper-ui/core
-
Import the default export of this lib:
// es2015+ import HUI from "@hyper-ui/core"; // or es5 const HUI = require("@hyper-ui/core");
-
Use it to build your app.
-
Include one of the following script tags in your HTML file:
via jsdelivr:
<script type="text/javascript" crossorigin="anonymous" src="https://cdn.jsdelivr.net/npm/@hyper-ui/core@latest/dist/hyper-ui.core.umd.min.js"></script>
or via unpkg:
<script type="text/javascript" crossorigin="anonymous" src="https://unpkg.com/@hyper-ui/core@latest/dist/hyper-ui.core.umd.min.js"></script>
-
Access the APIs via the
HUI
global.
If you want a specified version, just replace latest
with that in the url. By the way, it is recommended to use a specified version in production.
For more information about these two CDN sites, visit www.jsdelivr.com or unpkg.com.
This lib depends on some features such as Map
, Symbol
, array.includes
and so on. So, if you want to use it in some old browsers, consider including some polyfills. For instance, include hpolyfill
by putting one of the following script tags in your HTML: (It should be put before other script tags.)
via jsdelivr:
<script type="text/javascript" crossorigin="anonymous" src="https://cdn.jsdelivr.net/npm/hpolyfill@latest/dist/index.js"></script>
or via unpkg:
<script type="text/javascript" crossorigin="anonymous" src="https://unpkg.com/hpolyfill@latest/dist/index.js"></script>
Here is a todo app example:
// Define the editor
const Editor = HUI.define('Editor', {
// Initializer
init(props, store, context) {
// Set the submission handler
store.handle('submit', event => {
// Prevent default action
event.preventDefault();
// Get the input and its value
const input = store.get('input'),
content = input.value;
// Handle submission
if (content) {
// Add the item
context.push('items', content);
// Clear the input
input.value = '';
} else {
// Hint the user to type something
input.focus();
}
});
},
// Renderer
render(props, store) {
// Render a form with an input and a submit button in it
return HUI('form', { onsubmit: store.getHandler('submit') }, [
// The input
HUI('input', {
ref: store.setter('input'),
placeholder: props.placeholder
}),
// The submit button
HUI('button', { attr: { type: 'submit' } }, 'Add')
]);
}
});
// Define the item list
const ItemList = HUI.define('ItemList', {
// Required context
context: ['items'],
// Renderer
render(props, store, context) {
// Render an unordered item list
return HUI('ul', null, context.get('items').map((content, index) => (
// A list item
HUI('li', null, [
// Item content
HUI('span', { style: { marginRight: '1em' } }, content),
// Delete link
HUI('a', {
href: 'javascript:;',
// Handle `click` event
onclick(event) {
// Delete the item
context.splice('items', index, 1);
}
}, 'Delete')
])
)));
}
});
// Define the app
const App = HUI.define('App', {
// Renderer
render() {
return [
// Heading
HUI('h1', null, 'TODO'),
// Editor
HUI(Editor, { placeholder: 'content' }),
// Item list
HUI(ItemList)
];
}
});
// Render the app
HUI.render(
HUI(App),
{ defaultContext: { items: [] } }
);