Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
249 changes: 249 additions & 0 deletions dom/reactive-attributes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,249 @@
/**
* Reactive Attributes - Declarative DOM actions triggered by LiveTemplate lifecycle events.
*
* Attribute Pattern: lvt-{action}-on:{event}="param"
*
* Events:
* - pending: Action started, waiting for server response
* - success: Action completed successfully
* - error: Action completed with validation errors
* - done: Action completed (regardless of success/error)
*
* Event Scope:
* - Global: lvt-reset-on:success (any action)
* - Action-specific: lvt-reset-on:create-todo:success (specific action only)
*
* Actions:
* - reset: Calls form.reset()
* - disable: Sets element.disabled = true
* - enable: Sets element.disabled = false
* - addClass: Adds CSS class(es)
* - removeClass: Removes CSS class(es)
* - toggleClass: Toggles CSS class(es)
* - setAttr: Sets an attribute (name:value format)
* - toggleAttr: Toggles a boolean attribute
*/

export type ReactiveAction =
| "reset"
| "disable"
| "enable"
| "addClass"
| "removeClass"
| "toggleClass"
| "setAttr"
| "toggleAttr";

export type LifecycleEvent = "pending" | "success" | "error" | "done";

export interface ReactiveBinding {
action: ReactiveAction;
lifecycle: LifecycleEvent;
actionName?: string;
param?: string;
}

const LIFECYCLE_EVENTS: LifecycleEvent[] = ["pending", "success", "error", "done"];

const REACTIVE_ACTIONS: ReactiveAction[] = [
"reset",
"disable",
"enable",
"addClass",
"removeClass",
"toggleClass",
"setAttr",
"toggleAttr",
];
Comment on lines +48 to +57

Copilot AI Dec 1, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The REACTIVE_ACTIONS constant is defined but never used in the code. Action validation is performed using ACTION_MAP instead. Consider removing this constant to reduce maintenance burden, or use it for validation/documentation if needed.

Suggested change
const REACTIVE_ACTIONS: ReactiveAction[] = [
"reset",
"disable",
"enable",
"addClass",
"removeClass",
"toggleClass",
"setAttr",
"toggleAttr",
];

Copilot uses AI. Check for mistakes.

// Lowercase versions for case-insensitive matching (HTML attributes are lowercased)
const ACTION_MAP: Record<string, ReactiveAction> = {
reset: "reset",
disable: "disable",
enable: "enable",
addclass: "addClass",
removeclass: "removeClass",
toggleclass: "toggleClass",
setattr: "setAttr",
toggleattr: "toggleAttr",
};

/**
* Parse a reactive attribute name and value into a binding.
*
* Examples:
* parseReactiveAttribute("lvt-reset-on:success", "") => { action: "reset", lifecycle: "success" }
* parseReactiveAttribute("lvt-addClass-on:pending", "loading") => { action: "addClass", lifecycle: "pending", param: "loading" }
* parseReactiveAttribute("lvt-reset-on:create-todo:success", "") => { action: "reset", lifecycle: "success", actionName: "create-todo" }
*/
export function parseReactiveAttribute(
attrName: string,
attrValue: string
): ReactiveBinding | null {
// Pattern: lvt-{action}-on:{actionName?}:{lifecycle}
// The lifecycle must be at the end, action name is optional in the middle
// Note: HTML attributes are lowercased by browsers, so we match case-insensitively
const match = attrName.toLowerCase().match(/^lvt-(\w+)-on:(.+)$/);
if (!match) return null;

const actionKey = match[1];
const action = ACTION_MAP[actionKey];
if (!action) return null;

const eventPart = match[2];

// Check if the last segment is a lifecycle event
// Format: "{actionName}:{lifecycle}" or just "{lifecycle}"
const segments = eventPart.split(":");
const lastSegment = segments[segments.length - 1] as LifecycleEvent;

if (!LIFECYCLE_EVENTS.includes(lastSegment)) return null;

const lifecycle = lastSegment;
const actionName = segments.length > 1 ? segments.slice(0, -1).join(":") : undefined;

return {
action,
lifecycle,
actionName: actionName || undefined,
param: attrValue || undefined,
};
}

/**
* Execute a reactive action on an element.
*/
export function executeAction(
element: Element,
action: ReactiveAction,
param?: string
): void {
switch (action) {
case "reset":
if (element instanceof HTMLFormElement) {
element.reset();
}
break;

case "disable":
if ("disabled" in element) {
(element as HTMLButtonElement | HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement).disabled = true;
}
break;

case "enable":
if ("disabled" in element) {
(element as HTMLButtonElement | HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement).disabled = false;
}
break;

case "addClass":
if (param) {
const classes = param.split(/\s+/).filter(Boolean);
element.classList.add(...classes);
}
break;

case "removeClass":
if (param) {
const classes = param.split(/\s+/).filter(Boolean);
element.classList.remove(...classes);
}
break;

case "toggleClass":
if (param) {
const classes = param.split(/\s+/).filter(Boolean);
classes.forEach((c) => element.classList.toggle(c));
}
break;

case "setAttr":
if (param) {
const colonIndex = param.indexOf(":");
if (colonIndex > 0) {
const name = param.substring(0, colonIndex);
const value = param.substring(colonIndex + 1);
element.setAttribute(name, value);
}
}
break;

case "toggleAttr":
if (param) {
element.toggleAttribute(param);
}
break;
}
}

/**
* Check if an event matches a binding.
*
* @param binding The reactive binding to check
* @param lifecycle The lifecycle event that fired
* @param actionName The action name that triggered the event (optional)
*/
export function matchesEvent(
binding: ReactiveBinding,
lifecycle: LifecycleEvent,
actionName?: string
): boolean {
// Lifecycle must match
if (binding.lifecycle !== lifecycle) return false;

// If binding has no actionName, it's global (matches any action)
if (!binding.actionName) return true;

// If binding has actionName, it must match the fired action
return binding.actionName === actionName;
}

/**
* Process all reactive attributes for a lifecycle event.
*
* Instead of building complex selectors, we iterate all elements with any lvt-*-on:* attribute
* and check each one against the fired event.
*/
export function processReactiveAttributes(
lifecycle: LifecycleEvent,
actionName?: string
): void {
// Find all elements that might have reactive attributes
// This is a broad selector but avoids escaping issues with attribute names containing colons
const allElements = document.querySelectorAll("*");
Comment on lines +213 to +214

Copilot AI Dec 1, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using querySelectorAll("*") to find all elements in the document is inefficient and can cause performance issues, especially in large DOMs. This selector returns every single element in the document.

Consider using a more targeted approach:

  1. Use querySelectorAll('[lvt-reset-on], [lvt-disable-on], [lvt-enable-on], [lvt-addClass-on], [lvt-removeClass-on], [lvt-toggleClass-on], [lvt-setAttr-on], [lvt-toggleAttr-on]') with attribute prefix selectors
  2. Or use document.querySelectorAll('[class*="lvt-"][class*="-on:"]') to target elements with these attributes
  3. Or maintain a registry of elements with reactive attributes during initialization

The current approach iterates through every element in the document on every lifecycle event, which scales poorly.

Suggested change
// This is a broad selector but avoids escaping issues with attribute names containing colons
const allElements = document.querySelectorAll("*");
// Use a targeted selector for known reactive attribute names
const allElements = document.querySelectorAll(
'[lvt-reset-on], [lvt-disable-on], [lvt-enable-on], [lvt-addClass-on], [lvt-removeClass-on], [lvt-toggleClass-on], [lvt-setAttr-on], [lvt-toggleAttr-on]'
);

Copilot uses AI. Check for mistakes.

allElements.forEach((element) => {
// Check all attributes on this element for reactive bindings
Array.from(element.attributes).forEach((attr) => {
// Quick filter: only process lvt-*-on: attributes
if (!attr.name.startsWith("lvt-") || !attr.name.includes("-on:")) {
return;
}

const binding = parseReactiveAttribute(attr.name, attr.value);
if (binding && matchesEvent(binding, lifecycle, actionName)) {
executeAction(element, binding.action, binding.param);
}
});
});
}

/**
* Set up document-level event listeners for reactive attributes.
* This should be called once during client initialization.
*/
export function setupReactiveAttributeListeners(): void {
LIFECYCLE_EVENTS.forEach((lifecycle) => {
// Listen in capture phase to process before event bubbles
document.addEventListener(
`lvt:${lifecycle}`,
(e: Event) => {
const customEvent = e as CustomEvent;
const actionName = customEvent.detail?.action;
processReactiveAttributes(lifecycle, actionName);
},
true // capture phase
);
});
}
4 changes: 4 additions & 0 deletions livetemplate-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { ObserverManager } from "./dom/observer-manager";
import { ModalManager } from "./dom/modal-manager";
import { LoadingIndicator } from "./dom/loading-indicator";
import { FormDisabler } from "./dom/form-disabler";
import { setupReactiveAttributeListeners } from "./dom/reactive-attributes";
import { TreeRenderer } from "./state/tree-renderer";
import { FormLifecycleManager } from "./state/form-lifecycle-manager";
import { WebSocketManager } from "./transport/websocket";
Expand Down Expand Up @@ -350,6 +351,9 @@ export class LiveTemplateClient {
// Set up modal delegation
this.eventDelegator.setupModalDelegation();

// Set up reactive attribute listeners for lvt-{action}-on:{event} attributes
setupReactiveAttributeListeners();
Comment on lines +354 to +355

Copilot AI Dec 1, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Calling setupReactiveAttributeListeners() on every connect() will create duplicate event listeners, causing reactive attributes to execute multiple times per event. The setup should either:

  1. Be called only once during client initialization (move to constructor)
  2. Track if listeners are already set up and skip if so
  3. Provide a cleanup method called in disconnect() to remove listeners

Option 1 is simplest: move this line to the constructor since these are document-level listeners that don't depend on the wrapper element.

Copilot uses AI. Check for mistakes.

// Initialize focus tracking
this.focusManager.attach(this.wrapperElement);

Expand Down
Loading
Loading