Skip to content

Extraordinary JavaScript UI framework with unique declarative and functional architecture

License

Notifications You must be signed in to change notification settings

hybridsjs/hybrids

Repository files navigation

hybrids

build status coverage status npm version

An extraordinary JavaScript framework for creating client-side web applications, UI components libraries, or single web components with unique mixed declarative and functional architecture

Hybridsprovides a complete set of features for building modern web applications:

  • Component Modelbased on plain objects and pure functions
  • Global State Managementwith external storages, offline caching, relations, and more
  • App-like Routingbased on the graph structure of views
  • Layout Enginemaking UI layouts development much faster
  • Localizationwith automatic translation of the templates content
  • Hot Module Replacementsupport without any additional configuration

Documentation

The project documentation is available at thehybrids.js.orgsite.

Quick Look

Component Model

It's based on plain objects and pure functions1,still using theWeb Components APIunder the hood:

import{html,define}from"hybrids";

functionincreaseCount(host){
host.count+=1;
}

exportdefaultdefine({
tag:"simple-counter",
count:0,
render:({count})=>html`
<buttononclick= "${increaseCount}">
Count:${count}
</button>
`,
});
<simple-countercount= "42"></simple-counter>

Open in StackBlitz

You can read more in theComponent Modelsection.

Global State Management

A global state management uses declarative model definitions with support for async external storages, relations, offline caching, and many more:

import{define,store,html}from"hybrids";

constUser={
id:true,
firstName:"",
lastName:"",
[store.connect]:{
get:id=>fetch(`/users/${id}`).then(...),
},
};

define({
tag:"user-details",
user:store(User),
render:({user})=>html`
<div>
${store.pending(user)&&`Loading...`}
${store.error(user)&&`Something went wrong...`}

${store.ready(user)&&html`
<p>${user.firstName}${user.lastName}</p>
`}
</div>
`,
});
<user-detailsuser= "2"></user-details>

You can read more in theStoresection.

App-like Routing

Rather than just matching URLs with the corresponding components, the router depends on a tree-like structure of views, which have their own routing configuration. It makes the URLs optional, have out-the-box support for dialogs, protected views, and many more.

import{define,html,router}from"hybrids";

importDetailsfrom"./details.js";

constHome=define({
[router.connect]:{stack:[Details,...]},
tag:"app-home",
render:()=>html`
<templatelayout= "column">
<h1>Home</h1>
<navlayout= "row gap">
<ahref= "${router.url(Details)}">Details</a>
</nav>
...
</template>
`,
});

exportdefine({
tag:"app-router",
stack:router(Home),
render:({stack})=>html`
<templatelayout= "column">
${stack}
</template>
`,
});
<app-router></app-router>

You can read more in theRoutersection.

Layout Engine

Create CSS layouts in-place in templates, even without using Shadow DOM, but still keeping the encapsulation of the component's styles:

define({
tag:"app-home-view",
render:()=>html`
<templatelayout= "column center gap:2">
<divlayout= "grow grid:1|max">
<h1>Home</h1>
...
</div>

<footerlayout@768px= "hidden">...</footer>
</template>
`
});

You can read more in theLayout Enginesection of the documentation.

Localization

The library supports automatic translation of the component's content, which makes translation seamless and easy to integrate. Additionally, it provides a way to add dynamic messages with plural forms, HTML content, or use messages outside of the template context. Also, it comes with handy CLI tool to extract messages from the source code!

import{define,html,localize}from"hybrids";

exportdefaultdefine({
tag:"my-element",
name:"",
render:({name})=>html`
<div>Hello${name}!</div>
`,
});

localize("pl",{
"Hello ${0}!":{
message:"Witaj ${0}!",
},
});

You can read more in theLocalizationsection of the documentation.

Community

Do you need help? Something went wrong? Feel free to createan issuein the github repository or join theGitterchannel.

License

Hybridsis released under theMIT License.

Footnotes

  1. Pure functions only apply to the component definition. Side effects attached to event listeners might mutate the host element.