@mochi-css/vanilla 2.1.0 → 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/README.md +21 -11
- package/dist/index.d.mts +60 -46
- package/dist/index.d.ts +60 -46
- package/dist/index.js +41 -51
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +38 -50
- package/dist/index.mjs.map +1 -1
- package/package.json +3 -12
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
|
|
@@ -1582,10 +1597,6 @@ var CSSObject = class {
|
|
|
1582
1597
|
* }))
|
|
1583
1598
|
* styles.variant({ size: 'large' }) // Returns combined class names
|
|
1584
1599
|
*/
|
|
1585
|
-
const MOCHI_CSS_TYPEOF = Symbol.for("mochi-css.MochiCSS");
|
|
1586
|
-
function isMochiCSS(value) {
|
|
1587
|
-
return typeof value === "object" && value !== null && value["$$typeof"] === MOCHI_CSS_TYPEOF;
|
|
1588
|
-
}
|
|
1589
1600
|
var MochiCSS = class MochiCSS {
|
|
1590
1601
|
$$typeof = MOCHI_CSS_TYPEOF;
|
|
1591
1602
|
/**
|
|
@@ -1621,6 +1632,16 @@ var MochiCSS = class MochiCSS {
|
|
|
1621
1632
|
}));
|
|
1622
1633
|
}
|
|
1623
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
|
+
/**
|
|
1624
1645
|
* Creates a MochiCSS instance from a CSSObject.
|
|
1625
1646
|
* Extracts class names from the compiled CSS blocks.
|
|
1626
1647
|
* @template V - The variant definitions type
|
|
@@ -1679,51 +1700,18 @@ function mergeMochiCss(instances) {
|
|
|
1679
1700
|
function css(...props) {
|
|
1680
1701
|
const cssToMerge = [];
|
|
1681
1702
|
for (const p of props) {
|
|
1682
|
-
if (p == null
|
|
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;
|
|
1683
1709
|
if (p instanceof MochiCSS) cssToMerge.push(p);
|
|
1684
1710
|
else cssToMerge.push(MochiCSS.from(new CSSObject(p)));
|
|
1685
1711
|
}
|
|
1686
1712
|
return mergeMochiCss(cssToMerge);
|
|
1687
1713
|
}
|
|
1688
1714
|
|
|
1689
|
-
//#endregion
|
|
1690
|
-
//#region src/styled.ts
|
|
1691
|
-
/**
|
|
1692
|
-
* Creates a styled React component with CSS-in-JS support and variant props.
|
|
1693
|
-
* Similar to styled-components or Stitches, but with zero runtime overhead.
|
|
1694
|
-
*
|
|
1695
|
-
* @template T - The base element type or component type
|
|
1696
|
-
* @template V - The variant definitions tuple type
|
|
1697
|
-
* @param target - The HTML element tag name or React component to style
|
|
1698
|
-
* @param props - One or more style objects with optional variants
|
|
1699
|
-
* @returns A React functional component with merged props and variant support
|
|
1700
|
-
*
|
|
1701
|
-
* @example
|
|
1702
|
-
* const Button = styled('button', {
|
|
1703
|
-
* padding: 8,
|
|
1704
|
-
* borderRadius: 4,
|
|
1705
|
-
* variants: {
|
|
1706
|
-
* size: {
|
|
1707
|
-
* small: { padding: 4 },
|
|
1708
|
-
* large: { padding: 16 }
|
|
1709
|
-
* },
|
|
1710
|
-
* variant: {
|
|
1711
|
-
* primary: { backgroundColor: 'blue' },
|
|
1712
|
-
* secondary: { backgroundColor: 'gray' }
|
|
1713
|
-
* }
|
|
1714
|
-
* }
|
|
1715
|
-
* })
|
|
1716
|
-
*
|
|
1717
|
-
* // Usage: <Button size="large" variant="primary">Click me</Button>
|
|
1718
|
-
*/
|
|
1719
|
-
function styled(target, ...props) {
|
|
1720
|
-
const styles = css(...props);
|
|
1721
|
-
return ({ className,...p }) => createElement(target, {
|
|
1722
|
-
className: clsx(styles.variant(p), className),
|
|
1723
|
-
...p
|
|
1724
|
-
});
|
|
1725
|
-
}
|
|
1726
|
-
|
|
1727
1715
|
//#endregion
|
|
1728
1716
|
//#region src/keyframesObject.ts
|
|
1729
1717
|
var KeyframesObject = class KeyframesObject {
|
|
@@ -1870,4 +1858,4 @@ supportsFn.or = function(...conditions) {
|
|
|
1870
1858
|
const supports = supportsFn;
|
|
1871
1859
|
|
|
1872
1860
|
//#endregion
|
|
1873
|
-
export { CSSObject, CssObjectBlock, CssObjectSubBlock, GlobalCssObject, KeyframesObject, MochiCSS, MochiKeyframes, MochiSelector, Token, container, createToken, css, cssFromProps, globalCss, isAtRuleKey, isMochiCSS, keyframes, media, mergeMochiCss,
|
|
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 };
|