dripsy-variants 1.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/LICENSE +21 -0
- package/README.md +218 -0
- package/dist/index.d.ts +20 -0
- package/dist/index.js +27 -0
- package/package.json +19 -0
- package/src/index.ts +66 -0
- package/tsconfig.json +14 -0
package/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2025 Axel Valles
|
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
ADDED
@@ -0,0 +1,218 @@
|
|
1
|
+
# dripsy-variants
|
2
|
+
|
3
|
+
**Variant utility for Dripsy** – A lightweight helper to build style variants and compound variants using `sx` props.
|
4
|
+
|
5
|
+
> Inspired by Tailwind Variants and styled-system patterns.
|
6
|
+
|
7
|
+
## ✨ Features
|
8
|
+
|
9
|
+
- ✅ Define `base` styles
|
10
|
+
- ✅ Support for multiple `variants`
|
11
|
+
- ✅ Support for `compoundVariants`
|
12
|
+
- ✅ `defaultVariants` handling
|
13
|
+
- ✅ Type-safe with full TypeScript support
|
14
|
+
|
15
|
+
## 📦 Installation
|
16
|
+
|
17
|
+
```bash
|
18
|
+
npm install dripsy-variants
|
19
|
+
# or
|
20
|
+
yarn add dripsy-variants
|
21
|
+
```
|
22
|
+
|
23
|
+
> Make sure you also have `dripsy` installed.
|
24
|
+
|
25
|
+
## 🚀 Usage
|
26
|
+
|
27
|
+
```typescript
|
28
|
+
import { createDripsyVariant } from "dripsy-variants";
|
29
|
+
|
30
|
+
const getButtonStyles = createDripsyVariant({
|
31
|
+
base: {
|
32
|
+
flexDirection: "row",
|
33
|
+
alignItems: "center",
|
34
|
+
gap: 10,
|
35
|
+
},
|
36
|
+
variants: {
|
37
|
+
variant: {
|
38
|
+
primary: { backgroundColor: "#0F0F0F" },
|
39
|
+
secondary: { backgroundColor: "#E8E8E8" },
|
40
|
+
link: { backgroundColor: "transparent" },
|
41
|
+
},
|
42
|
+
size: {
|
43
|
+
md: { py: "$1_5", px: "$2", borderRadius: 16 },
|
44
|
+
sm: { py: "$1", px: "$2", borderRadius: 12 },
|
45
|
+
},
|
46
|
+
disabled: {
|
47
|
+
true: {},
|
48
|
+
false: {},
|
49
|
+
},
|
50
|
+
pressed: {
|
51
|
+
true: {},
|
52
|
+
false: {},
|
53
|
+
},
|
54
|
+
},
|
55
|
+
compoundVariants: [
|
56
|
+
{
|
57
|
+
pressed: ["true"],
|
58
|
+
variant: ["primary"],
|
59
|
+
sx: { backgroundColor: "#454545" },
|
60
|
+
},
|
61
|
+
{
|
62
|
+
pressed: ["true"],
|
63
|
+
variant: ["secondary"],
|
64
|
+
sx: { backgroundColor: "#D3D3D3" },
|
65
|
+
},
|
66
|
+
{
|
67
|
+
disabled: ["true"],
|
68
|
+
variant: ["primary"],
|
69
|
+
sx: { backgroundColor: "#F3F3F3" },
|
70
|
+
},
|
71
|
+
{
|
72
|
+
disabled: ["true"],
|
73
|
+
variant: ["secondary"],
|
74
|
+
sx: { backgroundColor: "#F3F3F3" },
|
75
|
+
},
|
76
|
+
],
|
77
|
+
defaultVariants: {
|
78
|
+
variant: "primary",
|
79
|
+
size: "md",
|
80
|
+
},
|
81
|
+
});
|
82
|
+
|
83
|
+
const getTextStyles = createSxVariant({
|
84
|
+
base: {
|
85
|
+
textAlign: "center",
|
86
|
+
flex: 1,
|
87
|
+
},
|
88
|
+
variants: {
|
89
|
+
variant: {
|
90
|
+
primary: { color: "$white" },
|
91
|
+
secondary: { color: "#2A2E2F" },
|
92
|
+
link: { color: "#2A2E2F" },
|
93
|
+
},
|
94
|
+
size: {
|
95
|
+
md: { variant: "text.$buttonMd" },
|
96
|
+
sm: { variant: "text.$buttonSm" },
|
97
|
+
},
|
98
|
+
disabled: {
|
99
|
+
true: {},
|
100
|
+
false: {},
|
101
|
+
},
|
102
|
+
pressed: {
|
103
|
+
true: {},
|
104
|
+
false: {},
|
105
|
+
},
|
106
|
+
},
|
107
|
+
compoundVariants: [
|
108
|
+
{
|
109
|
+
disabled: ["true"],
|
110
|
+
variant: ["primary"],
|
111
|
+
sx: { color: "#A8A8A8" },
|
112
|
+
},
|
113
|
+
{
|
114
|
+
disabled: ["true"],
|
115
|
+
variant: ["secondary"],
|
116
|
+
sx: { color: "#D3D3D3" },
|
117
|
+
},
|
118
|
+
{
|
119
|
+
disabled: ["true"],
|
120
|
+
variant: ["link"],
|
121
|
+
sx: { color: "#A8A8A8" },
|
122
|
+
},
|
123
|
+
],
|
124
|
+
defaultVariants: {
|
125
|
+
variant: "primary",
|
126
|
+
size: "md",
|
127
|
+
},
|
128
|
+
});
|
129
|
+
|
130
|
+
// Usage
|
131
|
+
export const Button = ({
|
132
|
+
title,
|
133
|
+
onPress = () => {},
|
134
|
+
icon,
|
135
|
+
isLoading = false,
|
136
|
+
disabled = false,
|
137
|
+
sx = {},
|
138
|
+
variant = "primary",
|
139
|
+
size = "md",
|
140
|
+
}: ButtonProps) => {
|
141
|
+
const sxConverter = useSx();
|
142
|
+
const { theme } = useDripsyTheme();
|
143
|
+
|
144
|
+
return (
|
145
|
+
<Pressable
|
146
|
+
disabled={isLoading || disabled}
|
147
|
+
onPress={onPress}
|
148
|
+
style={({ pressed }) =>
|
149
|
+
sxConverter({
|
150
|
+
...getButtonStyles({
|
151
|
+
variant,
|
152
|
+
size,
|
153
|
+
pressed: pressed ? "true" : "false",
|
154
|
+
disabled: disabled ? "true" : "false",
|
155
|
+
}),
|
156
|
+
...sx,
|
157
|
+
})
|
158
|
+
}
|
159
|
+
>
|
160
|
+
{({ pressed }) => {
|
161
|
+
const textStyles = getTextStyles({
|
162
|
+
variant,
|
163
|
+
size,
|
164
|
+
disabled: disabled ? "true" : "false",
|
165
|
+
pressed: pressed ? "true" : "false",
|
166
|
+
});
|
167
|
+
|
168
|
+
const colorToken = textStyles.color as keyof typeof theme.colors;
|
169
|
+
const resolvedColor = theme.colors?.[colorToken] ?? colorToken;
|
170
|
+
|
171
|
+
const renderedIcon = enhanceIconElement(icon, {
|
172
|
+
color: resolvedColor,
|
173
|
+
size: 16,
|
174
|
+
});
|
175
|
+
|
176
|
+
return (
|
177
|
+
<>
|
178
|
+
<View sx={{ width: 20 }}>{isLoading && <Spinner />}</View>
|
179
|
+
<Text sx={textStyles}>{title}</Text>
|
180
|
+
<View sx={{ width: 20 }}>{icon && renderedIcon}</View>
|
181
|
+
</>
|
182
|
+
);
|
183
|
+
}}
|
184
|
+
</Pressable>
|
185
|
+
);
|
186
|
+
};
|
187
|
+
```
|
188
|
+
|
189
|
+
## 🧠 API Reference
|
190
|
+
|
191
|
+
### `createDripsyVariant(config)`
|
192
|
+
|
193
|
+
#### `config.base?: SxProp`
|
194
|
+
|
195
|
+
Base styles applied to all variants.
|
196
|
+
|
197
|
+
#### `config.variants?: Record<string, Record<string, SxProp>>`
|
198
|
+
|
199
|
+
Object containing named variants (like `variant`, `size`, etc.).
|
200
|
+
|
201
|
+
#### `config.defaultVariants?: { [K in keyof Variants]?: keyof Variants[K] }`
|
202
|
+
|
203
|
+
Default values used when no variant prop is passed.
|
204
|
+
|
205
|
+
#### `config.compoundVariants?: CompoundVariant[]`
|
206
|
+
|
207
|
+
List of compound variant conditions to apply extra styles based on a combination of variant values.
|
208
|
+
|
209
|
+
Each `CompoundVariant` is:
|
210
|
+
|
211
|
+
{
|
212
|
+
sx: SxProp;
|
213
|
+
[key: string]: string[]; // matches variant keys and values
|
214
|
+
}
|
215
|
+
|
216
|
+
## 🪪 License
|
217
|
+
|
218
|
+
MIT License © 2025 Axel Valles
|
package/dist/index.d.ts
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
import { SxProp } from "dripsy";
|
2
|
+
type VariantDefinitions = Record<string, Record<string, SxProp>>;
|
3
|
+
type CompoundVariant = {
|
4
|
+
sx: SxProp;
|
5
|
+
} & {
|
6
|
+
[key: string]: string[] | undefined;
|
7
|
+
};
|
8
|
+
type Config<Variants extends VariantDefinitions = VariantDefinitions> = {
|
9
|
+
base?: SxProp;
|
10
|
+
variants?: Variants;
|
11
|
+
compoundVariants?: CompoundVariant[];
|
12
|
+
defaultVariants?: {
|
13
|
+
[K in keyof Variants]?: keyof Variants[K];
|
14
|
+
};
|
15
|
+
};
|
16
|
+
type VariantProps<V extends VariantDefinitions> = {
|
17
|
+
[K in keyof V]?: keyof V[K];
|
18
|
+
};
|
19
|
+
export declare function createDripsyVariant<V extends VariantDefinitions = VariantDefinitions>(config: Config<V>): (props?: VariantProps<V>) => any;
|
20
|
+
export {};
|
package/dist/index.js
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
export function createDripsyVariant(config) {
|
2
|
+
return (props = {}) => {
|
3
|
+
const { base = {}, variants = {}, compoundVariants = [], defaultVariants = {}, } = config;
|
4
|
+
const resolved = [base];
|
5
|
+
Object.keys(variants).forEach((key) => {
|
6
|
+
const selected = props[key] ?? defaultVariants[key];
|
7
|
+
if (selected && variants[key]?.[selected]) {
|
8
|
+
resolved.push(variants[key][selected]);
|
9
|
+
}
|
10
|
+
});
|
11
|
+
for (const cv of compoundVariants) {
|
12
|
+
const match = Object.entries(cv).every(([key, value]) => {
|
13
|
+
if (key === "sx")
|
14
|
+
return true;
|
15
|
+
const propKey = key;
|
16
|
+
const propValue = props[propKey] ?? defaultVariants[propKey];
|
17
|
+
return Array.isArray(value)
|
18
|
+
? value.includes(propValue)
|
19
|
+
: propValue === value;
|
20
|
+
});
|
21
|
+
if (match) {
|
22
|
+
resolved.push(cv.sx);
|
23
|
+
}
|
24
|
+
}
|
25
|
+
return Object.assign({}, ...resolved);
|
26
|
+
};
|
27
|
+
}
|
package/package.json
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
{
|
2
|
+
"name": "dripsy-variants",
|
3
|
+
"version": "1.0.0",
|
4
|
+
"main": "dist/index.js",
|
5
|
+
"types": "dist/index.d.ts",
|
6
|
+
"scripts": {
|
7
|
+
"build": "tsc"
|
8
|
+
},
|
9
|
+
"keywords": [],
|
10
|
+
"author": "axel valles",
|
11
|
+
"license": "MIT",
|
12
|
+
"description": "",
|
13
|
+
"dependencies": {
|
14
|
+
"dripsy": "^4.3.8"
|
15
|
+
},
|
16
|
+
"devDependencies": {
|
17
|
+
"typescript": "^5.8.3"
|
18
|
+
}
|
19
|
+
}
|
package/src/index.ts
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
// lib/createSxVariant.ts
|
2
|
+
import { SxProp } from "dripsy";
|
3
|
+
|
4
|
+
type VariantDefinitions = Record<string, Record<string, SxProp>>;
|
5
|
+
|
6
|
+
type CompoundVariant = {
|
7
|
+
sx: SxProp;
|
8
|
+
[key: string]: string[] | SxProp;
|
9
|
+
};
|
10
|
+
|
11
|
+
type Config<Variants extends VariantDefinitions = VariantDefinitions> = {
|
12
|
+
base?: SxProp;
|
13
|
+
variants?: Variants;
|
14
|
+
compoundVariants?: CompoundVariant[];
|
15
|
+
defaultVariants?: {
|
16
|
+
[K in keyof Variants]?: keyof Variants[K];
|
17
|
+
};
|
18
|
+
};
|
19
|
+
|
20
|
+
type VariantProps<V extends VariantDefinitions> = {
|
21
|
+
[K in keyof V]?: keyof V[K];
|
22
|
+
};
|
23
|
+
|
24
|
+
export function createDripsyVariant<
|
25
|
+
V extends VariantDefinitions = VariantDefinitions
|
26
|
+
>(config: Config<V>) {
|
27
|
+
return (props: VariantProps<V> = {}) => {
|
28
|
+
const {
|
29
|
+
base = {} as SxProp,
|
30
|
+
variants = {} as V,
|
31
|
+
compoundVariants = [],
|
32
|
+
defaultVariants = {} as {
|
33
|
+
[K in keyof V]?: keyof V[K];
|
34
|
+
},
|
35
|
+
} = config;
|
36
|
+
|
37
|
+
const resolved: SxProp[] = [base];
|
38
|
+
|
39
|
+
(Object.keys(variants) as (keyof V)[]).forEach((key) => {
|
40
|
+
const selected = props[key] ?? defaultVariants[key];
|
41
|
+
if (selected && variants[key]?.[selected]) {
|
42
|
+
resolved.push(variants[key][selected]);
|
43
|
+
}
|
44
|
+
});
|
45
|
+
|
46
|
+
for (const cv of compoundVariants) {
|
47
|
+
const match = Object.entries(cv).every(([key, value]) => {
|
48
|
+
if (key === "sx") return true;
|
49
|
+
|
50
|
+
const propKey = key as keyof V;
|
51
|
+
const propValue = props[propKey] ?? defaultVariants[propKey];
|
52
|
+
|
53
|
+
return Array.isArray(value)
|
54
|
+
? value.includes(propValue as string)
|
55
|
+
: propValue === (value as any);
|
56
|
+
});
|
57
|
+
|
58
|
+
if (match) {
|
59
|
+
resolved.push(cv.sx);
|
60
|
+
}
|
61
|
+
}
|
62
|
+
|
63
|
+
// ✅ Fusionar todos los objetos en uno
|
64
|
+
return Object.assign({}, ...resolved);
|
65
|
+
};
|
66
|
+
}
|
package/tsconfig.json
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
{
|
2
|
+
"compilerOptions": {
|
3
|
+
"target": "ES2020",
|
4
|
+
"module": "ESNext",
|
5
|
+
"lib": ["ES2020", "DOM"],
|
6
|
+
"declaration": true,
|
7
|
+
"outDir": "dist",
|
8
|
+
"moduleResolution": "node",
|
9
|
+
"esModuleInterop": true,
|
10
|
+
"strict": true,
|
11
|
+
"skipLibCheck": true
|
12
|
+
},
|
13
|
+
"include": ["src"]
|
14
|
+
}
|