Overview
HTMLX is a JavaScript library that provides a way to dynamically swap the content of HTML elements. It allows developers to define triggers, targets, and templates to swap content in response to user interactions.
Requirements
- The library should be initialized when the DOM is ready.
- The library should process all elements with the
lmx-triggerattribute and add event listeners to them. - When an element with the
lmx-triggerattribute is clicked, the library should call thehandleTriggermethod with the element and the event as arguments. - The
handleTriggermethod should:- Get the
lmx-targetattribute of the element and find the corresponding target element in the DOM. - Get the
lmx-template-idattribute of the element and find the corresponding template element in the DOM. - Clone the template content and swap it with the content of the target element using the
swapContentmethod. - Process the new content by adding event listeners to any elements with the
lmx-triggerattribute.
- Get the
- The
swapContentmethod should:- Replace the content of the target element with the cloned template content.
- Use the
lmx-swap-typeattribute to determine the swap type (innerHTML, outerHTML, beforebegin, afterend, etc.).
- The library should support multiple templates and allow developers to define multiple templates using the
<template>element. - The library should support conditional rendering and allow developers to specify a condition using the
lmx-conditionattribute that must be met for the template to be rendered. - The library should support dynamic attributes and allow developers to add dynamic attributes to the rendered HTML using the
lmx-dynamic-attributeandlmx-dynamic-attribute-valueattributes. - The library should support swap content and allow developers to swap the content of an element using the
swapContentmethod.
API
HTMLX.init(): Initializes the library and processes all elements with thelmx-triggerattribute.HTMLX.handleTrigger(element, event): Handles the trigger event and swaps the content of the target element with the cloned template content.HTMLX.swapContent(target, content, swapType): Swaps the content of the target element with the cloned template content using the specified swap type.HTMLX.addDynamicAttributes(content): Adds dynamic attributes to the rendered HTML.
Example
Here is an example of how to use HTMLX:
<!-- Define the templates -->
<template id="template-1">
<div>
<h2>Template 1</h2>
<p>This is template 1 content.</p>
</div>
</template>
<!-- Define the triggers -->
<button lmx-trigger="click" lmx-target="#target-1" lmx-template-id="template-1">Trigger 1</button>
<!-- Define the target -->
<div id="target-1"></div>
<!-- Initialize the library -->
<script>
HTMLX.init();
</script>
This example defines a template and a trigger that swaps the content of the target element with the cloned template content when clicked.