index.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. var __defProp = Object.defineProperty;
  2. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  3. var __getOwnPropNames = Object.getOwnPropertyNames;
  4. var __hasOwnProp = Object.prototype.hasOwnProperty;
  5. var __export = (target, all) => {
  6. for (var name in all)
  7. __defProp(target, name, { get: all[name], enumerable: true });
  8. };
  9. var __copyProps = (to, from, except, desc) => {
  10. if (from && typeof from === "object" || typeof from === "function") {
  11. for (let key of __getOwnPropNames(from))
  12. if (!__hasOwnProp.call(to, key) && key !== except)
  13. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  14. }
  15. return to;
  16. };
  17. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  18. var css_exports = {};
  19. __export(css_exports, {
  20. Style: () => Style,
  21. createCssContext: () => createCssContext,
  22. css: () => css,
  23. cx: () => cx,
  24. keyframes: () => keyframes,
  25. rawCssString: () => import_common2.rawCssString,
  26. viewTransition: () => viewTransition
  27. });
  28. module.exports = __toCommonJS(css_exports);
  29. var import_html = require("../../helper/html");
  30. var import_constants = require("../../jsx/constants");
  31. var import_css = require("../../jsx/dom/css");
  32. var import_common = require("./common");
  33. var import_common2 = require("./common");
  34. const createCssContext = ({
  35. id,
  36. classNameSlug,
  37. onInvalidSlug
  38. }) => {
  39. const [cssJsxDomObject, StyleRenderToDom] = (0, import_css.createCssJsxDomObjects)({ id });
  40. const contextMap = /* @__PURE__ */ new WeakMap();
  41. const nonceMap = /* @__PURE__ */ new WeakMap();
  42. const replaceStyleRe = new RegExp(`(<style id="${id}"(?: nonce="[^"]*")?>.*?)(</style>)`);
  43. const newCssClassNameObject = (cssClassName) => {
  44. const appendStyle = ({ buffer, context }) => {
  45. const [toAdd, added] = contextMap.get(context);
  46. const names = Object.keys(toAdd);
  47. if (!names.length) {
  48. return;
  49. }
  50. let stylesStr = "";
  51. names.forEach((className2) => {
  52. added[className2] = true;
  53. stylesStr += className2.startsWith(import_common.PSEUDO_GLOBAL_SELECTOR) ? toAdd[className2] : `${className2[0] === "@" ? "" : "."}${className2}{${toAdd[className2]}}`;
  54. });
  55. contextMap.set(context, [{}, added]);
  56. if (buffer && replaceStyleRe.test(buffer[0])) {
  57. buffer[0] = buffer[0].replace(replaceStyleRe, (_, pre, post) => `${pre}${stylesStr}${post}`);
  58. return;
  59. }
  60. const nonce = nonceMap.get(context);
  61. const appendStyleScript = `<script${nonce ? ` nonce="${nonce}"` : ""}>document.querySelector('#${id}').textContent+=${JSON.stringify(stylesStr)}</script>`;
  62. if (buffer) {
  63. buffer[0] = `${appendStyleScript}${buffer[0]}`;
  64. return;
  65. }
  66. return Promise.resolve(appendStyleScript);
  67. };
  68. const addClassNameToContext = ({ context }) => {
  69. if (!contextMap.has(context)) {
  70. contextMap.set(context, [{}, {}]);
  71. }
  72. const [toAdd, added] = contextMap.get(context);
  73. let allAdded = true;
  74. if (!added[cssClassName[import_common.SELECTOR]]) {
  75. allAdded = false;
  76. toAdd[cssClassName[import_common.SELECTOR]] = cssClassName[import_common.STYLE_STRING];
  77. }
  78. cssClassName[import_common.SELECTORS].forEach(
  79. ({ [import_common.CLASS_NAME]: className2, [import_common.STYLE_STRING]: styleString }) => {
  80. if (!added[className2]) {
  81. allAdded = false;
  82. toAdd[className2] = styleString;
  83. }
  84. }
  85. );
  86. if (allAdded) {
  87. return;
  88. }
  89. return Promise.resolve((0, import_html.raw)("", [appendStyle]));
  90. };
  91. const className = new String(cssClassName[import_common.CLASS_NAME]);
  92. Object.assign(className, cssClassName);
  93. className.isEscaped = true;
  94. className.callbacks = [addClassNameToContext];
  95. const promise = Promise.resolve(className);
  96. Object.assign(promise, cssClassName);
  97. promise.toString = cssJsxDomObject.toString;
  98. return promise;
  99. };
  100. const css2 = (strings, ...values) => {
  101. return newCssClassNameObject((0, import_common.cssCommon)(strings, values, classNameSlug, onInvalidSlug));
  102. };
  103. const cx2 = (...args) => {
  104. args = (0, import_common.cxCommon)(args);
  105. return css2(Array(args.length).fill(""), ...args);
  106. };
  107. const keyframes2 = (strings, ...values) => (0, import_common.keyframesCommon)(strings, values, classNameSlug, onInvalidSlug);
  108. const viewTransition2 = ((strings, ...values) => {
  109. return newCssClassNameObject(
  110. (0, import_common.viewTransitionCommon)(strings, values, classNameSlug, onInvalidSlug)
  111. // eslint-disable-line @typescript-eslint/no-explicit-any
  112. );
  113. });
  114. const Style2 = ({ children, nonce } = {}) => (0, import_html.raw)(
  115. `<style id="${id}"${nonce ? ` nonce="${nonce}"` : ""}>${children ? children[import_common.STYLE_STRING] : ""}</style>`,
  116. [
  117. ({ context }) => {
  118. nonceMap.set(context, nonce);
  119. return void 0;
  120. }
  121. ]
  122. );
  123. Style2[import_constants.DOM_RENDERER] = StyleRenderToDom;
  124. return {
  125. css: css2,
  126. cx: cx2,
  127. keyframes: keyframes2,
  128. viewTransition: viewTransition2,
  129. Style: Style2
  130. };
  131. };
  132. const defaultContext = createCssContext({
  133. id: import_common.DEFAULT_STYLE_ID
  134. });
  135. const css = defaultContext.css;
  136. const cx = defaultContext.cx;
  137. const keyframes = defaultContext.keyframes;
  138. const viewTransition = defaultContext.viewTransition;
  139. const Style = defaultContext.Style;
  140. // Annotate the CommonJS export names for ESM import in node:
  141. 0 && (module.exports = {
  142. Style,
  143. createCssContext,
  144. css,
  145. cx,
  146. keyframes,
  147. rawCssString,
  148. viewTransition
  149. });