Functional Web Components
**Building Web Components with Functional Programming.**

[](https://www.npmjs.com/package/@aggre/ullr)
```
___ ___
/__/\ / /\
\ \:\ / /::\
\ \:\ ___ ___ ___ ___ / /:/\:\
___ \ \:\ /__/\ / /\ /__/\ / /\ / /:/~/:/
/__/\ \__\:\ \ \:\ / /:/ \ \:\ / /:/ /__/:/ /:/___
\ \:\ / /:/ \ \:\ /:/ \ \:\ /:/ \ \:\/:::::/
\ \:\ /:/ \ \:\/:/ \ \:\/:/ \ \::/~~~~
\ \:\/:/ \ \::/ \ \::/ \ \:\
\ \::/ \__\/ \__\/ \ \:\
\__\/ \__\/
```
---
# Installation
Add to a lit project:
```bash
npm i @aggre/ullr
```
When creating a new project using lit as template and RxJS as the state management:
```bash
npm i @aggre/ullr lit rxjs
```
Partially supports run on Node.js (with jsdom).
# APIs
## `shadow`
`shadow` is a lit directive.
Encapsulate the template with Shadow DOM.
```ts
import { html } from 'lit'
import { shadow } from '@aggre/ullr'
export const main = (title: string, desc: string) =>
shadow(html`
${title}
${desc}
`)
```
| Browser | Node.js |
| ------- | --------------------------------------------------------------------------------------------------- |
| ✅ | 🚸
Shadow Dom isn't supported. An inside content of Shadow Dom is shown as just an innerHTML. |
---
## `subscribe`
`subscribe` is a lit directive.
Subscribe to RxJS `Observable` or Zustand-like stores and re-render with the passed callback function.
When the directive part is removed or the passed observable/store is changed, the unused subscription will automatically `unsubscribe`.
### With RxJS Observable
```ts
import { html } from 'lit'
import { subscribe } from '@aggre/ullr'
import { timer as _timer } from 'rxjs'
export const timer = (initialDelay: number, period: number) =>
subscribe(
_timer(initialDelay, period),
(x) => html` ${x}
`,
html` Default content
`,
)
```
### With Zustand-like Store
```ts
import { html } from 'lit'
import { subscribe } from '@aggre/ullr'
import { createStore } from 'zustand/vanilla'
const store = createStore((set) => ({
count: 0,
increment: () => set((state) => ({ count: state.count + 1 })),
}))
export const counter = () =>
subscribe(store, (state) => html` ${state.count}
`)
```
| Browser | Node.js |
| ------- | ------- |
| ✅ | ✅ |
---
## `createCustomElements`
`createCustomElements` creates a class that can be passed to `customElements.define`.
```ts
import { createCustomElements } from '@aggre/ullr'
import { main } from './main'
const observedAttributes = ['title', 'desc']
const template = ([title, desc]) => main(title, desc)
window.customElements.define(
'x-app',
createCustomElements(template, observedAttributes),
)
```
| Browser | Node.js |
| ------- | ------- |
| ✅ | ❌ |
---