@gears-frontx/react 0.2.0-alpha.2 → 0.2.0-alpha.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +106 -12
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +119 -17
- package/dist/index.d.ts +119 -17
- package/dist/index.js +106 -12
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -1282,6 +1282,9 @@ function useRegisteredPackages() {
|
|
|
1282
1282
|
// src/mfe/ThemeAwareReactLifecycle.tsx
|
|
1283
1283
|
var import_client = require("react-dom/client");
|
|
1284
1284
|
var import_jsx_runtime4 = require("react/jsx-runtime");
|
|
1285
|
+
var ADOPTED_HOST_STYLE_ATTR = "data-frontx-adopted-host-style";
|
|
1286
|
+
var BASE_RESETS_STYLE_ID = "__frontx-base-resets__";
|
|
1287
|
+
var SHELL_OWNED_SHADOW_STYLE_IDS = ["__frontx-shadow-isolation__", "__frontx-css-variables__"];
|
|
1285
1288
|
function resolveProviderMountOptions(app, bridge, mountContext) {
|
|
1286
1289
|
const extensionId = mountContext?.extensionId;
|
|
1287
1290
|
const domainId = mountContext?.domainId;
|
|
@@ -1336,27 +1339,108 @@ var ThemeAwareReactLifecycle = class {
|
|
|
1336
1339
|
/**
|
|
1337
1340
|
* Copy all inline <style> and <link rel="stylesheet"> from the host document
|
|
1338
1341
|
* into the shadow root so that Tailwind and component styles apply inside the MFE.
|
|
1342
|
+
*
|
|
1343
|
+
* The clones are inserted ahead of everything already in the shadow root, never
|
|
1344
|
+
* appended, because adopted host styles are context rather than authority: where
|
|
1345
|
+
* they and the MFE's own CSS declare the same property at equal specificity, the
|
|
1346
|
+
* MFE has to win. Appending inverted that. `MfeHandlerMF` injects the MFE's
|
|
1347
|
+
* compiled stylesheet into the shadow root before it calls mount, so an appended
|
|
1348
|
+
* clone of the shell's Tailwind preflight - whose
|
|
1349
|
+
* `button, [type='button'], [type='reset'], [type='submit']` rule sets
|
|
1350
|
+
* `background-color: transparent` at specificity (0,1,0) - tied with
|
|
1351
|
+
* `@gears-frontx/ui-kit`'s single-class button rule and won on document order,
|
|
1352
|
+
* leaving every kit Button transparent until it was hovered.
|
|
1353
|
+
*
|
|
1354
|
+
* Inserting at the front rather than pushing MFE styles to the back is what makes
|
|
1355
|
+
* the invariant hold for stylesheets that appear after mount too - a lazily
|
|
1356
|
+
* imported component's CSS module, or whatever initializeStyles() adds - since
|
|
1357
|
+
* those land behind the adopted block by construction.
|
|
1358
|
+
*
|
|
1359
|
+
* A cascade layer around the adopted CSS was the alternative and is rejected:
|
|
1360
|
+
* layered rules lose to unlayered ones at *any* specificity, so a host utility
|
|
1361
|
+
* class would stop overriding an MFE element selector. Document order keeps
|
|
1362
|
+
* specificity in charge and only settles the ties.
|
|
1363
|
+
*
|
|
1364
|
+
* The block replaces its predecessor instead of stacking on it, the same
|
|
1365
|
+
* upsert shape `injectStylesheet` and `injectCssVariables` use in
|
|
1366
|
+
* `@gears-frontx/mfes`' shadow utilities, widened from one id-keyed node to a
|
|
1367
|
+
* marked block. Replacement is required rather than tidy: `createShadowRoot`
|
|
1368
|
+
* hands back an existing `element.shadowRoot` instead of attaching a new one,
|
|
1369
|
+
* so remounting the same container runs this method again on a shadow root
|
|
1370
|
+
* that already holds a full adopted block, and appending would grow it once
|
|
1371
|
+
* per mount with no bound. It does not save re-resolving the cloned `<link>`s -
|
|
1372
|
+
* every mount clones them afresh and the browser resolves each clone; what
|
|
1373
|
+
* replacement buys is that the count stays at one block instead of one per
|
|
1374
|
+
* mount.
|
|
1375
|
+
*
|
|
1376
|
+
* The block is rebuilt from the host head rather than left in place, because
|
|
1377
|
+
* the head is not fixed: a lazily imported chunk adds stylesheets to it after
|
|
1378
|
+
* the first mount, and the second mount has to pick those up.
|
|
1379
|
+
*
|
|
1380
|
+
* The fresh block goes in before the previous one comes out. Removing first
|
|
1381
|
+
* leaves the shadow root with no host CSS at all, and everything between that
|
|
1382
|
+
* removal and the insertion is synchronous work in this same task - so the gap
|
|
1383
|
+
* never reaches a paint, but any code that reads computed styles from the tree
|
|
1384
|
+
* in between sees an unstyled one. Inserting first costs one moment with two
|
|
1385
|
+
* blocks present, which changes nothing: the old block is a clone of the same
|
|
1386
|
+
* host head and loses every tie to the new one on document order anyway.
|
|
1339
1387
|
*/
|
|
1340
1388
|
adoptHostStylesIntoShadowRoot(shadowRoot) {
|
|
1341
|
-
const
|
|
1342
|
-
|
|
1343
|
-
|
|
1344
|
-
|
|
1345
|
-
|
|
1346
|
-
|
|
1347
|
-
|
|
1348
|
-
|
|
1349
|
-
|
|
1350
|
-
|
|
1389
|
+
const supersededStyles = Array.from(shadowRoot.querySelectorAll(`[${ADOPTED_HOST_STYLE_ATTR}]`));
|
|
1390
|
+
const adoptedStyles = document.createDocumentFragment();
|
|
1391
|
+
const hostStyleNodes = document.head.querySelectorAll('style, link[rel="stylesheet"]');
|
|
1392
|
+
hostStyleNodes.forEach((el) => {
|
|
1393
|
+
if (el instanceof HTMLStyleElement) {
|
|
1394
|
+
const clone = document.createElement("style");
|
|
1395
|
+
clone.textContent = el.textContent ?? "";
|
|
1396
|
+
clone.setAttribute(ADOPTED_HOST_STYLE_ATTR, "");
|
|
1397
|
+
adoptedStyles.appendChild(clone);
|
|
1398
|
+
return;
|
|
1399
|
+
}
|
|
1400
|
+
const linkClone = el.cloneNode(true);
|
|
1401
|
+
linkClone.setAttribute(ADOPTED_HOST_STYLE_ATTR, "");
|
|
1402
|
+
adoptedStyles.appendChild(linkClone);
|
|
1351
1403
|
});
|
|
1404
|
+
shadowRoot.insertBefore(adoptedStyles, shadowRoot.firstChild);
|
|
1405
|
+
supersededStyles.forEach((el) => el.remove());
|
|
1352
1406
|
}
|
|
1353
1407
|
/**
|
|
1354
1408
|
* Box-model resets and :host defaults needed inside every shadow root.
|
|
1355
1409
|
* These aren't part of Tailwind's compiled output but are required for
|
|
1356
1410
|
* consistent rendering across browsers.
|
|
1411
|
+
*
|
|
1412
|
+
* Reuses the node a previous mount left behind, exactly as `injectStylesheet`
|
|
1413
|
+
* does in `@gears-frontx/mfes`' shadow utilities: a remount runs against a
|
|
1414
|
+
* shadow root that `createShadowRoot` handed back intact, and appending a
|
|
1415
|
+
* second copy would add one node per mount forever.
|
|
1416
|
+
*
|
|
1417
|
+
* Reuse alone is not enough, which is why the node is also positioned on every
|
|
1418
|
+
* mount rather than left where the previous one sat. These resets are context
|
|
1419
|
+
* like the adopted block is - `border-color: currentColor` on `*` is a 0-0-0
|
|
1420
|
+
* declaration that beats an MFE preflight's own 0-0-0 reset purely on document
|
|
1421
|
+
* order - so they have to stay ahead of the MFE's CSS, and where they land is
|
|
1422
|
+
* not stable on its own. `MfeHandlerMF.wrapLifecycleWithStylesheets` removes the
|
|
1423
|
+
* MFE's stylesheet on unmount and appends a fresh one before the next mount, so
|
|
1424
|
+
* a node merely left in place ends up ahead of that stylesheet on a remount and
|
|
1425
|
+
* behind it on the first mount: two different cascades for one component.
|
|
1426
|
+
* Anchoring it to the end of the shell-owned head gives every mount the same
|
|
1427
|
+
* order - adopted block, isolation, resets, MFE - which is also the order the
|
|
1428
|
+
* adoption method's invariant is written against.
|
|
1429
|
+
*
|
|
1430
|
+
* With no shell-owned node to anchor to, the resets go to the front. That is the
|
|
1431
|
+
* plain-`Element` container: no adopted block, no isolation style, and the MFE
|
|
1432
|
+
* stylesheet already appended by the handler, so the front is again the only
|
|
1433
|
+
* position that leaves the MFE winning ties.
|
|
1434
|
+
*
|
|
1435
|
+
* Looked up by attribute-selector form rather than `getElementById`, which
|
|
1436
|
+
* `ShadowRoot` has but `Element` does not, and this hook takes either.
|
|
1357
1437
|
*/
|
|
1358
1438
|
injectBaseResets(container) {
|
|
1359
|
-
const
|
|
1439
|
+
const existing = container.querySelector(
|
|
1440
|
+
`style[id="${BASE_RESETS_STYLE_ID}"]`
|
|
1441
|
+
);
|
|
1442
|
+
const style = existing ?? document.createElement("style");
|
|
1443
|
+
style.id = BASE_RESETS_STYLE_ID;
|
|
1360
1444
|
style.textContent = `
|
|
1361
1445
|
*, *::before, *::after {
|
|
1362
1446
|
box-sizing: border-box;
|
|
@@ -1374,7 +1458,17 @@ var ThemeAwareReactLifecycle = class {
|
|
|
1374
1458
|
background-color: hsl(var(--background));
|
|
1375
1459
|
}
|
|
1376
1460
|
`;
|
|
1377
|
-
container.
|
|
1461
|
+
const shellOwnedHead = container.querySelectorAll(
|
|
1462
|
+
[
|
|
1463
|
+
`[${ADOPTED_HOST_STYLE_ATTR}]`,
|
|
1464
|
+
...SHELL_OWNED_SHADOW_STYLE_IDS.map((id) => `[id="${id}"]`)
|
|
1465
|
+
].join(", ")
|
|
1466
|
+
);
|
|
1467
|
+
const lastShellOwnedNode = shellOwnedHead[shellOwnedHead.length - 1];
|
|
1468
|
+
container.insertBefore(
|
|
1469
|
+
style,
|
|
1470
|
+
lastShellOwnedNode ? lastShellOwnedNode.nextSibling : container.firstChild
|
|
1471
|
+
);
|
|
1378
1472
|
}
|
|
1379
1473
|
/**
|
|
1380
1474
|
* Hook for subclasses to inject additional CSS not covered by the adopted host
|