@mochi-css/vanilla 2.0.1 → 3.0.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.
package/dist/index.mjs CHANGED
@@ -1,5 +1,4 @@
1
1
  import clsx from "clsx";
2
- import { createElement } from "react";
3
2
 
4
3
  //#region src/token.ts
5
4
  /**
@@ -1131,6 +1130,12 @@ const knownPropertyNames = new Set([
1131
1130
  //#endregion
1132
1131
  //#region src/props.ts
1133
1132
  /**
1133
+ * Converts a kebab-case string to camelCase.
1134
+ */
1135
+ function kebabToCamel(str) {
1136
+ return str.replace(/-([a-z])/g, (_, c) => c.toUpperCase());
1137
+ }
1138
+ /**
1134
1139
  * Converts a camelCase string to kebab-case.
1135
1140
  */
1136
1141
  function camelToKebab(str) {
@@ -1471,12 +1476,13 @@ var CssObjectBlock = class {
1471
1476
  subBlocks = [];
1472
1477
  /**
1473
1478
  * Creates a new CSS block from style properties.
1474
- * Generates a unique class name based on the content hash.
1479
+ * Generates a unique class name based on the content hash, or uses the provided class name.
1475
1480
  * @param styles - The style properties to compile
1481
+ * @param className - Optional stable class name; if omitted, a content-hash-based name is generated
1476
1482
  */
1477
- constructor(styles) {
1483
+ constructor(styles, className) {
1478
1484
  const blocks = CssObjectSubBlock.fromProps(styles);
1479
- this.className = "c" + shortHash(blocks.map((b) => b.hash).join("+"));
1485
+ this.className = className ?? "c" + shortHash(blocks.map((b) => b.hash).join("+"));
1480
1486
  this.subBlocks = blocks;
1481
1487
  }
1482
1488
  /**
@@ -1524,9 +1530,14 @@ var CSSObject = class {
1524
1530
  /**
1525
1531
  * Creates a new CSSObject from style props.
1526
1532
  * Compiles main styles and all variant options into CSS blocks.
1533
+ * @param props - Base style props plus variant definitions
1534
+ * @param props.variants - Named variant groups, each mapping variant values to style props
1535
+ * @param props.defaultVariants - Default value for each variant when none is provided at runtime
1536
+ * @param props.compoundVariants - Style props applied when a specific combination of variants is active
1537
+ * @param className - Optional stable class name for the main block
1527
1538
  */
1528
- constructor({ variants, defaultVariants, compoundVariants,...props }) {
1529
- this.mainBlock = new CssObjectBlock(props);
1539
+ constructor({ variants, defaultVariants, compoundVariants,...props }, className) {
1540
+ this.mainBlock = new CssObjectBlock(props, className);
1530
1541
  this.variantBlocks = {};
1531
1542
  this.variantDefaults = defaultVariants ?? {};
1532
1543
  this.compoundVariants = [];
@@ -1568,6 +1579,10 @@ var CSSObject = class {
1568
1579
 
1569
1580
  //#endregion
1570
1581
  //#region src/css.ts
1582
+ const MOCHI_CSS_TYPEOF = Symbol.for("mochi-css.MochiCSS");
1583
+ function isMochiCSS(value) {
1584
+ return typeof value === "object" && value !== null && value["$$typeof"] === MOCHI_CSS_TYPEOF;
1585
+ }
1571
1586
  /**
1572
1587
  * Runtime representation of a CSS style definition with variant support.
1573
1588
  * Holds generated class names and provides methods to compute the final
@@ -1583,6 +1598,7 @@ var CSSObject = class {
1583
1598
  * styles.variant({ size: 'large' }) // Returns combined class names
1584
1599
  */
1585
1600
  var MochiCSS = class MochiCSS {
1601
+ $$typeof = MOCHI_CSS_TYPEOF;
1586
1602
  /**
1587
1603
  * Creates a new MochiCSS instance.
1588
1604
  * @param classNames - Base class names to always include
@@ -1616,6 +1632,16 @@ var MochiCSS = class MochiCSS {
1616
1632
  }));
1617
1633
  }
1618
1634
  /**
1635
+ * Returns the CSS selector for this style (e.g. `.abc123`).
1636
+ * Useful for targeting this component from another style.
1637
+ */
1638
+ get selector() {
1639
+ return this.classNames.map((n) => `.${n}`).join();
1640
+ }
1641
+ toString() {
1642
+ return this.selector;
1643
+ }
1644
+ /**
1619
1645
  * Creates a MochiCSS instance from a CSSObject.
1620
1646
  * Extracts class names from the compiled CSS blocks.
1621
1647
  * @template V - The variant definitions type
@@ -1674,51 +1700,18 @@ function mergeMochiCss(instances) {
1674
1700
  function css(...props) {
1675
1701
  const cssToMerge = [];
1676
1702
  for (const p of props) {
1677
- if (p == null || typeof p !== "object") continue;
1703
+ if (p == null) continue;
1704
+ if (typeof p === "string") {
1705
+ cssToMerge.push(new MochiCSS([p], {}, {}));
1706
+ continue;
1707
+ }
1708
+ if (typeof p !== "object") continue;
1678
1709
  if (p instanceof MochiCSS) cssToMerge.push(p);
1679
1710
  else cssToMerge.push(MochiCSS.from(new CSSObject(p)));
1680
1711
  }
1681
1712
  return mergeMochiCss(cssToMerge);
1682
1713
  }
1683
1714
 
1684
- //#endregion
1685
- //#region src/styled.ts
1686
- /**
1687
- * Creates a styled React component with CSS-in-JS support and variant props.
1688
- * Similar to styled-components or Stitches, but with zero runtime overhead.
1689
- *
1690
- * @template T - The base element type or component type
1691
- * @template V - The variant definitions tuple type
1692
- * @param target - The HTML element tag name or React component to style
1693
- * @param props - One or more style objects with optional variants
1694
- * @returns A React functional component with merged props and variant support
1695
- *
1696
- * @example
1697
- * const Button = styled('button', {
1698
- * padding: 8,
1699
- * borderRadius: 4,
1700
- * variants: {
1701
- * size: {
1702
- * small: { padding: 4 },
1703
- * large: { padding: 16 }
1704
- * },
1705
- * variant: {
1706
- * primary: { backgroundColor: 'blue' },
1707
- * secondary: { backgroundColor: 'gray' }
1708
- * }
1709
- * }
1710
- * })
1711
- *
1712
- * // Usage: <Button size="large" variant="primary">Click me</Button>
1713
- */
1714
- function styled(target, ...props) {
1715
- const styles = css(...props);
1716
- return ({ className,...p }) => createElement(target, {
1717
- className: clsx(styles.variant(p), className),
1718
- ...p
1719
- });
1720
- }
1721
-
1722
1715
  //#endregion
1723
1716
  //#region src/keyframesObject.ts
1724
1717
  var KeyframesObject = class KeyframesObject {
@@ -1865,4 +1858,4 @@ supportsFn.or = function(...conditions) {
1865
1858
  const supports = supportsFn;
1866
1859
 
1867
1860
  //#endregion
1868
- export { CSSObject, CssObjectBlock, CssObjectSubBlock, GlobalCssObject, KeyframesObject, MochiCSS, MochiKeyframes, MochiSelector, Token, container, createToken, css, cssFromProps, globalCss, isAtRuleKey, keyframes, media, mergeMochiCss, styled, supports };
1861
+ export { CSSObject, CssObjectBlock, CssObjectSubBlock, GlobalCssObject, KeyframesObject, MochiCSS, MochiKeyframes, MochiSelector, Token, camelToKebab, container, createToken, css, cssFromProps, globalCss, isAtRuleKey, isMochiCSS, kebabToCamel, keyframes, media, mergeMochiCss, numberToBase62, shortHash, supports };