css.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. // src/jsx/dom/css.ts
  2. import {
  3. CLASS_NAME,
  4. DEFAULT_STYLE_ID,
  5. PSEUDO_GLOBAL_SELECTOR,
  6. SELECTOR,
  7. SELECTORS,
  8. STYLE_STRING,
  9. cssCommon,
  10. cxCommon,
  11. keyframesCommon,
  12. viewTransitionCommon
  13. } from "../../helper/css/common.js";
  14. import { rawCssString } from "../../helper/css/common.js";
  15. var splitRule = (rule) => {
  16. const result = [];
  17. let startPos = 0;
  18. let depth = 0;
  19. for (let i = 0, len = rule.length; i < len; i++) {
  20. const char = rule[i];
  21. if (char === "'" || char === '"') {
  22. const quote = char;
  23. i++;
  24. for (; i < len; i++) {
  25. if (rule[i] === "\\") {
  26. i++;
  27. continue;
  28. }
  29. if (rule[i] === quote) {
  30. break;
  31. }
  32. }
  33. continue;
  34. }
  35. if (char === "{") {
  36. depth++;
  37. continue;
  38. }
  39. if (char === "}") {
  40. depth--;
  41. if (depth === 0) {
  42. result.push(rule.slice(startPos, i + 1));
  43. startPos = i + 1;
  44. }
  45. continue;
  46. }
  47. }
  48. return result;
  49. };
  50. var createCssJsxDomObjects = ({ id }) => {
  51. let styleSheet = void 0;
  52. const findStyleSheet = () => {
  53. if (!styleSheet) {
  54. styleSheet = document.querySelector(`style#${id}`)?.sheet;
  55. if (styleSheet) {
  56. ;
  57. styleSheet.addedStyles = /* @__PURE__ */ new Set();
  58. }
  59. }
  60. return styleSheet ? [styleSheet, styleSheet.addedStyles] : [];
  61. };
  62. const insertRule = (className, styleString) => {
  63. const [sheet, addedStyles] = findStyleSheet();
  64. if (!sheet || !addedStyles) {
  65. Promise.resolve().then(() => {
  66. if (!findStyleSheet()[0]) {
  67. throw new Error("style sheet not found");
  68. }
  69. insertRule(className, styleString);
  70. });
  71. return;
  72. }
  73. if (!addedStyles.has(className)) {
  74. addedStyles.add(className);
  75. (className.startsWith(PSEUDO_GLOBAL_SELECTOR) ? splitRule(styleString) : [`${className[0] === "@" ? "" : "."}${className}{${styleString}}`]).forEach((rule) => {
  76. sheet.insertRule(rule, sheet.cssRules.length);
  77. });
  78. }
  79. };
  80. const cssObject = {
  81. toString() {
  82. const selector = this[SELECTOR];
  83. insertRule(selector, this[STYLE_STRING]);
  84. this[SELECTORS].forEach(({ [CLASS_NAME]: className, [STYLE_STRING]: styleString }) => {
  85. insertRule(className, styleString);
  86. });
  87. return this[CLASS_NAME];
  88. }
  89. };
  90. const Style2 = ({ children, nonce }) => ({
  91. tag: "style",
  92. props: {
  93. id,
  94. nonce,
  95. children: children && (Array.isArray(children) ? children : [children]).map(
  96. (c) => c[STYLE_STRING]
  97. )
  98. }
  99. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  100. });
  101. return [cssObject, Style2];
  102. };
  103. var createCssContext = ({
  104. id,
  105. classNameSlug,
  106. onInvalidSlug
  107. }) => {
  108. const [cssObject, Style2] = createCssJsxDomObjects({ id });
  109. const newCssClassNameObject = (cssClassName) => {
  110. cssClassName.toString = cssObject.toString;
  111. return cssClassName;
  112. };
  113. const css2 = (strings, ...values) => {
  114. return newCssClassNameObject(cssCommon(strings, values, classNameSlug, onInvalidSlug));
  115. };
  116. const cx2 = (...args) => {
  117. args = cxCommon(args);
  118. return css2(Array(args.length).fill(""), ...args);
  119. };
  120. const keyframes2 = (strings, ...values) => keyframesCommon(strings, values, classNameSlug, onInvalidSlug);
  121. const viewTransition2 = ((strings, ...values) => {
  122. return newCssClassNameObject(
  123. viewTransitionCommon(strings, values, classNameSlug, onInvalidSlug)
  124. // eslint-disable-line @typescript-eslint/no-explicit-any
  125. );
  126. });
  127. return {
  128. css: css2,
  129. cx: cx2,
  130. keyframes: keyframes2,
  131. viewTransition: viewTransition2,
  132. Style: Style2
  133. };
  134. };
  135. var defaultContext = createCssContext({ id: DEFAULT_STYLE_ID });
  136. var css = defaultContext.css;
  137. var cx = defaultContext.cx;
  138. var keyframes = defaultContext.keyframes;
  139. var viewTransition = defaultContext.viewTransition;
  140. var Style = defaultContext.Style;
  141. export {
  142. Style,
  143. createCssContext,
  144. createCssJsxDomObjects,
  145. css,
  146. cx,
  147. keyframes,
  148. rawCssString,
  149. viewTransition
  150. };