@pairdraw/dom-css 0.19.2 → 0.20.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.
@@ -233,14 +233,15 @@ function gradientFillsToCSS(fills, width, height) {
233
233
  * border insets the box its absolutely positioned children are placed in, where a stroke does not.
234
234
  */
235
235
  function borderWidths(node) {
236
- if (!strokeColorToCSS(node.strokes.at(0))) return {
236
+ const stroke = node.strokes.find((item) => item.visible);
237
+ if (!strokeColorToCSS(stroke)) return {
237
238
  top: 0,
238
239
  right: 0,
239
240
  bottom: 0,
240
241
  left: 0
241
242
  };
242
243
  if (!node.independentStrokeWeights) {
243
- const weight = node.strokes[0]?.weight ?? 0;
244
+ const weight = stroke?.weight ?? 0;
244
245
  return {
245
246
  top: weight,
246
247
  right: weight,
@@ -1559,7 +1560,7 @@ function addCornerRadii(style, node, css) {
1559
1560
  if (node.cornerRadius > 0) style["border-radius"] = css("cornerRadius", px(node.cornerRadius));
1560
1561
  }
1561
1562
  function addStroke(style, node, css) {
1562
- const stroke = node.strokes.at(0);
1563
+ const stroke = node.strokes.find((item) => item.visible);
1563
1564
  const literalColor = strokeColorToCSS(stroke);
1564
1565
  if (!literalColor || !stroke) return;
1565
1566
  const color = css("strokes/0/color", literalColor);
@@ -1576,6 +1577,33 @@ function addStroke(style, node, css) {
1576
1577
  style["border-bottom-width"] = css("borderBottomWeight", px(node.borderBottomWeight));
1577
1578
  style["border-left-width"] = css("borderLeftWeight", px(node.borderLeftWeight));
1578
1579
  }
1580
+ /**
1581
+ * CSS gives an element one border, so the visible strokes after the first become rings in
1582
+ * `box-shadow`, in layer order. An outside ring grows from the border's outer edge, an inside one
1583
+ * from its inner edge, and a centred stroke gets a ring on each side of the border. Rings cannot
1584
+ * dash or take per-side weights, and outer and inner rings paint in separate CSS layers, so a
1585
+ * stroke order that alternates them is approximated.
1586
+ */
1587
+ function extraStrokeShadows(strokes, first) {
1588
+ const outer = [];
1589
+ const inner = [];
1590
+ let outside = 0;
1591
+ let inside = first.weight;
1592
+ for (const stroke of strokes) {
1593
+ if (!stroke.visible) continue;
1594
+ const color = strokeColorToCSS(stroke);
1595
+ if (!color) continue;
1596
+ if (stroke.align !== "OUTSIDE") {
1597
+ inside += stroke.align === "CENTER" ? stroke.weight / 2 : stroke.weight;
1598
+ inner.push(`inset 0 0 0 ${px(inside)} ${color}`);
1599
+ }
1600
+ if (stroke.align !== "INSIDE") {
1601
+ outside += stroke.align === "CENTER" ? stroke.weight / 2 : stroke.weight;
1602
+ outer.push(`0 0 0 ${px(outside)} ${color}`);
1603
+ }
1604
+ }
1605
+ return [...outer, ...inner];
1606
+ }
1579
1607
  const PADDING_FIELDS = [
1580
1608
  "paddingTop",
1581
1609
  "paddingRight",
@@ -1721,6 +1749,11 @@ function styleFromSceneNode(node, parent, css) {
1721
1749
  addImageStyle(style, node);
1722
1750
  addStroke(style, node, css);
1723
1751
  Object.assign(style, effectsToCSS(node.effects));
1752
+ const visibleStrokes = node.strokes.filter((stroke) => stroke.visible);
1753
+ if (visibleStrokes.length > 1) {
1754
+ const rings = extraStrokeShadows(visibleStrokes.slice(1), visibleStrokes[0]);
1755
+ if (rings.length > 0) style["box-shadow"] = [rings.join(", "), style["box-shadow"]].filter((shadow) => Boolean(shadow)).join(", ");
1756
+ }
1724
1757
  if (node.opacity < 1) style.opacity = css("opacity", String(node.opacity));
1725
1758
  const blend = CSS_BLEND_MODES[node.blendMode];
1726
1759
  if (blend) style["mix-blend-mode"] = blend;
@@ -1827,6 +1860,53 @@ function vectorDesignNode(context, node, attrs, style) {
1827
1860
  sourceSceneNode: node
1828
1861
  };
1829
1862
  }
1863
+ /** The outline a vector mask clips siblings to, in the sibling's own box; null where CSS cannot. */
1864
+ function vectorMaskClip(mask, sibling) {
1865
+ if (mask.maskType !== "VECTOR" || mask.childIds.length > 0 || mask.rotation !== 0) return null;
1866
+ const left = mask.x - sibling.x;
1867
+ const top = mask.y - sibling.y;
1868
+ if (mask.type === "RECTANGLE") {
1869
+ const right = left + mask.width;
1870
+ const bottom = top + mask.height;
1871
+ return `polygon(${px(left)} ${px(top)}, ${px(right)} ${px(top)}, ${px(right)} ${px(bottom)}, ${px(left)} ${px(bottom)})`;
1872
+ }
1873
+ if (mask.type === "ELLIPSE") {
1874
+ const rx = mask.width / 2;
1875
+ const ry = mask.height / 2;
1876
+ return `ellipse(${px(rx)} ${px(ry)} at ${px(left + rx)} ${px(top + ry)})`;
1877
+ }
1878
+ return null;
1879
+ }
1880
+ /**
1881
+ * Projects a parent's children as the canvas draws them: a mask layer is never a layer of its
1882
+ * own, and one vector mask clips the siblings after it. Alpha and luminance masks have no
1883
+ * faithful CSS form over several elements, and two masks combine, so those stay unclipped.
1884
+ */
1885
+ function projectChildren(context, parent, modes, root) {
1886
+ const projected = [];
1887
+ let masks = [];
1888
+ let afterMask = false;
1889
+ for (const child of nodeChildren(context.graph, parent)) {
1890
+ if (child.visible && child.isMask) {
1891
+ masks = afterMask ? [...masks, child] : [child];
1892
+ afterMask = true;
1893
+ continue;
1894
+ }
1895
+ afterMask = false;
1896
+ const node = sceneNodeToDesignNode(context, child, modes, root);
1897
+ if (!node) continue;
1898
+ const mask = masks.length === 1 ? masks[0] : null;
1899
+ if (mask && node.type === "element") {
1900
+ const clip = vectorMaskClip(mask, child);
1901
+ if (clip) node.inlineStyle = {
1902
+ ...node.inlineStyle,
1903
+ "clip-path": clip
1904
+ };
1905
+ }
1906
+ projected.push(node);
1907
+ }
1908
+ return projected;
1909
+ }
1830
1910
  function sceneNodeToDesignNode(context, node, inherited, root) {
1831
1911
  if (!node.visible && !context.shown?.has(node.id) || node.internalOnly) return null;
1832
1912
  const { graph, tokens } = context;
@@ -1854,7 +1934,7 @@ function sceneNodeToDesignNode(context, node, inherited, root) {
1854
1934
  const vector = vectorDesignNode(context, node, attrs, styleFromSceneNode(node, parent, css));
1855
1935
  if (vector) return vector;
1856
1936
  }
1857
- const children = nodeChildren(graph, node).map((child) => sceneNodeToDesignNode(context, child, modes, false)).filter((child) => child !== null);
1937
+ const children = projectChildren(context, node, modes, false);
1858
1938
  if (node.type === "CANVAS") return {
1859
1939
  type: "element",
1860
1940
  tagName: "main",
@@ -1891,7 +1971,7 @@ function sceneGraphToDesignDocument(graph, options = {}) {
1891
1971
  const root = graph.getNode(options.rootId ?? graph.rootId);
1892
1972
  const context = projectionContext(graph, options);
1893
1973
  const modes = context.tokens?.defaultModes() ?? /* @__PURE__ */ new Map();
1894
- const children = root ? nodeChildren(graph, root).map((child) => sceneNodeToDesignNode(context, child, modes, true)).filter((child) => child !== null) : [];
1974
+ const children = root ? projectChildren(context, root, modes, true) : [];
1895
1975
  return {
1896
1976
  type: "document",
1897
1977
  sourceGraph: graph,