@plumeria/react 22.2.5 → 22.3.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 +99 -0
- package/lib/styled.d.ts +6 -4
- package/package.json +1 -1
package/README.md
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
**@plumeria/react** adds `styled` to [Plumeria](https://www.npmjs.com/package/@plumeria/core). At build time, the compiler first rewrites a styled definition using `css.create` and the configured style prop (`sx` by default). Plumeria then compiles those styles as usual.
|
|
2
|
+
|
|
3
|
+
## Installation
|
|
4
|
+
|
|
5
|
+
```sh
|
|
6
|
+
pnpm add -D @plumeria/react
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
`@plumeria/react` contains type definitions only. It needs `@plumeria/core` and a bundler integration — see the [installation guide](https://plumeria.dev/docs/installation).
|
|
10
|
+
|
|
11
|
+
## Example
|
|
12
|
+
|
|
13
|
+
Define a styled element at module scope and use it with the native element's props.
|
|
14
|
+
|
|
15
|
+
```tsx
|
|
16
|
+
import '@plumeria/core';
|
|
17
|
+
import { styled } from '@plumeria/react';
|
|
18
|
+
|
|
19
|
+
export const Button = styled.button({
|
|
20
|
+
padding: '8px',
|
|
21
|
+
color: 'navy',
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
export function SaveButton() {
|
|
25
|
+
return <Button type="submit">Save</Button>;
|
|
26
|
+
}
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## Values from props
|
|
30
|
+
|
|
31
|
+
Read a prop directly in a style value. Its parameter type tells TypeScript which props the component requires.
|
|
32
|
+
|
|
33
|
+
```tsx
|
|
34
|
+
import '@plumeria/core';
|
|
35
|
+
import { styled } from '@plumeria/react';
|
|
36
|
+
|
|
37
|
+
const Swatch = styled.div({
|
|
38
|
+
width: '40px',
|
|
39
|
+
height: '40px',
|
|
40
|
+
backgroundColor: (props: { fill: string }) => props.fill,
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
export function ColorPreview({ color }: { color: string }) {
|
|
44
|
+
return <Swatch fill={color} aria-label={color} role="img" />;
|
|
45
|
+
}
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
The compiler passes `fill` to the element through a CSS variable and omits the `fill` attribute. For a conditional color, choose the value where you render `Swatch`, for example `<Swatch fill={active ? 'teal' : 'gray'} />`.
|
|
49
|
+
|
|
50
|
+
## Extending and overriding styles
|
|
51
|
+
|
|
52
|
+
Styles merge in order from the base component through each extension to the `sx` prop at the call site. When two styles set the same property, the later style wins. Within an `sx` array, the rightmost active style wins.
|
|
53
|
+
|
|
54
|
+
Here, `DeleteButton` keeps the padding from `Button` and changes its color to maroon. When `urgent` is true, `sx` changes the color to red.
|
|
55
|
+
|
|
56
|
+
```tsx
|
|
57
|
+
import * as css from '@plumeria/core';
|
|
58
|
+
import { styled } from '@plumeria/react';
|
|
59
|
+
import { Button } from './Button';
|
|
60
|
+
|
|
61
|
+
const DeleteButton = styled(Button)({
|
|
62
|
+
color: 'maroon',
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
const styles = css.create({
|
|
66
|
+
urgent: {
|
|
67
|
+
color: 'red',
|
|
68
|
+
},
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
export function DeleteAction({ urgent }: { urgent: boolean }) {
|
|
72
|
+
return (
|
|
73
|
+
<DeleteButton type="button" sx={[urgent && styles.urgent]}>
|
|
74
|
+
Delete
|
|
75
|
+
</DeleteButton>
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
A chain can be up to 16 levels deep.
|
|
81
|
+
|
|
82
|
+
## Other JSX frameworks
|
|
83
|
+
|
|
84
|
+
`styled` takes its tags, props and return type from React by default. For Preact, Qwik and others, register the framework's `JSX` types once:
|
|
85
|
+
|
|
86
|
+
```ts
|
|
87
|
+
import type { JSX } from '@builder.io/qwik';
|
|
88
|
+
|
|
89
|
+
declare module '@plumeria/react' {
|
|
90
|
+
interface JSXRegister {
|
|
91
|
+
IntrinsicElements: JSX.IntrinsicElements;
|
|
92
|
+
Element: JSX.Element;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
## License
|
|
98
|
+
|
|
99
|
+
Plumeria is [MIT licensed](https://github.com/zss-labs/plumeria/blob/main/LICENSE).
|
package/lib/styled.d.ts
CHANGED
|
@@ -32,7 +32,9 @@ type Reads<R> =
|
|
|
32
32
|
R extends AtomicClassNameFor<string, infer V>
|
|
33
33
|
? Reads<V>
|
|
34
34
|
: R extends (props: infer A) => any
|
|
35
|
-
? A
|
|
35
|
+
? 0 extends 1 & A
|
|
36
|
+
? Record<string, any>
|
|
37
|
+
: A
|
|
36
38
|
: R extends object
|
|
37
39
|
? { [K in keyof R]: Reads<R[K]> }[keyof R]
|
|
38
40
|
: never;
|
|
@@ -85,7 +87,7 @@ type Known<T> = {
|
|
|
85
87
|
export type StyledProps<T extends StyledTarget, P> = P &
|
|
86
88
|
Omit<
|
|
87
89
|
Framework['useReactProps'] extends true ? PropsOf<T> : Known<PropsOf<T>>,
|
|
88
|
-
keyof P | 'sx'
|
|
90
|
+
keyof Known<P> | 'sx'
|
|
89
91
|
> & {
|
|
90
92
|
sx?: Style;
|
|
91
93
|
};
|
|
@@ -101,7 +103,7 @@ export type StyledComponent<T extends StyledTarget, R> = ((
|
|
|
101
103
|
};
|
|
102
104
|
|
|
103
105
|
export type StyledFactory<T extends StyledTarget> = {
|
|
104
|
-
<const R extends StyledRule<
|
|
106
|
+
<const R extends StyledRule<any>>(
|
|
105
107
|
rule: R,
|
|
106
108
|
): StyledComponent<T, AtomicStyle<R>>;
|
|
107
109
|
new (rule: never): never;
|
|
@@ -110,7 +112,7 @@ export type StyledFactory<T extends StyledTarget> = {
|
|
|
110
112
|
export type styled = {
|
|
111
113
|
<C extends ((props: any) => any) & Branded>(component: C): StyledFactory<C>;
|
|
112
114
|
} & {
|
|
113
|
-
[T in keyof Intrinsic
|
|
115
|
+
[T in keyof Intrinsic]: StyledFactory<T>;
|
|
114
116
|
};
|
|
115
117
|
|
|
116
118
|
export const styled: styled;
|