@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/README.md
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
# 🧁 Mochi-CSS/vanilla
|
|
2
2
|
|
|
3
3
|
This package is part of the [Mochi-CSS project](https://github.com/Niikelion/mochi-css).
|
|
4
|
-
It provides type-safe CSS-in-JS styling functions with static extraction support,
|
|
4
|
+
It provides type-safe CSS-in-JS styling functions with static extraction support,
|
|
5
|
+
allowing you to write styles in TypeScript that get extracted to plain CSS at build time.
|
|
5
6
|
|
|
6
7
|
## Installation
|
|
7
8
|
|
|
@@ -45,7 +46,8 @@ const buttonStyles = css(textStyles, {
|
|
|
45
46
|
|
|
46
47
|
### `globalCss(styles)`
|
|
47
48
|
|
|
48
|
-
`globalCss` injects styles into the global scope
|
|
49
|
+
`globalCss` injects styles into the global scope - they are not scoped to any class and apply to all matching elements.
|
|
50
|
+
Use it for resets, base typography, or any styles that must target plain HTML elements.
|
|
49
51
|
|
|
50
52
|
```ts
|
|
51
53
|
import { globalCss } from "@mochi-css/vanilla"
|
|
@@ -59,10 +61,11 @@ globalCss({
|
|
|
59
61
|
|
|
60
62
|
### `styled(component, ...styles)`
|
|
61
63
|
|
|
62
|
-
`styled` creates a styled component by combining a base element or component with style definitions.
|
|
64
|
+
`styled` creates a styled component by combining a base element or component with style definitions.
|
|
65
|
+
It automatically applies the generated class names and forwards variant props.
|
|
63
66
|
|
|
64
67
|
```tsx
|
|
65
|
-
import {styled} from "@mochi-css/
|
|
68
|
+
import { styled } from "@mochi-css/react"
|
|
66
69
|
|
|
67
70
|
const Button = styled("button", {
|
|
68
71
|
borderRadius: 10,
|
|
@@ -127,7 +130,8 @@ A style definition is either a bundle of styles returned by `css`, or an object
|
|
|
127
130
|
|
|
128
131
|
## Nested Selectors
|
|
129
132
|
|
|
130
|
-
Mochi-CSS supports nested selectors, allowing you to define styles for child elements, pseudo-classes,
|
|
133
|
+
Mochi-CSS supports nested selectors, allowing you to define styles for child elements, pseudo-classes,
|
|
134
|
+
and pseudo-elements directly within your style definitions.
|
|
131
135
|
|
|
132
136
|
The `&` character represents the parent selector and must be included in every nested selector:
|
|
133
137
|
|
|
@@ -400,11 +404,14 @@ const redButtonStyle = css(baseButtonStyle, {
|
|
|
400
404
|
})
|
|
401
405
|
```
|
|
402
406
|
|
|
403
|
-
This works, but requires you to either manually select which style to apply in your component logic,
|
|
404
|
-
|
|
407
|
+
This works, but requires you to either manually select which style to apply in your component logic,
|
|
408
|
+
or create separate components for each variant.
|
|
409
|
+
Mochi-CSS allows you to define variants directly in your style definition
|
|
410
|
+
and automatically generates the corresponding props for your component.
|
|
405
411
|
|
|
406
412
|
```tsx
|
|
407
|
-
import {
|
|
413
|
+
import { css } from "@mochi-css/vanilla"
|
|
414
|
+
import { styled } from "@mochi-css/react"
|
|
408
415
|
|
|
409
416
|
const buttonStyle = css({
|
|
410
417
|
border: "2px solid black",
|
|
@@ -433,13 +440,16 @@ const SomeComponent = () => <div>
|
|
|
433
440
|
</div>
|
|
434
441
|
```
|
|
435
442
|
|
|
436
|
-
`defaultVariants` is optional,
|
|
443
|
+
`defaultVariants` is optional,
|
|
444
|
+
but specifying defaults for all variants ensures predictable styling when variant props are omitted.
|
|
437
445
|
|
|
438
446
|
## Tokens
|
|
439
447
|
|
|
440
|
-
Mochi-CSS provides typed wrappers around CSS variables to help with type safety.
|
|
448
|
+
Mochi-CSS provides typed wrappers around CSS variables to help with type safety.
|
|
449
|
+
Tokens ensure that only valid values are assigned to your CSS variables.
|
|
441
450
|
|
|
442
|
-
Create tokens using the `createToken<T>(name)` function,
|
|
451
|
+
Create tokens using the `createToken<T>(name)` function,
|
|
452
|
+
where `T` is a CSS value type like `CssColorLike`, `CssLengthLike`, or `string`:
|
|
443
453
|
|
|
444
454
|
```ts
|
|
445
455
|
import {createToken, css, CssColorLike} from "@mochi-css/vanilla"
|
package/dist/index.d.mts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { ComponentProps, ComponentType, FC, HTMLElementType } from "react";
|
|
2
1
|
import { ObsoleteProperties, Properties } from "csstype";
|
|
3
2
|
|
|
4
3
|
//#region src/values/index.d.ts
|
|
@@ -450,7 +449,11 @@ type PropsWithUnit = PropertyWithUnit & keyof Props;
|
|
|
450
449
|
/**
|
|
451
450
|
* Converts a kebab-case string to camelCase.
|
|
452
451
|
*/
|
|
453
|
-
|
|
452
|
+
declare function kebabToCamel(str: string): string;
|
|
453
|
+
/**
|
|
454
|
+
* Converts a camelCase string to kebab-case.
|
|
455
|
+
*/
|
|
456
|
+
declare function camelToKebab(str: string): string;
|
|
454
457
|
/**
|
|
455
458
|
* Checks if a key represents a CSS at-rule (media, container, supports, layer).
|
|
456
459
|
*/
|
|
@@ -542,10 +545,11 @@ declare class CssObjectBlock {
|
|
|
542
545
|
readonly subBlocks: CssObjectSubBlock[];
|
|
543
546
|
/**
|
|
544
547
|
* Creates a new CSS block from style properties.
|
|
545
|
-
* Generates a unique class name based on the content hash.
|
|
548
|
+
* Generates a unique class name based on the content hash, or uses the provided class name.
|
|
546
549
|
* @param styles - The style properties to compile
|
|
550
|
+
* @param className - Optional stable class name; if omitted, a content-hash-based name is generated
|
|
547
551
|
*/
|
|
548
|
-
constructor(styles: StyleProps);
|
|
552
|
+
constructor(styles: StyleProps, className?: string);
|
|
549
553
|
/**
|
|
550
554
|
* Gets the CSS class selector for this block.
|
|
551
555
|
*/
|
|
@@ -632,13 +636,18 @@ declare class CSSObject<V extends AllVariants = DefaultVariants> {
|
|
|
632
636
|
/**
|
|
633
637
|
* Creates a new CSSObject from style props.
|
|
634
638
|
* Compiles main styles and all variant options into CSS blocks.
|
|
639
|
+
* @param props - Base style props plus variant definitions
|
|
640
|
+
* @param props.variants - Named variant groups, each mapping variant values to style props
|
|
641
|
+
* @param props.defaultVariants - Default value for each variant when none is provided at runtime
|
|
642
|
+
* @param props.compoundVariants - Style props applied when a specific combination of variants is active
|
|
643
|
+
* @param className - Optional stable class name for the main block
|
|
635
644
|
*/
|
|
636
645
|
constructor({
|
|
637
646
|
variants,
|
|
638
647
|
defaultVariants,
|
|
639
648
|
compoundVariants,
|
|
640
649
|
...props
|
|
641
|
-
}: MochiCSSProps<V
|
|
650
|
+
}: MochiCSSProps<V>, className?: string);
|
|
642
651
|
/**
|
|
643
652
|
* Serializes the entire CSS object to a CSS string.
|
|
644
653
|
* Outputs main block first, then all variant blocks in sorted order.
|
|
@@ -649,6 +658,20 @@ declare class CSSObject<V extends AllVariants = DefaultVariants> {
|
|
|
649
658
|
//#endregion
|
|
650
659
|
//#region src/css.d.ts
|
|
651
660
|
declare function isMochiCSS(value: unknown): value is MochiCSS;
|
|
661
|
+
/**
|
|
662
|
+
* Runtime representation of a CSS style definition with variant support.
|
|
663
|
+
* Holds generated class names and provides methods to compute the final
|
|
664
|
+
* className string based on selected variants.
|
|
665
|
+
*
|
|
666
|
+
* @template V - The variant definitions type mapping variant names to their options
|
|
667
|
+
*
|
|
668
|
+
* @example
|
|
669
|
+
* const styles = MochiCSS.from(new CSSObject({
|
|
670
|
+
* color: 'blue',
|
|
671
|
+
* variants: { size: { small: { fontSize: 12 }, large: { fontSize: 18 } } }
|
|
672
|
+
* }))
|
|
673
|
+
* styles.variant({ size: 'large' }) // Returns combined class names
|
|
674
|
+
*/
|
|
652
675
|
declare class MochiCSS<V extends AllVariants = DefaultVariants> {
|
|
653
676
|
readonly classNames: string[];
|
|
654
677
|
readonly variantClassNames: { [K in keyof V]: { [P in keyof V[K]]: string } };
|
|
@@ -669,6 +692,12 @@ declare class MochiCSS<V extends AllVariants = DefaultVariants> {
|
|
|
669
692
|
* @returns Combined className string for use in components
|
|
670
693
|
*/
|
|
671
694
|
variant(props: Partial<RefineVariants<V>>): string;
|
|
695
|
+
/**
|
|
696
|
+
* Returns the CSS selector for this style (e.g. `.abc123`).
|
|
697
|
+
* Useful for targeting this component from another style.
|
|
698
|
+
*/
|
|
699
|
+
get selector(): string;
|
|
700
|
+
toString(): string;
|
|
672
701
|
/**
|
|
673
702
|
* Creates a MochiCSS instance from a CSSObject.
|
|
674
703
|
* Extracts class names from the compiled CSS blocks.
|
|
@@ -685,46 +714,7 @@ declare class MochiCSS<V extends AllVariants = DefaultVariants> {
|
|
|
685
714
|
* @returns A new MochiCSS instance with all styles combined
|
|
686
715
|
*/
|
|
687
716
|
declare function mergeMochiCss<V extends AllVariants[]>(instances: MochiCSS<AllVariants>[]): MochiCSS<MergeCSSVariants<V>>;
|
|
688
|
-
declare function css<V extends AllVariants[]>(...props: { [K in keyof V]: MochiCSSProps<V[K]> | MochiCSS }): MochiCSS<MergeCSSVariants<V>>;
|
|
689
|
-
//#endregion
|
|
690
|
-
//#region src/styled.d.ts
|
|
691
|
-
/** Props added by MochiCSS to styled components */
|
|
692
|
-
type MochiProps<V extends AllVariants[]> = {
|
|
693
|
-
className?: string;
|
|
694
|
-
} & Partial<RefineVariants<MergeCSSVariants<V>>>;
|
|
695
|
-
/** Minimal interface for components that accept className */
|
|
696
|
-
type Cls = {
|
|
697
|
-
className?: string;
|
|
698
|
-
};
|
|
699
|
-
/**
|
|
700
|
-
* Creates a styled React component with CSS-in-JS support and variant props.
|
|
701
|
-
* Similar to styled-components or Stitches, but with zero runtime overhead.
|
|
702
|
-
*
|
|
703
|
-
* @template T - The base element type or component type
|
|
704
|
-
* @template V - The variant definitions tuple type
|
|
705
|
-
* @param target - The HTML element tag name or React component to style
|
|
706
|
-
* @param props - One or more style objects with optional variants
|
|
707
|
-
* @returns A React functional component with merged props and variant support
|
|
708
|
-
*
|
|
709
|
-
* @example
|
|
710
|
-
* const Button = styled('button', {
|
|
711
|
-
* padding: 8,
|
|
712
|
-
* borderRadius: 4,
|
|
713
|
-
* variants: {
|
|
714
|
-
* size: {
|
|
715
|
-
* small: { padding: 4 },
|
|
716
|
-
* large: { padding: 16 }
|
|
717
|
-
* },
|
|
718
|
-
* variant: {
|
|
719
|
-
* primary: { backgroundColor: 'blue' },
|
|
720
|
-
* secondary: { backgroundColor: 'gray' }
|
|
721
|
-
* }
|
|
722
|
-
* }
|
|
723
|
-
* })
|
|
724
|
-
*
|
|
725
|
-
* // Usage: <Button size="large" variant="primary">Click me</Button>
|
|
726
|
-
*/
|
|
727
|
-
declare function styled<T extends HTMLElementType | ComponentType<Cls>, V extends AllVariants[]>(target: T, ...props: { [K in keyof V]: MochiCSSProps<V[K]> }): FC<Omit<ComponentProps<T>, keyof MochiProps<V>> & MochiProps<V>>;
|
|
717
|
+
declare function css<V extends AllVariants[]>(...props: { [K in keyof V]: MochiCSSProps<V[K]> | MochiCSS | string }): MochiCSS<MergeCSSVariants<V>>;
|
|
728
718
|
//#endregion
|
|
729
719
|
//#region src/keyframesObject.d.ts
|
|
730
720
|
type KeyframeStops = Record<string, SimpleStyleProps>;
|
|
@@ -821,4 +811,28 @@ interface SupportsHelper {
|
|
|
821
811
|
/** Helper for constructing `@supports` at-rule keys. */
|
|
822
812
|
declare const supports: SupportsHelper;
|
|
823
813
|
//#endregion
|
|
824
|
-
|
|
814
|
+
//#region src/hash.d.ts
|
|
815
|
+
/**
|
|
816
|
+
* Hashing utilities for generating short, deterministic class names.
|
|
817
|
+
* Uses djb2 algorithm for fast string hashing.
|
|
818
|
+
* @module hash
|
|
819
|
+
*/
|
|
820
|
+
/**
|
|
821
|
+
* Converts a number to a base-62 string representation.
|
|
822
|
+
* @param num - The number to convert
|
|
823
|
+
* @param maxLength - Optional maximum length of the output string
|
|
824
|
+
* @returns Base-62 encoded string representation of the number
|
|
825
|
+
*/
|
|
826
|
+
declare function numberToBase62(num: number, maxLength?: number): string;
|
|
827
|
+
/**
|
|
828
|
+
* Generates a short hash string from input using the djb2 algorithm.
|
|
829
|
+
* Used to create unique, deterministic CSS class names from style content.
|
|
830
|
+
* @param input - The string to hash
|
|
831
|
+
* @param length - Maximum length of the hash output (default: 8)
|
|
832
|
+
* @returns A short, css-safe hash string
|
|
833
|
+
* @example
|
|
834
|
+
* shortHash("color: red;") // Returns something like "A1b2C3d4"
|
|
835
|
+
*/
|
|
836
|
+
declare function shortHash(input: string, length?: number): string;
|
|
837
|
+
//#endregion
|
|
838
|
+
export { AllVariants, type AtRuleKey, CSSObject, CompoundVariant, CssLike, CssLikeObject, CssObjectBlock, CssObjectSubBlock, CssVar, CssVarVal, DefaultVariants, GlobalCssObject, type GlobalCssStyles, type KeyframeStops, KeyframesObject, MergeCSSVariants, MochiCSS, MochiCSSProps, MochiKeyframes, MochiSelector, RefineVariants, type StyleProps, Token, VariantProps, camelToKebab, container, createToken, css, cssFromProps, globalCss, isAtRuleKey, isMochiCSS, kebabToCamel, keyframes, media, mergeMochiCss, numberToBase62, shortHash, supports };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { ObsoleteProperties, Properties } from "csstype";
|
|
2
|
-
import { ComponentProps, ComponentType, FC, HTMLElementType } from "react";
|
|
3
2
|
|
|
4
3
|
//#region src/values/index.d.ts
|
|
5
4
|
type CssVar = `--${string}`;
|
|
@@ -450,7 +449,11 @@ type PropsWithUnit = PropertyWithUnit & keyof Props;
|
|
|
450
449
|
/**
|
|
451
450
|
* Converts a kebab-case string to camelCase.
|
|
452
451
|
*/
|
|
453
|
-
|
|
452
|
+
declare function kebabToCamel(str: string): string;
|
|
453
|
+
/**
|
|
454
|
+
* Converts a camelCase string to kebab-case.
|
|
455
|
+
*/
|
|
456
|
+
declare function camelToKebab(str: string): string;
|
|
454
457
|
/**
|
|
455
458
|
* Checks if a key represents a CSS at-rule (media, container, supports, layer).
|
|
456
459
|
*/
|
|
@@ -542,10 +545,11 @@ declare class CssObjectBlock {
|
|
|
542
545
|
readonly subBlocks: CssObjectSubBlock[];
|
|
543
546
|
/**
|
|
544
547
|
* Creates a new CSS block from style properties.
|
|
545
|
-
* Generates a unique class name based on the content hash.
|
|
548
|
+
* Generates a unique class name based on the content hash, or uses the provided class name.
|
|
546
549
|
* @param styles - The style properties to compile
|
|
550
|
+
* @param className - Optional stable class name; if omitted, a content-hash-based name is generated
|
|
547
551
|
*/
|
|
548
|
-
constructor(styles: StyleProps);
|
|
552
|
+
constructor(styles: StyleProps, className?: string);
|
|
549
553
|
/**
|
|
550
554
|
* Gets the CSS class selector for this block.
|
|
551
555
|
*/
|
|
@@ -632,13 +636,18 @@ declare class CSSObject<V extends AllVariants = DefaultVariants> {
|
|
|
632
636
|
/**
|
|
633
637
|
* Creates a new CSSObject from style props.
|
|
634
638
|
* Compiles main styles and all variant options into CSS blocks.
|
|
639
|
+
* @param props - Base style props plus variant definitions
|
|
640
|
+
* @param props.variants - Named variant groups, each mapping variant values to style props
|
|
641
|
+
* @param props.defaultVariants - Default value for each variant when none is provided at runtime
|
|
642
|
+
* @param props.compoundVariants - Style props applied when a specific combination of variants is active
|
|
643
|
+
* @param className - Optional stable class name for the main block
|
|
635
644
|
*/
|
|
636
645
|
constructor({
|
|
637
646
|
variants,
|
|
638
647
|
defaultVariants,
|
|
639
648
|
compoundVariants,
|
|
640
649
|
...props
|
|
641
|
-
}: MochiCSSProps<V
|
|
650
|
+
}: MochiCSSProps<V>, className?: string);
|
|
642
651
|
/**
|
|
643
652
|
* Serializes the entire CSS object to a CSS string.
|
|
644
653
|
* Outputs main block first, then all variant blocks in sorted order.
|
|
@@ -649,6 +658,20 @@ declare class CSSObject<V extends AllVariants = DefaultVariants> {
|
|
|
649
658
|
//#endregion
|
|
650
659
|
//#region src/css.d.ts
|
|
651
660
|
declare function isMochiCSS(value: unknown): value is MochiCSS;
|
|
661
|
+
/**
|
|
662
|
+
* Runtime representation of a CSS style definition with variant support.
|
|
663
|
+
* Holds generated class names and provides methods to compute the final
|
|
664
|
+
* className string based on selected variants.
|
|
665
|
+
*
|
|
666
|
+
* @template V - The variant definitions type mapping variant names to their options
|
|
667
|
+
*
|
|
668
|
+
* @example
|
|
669
|
+
* const styles = MochiCSS.from(new CSSObject({
|
|
670
|
+
* color: 'blue',
|
|
671
|
+
* variants: { size: { small: { fontSize: 12 }, large: { fontSize: 18 } } }
|
|
672
|
+
* }))
|
|
673
|
+
* styles.variant({ size: 'large' }) // Returns combined class names
|
|
674
|
+
*/
|
|
652
675
|
declare class MochiCSS<V extends AllVariants = DefaultVariants> {
|
|
653
676
|
readonly classNames: string[];
|
|
654
677
|
readonly variantClassNames: { [K in keyof V]: { [P in keyof V[K]]: string } };
|
|
@@ -669,6 +692,12 @@ declare class MochiCSS<V extends AllVariants = DefaultVariants> {
|
|
|
669
692
|
* @returns Combined className string for use in components
|
|
670
693
|
*/
|
|
671
694
|
variant(props: Partial<RefineVariants<V>>): string;
|
|
695
|
+
/**
|
|
696
|
+
* Returns the CSS selector for this style (e.g. `.abc123`).
|
|
697
|
+
* Useful for targeting this component from another style.
|
|
698
|
+
*/
|
|
699
|
+
get selector(): string;
|
|
700
|
+
toString(): string;
|
|
672
701
|
/**
|
|
673
702
|
* Creates a MochiCSS instance from a CSSObject.
|
|
674
703
|
* Extracts class names from the compiled CSS blocks.
|
|
@@ -685,46 +714,7 @@ declare class MochiCSS<V extends AllVariants = DefaultVariants> {
|
|
|
685
714
|
* @returns A new MochiCSS instance with all styles combined
|
|
686
715
|
*/
|
|
687
716
|
declare function mergeMochiCss<V extends AllVariants[]>(instances: MochiCSS<AllVariants>[]): MochiCSS<MergeCSSVariants<V>>;
|
|
688
|
-
declare function css<V extends AllVariants[]>(...props: { [K in keyof V]: MochiCSSProps<V[K]> | MochiCSS }): MochiCSS<MergeCSSVariants<V>>;
|
|
689
|
-
//#endregion
|
|
690
|
-
//#region src/styled.d.ts
|
|
691
|
-
/** Props added by MochiCSS to styled components */
|
|
692
|
-
type MochiProps<V extends AllVariants[]> = {
|
|
693
|
-
className?: string;
|
|
694
|
-
} & Partial<RefineVariants<MergeCSSVariants<V>>>;
|
|
695
|
-
/** Minimal interface for components that accept className */
|
|
696
|
-
type Cls = {
|
|
697
|
-
className?: string;
|
|
698
|
-
};
|
|
699
|
-
/**
|
|
700
|
-
* Creates a styled React component with CSS-in-JS support and variant props.
|
|
701
|
-
* Similar to styled-components or Stitches, but with zero runtime overhead.
|
|
702
|
-
*
|
|
703
|
-
* @template T - The base element type or component type
|
|
704
|
-
* @template V - The variant definitions tuple type
|
|
705
|
-
* @param target - The HTML element tag name or React component to style
|
|
706
|
-
* @param props - One or more style objects with optional variants
|
|
707
|
-
* @returns A React functional component with merged props and variant support
|
|
708
|
-
*
|
|
709
|
-
* @example
|
|
710
|
-
* const Button = styled('button', {
|
|
711
|
-
* padding: 8,
|
|
712
|
-
* borderRadius: 4,
|
|
713
|
-
* variants: {
|
|
714
|
-
* size: {
|
|
715
|
-
* small: { padding: 4 },
|
|
716
|
-
* large: { padding: 16 }
|
|
717
|
-
* },
|
|
718
|
-
* variant: {
|
|
719
|
-
* primary: { backgroundColor: 'blue' },
|
|
720
|
-
* secondary: { backgroundColor: 'gray' }
|
|
721
|
-
* }
|
|
722
|
-
* }
|
|
723
|
-
* })
|
|
724
|
-
*
|
|
725
|
-
* // Usage: <Button size="large" variant="primary">Click me</Button>
|
|
726
|
-
*/
|
|
727
|
-
declare function styled<T extends HTMLElementType | ComponentType<Cls>, V extends AllVariants[]>(target: T, ...props: { [K in keyof V]: MochiCSSProps<V[K]> }): FC<Omit<ComponentProps<T>, keyof MochiProps<V>> & MochiProps<V>>;
|
|
717
|
+
declare function css<V extends AllVariants[]>(...props: { [K in keyof V]: MochiCSSProps<V[K]> | MochiCSS | string }): MochiCSS<MergeCSSVariants<V>>;
|
|
728
718
|
//#endregion
|
|
729
719
|
//#region src/keyframesObject.d.ts
|
|
730
720
|
type KeyframeStops = Record<string, SimpleStyleProps>;
|
|
@@ -821,5 +811,29 @@ interface SupportsHelper {
|
|
|
821
811
|
/** Helper for constructing `@supports` at-rule keys. */
|
|
822
812
|
declare const supports: SupportsHelper;
|
|
823
813
|
//#endregion
|
|
824
|
-
|
|
814
|
+
//#region src/hash.d.ts
|
|
815
|
+
/**
|
|
816
|
+
* Hashing utilities for generating short, deterministic class names.
|
|
817
|
+
* Uses djb2 algorithm for fast string hashing.
|
|
818
|
+
* @module hash
|
|
819
|
+
*/
|
|
820
|
+
/**
|
|
821
|
+
* Converts a number to a base-62 string representation.
|
|
822
|
+
* @param num - The number to convert
|
|
823
|
+
* @param maxLength - Optional maximum length of the output string
|
|
824
|
+
* @returns Base-62 encoded string representation of the number
|
|
825
|
+
*/
|
|
826
|
+
declare function numberToBase62(num: number, maxLength?: number): string;
|
|
827
|
+
/**
|
|
828
|
+
* Generates a short hash string from input using the djb2 algorithm.
|
|
829
|
+
* Used to create unique, deterministic CSS class names from style content.
|
|
830
|
+
* @param input - The string to hash
|
|
831
|
+
* @param length - Maximum length of the hash output (default: 8)
|
|
832
|
+
* @returns A short, css-safe hash string
|
|
833
|
+
* @example
|
|
834
|
+
* shortHash("color: red;") // Returns something like "A1b2C3d4"
|
|
835
|
+
*/
|
|
836
|
+
declare function shortHash(input: string, length?: number): string;
|
|
837
|
+
//#endregion
|
|
838
|
+
export { AllVariants, type AtRuleKey, CSSObject, CompoundVariant, CssLike, CssLikeObject, CssObjectBlock, CssObjectSubBlock, CssVar, CssVarVal, DefaultVariants, GlobalCssObject, type GlobalCssStyles, type KeyframeStops, KeyframesObject, MergeCSSVariants, MochiCSS, MochiCSSProps, MochiKeyframes, MochiSelector, RefineVariants, type StyleProps, Token, VariantProps, camelToKebab, container, createToken, css, cssFromProps, globalCss, isAtRuleKey, isMochiCSS, kebabToCamel, keyframes, media, mergeMochiCss, numberToBase62, shortHash, supports };
|
|
825
839
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.js
CHANGED
|
@@ -23,8 +23,6 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
|
|
|
23
23
|
//#endregion
|
|
24
24
|
let clsx = require("clsx");
|
|
25
25
|
clsx = __toESM(clsx);
|
|
26
|
-
let react = require("react");
|
|
27
|
-
react = __toESM(react);
|
|
28
26
|
|
|
29
27
|
//#region src/token.ts
|
|
30
28
|
/**
|
|
@@ -1156,6 +1154,12 @@ const knownPropertyNames = new Set([
|
|
|
1156
1154
|
//#endregion
|
|
1157
1155
|
//#region src/props.ts
|
|
1158
1156
|
/**
|
|
1157
|
+
* Converts a kebab-case string to camelCase.
|
|
1158
|
+
*/
|
|
1159
|
+
function kebabToCamel(str) {
|
|
1160
|
+
return str.replace(/-([a-z])/g, (_, c) => c.toUpperCase());
|
|
1161
|
+
}
|
|
1162
|
+
/**
|
|
1159
1163
|
* Converts a camelCase string to kebab-case.
|
|
1160
1164
|
*/
|
|
1161
1165
|
function camelToKebab(str) {
|
|
@@ -1496,12 +1500,13 @@ var CssObjectBlock = class {
|
|
|
1496
1500
|
subBlocks = [];
|
|
1497
1501
|
/**
|
|
1498
1502
|
* Creates a new CSS block from style properties.
|
|
1499
|
-
* Generates a unique class name based on the content hash.
|
|
1503
|
+
* Generates a unique class name based on the content hash, or uses the provided class name.
|
|
1500
1504
|
* @param styles - The style properties to compile
|
|
1505
|
+
* @param className - Optional stable class name; if omitted, a content-hash-based name is generated
|
|
1501
1506
|
*/
|
|
1502
|
-
constructor(styles) {
|
|
1507
|
+
constructor(styles, className) {
|
|
1503
1508
|
const blocks = CssObjectSubBlock.fromProps(styles);
|
|
1504
|
-
this.className = "c" + shortHash(blocks.map((b) => b.hash).join("+"));
|
|
1509
|
+
this.className = className ?? "c" + shortHash(blocks.map((b) => b.hash).join("+"));
|
|
1505
1510
|
this.subBlocks = blocks;
|
|
1506
1511
|
}
|
|
1507
1512
|
/**
|
|
@@ -1549,9 +1554,14 @@ var CSSObject = class {
|
|
|
1549
1554
|
/**
|
|
1550
1555
|
* Creates a new CSSObject from style props.
|
|
1551
1556
|
* Compiles main styles and all variant options into CSS blocks.
|
|
1557
|
+
* @param props - Base style props plus variant definitions
|
|
1558
|
+
* @param props.variants - Named variant groups, each mapping variant values to style props
|
|
1559
|
+
* @param props.defaultVariants - Default value for each variant when none is provided at runtime
|
|
1560
|
+
* @param props.compoundVariants - Style props applied when a specific combination of variants is active
|
|
1561
|
+
* @param className - Optional stable class name for the main block
|
|
1552
1562
|
*/
|
|
1553
|
-
constructor({ variants, defaultVariants, compoundVariants,...props }) {
|
|
1554
|
-
this.mainBlock = new CssObjectBlock(props);
|
|
1563
|
+
constructor({ variants, defaultVariants, compoundVariants,...props }, className) {
|
|
1564
|
+
this.mainBlock = new CssObjectBlock(props, className);
|
|
1555
1565
|
this.variantBlocks = {};
|
|
1556
1566
|
this.variantDefaults = defaultVariants ?? {};
|
|
1557
1567
|
this.compoundVariants = [];
|
|
@@ -1593,6 +1603,10 @@ var CSSObject = class {
|
|
|
1593
1603
|
|
|
1594
1604
|
//#endregion
|
|
1595
1605
|
//#region src/css.ts
|
|
1606
|
+
const MOCHI_CSS_TYPEOF = Symbol.for("mochi-css.MochiCSS");
|
|
1607
|
+
function isMochiCSS(value) {
|
|
1608
|
+
return typeof value === "object" && value !== null && value["$$typeof"] === MOCHI_CSS_TYPEOF;
|
|
1609
|
+
}
|
|
1596
1610
|
/**
|
|
1597
1611
|
* Runtime representation of a CSS style definition with variant support.
|
|
1598
1612
|
* Holds generated class names and provides methods to compute the final
|
|
@@ -1607,10 +1621,6 @@ var CSSObject = class {
|
|
|
1607
1621
|
* }))
|
|
1608
1622
|
* styles.variant({ size: 'large' }) // Returns combined class names
|
|
1609
1623
|
*/
|
|
1610
|
-
const MOCHI_CSS_TYPEOF = Symbol.for("mochi-css.MochiCSS");
|
|
1611
|
-
function isMochiCSS(value) {
|
|
1612
|
-
return typeof value === "object" && value !== null && value["$$typeof"] === MOCHI_CSS_TYPEOF;
|
|
1613
|
-
}
|
|
1614
1624
|
var MochiCSS = class MochiCSS {
|
|
1615
1625
|
$$typeof = MOCHI_CSS_TYPEOF;
|
|
1616
1626
|
/**
|
|
@@ -1646,6 +1656,16 @@ var MochiCSS = class MochiCSS {
|
|
|
1646
1656
|
}));
|
|
1647
1657
|
}
|
|
1648
1658
|
/**
|
|
1659
|
+
* Returns the CSS selector for this style (e.g. `.abc123`).
|
|
1660
|
+
* Useful for targeting this component from another style.
|
|
1661
|
+
*/
|
|
1662
|
+
get selector() {
|
|
1663
|
+
return this.classNames.map((n) => `.${n}`).join();
|
|
1664
|
+
}
|
|
1665
|
+
toString() {
|
|
1666
|
+
return this.selector;
|
|
1667
|
+
}
|
|
1668
|
+
/**
|
|
1649
1669
|
* Creates a MochiCSS instance from a CSSObject.
|
|
1650
1670
|
* Extracts class names from the compiled CSS blocks.
|
|
1651
1671
|
* @template V - The variant definitions type
|
|
@@ -1704,51 +1724,18 @@ function mergeMochiCss(instances) {
|
|
|
1704
1724
|
function css(...props) {
|
|
1705
1725
|
const cssToMerge = [];
|
|
1706
1726
|
for (const p of props) {
|
|
1707
|
-
if (p == null
|
|
1727
|
+
if (p == null) continue;
|
|
1728
|
+
if (typeof p === "string") {
|
|
1729
|
+
cssToMerge.push(new MochiCSS([p], {}, {}));
|
|
1730
|
+
continue;
|
|
1731
|
+
}
|
|
1732
|
+
if (typeof p !== "object") continue;
|
|
1708
1733
|
if (p instanceof MochiCSS) cssToMerge.push(p);
|
|
1709
1734
|
else cssToMerge.push(MochiCSS.from(new CSSObject(p)));
|
|
1710
1735
|
}
|
|
1711
1736
|
return mergeMochiCss(cssToMerge);
|
|
1712
1737
|
}
|
|
1713
1738
|
|
|
1714
|
-
//#endregion
|
|
1715
|
-
//#region src/styled.ts
|
|
1716
|
-
/**
|
|
1717
|
-
* Creates a styled React component with CSS-in-JS support and variant props.
|
|
1718
|
-
* Similar to styled-components or Stitches, but with zero runtime overhead.
|
|
1719
|
-
*
|
|
1720
|
-
* @template T - The base element type or component type
|
|
1721
|
-
* @template V - The variant definitions tuple type
|
|
1722
|
-
* @param target - The HTML element tag name or React component to style
|
|
1723
|
-
* @param props - One or more style objects with optional variants
|
|
1724
|
-
* @returns A React functional component with merged props and variant support
|
|
1725
|
-
*
|
|
1726
|
-
* @example
|
|
1727
|
-
* const Button = styled('button', {
|
|
1728
|
-
* padding: 8,
|
|
1729
|
-
* borderRadius: 4,
|
|
1730
|
-
* variants: {
|
|
1731
|
-
* size: {
|
|
1732
|
-
* small: { padding: 4 },
|
|
1733
|
-
* large: { padding: 16 }
|
|
1734
|
-
* },
|
|
1735
|
-
* variant: {
|
|
1736
|
-
* primary: { backgroundColor: 'blue' },
|
|
1737
|
-
* secondary: { backgroundColor: 'gray' }
|
|
1738
|
-
* }
|
|
1739
|
-
* }
|
|
1740
|
-
* })
|
|
1741
|
-
*
|
|
1742
|
-
* // Usage: <Button size="large" variant="primary">Click me</Button>
|
|
1743
|
-
*/
|
|
1744
|
-
function styled(target, ...props) {
|
|
1745
|
-
const styles = css(...props);
|
|
1746
|
-
return ({ className,...p }) => (0, react.createElement)(target, {
|
|
1747
|
-
className: (0, clsx.default)(styles.variant(p), className),
|
|
1748
|
-
...p
|
|
1749
|
-
});
|
|
1750
|
-
}
|
|
1751
|
-
|
|
1752
1739
|
//#endregion
|
|
1753
1740
|
//#region src/keyframesObject.ts
|
|
1754
1741
|
var KeyframesObject = class KeyframesObject {
|
|
@@ -1904,6 +1891,7 @@ exports.MochiCSS = MochiCSS;
|
|
|
1904
1891
|
exports.MochiKeyframes = MochiKeyframes;
|
|
1905
1892
|
exports.MochiSelector = MochiSelector;
|
|
1906
1893
|
exports.Token = Token;
|
|
1894
|
+
exports.camelToKebab = camelToKebab;
|
|
1907
1895
|
exports.container = container;
|
|
1908
1896
|
exports.createToken = createToken;
|
|
1909
1897
|
exports.css = css;
|
|
@@ -1911,9 +1899,11 @@ exports.cssFromProps = cssFromProps;
|
|
|
1911
1899
|
exports.globalCss = globalCss;
|
|
1912
1900
|
exports.isAtRuleKey = isAtRuleKey;
|
|
1913
1901
|
exports.isMochiCSS = isMochiCSS;
|
|
1902
|
+
exports.kebabToCamel = kebabToCamel;
|
|
1914
1903
|
exports.keyframes = keyframes;
|
|
1915
1904
|
exports.media = media;
|
|
1916
1905
|
exports.mergeMochiCss = mergeMochiCss;
|
|
1917
|
-
exports.
|
|
1906
|
+
exports.numberToBase62 = numberToBase62;
|
|
1907
|
+
exports.shortHash = shortHash;
|
|
1918
1908
|
exports.supports = supports;
|
|
1919
1909
|
//# sourceMappingURL=index.js.map
|