@plumeria/react 0.0.0-stage → 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/LICENSE +21 -0
- package/README.md +98 -2
- package/lib/styled.d.ts +118 -0
- package/package.json +40 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) zss-labs contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,99 @@
|
|
|
1
|
-
|
|
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
2
|
|
|
3
|
-
|
|
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
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Type definitions. Without runtime implementation.
|
|
3
|
+
* Bundler plugin required.
|
|
4
|
+
* ```ts
|
|
5
|
+
* type styled = { [T in keyof JSX.IntrinsicElements]: <const R>(rule: R) => StyledComponent<T, R> };
|
|
6
|
+
* ```
|
|
7
|
+
*/
|
|
8
|
+
import type {
|
|
9
|
+
ComponentPropsWithRef,
|
|
10
|
+
JSX,
|
|
11
|
+
JSXElementConstructor,
|
|
12
|
+
ReactElement,
|
|
13
|
+
} from 'react';
|
|
14
|
+
import type {
|
|
15
|
+
AtomicClassNameFor,
|
|
16
|
+
AtomicStyle,
|
|
17
|
+
CSSProperties,
|
|
18
|
+
Style,
|
|
19
|
+
} from '@plumeria/core';
|
|
20
|
+
|
|
21
|
+
type NestedKey = `:${string}` | `[${string}` | `@${string}`;
|
|
22
|
+
|
|
23
|
+
type Dynamic<T, P> = {
|
|
24
|
+
[K in keyof T]: K extends NestedKey
|
|
25
|
+
? Dynamic<T[K], P>
|
|
26
|
+
: T[K] | ((props: P) => T[K]);
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
export type StyledRule<P> = Dynamic<CSSProperties, P>;
|
|
30
|
+
|
|
31
|
+
type Reads<R> =
|
|
32
|
+
R extends AtomicClassNameFor<string, infer V>
|
|
33
|
+
? Reads<V>
|
|
34
|
+
: R extends (props: infer A) => any
|
|
35
|
+
? 0 extends 1 & A
|
|
36
|
+
? Record<string, any>
|
|
37
|
+
: A
|
|
38
|
+
: R extends object
|
|
39
|
+
? { [K in keyof R]: Reads<R[K]> }[keyof R]
|
|
40
|
+
: never;
|
|
41
|
+
|
|
42
|
+
type Intersect<U> = (U extends any ? (x: U) => void : never) extends (
|
|
43
|
+
x: infer I,
|
|
44
|
+
) => void
|
|
45
|
+
? I
|
|
46
|
+
: never;
|
|
47
|
+
|
|
48
|
+
export type StyledPropsOf<R> = [Reads<R>] extends [never]
|
|
49
|
+
? {}
|
|
50
|
+
: Intersect<Reads<R>>;
|
|
51
|
+
|
|
52
|
+
export interface JSXRegister {}
|
|
53
|
+
|
|
54
|
+
type Framework = JSXRegister extends {
|
|
55
|
+
IntrinsicElements: infer I;
|
|
56
|
+
Element: infer E;
|
|
57
|
+
}
|
|
58
|
+
? { intrinsic: I; element: E; useReactProps: false }
|
|
59
|
+
: {
|
|
60
|
+
intrinsic: JSX.IntrinsicElements;
|
|
61
|
+
element: ReactElement;
|
|
62
|
+
useReactProps: true;
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
type Intrinsic = Framework['intrinsic'];
|
|
66
|
+
|
|
67
|
+
export type StyledTarget =
|
|
68
|
+
| keyof Intrinsic
|
|
69
|
+
| (Framework['useReactProps'] extends true
|
|
70
|
+
? JSXElementConstructor<any>
|
|
71
|
+
: (props: any) => any);
|
|
72
|
+
|
|
73
|
+
type PropsOf<T> = Framework['useReactProps'] extends true
|
|
74
|
+
? T extends keyof JSX.IntrinsicElements | JSXElementConstructor<any>
|
|
75
|
+
? ComponentPropsWithRef<T>
|
|
76
|
+
: never
|
|
77
|
+
: T extends keyof Intrinsic
|
|
78
|
+
? Intrinsic[T]
|
|
79
|
+
: T extends (props: infer P) => any
|
|
80
|
+
? P
|
|
81
|
+
: never;
|
|
82
|
+
|
|
83
|
+
type Known<T> = {
|
|
84
|
+
[K in keyof T as string extends K ? never : K]: T[K];
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
export type StyledProps<T extends StyledTarget, P> = P &
|
|
88
|
+
Omit<
|
|
89
|
+
Framework['useReactProps'] extends true ? PropsOf<T> : Known<PropsOf<T>>,
|
|
90
|
+
keyof Known<P> | 'sx'
|
|
91
|
+
> & {
|
|
92
|
+
sx?: Style;
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
declare const styledBrand: unique symbol;
|
|
96
|
+
|
|
97
|
+
type Branded = { readonly [styledBrand]: unknown };
|
|
98
|
+
|
|
99
|
+
export type StyledComponent<T extends StyledTarget, R> = ((
|
|
100
|
+
props: StyledProps<T, StyledPropsOf<R>>,
|
|
101
|
+
) => Framework['element']) & {
|
|
102
|
+
readonly [styledBrand]: R;
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
export type StyledFactory<T extends StyledTarget> = {
|
|
106
|
+
<const R extends StyledRule<any>>(
|
|
107
|
+
rule: R,
|
|
108
|
+
): StyledComponent<T, AtomicStyle<R>>;
|
|
109
|
+
new (rule: never): never;
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
export type styled = {
|
|
113
|
+
<C extends ((props: any) => any) & Branded>(component: C): StyledFactory<C>;
|
|
114
|
+
} & {
|
|
115
|
+
[T in keyof Intrinsic]: StyledFactory<T>;
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
export const styled: styled;
|
package/package.json
CHANGED
|
@@ -1,6 +1,43 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@plumeria/react",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "22.3.0",
|
|
4
|
+
"description": "Styled components for Plumeria, compiled to the sx prop",
|
|
5
|
+
"author": "zss-labs",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"homepage": "https://plumeria.dev",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/zss-labs/plumeria.git",
|
|
11
|
+
"directory": "packages/react"
|
|
12
|
+
},
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/zss-labs/plumeria/issues"
|
|
15
|
+
},
|
|
16
|
+
"keywords": [
|
|
17
|
+
"plumeria",
|
|
18
|
+
"react",
|
|
19
|
+
"styled",
|
|
20
|
+
"css-in-js",
|
|
21
|
+
"atomic-css",
|
|
22
|
+
"zero-runtime",
|
|
23
|
+
"type-safe",
|
|
24
|
+
"typescript"
|
|
25
|
+
],
|
|
26
|
+
"exports": {
|
|
27
|
+
".": {
|
|
28
|
+
"types": "./lib/styled.d.ts"
|
|
29
|
+
}
|
|
30
|
+
},
|
|
31
|
+
"types": "./lib/styled.d.ts",
|
|
32
|
+
"files": [
|
|
33
|
+
"lib/"
|
|
34
|
+
],
|
|
35
|
+
"devDependencies": {
|
|
36
|
+
"@types/react": "^19.3.0",
|
|
37
|
+
"@plumeria/core": "22.3.0"
|
|
38
|
+
},
|
|
39
|
+
"publishConfig": {
|
|
40
|
+
"access": "public",
|
|
41
|
+
"provenance": true
|
|
42
|
+
}
|
|
6
43
|
}
|