Projects

Beskydy

Like the Alps, but smaller. Define small interactive partitions within your HTML without writing JavaScript

View / Source
Type

library

Active

2023 - 2026

Status

Done

Tags

Library, Reactivity, Interactive Islands, TypeScript

Projects
Beskydylibrary2023 - 2026View / Source

Beskydy is a small library used to add small reactive bits into your HTML, inspired by Alpine.js and petite-vue.

Here’s a simple example:

<div x-scope="{ count: 0 }">
  <button x-data="{ incrBy: 2 }" x-on:click="count += incrBy" x-text="`Add ${incrBy}`"></button>
  <span>Count is: {{ count }}</span>
</div>

You can initialize a reactive scope anywhere in the document using the x-scope attribute. This element and all its descendants become part of the scope. Any variables declared within the scope will be available and update the markup if they’re changed.

How does it work?

When the browser loads, we look for the x-scope attribute across the entire DOM. Each scope then receives a Context class, which stores the root element, refs and reactive data. Beskydy leverages the @vue/reactivity library for reactivity, as writing the whole system myself would be quite complex.

All descendants of the scope root element are analyzed for directive definitions - Beskydy is composed of 18 directives, each with unique functionality that allows the creation of interactive user interfaces.

Customization

I am deeply fascinated by modular and extensible systems. I always want to work on tools that we can easily extend. Beskydy works this way too. Each directive is a simple function with 3 arguments.

interface Directive {
  // Current scope's root
  ctx: ContextAny
  // The element which this directive was used on
  node: Element
  // The directive's original attribute node
  attr: Attr
}

// Add custom directives
// This directive will append HIHI after the provided text property
app.defineDirective('x-funny', (ctx, node, attr) => {
  // Usage
  // <div x-scope="{ text: 'hello' }">
  // -> <span x-funny="text"></span>

  // Whenever a reactive property is updated (text), this function is run
  ctx.effect(() => {
    // Eval returns a value from a string we provide
    // In our example, attr.value is a "text",
    // which means we're referencing a value defined in the data object
    const value = ctx.eval(attr.value)

    // Here's the funny part, we add HIHI after the text value
    // Result is <span>hello HIHI</span>
    node.textContent = `${String(value)} HIHI`

    // If the text property is changed to 'world' (or anything else)
    // This element will automatically update
    // <span>world HIHI</span>
  })
})

Similarly, you can create custom event and model modifiers.

// Add custom event modifier. Read more about modifiers in the `x-on` section below.
// The modifier must return true for the event handler to run.
app.defineEventModifier('save', (event, customState, param) => {
  localStorage.setItem(String(param), String(event.data))
  return true
})

// Add custom `x-model` modifier. Also supports modifiers with parameters
app.defineModelModifier('toLowerCase', (newValue, oldValue, param) => {
  return String(newValue).toLowerCase()
})

Example of event modifiers

<button x-on:click.only[5]="doNothingAfterFiveClicks()" />

<div x-scope="{ limit: 5 }">
  <button x-on:click.only[limit]="doNothingAfterFiveClicks()" />
</div>

As usual, the repo contains the detailed documentation and examples. Try it in your project! Feedback, issues, PRs are always welcome.