refactor(index): wire boot() to mountWidget for classic + shadow modes
The IIFE that injected styles into document.head unconditionally is gone. boot() now reads config.shadow, derives the mount mode, and lets mountWidget handle DOM topology while a small helper keeps light-DOM style injection idempotent for the classic path. Behaviour unchanged for any embed without data-shadow="true": styles still land in document.head, render still targets #messenzy-root in light DOM. With data-shadow="true", the shadow path is exercised end-to-end. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
f74ff56fc4
commit
1b94469a60
30
src/index.ts
30
src/index.ts
@ -1,18 +1,19 @@
|
||||
import { h, render } from 'preact';
|
||||
import { Widget } from './ui/widget.js';
|
||||
import { parseConfig } from './config.js';
|
||||
import { mountWidget, type MountMode } from './mount.js';
|
||||
import cssText from './ui/theme.css?inline';
|
||||
|
||||
// Inject styles into shadow-free <style> on first load
|
||||
(function injectStyles() {
|
||||
if (document.getElementById('messenzy-styles')) return;
|
||||
const GLOBAL_STYLE_ID = 'messenzy-styles';
|
||||
|
||||
function maybeInjectGlobalStyles(mode: MountMode): void {
|
||||
if (mode === 'shadow') return;
|
||||
if (document.getElementById(GLOBAL_STYLE_ID)) return;
|
||||
const style = document.createElement('style');
|
||||
style.id = 'messenzy-styles';
|
||||
style.id = GLOBAL_STYLE_ID;
|
||||
style.textContent = cssText;
|
||||
document.head.appendChild(style);
|
||||
})();
|
||||
}
|
||||
|
||||
function boot() {
|
||||
function boot(): void {
|
||||
const config = parseConfig();
|
||||
if (!config) {
|
||||
console.warn(
|
||||
@ -21,16 +22,9 @@ function boot() {
|
||||
return;
|
||||
}
|
||||
|
||||
const root =
|
||||
document.getElementById('messenzy-root') ??
|
||||
(() => {
|
||||
const d = document.createElement('div');
|
||||
d.id = 'messenzy-root';
|
||||
document.body.appendChild(d);
|
||||
return d;
|
||||
})();
|
||||
|
||||
render(h(Widget, { config }), root);
|
||||
const mode: MountMode = config.shadow ? 'shadow' : 'classic';
|
||||
maybeInjectGlobalStyles(mode);
|
||||
mountWidget(config, cssText, mode);
|
||||
}
|
||||
|
||||
if (document.readyState === 'loading') {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user