@mochi-css/react 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 +122 -0
- package/dist/index.d.mts +56 -0
- package/dist/index.d.ts +56 -0
- package/dist/index.js +1752 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +1727 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +60 -0
package/README.md
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
# 🧁 Mochi-CSS/react
|
|
2
|
+
|
|
3
|
+
This package is part of the [Mochi-CSS project](https://github.com/Niikelion/mochi-css).
|
|
4
|
+
It provides the `styled` utility for creating type-safe styled React components.
|
|
5
|
+
|
|
6
|
+
## Installation
|
|
7
|
+
|
|
8
|
+
```bash
|
|
9
|
+
npm i @mochi-css/react
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
---
|
|
13
|
+
|
|
14
|
+
## `styled(component, ...styles)`
|
|
15
|
+
|
|
16
|
+
`styled` creates a React component by combining a base element or component with Mochi-CSS style definitions.
|
|
17
|
+
Variant props are automatically extracted, applied as class names, and never forwarded to the underlying element.
|
|
18
|
+
|
|
19
|
+
```tsx
|
|
20
|
+
import { css } from "@mochi-css/vanilla"
|
|
21
|
+
import { styled } from "@mochi-css/react"
|
|
22
|
+
|
|
23
|
+
const Button = styled("button", {
|
|
24
|
+
padding: "8px 16px",
|
|
25
|
+
borderRadius: 4,
|
|
26
|
+
border: "none",
|
|
27
|
+
cursor: "pointer",
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
// <Button>Click me</Button>
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
### With variants
|
|
34
|
+
|
|
35
|
+
Pass a style object with a `variants` key to generate typed variant props:
|
|
36
|
+
|
|
37
|
+
```tsx
|
|
38
|
+
import { styled } from "@mochi-css/react"
|
|
39
|
+
|
|
40
|
+
const Button = styled("button", {
|
|
41
|
+
padding: "8px 16px",
|
|
42
|
+
borderRadius: 4,
|
|
43
|
+
variants: {
|
|
44
|
+
intent: {
|
|
45
|
+
primary: { backgroundColor: "blue", color: "white" },
|
|
46
|
+
danger: { backgroundColor: "red", color: "white" },
|
|
47
|
+
},
|
|
48
|
+
size: {
|
|
49
|
+
small: { fontSize: 12, padding: "4px 8px" },
|
|
50
|
+
large: { fontSize: 18, padding: "12px 24px" },
|
|
51
|
+
},
|
|
52
|
+
},
|
|
53
|
+
defaultVariants: {
|
|
54
|
+
intent: "primary",
|
|
55
|
+
size: "small",
|
|
56
|
+
},
|
|
57
|
+
})
|
|
58
|
+
|
|
59
|
+
// <Button intent="danger" size="large">Delete</Button>
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
### With a shared `css()` style
|
|
63
|
+
|
|
64
|
+
Pass a `MochiCSS` object (the return value of `css()`) to share styles across multiple components:
|
|
65
|
+
|
|
66
|
+
```tsx
|
|
67
|
+
import { css } from "@mochi-css/vanilla"
|
|
68
|
+
import { styled } from "@mochi-css/react"
|
|
69
|
+
|
|
70
|
+
const baseButton = css({
|
|
71
|
+
borderRadius: 4,
|
|
72
|
+
border: "none",
|
|
73
|
+
cursor: "pointer",
|
|
74
|
+
})
|
|
75
|
+
|
|
76
|
+
const PrimaryButton = styled("button", baseButton, {
|
|
77
|
+
backgroundColor: "blue",
|
|
78
|
+
color: "white",
|
|
79
|
+
})
|
|
80
|
+
|
|
81
|
+
const SecondaryButton = styled("button", baseButton, {
|
|
82
|
+
backgroundColor: "gray",
|
|
83
|
+
color: "white",
|
|
84
|
+
})
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
### Component targeting
|
|
88
|
+
|
|
89
|
+
Each styled component has a `.selector` getter (and a `toString()` that returns the same value) for use in parent component styles:
|
|
90
|
+
|
|
91
|
+
```tsx
|
|
92
|
+
const Icon = styled("span", { marginRight: 4 })
|
|
93
|
+
|
|
94
|
+
const Button = styled("button", {
|
|
95
|
+
display: "flex",
|
|
96
|
+
|
|
97
|
+
// target Icon inside Button
|
|
98
|
+
[`${Icon} &`]: { color: "inherit" },
|
|
99
|
+
})
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
### Styling an existing component
|
|
103
|
+
|
|
104
|
+
`styled` accepts any React component that accepts a `className` prop:
|
|
105
|
+
|
|
106
|
+
```tsx
|
|
107
|
+
import { Link } from "react-router-dom"
|
|
108
|
+
|
|
109
|
+
const NavLink = styled(Link, {
|
|
110
|
+
color: "inherit",
|
|
111
|
+
textDecoration: "none",
|
|
112
|
+
"&:hover": { textDecoration: "underline" },
|
|
113
|
+
})
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
---
|
|
117
|
+
|
|
118
|
+
## Type exports
|
|
119
|
+
|
|
120
|
+
| Export | Description |
|
|
121
|
+
|------------------------|------------------------------------------------|
|
|
122
|
+
| `MochiStyledComponent` | The type of a component returned by `styled()` |
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { ComponentProps, ComponentType, FC, HTMLElementType } from "react";
|
|
2
|
+
import { AllVariants, MergeCSSVariants, MochiCSS, MochiCSSProps, RefineVariants } from "@mochi-css/vanilla";
|
|
3
|
+
|
|
4
|
+
//#region src/styled.d.ts
|
|
5
|
+
|
|
6
|
+
/** Props added by MochiCSS to styled components */
|
|
7
|
+
type MochiProps<V extends AllVariants[]> = {
|
|
8
|
+
className?: string;
|
|
9
|
+
} & Partial<RefineVariants<MergeCSSVariants<V>>>;
|
|
10
|
+
/** Minimal interface for components that accept className */
|
|
11
|
+
type Cls = {
|
|
12
|
+
className?: string;
|
|
13
|
+
};
|
|
14
|
+
/** A styled component FC augmented with a CSS selector for component targeting */
|
|
15
|
+
type MochiStyledComponent<T extends HTMLElementType | ComponentType<Cls>, V extends AllVariants[]> = FC<Omit<ComponentProps<T>, keyof MochiProps<V>> & MochiProps<V>> & {
|
|
16
|
+
toString(): string;
|
|
17
|
+
selector: string;
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* Creates a styled React component with CSS-in-JS support and variant props.
|
|
21
|
+
* Similar to styled-components or Stitches, but with zero runtime overhead.
|
|
22
|
+
*
|
|
23
|
+
* @template T - The base element type or component type
|
|
24
|
+
* @template V - The variant definitions tuple type
|
|
25
|
+
* @param target - The HTML element tag name or React component to style
|
|
26
|
+
* @param props - One or more style objects with optional variants
|
|
27
|
+
* @returns A React functional component with merged props and variant support
|
|
28
|
+
*
|
|
29
|
+
* @remarks
|
|
30
|
+
* Variant props are automatically stripped and never forwarded to the underlying
|
|
31
|
+
* element or component. This prevents unknown prop warnings on DOM elements.
|
|
32
|
+
* If the inner component has a prop with the same
|
|
33
|
+
* name as a variant, it will not receive that prop.
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* const Button = styled('button', {
|
|
37
|
+
* padding: 8,
|
|
38
|
+
* borderRadius: 4,
|
|
39
|
+
* variants: {
|
|
40
|
+
* size: {
|
|
41
|
+
* small: { padding: 4 },
|
|
42
|
+
* large: { padding: 16 }
|
|
43
|
+
* },
|
|
44
|
+
* variant: {
|
|
45
|
+
* primary: { backgroundColor: 'blue' },
|
|
46
|
+
* secondary: { backgroundColor: 'gray' }
|
|
47
|
+
* }
|
|
48
|
+
* }
|
|
49
|
+
* })
|
|
50
|
+
*
|
|
51
|
+
* // Usage: <Button size="large" variant="primary">Click me</Button>
|
|
52
|
+
*/
|
|
53
|
+
declare function styled<T extends HTMLElementType | ComponentType<Cls>, V extends AllVariants[]>(target: T, ...props: { [K in keyof V]: MochiCSSProps<V[K]> | MochiCSS | string }): MochiStyledComponent<T, V>;
|
|
54
|
+
//#endregion
|
|
55
|
+
export { MochiStyledComponent, styled };
|
|
56
|
+
//# sourceMappingURL=index.d.mts.map
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { ComponentProps, ComponentType, FC, HTMLElementType } from "react";
|
|
2
|
+
import { AllVariants, MergeCSSVariants, MochiCSS, MochiCSSProps, RefineVariants } from "@mochi-css/vanilla";
|
|
3
|
+
|
|
4
|
+
//#region src/styled.d.ts
|
|
5
|
+
|
|
6
|
+
/** Props added by MochiCSS to styled components */
|
|
7
|
+
type MochiProps<V extends AllVariants[]> = {
|
|
8
|
+
className?: string;
|
|
9
|
+
} & Partial<RefineVariants<MergeCSSVariants<V>>>;
|
|
10
|
+
/** Minimal interface for components that accept className */
|
|
11
|
+
type Cls = {
|
|
12
|
+
className?: string;
|
|
13
|
+
};
|
|
14
|
+
/** A styled component FC augmented with a CSS selector for component targeting */
|
|
15
|
+
type MochiStyledComponent<T extends HTMLElementType | ComponentType<Cls>, V extends AllVariants[]> = FC<Omit<ComponentProps<T>, keyof MochiProps<V>> & MochiProps<V>> & {
|
|
16
|
+
toString(): string;
|
|
17
|
+
selector: string;
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* Creates a styled React component with CSS-in-JS support and variant props.
|
|
21
|
+
* Similar to styled-components or Stitches, but with zero runtime overhead.
|
|
22
|
+
*
|
|
23
|
+
* @template T - The base element type or component type
|
|
24
|
+
* @template V - The variant definitions tuple type
|
|
25
|
+
* @param target - The HTML element tag name or React component to style
|
|
26
|
+
* @param props - One or more style objects with optional variants
|
|
27
|
+
* @returns A React functional component with merged props and variant support
|
|
28
|
+
*
|
|
29
|
+
* @remarks
|
|
30
|
+
* Variant props are automatically stripped and never forwarded to the underlying
|
|
31
|
+
* element or component. This prevents unknown prop warnings on DOM elements.
|
|
32
|
+
* If the inner component has a prop with the same
|
|
33
|
+
* name as a variant, it will not receive that prop.
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* const Button = styled('button', {
|
|
37
|
+
* padding: 8,
|
|
38
|
+
* borderRadius: 4,
|
|
39
|
+
* variants: {
|
|
40
|
+
* size: {
|
|
41
|
+
* small: { padding: 4 },
|
|
42
|
+
* large: { padding: 16 }
|
|
43
|
+
* },
|
|
44
|
+
* variant: {
|
|
45
|
+
* primary: { backgroundColor: 'blue' },
|
|
46
|
+
* secondary: { backgroundColor: 'gray' }
|
|
47
|
+
* }
|
|
48
|
+
* }
|
|
49
|
+
* })
|
|
50
|
+
*
|
|
51
|
+
* // Usage: <Button size="large" variant="primary">Click me</Button>
|
|
52
|
+
*/
|
|
53
|
+
declare function styled<T extends HTMLElementType | ComponentType<Cls>, V extends AllVariants[]>(target: T, ...props: { [K in keyof V]: MochiCSSProps<V[K]> | MochiCSS | string }): MochiStyledComponent<T, V>;
|
|
54
|
+
//#endregion
|
|
55
|
+
export { MochiStyledComponent, styled };
|
|
56
|
+
//# sourceMappingURL=index.d.ts.map
|