mirror of
https://github.com/hunvreus/basecoat.git
synced 2026-11-14 14:08:18 +00:00
Add force init lifecycle option
This commit is contained in:
@@ -159,7 +159,33 @@ const styleUrls = {
|
||||
}
|
||||
};
|
||||
|
||||
const clearSerializedRuntimeState = (root) => {
|
||||
for (const element of root.querySelectorAll?.("*") || []) {
|
||||
delete element.dataset.basecoatComponent;
|
||||
for (const attribute of Array.from(element.attributes)) {
|
||||
if (/^data-[a-z0-9-]+-initialized$/.test(attribute.name)) {
|
||||
element.removeAttribute(attribute.name);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const cleanHistoryItem = (event) => {
|
||||
const item = event.detail?.item;
|
||||
if (typeof item?.content !== "string") return;
|
||||
|
||||
const template = document.createElement("template");
|
||||
template.innerHTML = item.content;
|
||||
clearSerializedRuntimeState(template.content);
|
||||
item.content = template.innerHTML;
|
||||
};
|
||||
|
||||
document.addEventListener("DOMContentLoaded", syncSidebar);
|
||||
document.addEventListener("htmx:historyItemCreated", cleanHistoryItem);
|
||||
document.addEventListener("htmx:historyRestore", () => {
|
||||
window.basecoat?.initAll?.({ force: true });
|
||||
syncSidebar();
|
||||
});
|
||||
document.addEventListener("htmx:afterSettle", () => {
|
||||
window.basecoat?.initAll?.();
|
||||
syncSidebar();
|
||||
|
||||
@@ -243,6 +243,14 @@ Chart uses a separate helper and is not included in `basecoat-css/all`.
|
||||
|
||||
Each component page shows the minimal scripts or imports required for that component.
|
||||
|
||||
### JavaScript lifecycle
|
||||
|
||||
Basecoat initializes registered components on page load and when new DOM is inserted.
|
||||
|
||||
Use `window.basecoat.initAll()` after manually inserting HTML that contains Basecoat components. It only initializes components that are not already initialized.
|
||||
|
||||
Use `window.basecoat.initAll({ force: true })` when a framework or navigation library restores previously initialized DOM from a cache, such as an HTMX history restore. Force mode destroys existing component instances before reinitializing them, so it also resets transient state such as open menus, focus state, and active interactions.
|
||||
|
||||
## Compatibility
|
||||
|
||||
Basecoat 1.0 introduces new style packs and a smaller public API based on root classes plus semantic HTML and documented attributes. If you need pre-1.0 class aliases while migrating, load the compatibility stylesheet after your Basecoat stylesheet.
|
||||
|
||||
Reference in New Issue
Block a user