### Perform setup and cleanup with use.mount
Source: https://github.com/benev-gg/slate/blob/main/README.md
Executes setup logic when the DOM is connected and cleanup when disconnected.
```ts
use.mount(() => {
const interval = setInterval(increment, 1000)
return () => clearInterval(interval)
})
```
--------------------------------
### use.init(initializer)
Source: https://github.com/benev-gg/slate/blob/main/README.md
Performs setup and cleanup while returning a value.
```APIDOC
## use.init(initializer)
### Description
Performs setup and cleanup, returning a value and a cleanup function.
### Usage
`const scene = use.init(() => { ... return [scene, () => scene.cleanup()] })`
```
--------------------------------
### Install Slate
Source: https://github.com/benev-gg/slate/blob/main/README.md
Install the package via npm.
```sh
npm i @benev/slate
```
--------------------------------
### Initialize and return a value with use.init
Source: https://github.com/benev-gg/slate/blob/main/README.md
Performs setup and cleanup while returning a value for use in the component.
```ts
const scene = use.init(() => {
// called whenever dom is connected
const scene = setup_3d_scene_for_example()
return [
scene, // value returned
() => scene.cleanup(), // cleanup called on dom disconnect
]
})
```
--------------------------------
### use.mount(callback)
Source: https://github.com/benev-gg/slate/blob/main/README.md
Performs setup and cleanup on DOM connection and disconnection.
```APIDOC
## use.mount(callback)
### Description
Executes a setup function when the component is connected to the DOM and runs the returned cleanup function when disconnected.
### Usage
`use.mount(() => { ... return () => cleanup() })`
```
--------------------------------
### use.op()
Source: https://github.com/benev-gg/slate/blob/main/README.md
Creates an OpSignal for loading/error/ready states.
```APIDOC
## use.op()
### Description
Creates an OpSignal to manage asynchronous loading states.
### Usage
`const count = use.op(); count.load(async() => fetchCount("/count"))`
```
--------------------------------
### use.load(callback)
Source: https://github.com/benev-gg/slate/blob/main/README.md
Shorthand for creating an OpSignal and loading data.
```APIDOC
## use.load(callback)
### Description
Creates an OpSignal and immediately executes the load function.
### Usage
`const count = use.load(() => fetchCount("/count"))`
```
--------------------------------
### Create ops
Source: https://github.com/benev-gg/slate/blob/main/README.md
Initialize loading, error, or ready states using the Op utility.
```ts
import {Op} from "@benev/slate"
Op.loading()
//= {status: "loading"}
Op.error("a fail occurred")
//= {status: "error", reason: "a fail occurred"}
Op.ready(123)
//= {status: "ready", payload: 123}
```
--------------------------------
### Deploy a lightView
Source: https://github.com/benev-gg/slate/blob/main/README.md
Injects a light view into a template, passing props directly as arguments.
```ts
html`
${MyLightView(123)}
`
```
--------------------------------
### Use Components in HTML
Source: https://github.com/benev-gg/slate/blob/main/README.md
Use registered components in HTML using their dashified names.
```html
```
--------------------------------
### use.state(initialValue)
Source: https://github.com/benev-gg/slate/blob/main/README.md
Manages local component state.
```APIDOC
## use.state(initialValue)
### Description
Works like the React useState hook to manage local state.
### Usage
`const [count, setCount] = use.state(0)`
```
--------------------------------
### Manage loading states with OpSignals
Source: https://github.com/benev-gg/slate/blob/main/README.md
Creates an OpSignal to handle loading, error, and ready states.
```ts
const count = use.op()
count.load(async() => fetchCount("/count"))
```
```ts
const count = use.load(() => fetchCount("/count"))
```
--------------------------------
### Import Templating Functions
Source: https://github.com/benev-gg/slate/blob/main/README.md
Import augmented Lit templating functions that support signals.
```ts
import {html, css, svg} from "@benev/slate"
```
--------------------------------
### Initialize isolated flatstate instances
Source: https://github.com/benev-gg/slate/blob/main/README.md
Create multiple independent instances of the flatstate system.
```ts
const flat1 = new Flat()
const flat2 = new Flat()
```
--------------------------------
### Initialize a Custom Signal Tower
Source: https://github.com/benev-gg/slate/blob/main/README.md
Create an isolated signal tower instance for independent state management.
```ts
import {SignalTower} from "@benev/slate"
const signals = new SignalTower()
```
--------------------------------
### Pipe utility usage
Source: https://github.com/benev-gg/slate/blob/main/README.md
Demonstrates replacing nested function calls with a fluent pipe interface.
```ts
// bad
register_to_dom(
apply.signals(signals)(
apply.flat(flat)(
apply.css(theme)(
requirement.provide(context)(elements)
)
)
)
)
```
```ts
import {Pipe} from "@benev/slate"
// good
Pipe.with(elements)
.to(requirement.provide(context))
.to(apply.css(theme))
.to(apply.flat(flat))
.to(apply.signals(signals))
.to(register_to_dom)
```
--------------------------------
### Deploy a shadowView
Source: https://github.com/benev-gg/slate/blob/main/README.md
Injects a shadow view into a template, requiring props to be passed as an array.
```ts
html`
`
```
--------------------------------
### Execute logic after render with use.defer
Source: https://github.com/benev-gg/slate/blob/main/README.md
Runs a function after every render, useful for querying rendered elements.
```ts
use.defer(() => {
const div = document.querySelector("div")
const rect = div.getBoundingClientRect()
report_rect(rect)
})
```
```ts
const div = use.defer(() => document.querySelector("div"))
console.log(div.value)
// undefined (until the first render is complete)
const handleClick = () => console.log(div.value)
// HTMLDivElement (after the first render)
```
--------------------------------
### use.once(initializer)
Source: https://github.com/benev-gg/slate/blob/main/README.md
Initializes a value once for the component lifecycle.
```APIDOC
## use.once(initializer)
### Description
Initializes a value only once.
### Usage
`const random_number = use.once(() => Math.random())`
```
--------------------------------
### Import loading effects
Source: https://github.com/benev-gg/slate/blob/main/README.md
Import pre-made loading indicators.
```ts
import {loading} from "@benev/slate"
```
--------------------------------
### Op Utility
Source: https://github.com/benev-gg/slate/blob/main/README.md
Utilities for managing asynchronous operation states (loading, error, ready).
```APIDOC
## Op Utility
### Description
Provides static methods to create and manipulate operation state objects, and type guards to check their status.
### Methods
- `Op.loading()`: Returns a loading state object.
- `Op.error(reason: string)`: Returns an error state object.
- `Op.ready(payload: any)`: Returns a ready state object with a payload.
- `Op.is.loading(op)`: Type guard for loading state.
- `Op.is.error(op)`: Type guard for error state.
- `Op.is.ready(op)`: Type guard for ready state.
- `Op.payload(op)`: Returns the payload if ready, otherwise undefined.
- `Op.load(setter: (op) => void, task: () => Promise)`: Executes an async task and updates the op via the setter.
```
--------------------------------
### use.styles(css)
Source: https://github.com/benev-gg/slate/blob/main/README.md
Assigns a stylesheet to the shadow root of a view or component.
```APIDOC
## use.styles(css)
### Description
Assigns a stylesheet to the shadow root. Only works on shadow views or components.
### Usage
`use.styles(css`span { color: yellow }`)`
```
--------------------------------
### Create a lightView
Source: https://github.com/benev-gg/slate/blob/main/README.md
Defines a view without shadow DOM, suitable for simpler components without isolated styles.
```ts
export const MyLightView = lightView(use => (start: number) => {
use.name("my-light-view")
const count = use.signal(start)
const increment = () => count.value++
return html`
${count}
`
})
```
--------------------------------
### use.signal(initialValue)
Source: https://github.com/benev-gg/slate/blob/main/README.md
Creates a reactive container for a value.
```APIDOC
## use.signal(initialValue)
### Description
Creates a reactive signal container.
### Usage
`const count = use.signal(0)`
```
--------------------------------
### use.name(name)
Source: https://github.com/benev-gg/slate/blob/main/README.md
Assigns a name to a shadow or light view.
```APIDOC
## use.name(name)
### Description
Assigns a stylesheet name to the shadow root. This is only applicable to views.
### Usage
`use.name("my-cool-view")`
```
--------------------------------
### use.flatstate(object)
Source: https://github.com/benev-gg/slate/blob/main/README.md
Creates a reactive object.
```APIDOC
## use.flatstate(object)
### Description
Creates a reactive state object.
### Usage
`const state = use.flatstate({count: 0})`
```
--------------------------------
### use.defer(callback)
Source: https://github.com/benev-gg/slate/blob/main/README.md
Executes a function every time a render finishes.
```APIDOC
## use.defer(callback)
### Description
Executes a function after every render, useful for querying rendered elements.
### Usage
`use.defer(() => { ... })`
```
--------------------------------
### Create a shadowView
Source: https://github.com/benev-gg/slate/blob/main/README.md
Defines a view with shadow DOM encapsulation, supporting styles and internal state management.
```ts
import {shadowView, html, css} from "@benev/slate"
export const MyShadowView = shadowView(use => (start: number) => {
use.name("my-shadow-view")
use.styles(css`span {color: yellow}`)
const count = use.signal(start)
const increment = () => count.value++
return html`
${count}
`
})
```
--------------------------------
### Define a LightElement
Source: https://github.com/benev-gg/slate/blob/main/README.md
Creates a custom element using Light DOM with reactivity and flat state management.
```ts
import {LightElement, mixin, attributes, flat} from "@benev/slate"
@mixin.reactivity()
export class MySilver extends LightElement {
#attrs = attributes(this as LightElement, {
label: String
})
#state = flat.state({
count: 0,
})
render() {
return html`
${this.#state.count}
`
}
}
```
--------------------------------
### Initialize and Update Signals
Source: https://github.com/benev-gg/slate/blob/main/README.md
Create and modify signal values using the signal constructor and the .value property.
```ts
import {signal, signals} from "@benev/slate"
const count = signal(0)
const greeting = signal("hello")
count.value++
greeting.value = "bonjour"
console.log(count.value) //> 1
console.log(greeting.value) //> "bonjour"
```
--------------------------------
### reactor.reaction
Source: https://github.com/benev-gg/slate/blob/main/README.md
Creates a reaction that listens to signals and flatstates. It supports both one-function and two-function syntax and returns a stop function to terminate the reaction.
```APIDOC
## reactor.reaction
### Description
Registers a reaction function that executes when observed signals or flatstates change. Returns a function that can be called to stop the reaction.
### Syntax
- `reactor.reaction(callback: () => void)`
- `reactor.reaction(selector: () => T, effect: (result: T) => void)`
### Example
```ts
const stop = reactor.reaction(() => console.log(state.count))
// Stop the reaction
stop()
```
```
--------------------------------
### Define a simple reaction
Source: https://github.com/benev-gg/slate/blob/main/README.md
Register a function that runs immediately and re-runs whenever accessed state properties change.
```ts
flat.reaction(() => console.log(state.count))
```
--------------------------------
### Initialize a flat state object
Source: https://github.com/benev-gg/slate/blob/main/README.md
Create a reactive state object using the flat.state function.
```ts
import {flat} from "@benev/slate"
const state = flat.state({count: 0})
```
--------------------------------
### Register Components to DOM
Source: https://github.com/benev-gg/slate/blob/main/README.md
Register components to the DOM for use in HTML.
```ts
import {register_to_dom} from "@benev/slate"
register_to_dom({MyShadowComponent, MyLightComponent})
```
--------------------------------
### Create reactive objects with use.flatstate
Source: https://github.com/benev-gg/slate/blob/main/README.md
Creates a reactive object for state management.
```ts
const state = use.flatstate({count: 0})
const increment = () => state.count++
```
--------------------------------
### Create reactive signals
Source: https://github.com/benev-gg/slate/blob/main/README.md
Creates reactive containers for values and injects them into HTML.
```ts
const count = use.signal(0)
const increment = () => count.value++
```
```ts
html`${count}`
```
--------------------------------
### Export Components for Libraries
Source: https://github.com/benev-gg/slate/blob/main/README.md
Export components and registration utilities for downstream library consumption.
```ts
export {register_to_dom, apply, css} from "@benev/slate"
export const myComponents = {MyShadowComponent, MyLightComponent}
```
--------------------------------
### OpSignal
Source: https://github.com/benev-gg/slate/blob/main/README.md
An Op wrapped in a signal for better ergonomics in UI components.
```APIDOC
## OpSignal
### Description
An OpSignal is an operation state wrapped in a signal, providing methods to update state and check status directly.
### Methods
- `load(task: () => Promise)`: Runs an async operation and updates the signal state.
- `isLoading()`, `isError()`, `isReady()`: Boolean checks for current state.
- `payload`: Property to access the current payload.
- `setLoading()`, `setError(reason)`, `setReady(payload)`: Direct state assignment methods.
```
--------------------------------
### Apply Custom Theme to Components
Source: https://github.com/benev-gg/slate/blob/main/README.md
Apply custom CSS themes to a collection of components before registration.
```ts
import {myComponents, register_to_dom, apply, css} from "@benev/slate"
const myCustomTheme = css`button { color: red; }`
register_to_dom(
apply.css(myCustomTheme)(
myComponents,
)
)
```
--------------------------------
### Access app context
Source: https://github.com/benev-gg/slate/blob/main/README.md
Accesses the application context.
```ts
// access your own things on the context
use.context.my_cool_thing
```
--------------------------------
### use.watch(callback)
Source: https://github.com/benev-gg/slate/blob/main/README.md
Rerenders when a part of a StateTree changes.
```APIDOC
## use.watch(callback)
### Description
Triggers a rerender when the accessed state changes.
### Usage
`const whatever = use.watch(() => use.context.state.whatever)`
```
--------------------------------
### Create a reaction with reactor
Source: https://github.com/benev-gg/slate/blob/main/README.md
Use the reactor to listen to both signals and flatstates simultaneously outside of components.
```ts
import {reactor, flatstate, signal} from "@benev/slate"
const state = state({count: 0})
const count = signal(0)
// use the reactor to setup a reaction
reactor.reaction(() => console.log(`
flat count is ${state.count},
signal count is ${count.value}
`))
```
--------------------------------
### Two-function reaction syntax
Source: https://github.com/benev-gg/slate/blob/main/README.md
Use a selector function to return values and a second function to handle the results.
```ts
reactor.reaction(
() => ({a: state.count, b: count.value}),
results => console.log(results),
)
```
--------------------------------
### Use loading effects in views
Source: https://github.com/benev-gg/slate/blob/main/README.md
Apply loading effects to ops or op signals in your UI.
```ts
return loading.binary(videoOp, video => html`
video is done loading!
${video}
`)
```
--------------------------------
### use.attrs(definitions)
Source: https://github.com/benev-gg/slate/blob/main/README.md
Declares accessors for HTML attributes.
```APIDOC
## use.attrs(definitions)
### Description
Defines reactive accessors for HTML attributes.
### Usage
`const attrs = use.attrs({ start: Number, label: String })`
```
--------------------------------
### Create Light Component
Source: https://github.com/benev-gg/slate/blob/main/README.md
Define a custom element without shadow DOM using lightComponent.
```ts
import {lightComponent, html, css} from "@benev/slate"
export const MyLightComponent = lightComponent(use => {
const count = use.signal(0)
const increment = () => count.value++
return html`
${count}
`
})
```
--------------------------------
### Use OpSignal for async operations
Source: https://github.com/benev-gg/slate/blob/main/README.md
OpSignal provides better ergonomics for managing async states within signals.
```ts
const count = signals.op()
// run an async operation
await count.load(async() => {
await sleep(1000)
return 123
})
// check the status of this OpSignal
count.isLoading() //= false
count.isError() //= false
count.isReady() //= true
// grab the payload (undefined when not ready)
count.payload //= 123
// directly assign the op signal
count.setLoading()
count.setError("big fail")
count.setReady(123)
```
--------------------------------
### Track Async Operations with Op Signals
Source: https://github.com/benev-gg/slate/blob/main/README.md
Manage asynchronous data loading states and payloads using the op signal utility.
```ts
const json = signals.op()
console.log(json.isLoading()) //> true
await json.load(async() => {
const data = await fetch_remote_data()
return JSON.parse(data)
})
console.log(json.isReady()) //> true
console.log(json.payload) //> {"your": "json data"}
```
--------------------------------
### Manage state with use.state
Source: https://github.com/benev-gg/slate/blob/main/README.md
Works like the React useState hook for local state management.
```ts
const [count, setCount] = use.state(0)
const increment = () => setCount(count + 1)
```
--------------------------------
### Define a ShadowElement
Source: https://github.com/benev-gg/slate/blob/main/README.md
Creates a custom element using Shadow DOM with reactivity and attribute management.
```ts
import {ShadowElement, mixin, attributes, signal} from "@benev/slate"
@mixin.css(css`span {color: blue}`)
@mixin.reactivity()
export class MyGold extends ShadowElement {
#attrs = attributes(this as ShadowElement, {
label: String
})
#count = signal(0)
render() {
return html`
${this.#count.value}
`
}
}
```
--------------------------------
### Create Shadow Component
Source: https://github.com/benev-gg/slate/blob/main/README.md
Define a custom element with shadow DOM using shadowComponent.
```ts
import {shadowComponent, html, css} from "@benev/slate"
export const MyShadowComponent = shadowComponent(use => {
use.styles(css`span {color: yellow}`)
const count = use.signal(0)
const increment = () => count.value++
return html`
${count}
`
})
```
--------------------------------
### Assign a name to a view
Source: https://github.com/benev-gg/slate/blob/main/README.md
Assigns a stylesheet name to the shadow root of a view.
```ts
use.name("my-cool-view")
```
--------------------------------
### Access DOM elements
Source: https://github.com/benev-gg/slate/blob/main/README.md
Accesses the underlying HTML element or shadow root.
```ts
use.element.querySelector("p")
```
```ts
use.shadow.querySelector("slot")
```
--------------------------------
### Integrate flatstate with frontend elements
Source: https://github.com/benev-gg/slate/blob/main/README.md
Connect components to flatstate to trigger re-renders on state changes.
```ts
import {apply} from "@benev/slate"
const MyElement2 = mixin.flat(flat)(MyElement)
// can also be a class decorator
const elements2 = apply.flat(flat)(elements)
```
--------------------------------
### Use Signals in HTML Templates
Source: https://github.com/benev-gg/slate/blob/main/README.md
Signals can be passed directly into HTML templates without needing to access the .value property.
```ts
html`
count is ${count}
`
```
--------------------------------
### Initialize a value once
Source: https://github.com/benev-gg/slate/blob/main/README.md
Initializes a value only once during the component lifecycle.
```ts
const random_number = use.once(() => Math.random())
```
--------------------------------
### use.computed(callback)
Source: https://github.com/benev-gg/slate/blob/main/README.md
Creates a signal derived from other signals.
```APIDOC
## use.computed(callback)
### Description
Creates a signal that automatically updates when its dependencies change.
### Usage
`const tripled = use.computed(() => count.value * 3)`
```
--------------------------------
### Register elements to the DOM
Source: https://github.com/benev-gg/slate/blob/main/README.md
Registers custom elements to the browser DOM.
```ts
register_to_dom({MyGold, MySilver})
```
--------------------------------
### Stop a reaction
Source: https://github.com/benev-gg/slate/blob/main/README.md
Capture the return value of the reaction to stop it later.
```ts
const stop = reactor.reaction(
() => console.log(state.count)
)
// end this reaction
stop()
```
--------------------------------
### Assign styles to a shadow root
Source: https://github.com/benev-gg/slate/blob/main/README.md
Applies a stylesheet to the shadow root of a shadow view or component.
```ts
use.styles(css`span { color: yellow }`)
```
--------------------------------
### Wait for debouncer
Source: https://github.com/benev-gg/slate/blob/main/README.md
Await the reactor's debouncer.
```ts
await reactor.wait
```
--------------------------------
### Declare and use HTML attributes
Source: https://github.com/benev-gg/slate/blob/main/README.md
Defines accessors for HTML attributes and interacts with them as properties.
```ts
const attrs = use.attrs({
start: Number,
label: String,
["data-active"]: Boolean,
})
```
```ts
attrs.start = 123
attrs.label = "hello"
attrs["data-active"] = true
```
```ts
console.log(attrs.start) // 123
console.log(attrs.label) // "hello"
console.log(attrs["data-active"]) // true
```
--------------------------------
### Handle debounced state updates
Source: https://github.com/benev-gg/slate/blob/main/README.md
Reactions are debounced; use flat.wait to ensure state changes have propagated before reading.
```ts
const state = flat.state({amount: 100})
state.amount = 101
console.log(state.amount) //> 100 (old value)
await flat.wait
console.log(state.amount) //> 101 (now it's ready)
```
--------------------------------
### Run async operation with op
Source: https://github.com/benev-gg/slate/blob/main/README.md
Update an op state automatically during an async operation.
```ts
let my_op = Op.loading()
await Op.load(
// your setter designates which op to overwrite
op => my_op = op,
// your async function which returns the ready payload
async() => {
await nap(1000)
return 123
}
)
```
--------------------------------
### Grab op payload
Source: https://github.com/benev-gg/slate/blob/main/README.md
Retrieve the payload from an op, which returns undefined if not ready.
```ts
const count = Op.ready(123)
const loadingCount = Op.loading()
Op.payload(count)
//= 123
Op.payload(loadingCount)
//= undefined
```
--------------------------------
### Watch state changes with use.watch
Source: https://github.com/benev-gg/slate/blob/main/README.md
Triggers a rerender when a specific part of a StateTree changes.
```ts
const whatever = use.watch(() => use.context.state.whatever)
```
--------------------------------
### Create Computed Signals
Source: https://github.com/benev-gg/slate/blob/main/README.md
Derive new signal values based on the state of other signals.
```ts
count.value = 1
const tripled = signals.computed(() => count.value * 3)
console.log(tripled.value) //> 3
```
--------------------------------
### Check op status
Source: https://github.com/benev-gg/slate/blob/main/README.md
Use type guards to check the current status of an op.
```ts
Op.is.loading(op)
//= false
Op.is.error(op)
//= false
Op.is.ready(op)
//= true
```
--------------------------------
### Wait for Debounced Signal Updates
Source: https://github.com/benev-gg/slate/blob/main/README.md
Use the signals.wait promise to synchronize with debounced signal updates.
```ts
const tripled = signals.computed(() => count.value * 3)
console.log(tripled.value) //> 3
count.value = 10
console.log(tripled.value) //> 3 (too soon!)
await signals.wait
console.log(tripled.value) //> 30 (there we go)
```
--------------------------------
### Stop a reaction
Source: https://github.com/benev-gg/slate/blob/main/README.md
Invoke the returned stop function to terminate a specific reaction.
```ts
const stop = flat.reaction(() => console.log(state.count))
stop() // end this particular reaction
```
--------------------------------
### Define Signal Reactions
Source: https://github.com/benev-gg/slate/blob/main/README.md
Execute side effects automatically when referenced signals change.
```ts
signals.reaction(() => console.log("doubled", count.value * 2))
//> doubled 2
count.value = 2
//> doubled 4
```
--------------------------------
### Create readonly state access
Source: https://github.com/benev-gg/slate/blob/main/README.md
Restrict modifications to a state object by creating a readonly proxy.
```ts
const state = flat.state({count: 0})
const rstate = Flat.readonly(state)
state.count = 1
await flat.wait
console.log(rstate.count) //> 1
rstate.count = 2 // !! ReadonlyError !!
```
--------------------------------
### Derive signals with use.computed
Source: https://github.com/benev-gg/slate/blob/main/README.md
Creates a signal derived from other existing signals.
```ts
const count = use.signal(2)
const tripled = use.computed(() => count.value * 3)
console.log(tripled.value) //> 6
```
--------------------------------
### Define a two-function reaction
Source: https://github.com/benev-gg/slate/blob/main/README.md
Separate data collection from the response logic to control when the responder function triggers.
```ts
flat.reaction(
// your "collector" function
() => ({count: state.count}),
// your "responder" function
({count}) => console.log(count),
)
```
=== COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.