@webstudio-is/css-engine 0.1.0 → 0.3.0

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.
@@ -1,8 +1,4 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.createCssEngine = void 0;
4
- const css_engine_1 = require("./css-engine");
5
- const createCssEngine = () => {
6
- return new css_engine_1.CssEngine();
1
+ import { CssEngine } from "./css-engine";
2
+ export const createCssEngine = () => {
3
+ return new CssEngine();
7
4
  };
8
- exports.createCssEngine = createCssEngine;
@@ -4,11 +4,12 @@ export declare class CssEngine {
4
4
  #private;
5
5
  constructor();
6
6
  addMediaRule(id: string, options?: MediaRuleOptions): MediaRule;
7
- addStyleRule(selectorText: string, rule: CssRule): StyleRule | PlaintextRule;
7
+ addStyleRule(selectorText: string, rule: CssRule): StyleRule;
8
8
  addPlaintextRule(cssText: string): PlaintextRule | Map<string, PlaintextRule>;
9
9
  addFontFaceRule(options: FontFaceOptions): number;
10
10
  clear(): void;
11
11
  render(): void;
12
+ unmount(): void;
12
13
  get cssText(): string;
13
14
  }
14
15
  //# sourceMappingURL=css-engine.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"css-engine.d.ts","sourceRoot":"","sources":["../../src/core/css-engine.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,wBAAwB,CAAC;AACtD,OAAO,EAEL,SAAS,EACT,aAAa,EACb,SAAS,EACT,KAAK,eAAe,EACpB,KAAK,gBAAgB,EACtB,MAAM,SAAS,CAAC;AAMjB,qBAAa,SAAS;;;IAapB,YAAY,CAAC,EAAE,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,gBAAgB;IASnD,YAAY,CAAC,YAAY,EAAE,MAAM,EAAE,IAAI,EAAE,OAAO;IAahD,gBAAgB,CAAC,OAAO,EAAE,MAAM;IAKhC,eAAe,CAAC,OAAO,EAAE,eAAe;IAGxC,KAAK;IAML,MAAM;IAKN,IAAI,OAAO,WAiBV;CAKF"}
1
+ {"version":3,"file":"css-engine.d.ts","sourceRoot":"","sources":["../../src/core/css-engine.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,wBAAwB,CAAC;AACtD,OAAO,EAEL,SAAS,EACT,aAAa,EACb,SAAS,EACT,KAAK,eAAe,EACpB,KAAK,gBAAgB,EACtB,MAAM,SAAS,CAAC;AAMjB,qBAAa,SAAS;;;IAYpB,YAAY,CAAC,EAAE,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,gBAAgB;IASnD,YAAY,CAAC,YAAY,EAAE,MAAM,EAAE,IAAI,EAAE,OAAO;IAYhD,gBAAgB,CAAC,OAAO,EAAE,MAAM;IAMhC,eAAe,CAAC,OAAO,EAAE,eAAe;IAIxC,KAAK;IAML,MAAM;IAKN,OAAO;IAGP,IAAI,OAAO,WAiBV;CAKF"}
@@ -1,4 +1,3 @@
1
- "use strict";
2
1
  var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
3
2
  if (kind === "m") throw new TypeError("Private method is not writable");
4
3
  if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
@@ -11,13 +10,11 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
11
10
  return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
12
11
  };
13
12
  var _CssEngine_element, _CssEngine_mediaRules, _CssEngine_plainRules, _CssEngine_fontFaceRules, _CssEngine_sheet, _CssEngine_isDirty, _CssEngine_cssText, _CssEngine_onChangeRule;
14
- Object.defineProperty(exports, "__esModule", { value: true });
15
- exports.CssEngine = void 0;
16
- const rules_1 = require("./rules");
17
- const style_element_1 = require("./style-element");
18
- const style_sheet_1 = require("./style-sheet");
13
+ import { FontFaceRule, MediaRule, PlaintextRule, StyleRule, } from "./rules";
14
+ import { StyleElement } from "./style-element";
15
+ import { StyleSheet } from "./style-sheet";
19
16
  const defaultMediaRuleId = "__default-media-rule__";
20
- class CssEngine {
17
+ export class CssEngine {
21
18
  constructor() {
22
19
  _CssEngine_element.set(this, void 0);
23
20
  _CssEngine_mediaRules.set(this, new Map());
@@ -29,39 +26,40 @@ class CssEngine {
29
26
  _CssEngine_onChangeRule.set(this, () => {
30
27
  __classPrivateFieldSet(this, _CssEngine_isDirty, true, "f");
31
28
  });
32
- __classPrivateFieldSet(this, _CssEngine_element, new style_element_1.StyleElement(), "f");
33
- __classPrivateFieldSet(this, _CssEngine_sheet, new style_sheet_1.StyleSheet(__classPrivateFieldGet(this, _CssEngine_element, "f")), "f");
34
- this.addMediaRule(defaultMediaRuleId);
29
+ __classPrivateFieldSet(this, _CssEngine_element, new StyleElement(), "f");
30
+ __classPrivateFieldSet(this, _CssEngine_sheet, new StyleSheet(__classPrivateFieldGet(this, _CssEngine_element, "f")), "f");
35
31
  }
36
32
  addMediaRule(id, options) {
37
33
  let mediaRule = __classPrivateFieldGet(this, _CssEngine_mediaRules, "f").get(id);
38
34
  if (mediaRule === undefined) {
39
- mediaRule = new rules_1.MediaRule(options);
35
+ mediaRule = new MediaRule(options);
40
36
  __classPrivateFieldGet(this, _CssEngine_mediaRules, "f").set(id, mediaRule);
41
37
  __classPrivateFieldSet(this, _CssEngine_isDirty, true, "f");
42
38
  }
43
39
  return mediaRule;
44
40
  }
45
41
  addStyleRule(selectorText, rule) {
46
- const mediaRule = __classPrivateFieldGet(this, _CssEngine_mediaRules, "f").get(rule.breakpoint) ??
47
- __classPrivateFieldGet(this, _CssEngine_mediaRules, "f").get(defaultMediaRuleId);
42
+ const mediaRule = this.addMediaRule(rule.breakpoint || defaultMediaRuleId);
48
43
  __classPrivateFieldSet(this, _CssEngine_isDirty, true, "f");
49
- const styleRule = new rules_1.StyleRule(selectorText, rule.style);
44
+ const styleRule = new StyleRule(selectorText, rule.style);
50
45
  styleRule.onChange = __classPrivateFieldGet(this, _CssEngine_onChangeRule, "f");
51
46
  if (mediaRule === undefined) {
52
47
  // Should be impossible to reach.
53
48
  throw new Error("No media rule found");
54
49
  }
55
- return mediaRule.insertRule(styleRule);
50
+ mediaRule.insertRule(styleRule);
51
+ return styleRule;
56
52
  }
57
53
  addPlaintextRule(cssText) {
58
54
  const rule = __classPrivateFieldGet(this, _CssEngine_plainRules, "f").get(cssText);
59
55
  if (rule !== undefined)
60
56
  return rule;
61
- return __classPrivateFieldGet(this, _CssEngine_plainRules, "f").set(cssText, new rules_1.PlaintextRule(cssText));
57
+ __classPrivateFieldSet(this, _CssEngine_isDirty, true, "f");
58
+ return __classPrivateFieldGet(this, _CssEngine_plainRules, "f").set(cssText, new PlaintextRule(cssText));
62
59
  }
63
60
  addFontFaceRule(options) {
64
- return __classPrivateFieldGet(this, _CssEngine_fontFaceRules, "f").push(new rules_1.FontFaceRule(options));
61
+ __classPrivateFieldSet(this, _CssEngine_isDirty, true, "f");
62
+ return __classPrivateFieldGet(this, _CssEngine_fontFaceRules, "f").push(new FontFaceRule(options));
65
63
  }
66
64
  clear() {
67
65
  __classPrivateFieldGet(this, _CssEngine_mediaRules, "f").clear();
@@ -74,6 +72,9 @@ class CssEngine {
74
72
  // This isn't going to do anything if the `cssText` hasn't changed.
75
73
  __classPrivateFieldGet(this, _CssEngine_sheet, "f").replaceSync(this.cssText);
76
74
  }
75
+ unmount() {
76
+ __classPrivateFieldGet(this, _CssEngine_element, "f").unmount();
77
+ }
77
78
  get cssText() {
78
79
  if (__classPrivateFieldGet(this, _CssEngine_isDirty, "f") === false) {
79
80
  return __classPrivateFieldGet(this, _CssEngine_cssText, "f");
@@ -93,5 +94,4 @@ class CssEngine {
93
94
  return __classPrivateFieldGet(this, _CssEngine_cssText, "f");
94
95
  }
95
96
  }
96
- exports.CssEngine = CssEngine;
97
97
  _CssEngine_element = new WeakMap(), _CssEngine_mediaRules = new WeakMap(), _CssEngine_plainRules = new WeakMap(), _CssEngine_fontFaceRules = new WeakMap(), _CssEngine_sheet = new WeakMap(), _CssEngine_isDirty = new WeakMap(), _CssEngine_cssText = new WeakMap(), _CssEngine_onChangeRule = new WeakMap();
@@ -1,3 +1,4 @@
1
+ /// <reference types="react" />
1
2
  declare const _default: {
2
3
  component: string;
3
4
  };
@@ -1 +1 @@
1
- {"version":3,"file":"css-engine.stories.d.ts","sourceRoot":"","sources":["../../src/core/css-engine.stories.tsx"],"names":[],"mappings":";;;AAEA,wBAEE;AASF,eAAO,MAAM,KAAK,mBAkCjB,CAAC"}
1
+ {"version":3,"file":"css-engine.stories.d.ts","sourceRoot":"","sources":["../../src/core/css-engine.stories.tsx"],"names":[],"mappings":";;;;AAEA,wBAEE;AASF,eAAO,MAAM,KAAK,mBAkCjB,CAAC"}
@@ -1,9 +1,6 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Basic = void 0;
4
- const jsx_runtime_1 = require("react/jsx-runtime");
5
- const css_engine_1 = require("./css-engine");
6
- exports.default = {
1
+ import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
2
+ import { CssEngine } from "./css-engine";
3
+ export default {
7
4
  component: "CssEngine",
8
5
  };
9
6
  const style0 = {
@@ -11,18 +8,18 @@ const style0 = {
11
8
  };
12
9
  const mediaRuleOptions0 = { minWidth: 0 };
13
10
  const mediaId = "0";
14
- const Basic = () => {
15
- const engine = new css_engine_1.CssEngine();
11
+ export const Basic = () => {
12
+ const engine = new CssEngine();
16
13
  engine.addMediaRule(mediaId, mediaRuleOptions0);
17
14
  const rule = engine.addStyleRule(".test", {
18
15
  style: style0,
19
16
  breakpoint: "0",
20
17
  });
21
18
  engine.render();
22
- return ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)("div", { className: "test", children: "Should be red" }), (0, jsx_runtime_1.jsx)("button", { onClick: () => {
19
+ return (_jsxs(_Fragment, { children: [_jsx("div", { className: "test", children: "Should be red" }), _jsx("button", { onClick: () => {
23
20
  rule.styleMap.set("color", { type: "keyword", value: "green" });
24
21
  engine.render();
25
- }, children: "Make it green" }), (0, jsx_runtime_1.jsx)("button", { onClick: () => {
22
+ }, children: "Make it green" }), _jsx("button", { onClick: () => {
26
23
  engine.addStyleRule(".test", {
27
24
  style: {
28
25
  background: { type: "keyword", value: "yellow" },
@@ -32,4 +29,3 @@ const Basic = () => {
32
29
  engine.render();
33
30
  }, children: "Add rule with yellow background" })] }));
34
31
  };
35
- exports.Basic = Basic;
@@ -1,6 +1,4 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const css_engine_1 = require("./css-engine");
1
+ import { CssEngine } from "./css-engine";
4
2
  const style0 = {
5
3
  display: { type: "keyword", value: "block" },
6
4
  };
@@ -9,12 +7,12 @@ const mediaId = "0";
9
7
  describe("CssEngine", () => {
10
8
  let engine;
11
9
  beforeEach(() => {
12
- engine = new css_engine_1.CssEngine();
10
+ engine = new CssEngine();
13
11
  });
14
12
  test("use default media rule when there is no matching one registrered", () => {
15
13
  engine.addStyleRule(".c", {
16
14
  style: style0,
17
- breakpoint: "0",
15
+ breakpoint: "x",
18
16
  });
19
17
  expect(engine.cssText).toMatchInlineSnapshot(`
20
18
  "@media all {
@@ -23,7 +21,7 @@ describe("CssEngine", () => {
23
21
  `);
24
22
  engine.addStyleRule(".c1", {
25
23
  style: { color: { type: "keyword", value: "red" } },
26
- breakpoint: "0",
24
+ breakpoint: "x",
27
25
  });
28
26
  expect(engine.cssText).toMatchInlineSnapshot(`
29
27
  "@media all {
@@ -34,7 +32,7 @@ describe("CssEngine", () => {
34
32
  engine.addMediaRule(mediaId, mediaRuleOptions0);
35
33
  engine.addStyleRule(".c1", {
36
34
  style: { color: { type: "keyword", value: "blue" } },
37
- breakpoint: "0",
35
+ breakpoint: mediaId,
38
36
  });
39
37
  // Default media query should allways be the first to have the lowest source order specificity
40
38
  expect(engine.cssText).toMatchInlineSnapshot(`
package/lib/core/index.js CHANGED
@@ -1,21 +1,3 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
- };
16
- Object.defineProperty(exports, "__esModule", { value: true });
17
- exports.CssEngine = void 0;
18
- var css_engine_1 = require("./css-engine");
19
- Object.defineProperty(exports, "CssEngine", { enumerable: true, get: function () { return css_engine_1.CssEngine; } });
20
- __exportStar(require("./create-css-engine"), exports);
21
- __exportStar(require("./to-value"), exports);
1
+ export { CssEngine } from "./css-engine";
2
+ export * from "./create-css-engine";
3
+ export * from "./to-value";
@@ -3,6 +3,7 @@ declare class StylePropertyMap {
3
3
  #private;
4
4
  onChange?: () => void;
5
5
  set(property: StyleProperty, value?: StyleValue): void;
6
+ clear(): void;
6
7
  toString(): string;
7
8
  }
8
9
  export declare class StyleRule {
@@ -1 +1 @@
1
- {"version":3,"file":"rules.d.ts","sourceRoot":"","sources":["../../src/core/rules.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,aAAa,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAC;AAI/E,cAAM,gBAAgB;;IAIpB,QAAQ,CAAC,EAAE,MAAM,IAAI,CAAC;IACtB,GAAG,CAAC,QAAQ,EAAE,aAAa,EAAE,KAAK,CAAC,EAAE,UAAU;IAK/C,QAAQ;CAaT;AAED,qBAAa,SAAS;;IACpB,QAAQ,mBAAC;IACT,YAAY,SAAC;IACb,QAAQ,CAAC,EAAE,MAAM,IAAI,CAAC;gBACV,YAAY,EAAE,MAAM,EAAE,KAAK,EAAE,KAAK;IAY9C,IAAI,OAAO,WAEV;CACF;AAED,oBAAY,gBAAgB,GAAG;IAC7B,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,SAAS,CAAC,EAAE,KAAK,GAAG,QAAQ,GAAG,OAAO,CAAC;CACxC,CAAC;AAEF,qBAAa,SAAS;;IAEpB,KAAK,EAAE,KAAK,CAAC,SAAS,GAAG,aAAa,CAAC,CAAM;gBAEjC,OAAO,GAAE,gBAAqB;IAI1C,UAAU,CAAC,IAAI,EAAE,SAAS,GAAG,aAAa;IAI1C,IAAI,OAAO,WAcV;CACF;AAED,qBAAa,aAAa;IACxB,OAAO,SAAC;IACR,QAAQ,gBAAa;gBACT,OAAO,EAAE,MAAM;CAG5B;AAED,oBAAY,eAAe,GAAG;IAC5B,UAAU,EAAE,MAAM,CAAC;IACnB,SAAS,EAAE,QAAQ,GAAG,QAAQ,GAAG,SAAS,CAAC;IAC3C,UAAU,EAAE,MAAM,CAAC;IACnB,WAAW,EAAE,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,UAAU,GAAG,UAAU,CAAC;IACjE,GAAG,EAAE,MAAM,CAAC;CACb,CAAC;AAEF,qBAAa,YAAY;;gBAEX,OAAO,EAAE,eAAe;IAGpC,IAAI,OAAO,WAIV;CACF;AAED,oBAAY,OAAO,GAAG,SAAS,GAAG,SAAS,GAAG,aAAa,GAAG,YAAY,CAAC"}
1
+ {"version":3,"file":"rules.d.ts","sourceRoot":"","sources":["../../src/core/rules.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,aAAa,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAC;AAI/E,cAAM,gBAAgB;;IAIpB,QAAQ,CAAC,EAAE,MAAM,IAAI,CAAC;IACtB,GAAG,CAAC,QAAQ,EAAE,aAAa,EAAE,KAAK,CAAC,EAAE,UAAU;IAK/C,KAAK;IAKL,QAAQ;CAaT;AAED,qBAAa,SAAS;;IACpB,QAAQ,mBAAC;IACT,YAAY,SAAC;IACb,QAAQ,CAAC,EAAE,MAAM,IAAI,CAAC;gBACV,YAAY,EAAE,MAAM,EAAE,KAAK,EAAE,KAAK;IAY9C,IAAI,OAAO,WAEV;CACF;AAED,oBAAY,gBAAgB,GAAG;IAC7B,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,SAAS,CAAC,EAAE,KAAK,GAAG,QAAQ,GAAG,OAAO,CAAC;CACxC,CAAC;AAEF,qBAAa,SAAS;;IAEpB,KAAK,EAAE,KAAK,CAAC,SAAS,GAAG,aAAa,CAAC,CAAM;gBAEjC,OAAO,GAAE,gBAAqB;IAI1C,UAAU,CAAC,IAAI,EAAE,SAAS,GAAG,aAAa;IAI1C,IAAI,OAAO,WAcV;CACF;AAED,qBAAa,aAAa;IACxB,OAAO,SAAC;IACR,QAAQ,gBAAa;gBACT,OAAO,EAAE,MAAM;CAG5B;AAED,oBAAY,eAAe,GAAG;IAC5B,UAAU,EAAE,MAAM,CAAC;IACnB,SAAS,EAAE,QAAQ,GAAG,QAAQ,GAAG,SAAS,CAAC;IAC3C,UAAU,EAAE,MAAM,CAAC;IACnB,WAAW,EAAE,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,UAAU,GAAG,UAAU,CAAC;IACjE,GAAG,EAAE,MAAM,CAAC;CACb,CAAC;AAEF,qBAAa,YAAY;;gBAEX,OAAO,EAAE,eAAe;IAGpC,IAAI,OAAO,WAIV;CACF;AAED,oBAAY,OAAO,GAAG,SAAS,GAAG,SAAS,GAAG,aAAa,GAAG,YAAY,CAAC"}
package/lib/core/rules.js CHANGED
@@ -1,4 +1,3 @@
1
- "use strict";
2
1
  var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
3
2
  if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
4
3
  if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
@@ -10,14 +9,9 @@ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (
10
9
  if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
11
10
  return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
12
11
  };
13
- var __importDefault = (this && this.__importDefault) || function (mod) {
14
- return (mod && mod.__esModule) ? mod : { "default": mod };
15
- };
16
12
  var _StylePropertyMap_styleMap, _StylePropertyMap_isDirty, _StylePropertyMap_string, _StyleRule_onChange, _MediaRule_options, _MediaRule_mediaType, _FontFaceRule_options;
17
- Object.defineProperty(exports, "__esModule", { value: true });
18
- exports.FontFaceRule = exports.PlaintextRule = exports.MediaRule = exports.StyleRule = void 0;
19
- const hyphenate_style_name_1 = __importDefault(require("hyphenate-style-name"));
20
- const to_value_1 = require("./to-value");
13
+ import hyphenate from "hyphenate-style-name";
14
+ import { toValue } from "./to-value";
21
15
  class StylePropertyMap {
22
16
  constructor() {
23
17
  _StylePropertyMap_styleMap.set(this, new Map());
@@ -29,6 +23,11 @@ class StylePropertyMap {
29
23
  __classPrivateFieldSet(this, _StylePropertyMap_isDirty, true, "f");
30
24
  this.onChange?.();
31
25
  }
26
+ clear() {
27
+ __classPrivateFieldGet(this, _StylePropertyMap_styleMap, "f").clear();
28
+ __classPrivateFieldSet(this, _StylePropertyMap_isDirty, true, "f");
29
+ this.onChange?.();
30
+ }
32
31
  toString() {
33
32
  if (__classPrivateFieldGet(this, _StylePropertyMap_isDirty, "f") === false) {
34
33
  return __classPrivateFieldGet(this, _StylePropertyMap_string, "f");
@@ -37,7 +36,7 @@ class StylePropertyMap {
37
36
  for (const [property, value] of __classPrivateFieldGet(this, _StylePropertyMap_styleMap, "f")) {
38
37
  if (value === undefined)
39
38
  continue;
40
- block.push(`${(0, hyphenate_style_name_1.default)(property)}: ${(0, to_value_1.toValue)(value)}`);
39
+ block.push(`${hyphenate(property)}: ${toValue(value)}`);
41
40
  }
42
41
  __classPrivateFieldSet(this, _StylePropertyMap_string, block.join("; "), "f");
43
42
  __classPrivateFieldSet(this, _StylePropertyMap_isDirty, false, "f");
@@ -45,7 +44,7 @@ class StylePropertyMap {
45
44
  }
46
45
  }
47
46
  _StylePropertyMap_styleMap = new WeakMap(), _StylePropertyMap_isDirty = new WeakMap(), _StylePropertyMap_string = new WeakMap();
48
- class StyleRule {
47
+ export class StyleRule {
49
48
  constructor(selectorText, style) {
50
49
  _StyleRule_onChange.set(this, () => {
51
50
  this.onChange?.();
@@ -62,9 +61,8 @@ class StyleRule {
62
61
  return `${this.selectorText} { ${this.styleMap} }`;
63
62
  }
64
63
  }
65
- exports.StyleRule = StyleRule;
66
64
  _StyleRule_onChange = new WeakMap();
67
- class MediaRule {
65
+ export class MediaRule {
68
66
  constructor(options = {}) {
69
67
  _MediaRule_options.set(this, void 0);
70
68
  this.rules = [];
@@ -94,16 +92,14 @@ class MediaRule {
94
92
  return `@media ${__classPrivateFieldGet(this, _MediaRule_mediaType, "f")} ${conditionText}{\n${rules.join("\n")}\n}`;
95
93
  }
96
94
  }
97
- exports.MediaRule = MediaRule;
98
95
  _MediaRule_options = new WeakMap(), _MediaRule_mediaType = new WeakMap();
99
- class PlaintextRule {
96
+ export class PlaintextRule {
100
97
  constructor(cssText) {
101
98
  this.styleMap = new Map();
102
99
  this.cssText = cssText;
103
100
  }
104
101
  }
105
- exports.PlaintextRule = PlaintextRule;
106
- class FontFaceRule {
102
+ export class FontFaceRule {
107
103
  constructor(options) {
108
104
  _FontFaceRule_options.set(this, void 0);
109
105
  __classPrivateFieldSet(this, _FontFaceRule_options, options, "f");
@@ -113,5 +109,4 @@ class FontFaceRule {
113
109
  return `@font-face {\n font-family: ${fontFamily}; font-style: ${fontStyle}; font-weight: ${fontWeight}; font-display: ${fontDisplay}; src: ${src};\n}`;
114
110
  }
115
111
  }
116
- exports.FontFaceRule = FontFaceRule;
117
112
  _FontFaceRule_options = new WeakMap();
@@ -1,4 +1,3 @@
1
- "use strict";
2
1
  var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
3
2
  if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
4
3
  if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
@@ -11,9 +10,7 @@ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (
11
10
  return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
12
11
  };
13
12
  var _StyleElement_element;
14
- Object.defineProperty(exports, "__esModule", { value: true });
15
- exports.StyleElement = void 0;
16
- class StyleElement {
13
+ export class StyleElement {
17
14
  constructor() {
18
15
  _StyleElement_element.set(this, void 0);
19
16
  }
@@ -39,5 +36,4 @@ class StyleElement {
39
36
  }
40
37
  }
41
38
  }
42
- exports.StyleElement = StyleElement;
43
39
  _StyleElement_element = new WeakMap();
@@ -1,4 +1,3 @@
1
- "use strict";
2
1
  var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
3
2
  if (kind === "m") throw new TypeError("Private method is not writable");
4
3
  if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
@@ -11,9 +10,7 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
11
10
  return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
12
11
  };
13
12
  var _StyleSheet_cssText, _StyleSheet_element;
14
- Object.defineProperty(exports, "__esModule", { value: true });
15
- exports.StyleSheet = void 0;
16
- class StyleSheet {
13
+ export class StyleSheet {
17
14
  constructor(element) {
18
15
  _StyleSheet_cssText.set(this, "");
19
16
  _StyleSheet_element.set(this, void 0);
@@ -26,5 +23,4 @@ class StyleSheet {
26
23
  }
27
24
  }
28
25
  }
29
- exports.StyleSheet = StyleSheet;
30
26
  _StyleSheet_cssText = new WeakMap(), _StyleSheet_element = new WeakMap();
@@ -1,11 +1,8 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.toValue = void 0;
4
- const fonts_1 = require("@webstudio-is/fonts");
1
+ import { DEFAULT_FONT_FALLBACK, SYSTEM_FONTS } from "@webstudio-is/fonts";
5
2
  const defaultOptions = {
6
3
  withFallback: true,
7
4
  };
8
- const toValue = (value, options = defaultOptions) => {
5
+ export const toValue = (value, options = defaultOptions) => {
9
6
  if (value === undefined)
10
7
  return "";
11
8
  if (value.type === "unit") {
@@ -16,20 +13,19 @@ const toValue = (value, options = defaultOptions) => {
16
13
  return value.value[0];
17
14
  }
18
15
  const family = value.value[0];
19
- const fallbacks = fonts_1.SYSTEM_FONTS.get(family);
16
+ const fallbacks = SYSTEM_FONTS.get(family);
20
17
  if (Array.isArray(fallbacks)) {
21
18
  return [...value.value, ...fallbacks].join(", ");
22
19
  }
23
- return [...value.value, fonts_1.DEFAULT_FONT_FALLBACK].join(", ");
20
+ return [...value.value, DEFAULT_FONT_FALLBACK].join(", ");
24
21
  }
25
22
  if (value.type === "var") {
26
23
  const fallbacks = [];
27
24
  for (const fallback of value.fallbacks) {
28
- fallbacks.push((0, exports.toValue)(fallback, options));
25
+ fallbacks.push(toValue(fallback, options));
29
26
  }
30
27
  const fallbacksString = fallbacks.length > 0 ? `, ${fallbacks.join(", ")}` : "";
31
28
  return `var(--${value.value}${fallbacksString})`;
32
29
  }
33
30
  return value.value;
34
31
  };
35
- exports.toValue = toValue;
@@ -1,29 +1,27 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const to_value_1 = require("./to-value");
1
+ import { toValue } from "./to-value";
4
2
  describe("Convert WS CSS Values to native CSS strings", () => {
5
3
  test("keyword", () => {
6
- const value = (0, to_value_1.toValue)({ type: "keyword", value: "red" });
4
+ const value = toValue({ type: "keyword", value: "red" });
7
5
  expect(value).toBe("red");
8
6
  });
9
7
  test("unit", () => {
10
- const value = (0, to_value_1.toValue)({ type: "unit", value: 10, unit: "px" });
8
+ const value = toValue({ type: "unit", value: 10, unit: "px" });
11
9
  expect(value).toBe("10px");
12
10
  });
13
11
  test("invalid", () => {
14
- const value = (0, to_value_1.toValue)({ type: "invalid", value: "bad" });
12
+ const value = toValue({ type: "invalid", value: "bad" });
15
13
  expect(value).toBe("bad");
16
14
  });
17
15
  test("unset", () => {
18
- const value = (0, to_value_1.toValue)({ type: "unset", value: "" });
16
+ const value = toValue({ type: "unset", value: "" });
19
17
  expect(value).toBe("");
20
18
  });
21
19
  test("var", () => {
22
- const value = (0, to_value_1.toValue)({ type: "var", value: "namespace", fallbacks: [] });
20
+ const value = toValue({ type: "var", value: "namespace", fallbacks: [] });
23
21
  expect(value).toBe("var(--namespace)");
24
22
  });
25
23
  test("var with fallbacks", () => {
26
- const value = (0, to_value_1.toValue)({
24
+ const value = toValue({
27
25
  type: "var",
28
26
  value: "namespace",
29
27
  fallbacks: [
@@ -41,14 +39,14 @@ describe("Convert WS CSS Values to native CSS strings", () => {
41
39
  expect(value).toBe("var(--namespace, normal, 10px)");
42
40
  });
43
41
  test("fontFamily", () => {
44
- const value = (0, to_value_1.toValue)({
42
+ const value = toValue({
45
43
  type: "fontFamily",
46
44
  value: ["Courier New"],
47
45
  });
48
46
  expect(value).toBe("Courier New, monospace");
49
47
  });
50
48
  test("withFallback=false", () => {
51
- const value = (0, to_value_1.toValue)({
49
+ const value = toValue({
52
50
  type: "fontFamily",
53
51
  value: ["Courier New"],
54
52
  }, { withFallback: false });
package/lib/index.js CHANGED
@@ -1,17 +1 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
- };
16
- Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./core"), exports);
1
+ export * from "./core";
package/package.json CHANGED
@@ -1,18 +1,20 @@
1
1
  {
2
2
  "name": "@webstudio-is/css-engine",
3
- "version": "0.1.0",
3
+ "version": "0.3.0",
4
4
  "description": "CSS Renderer for Webstudio",
5
5
  "author": "Webstudio <github@webstudio.is>",
6
6
  "homepage": "https://webstudio.is",
7
+ "type": "module",
7
8
  "scripts": {
8
9
  "typecheck": "tsc --noEmit",
9
10
  "checks": "yarn typecheck && yarn lint && yarn test",
10
11
  "dev": "tsup --watch",
11
12
  "build": "rm -fr lib tsconfig.tsbuildinfo && tsc",
12
- "test": "jest",
13
+ "test": "NODE_OPTIONS=--experimental-vm-modules jest",
13
14
  "lint": "eslint ./src --ext .ts,.tsx --max-warnings 0",
14
15
  "storybook:run": "start-storybook -p 6006",
15
- "storybook:build": "build-storybook"
16
+ "storybook:build": "build-storybook",
17
+ "publish-to-npm": "bash ../../bin/publish-to-npm.sh"
16
18
  },
17
19
  "dependencies": {
18
20
  "hyphenate-style-name": "^1.0.4",
@@ -37,7 +39,7 @@
37
39
  "@types/react": "^17.0.24",
38
40
  "@types/react-dom": "^17.0.9"
39
41
  },
40
- "main": "lib/index.js",
42
+ "exports": "./lib/index.js",
41
43
  "types": "lib/index.d.ts",
42
44
  "files": [
43
45
  "lib/*",
@@ -51,7 +53,7 @@
51
53
  "entry": [
52
54
  "src/index.ts"
53
55
  ],
54
- "format": "cjs",
56
+ "format": "esm",
55
57
  "outDir": "lib"
56
58
  }
57
59
  }