Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/vimeshjs/vimesh-ui
Vimesh UI is an ultra lightweight library to build UI components for Alpine.js
https://github.com/vimeshjs/vimesh-ui
alpinejs ui
Last synced: 2 days ago
JSON representation
Vimesh UI is an ultra lightweight library to build UI components for Alpine.js
- Host: GitHub
- URL: https://github.com/vimeshjs/vimesh-ui
- Owner: vimeshjs
- License: mit
- Created: 2022-02-21T13:33:31.000Z (almost 3 years ago)
- Default Branch: master
- Last Pushed: 2024-09-11T12:44:34.000Z (3 months ago)
- Last Synced: 2024-12-04T03:04:20.136Z (9 days ago)
- Topics: alpinejs, ui
- Language: JavaScript
- Homepage:
- Size: 591 KB
- Stars: 137
- Watchers: 6
- Forks: 5
- Open Issues: 8
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome - Vimesh UI - Agile UI framework for Alpine.js
README
# Why Vimesh UI
I hate compiling frontend code with complex toolchains, like webpack, rollup, vite etc. Unfortunately, most frontend frameworks heavily depends on them. Alpine.js is clean, powerful and without extra build process. While it is a challenge to develop a UI library directly with Alpine.js. Vimesh UI is an ultra lightweight library to build UI components for Alpine.js.# Basic Usages
Just add Vimesh UI CDN url before Alpine.js
```html
```
Now there are three important Alpine.js directives to build your own UI components## x-component
This directive creates an HTML native custom element around Alpine.js template.```html
Vimesh UI
Hello
```
[Run on codepen](https://codepen.io/vimeshjs/pen/mdKKMpb)It shows `Hello Vimesh UI`. Now let's add some interaction logic. There are two magics `$api` and `$prop` for a Vimesh UI component. `$api` comes from the return object of the first `` inside of component template. `$prop` is function to get the passed value of component property:
```html
<head>
<script src="https://unpkg.com/@vimesh/ui">
Click me
return {
say() {
alert(this.$prop('greeting-word') + ' ' + this.$prop('who'))
}
}
```
[Run on codepen](https://codepen.io/vimeshjs/pen/JjZBvPy)
The default custom element namespace is `vui`, which could be modified in config. You could also give a different namespace with format `x-component:{namespace}="component name"`
```html
$vui.config = {
namespace: 'myui'
}
My UI
My UI
Hello
Hi
```
[Run on codepen](https://codepen.io/vimeshjs/pen/LYrrjdq)The final html result will be
```html
...
Hello My UI
Hi My UI
```
In some cases, we do not want the component tag to exist in the result. We could just add an `unwrap` modifier in `x-component`.```html
...My UI
My UI
Hello
Hi
```
The component tags `myui-greeting` and `new-greeting` will no longer exist in the final html result
```html
...
Hello My UI
Hi My UI
```
## x-import
Of course, we don't want to embed common components in every page. The `x-import` directive helps to load remote components asynchronously. Let's extract the greeting component into a standalone file.
> /hello-remote.html
```html
$vui.config.importMap = {
"*": './components/${component}.html'
}
[x-cloak] {
display: none !important;
}
Vimesh UI
```
> /components/greeting.html
```html
Cloud Hello
```
The components could be loaded from anywhere, like
```html
$vui.config.importMap = {
"*": 'https://unpkg.com/@vimesh/ui/examples/components/${component}.html'
}
```
[Run on codepen](https://codepen.io/vimeshjs/pen/poKKrYd)`x-import` could load components from different namespaces. The syntax is `x-import="namespace1:comp11,comp12;namespace2:comp21,comp22;..."`. For default namespace, it could be omitted.
Here is a more complete example:
> /counters.html
```html
$vui.config.importMap = {
"*": './components/${component}.html'
}
[x-cloak] {
display: none !important;
}
Rename the 2nd counter :
```
> /components/counter.html
```html
Owner:
Step:
Value :
Increase
return {
init() {
console.log(`Value : ${this.value} , Step : ${this.step}`)
},
increase() {
this.value += this.step
}
}
```
> /components/counter-trigger.html
```html
Tigger from child element```
[Run on codepen](https://codepen.io/vimeshjs/pen/RwJBygE)### How to import dynamic components ?
Add `dynamic` modifier to `x-import`, it will evaluate the express to a string or array and then import all these components. Please refer the example in `examples/spa/app.html`.
```html
...
```### Auto import all components
Set the `autoImport` config to `true`, Vimesh UI will automatically try to import all custom html elements. Most `x-import` could be omitted. For example the previous `counters.html` could be rewritten as
```html
$vui.config.importMap = {
"*": './components/${component}.html'
}
$vui.config.autoImport = true
[x-cloak] {
display: none !important;
}
Rename the 2nd counter :
```
## x-include
Sometimes we just need to load a piece of html. The `x-include` is convenient to use in this case. The `unwrap` modifier is used to remove the host html tag.> /include-article.html
```html
Load into the external "div" tag:
Unwrap the external "div" tag:
```
> /static/article.html
```htmlTitle
Content
```The final result will be
```html
Load into the external "div" tag:
Title
Content
Unwrap the external "div" tag:
Title
Content
```
[Run on codepen](https://codepen.io/vimeshjs/pen/ExRpLbb)
## x-shtml
In Vimesh UI, please use x-shtml instead of Alpine.js original x-html, which has wrong behaviors in case of complex component lifecycle.# Advanced Usage
## $api for component
`x-data` is very convenient to use. Its data is accessible to all descendant elements. There is no problems for simple static web page. When developping reusable components, `x-data` is too open to store component own states. We do not want the properties to be modified occasionally just because of name confliction. `$api` allows to define **private** properties and methods. `$api` is only available to current component. At the same time, it inherets from x-data. That means if `this.somePropOrMethod` does not exist in $api, it will check `somePropOrMethod` from x-data. `$api` has some predefined properties and methods:| Properties | Description |
| ----------- | ----------- |
| $meta | Get the meta info of current component, including type, namespace, prefix. |
| $parent | Get the closest parent component element || Methods | Description |
| ----------- | ----------- |
| $of('component type') | Find the $api of specific component type of its ancestors. If the component type is empty, it will return the $api of its closest parent component |
| $closest(filter) | Find the closest ancestor component element according to the filter, which could be component type or a function |
| $find(filter) | Find all descendant component element according to the filter, which could be component type or a function |
| $findOne(filter) | It is similar to $find, but only return the first component element match the filter |`x-data` has two lifecycle hooks: init and destroy. `$api` has equivalents:
| x-data | $api |
| ----------- | ----------- |
| init() | onMounted() |
| destroy() | onUnmounted() |## $vui global variable
Once Vimesh UI is initialized, there is a global variable `$vui` attached to `window`.| Property/Method | Description |
| ----------- | ----------- |
| $vui.config | A config object of `debug` mode flag and `importMap` |
| $vui._ | An utility object of some most used functions: `isString`, `isArray`, `isFunction`, `isPlainObject`, `each`, `map`, `filter`, `extend` |
| $vui.getComponentMeta(element) | Get component type and namespace from html element |
| $vui.isComponent(element) | Return true if an html element is a Vimesh UI component |
| $vui.visitComponents(elContainer, callback) | Recursively visit all components inside an html container element with callback |
| $vui.findChildComponents(elContainer, filter) | Find all child component inside an html container element with specific filter, which could be component type or a function |
| $vui.getParentComponent(element) | Get the parent component of specific html element |
| $vui.findClosestComponent(element, filter) | Find the closest ancestor component element according to the filter, which could be component type or a function |
| $vui.$api(element) | Get $api of a component element |
| $vui.$data(element) | Alias of Alpine.$data(element) |
| $vui.setHtml(elContainer, html) | Load html into a container element. And Vimesh UI components in the html will be correctly initialized |
| $vui.defer(callback) | Execute callback in next event loop. |
| $vui.dom(html) | Load a plain html into dom with Vimesh UI components correctly initialized |
| $vui.nextTick(callback) | Alias of Alpine.nextTick(callback) |
| $vui.effect(callback) | Alias of Alpine.effect(callback) |
| $vui.focus(element, option) | Try to make an html element focused |
| $vui.scrollIntoView(element) | Try to scroll an html element into view |## Multi pages application
Check [mpa example](/examples/mpa)## Single page application
Check [spa example](/examples/spa/app.html)## Real UI components
[Vimesh Headless UI](https://github.com/vimeshjs/vimesh-headless) includes some useful components, like Listbox, Combobox, Menu, Dialog, Tabs, Switch etc. It is a good start point for you to develop your own UI library.![](./assets/vimesh002.jpg)
![](./assets/vimesh003.jpg)