react-native-color-picker-palette 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 +340 -0
- package/package.json +70 -0
- package/src/core/hooks/index.ts +2 -0
- package/src/core/hooks/useColor.ts +93 -0
- package/src/core/hooks/useComponentLayout.ts +38 -0
- package/src/core/index.ts +19 -0
- package/src/core/services/ColorService.ts +338 -0
- package/src/core/services/index.ts +1 -0
- package/src/core/types/index.ts +211 -0
- package/src/core/utils/clamp.ts +16 -0
- package/src/core/utils/format.ts +59 -0
- package/src/core/utils/index.ts +8 -0
- package/src/full/components/Alpha.tsx +221 -0
- package/src/full/components/ColorPicker.tsx +206 -0
- package/src/full/components/Fields/HexField.tsx +125 -0
- package/src/full/components/Fields/RgbFields.tsx +192 -0
- package/src/full/components/Fields/index.tsx +70 -0
- package/src/full/components/Hue.tsx +188 -0
- package/src/full/components/RectangleSaturation.tsx +203 -0
- package/src/full/components/Saturation.tsx +258 -0
- package/src/full/components/Thumb.tsx +47 -0
- package/src/full/components/Value.tsx +192 -0
- package/src/full/components/index.ts +8 -0
- package/src/full/index.ts +69 -0
- package/src/index.ts +19 -0
- package/src/lite/components/Alpha.tsx +228 -0
- package/src/lite/components/ColorPicker.tsx +209 -0
- package/src/lite/components/Fields/HexField.tsx +103 -0
- package/src/lite/components/Fields/RgbFields.tsx +138 -0
- package/src/lite/components/Fields/index.tsx +53 -0
- package/src/lite/components/Hue.tsx +192 -0
- package/src/lite/components/RectangleSaturation.tsx +238 -0
- package/src/lite/components/Saturation.tsx +289 -0
- package/src/lite/components/Thumb.tsx +47 -0
- package/src/lite/components/Value.tsx +201 -0
- package/src/lite/components/index.ts +8 -0
- package/src/lite/index.ts +75 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Sabri Ghazali
|
|
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,340 @@
|
|
|
1
|
+
# React Native Color Palette
|
|
2
|
+
|
|
3
|
+
A versatile, customizable color picker for React Native with two flavors:
|
|
4
|
+
|
|
5
|
+
- **Full Version**: Uses `react-native-svg` for smooth gradients and precise rendering
|
|
6
|
+
- **Lite Version**: Zero external dependencies, uses pure React Native Views
|
|
7
|
+
|
|
8
|
+
<p align="center">
|
|
9
|
+
<img src="assets/rectangle-full-version.png" alt="Rectangle Picker" width="280" />
|
|
10
|
+
<img src="assets/circle-full-version.png" alt="Circle Picker" width="280" />
|
|
11
|
+
</p>
|
|
12
|
+
|
|
13
|
+
## Features
|
|
14
|
+
|
|
15
|
+
- Circular color wheel (hue + saturation)
|
|
16
|
+
- Rectangle saturation/value picker
|
|
17
|
+
- Hue slider bar
|
|
18
|
+
- Alpha slider with checkerboard background
|
|
19
|
+
- Value/Brightness slider
|
|
20
|
+
- HEX and RGB input fields
|
|
21
|
+
- Fully customizable (size, colors, visibility)
|
|
22
|
+
- TypeScript support
|
|
23
|
+
- iOS and Android compatible
|
|
24
|
+
|
|
25
|
+
## Installation
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
# Using npm
|
|
29
|
+
npm install react-native-color-picker-palette
|
|
30
|
+
|
|
31
|
+
# Using yarn
|
|
32
|
+
yarn add react-native-color-picker-palette
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
### Full Version (with react-native-svg)
|
|
36
|
+
|
|
37
|
+
If you want to use the full version with smooth SVG gradients:
|
|
38
|
+
|
|
39
|
+
```bash
|
|
40
|
+
npm install react-native-svg
|
|
41
|
+
cd ios && pod install
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
### Lite Version (zero dependencies)
|
|
45
|
+
|
|
46
|
+
The lite version works out of the box - no additional dependencies needed!
|
|
47
|
+
|
|
48
|
+
## Usage
|
|
49
|
+
|
|
50
|
+
### Basic Usage (Full Version)
|
|
51
|
+
|
|
52
|
+
```tsx
|
|
53
|
+
import React from 'react';
|
|
54
|
+
import { View } from 'react-native';
|
|
55
|
+
import { ColorPicker, useColor } from 'react-native-color-picker-palette';
|
|
56
|
+
|
|
57
|
+
function MyColorPicker() {
|
|
58
|
+
const [color, setColor] = useColor('#FF0000');
|
|
59
|
+
|
|
60
|
+
return (
|
|
61
|
+
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
|
|
62
|
+
<ColorPicker
|
|
63
|
+
color={color}
|
|
64
|
+
onChange={setColor}
|
|
65
|
+
onChangeComplete={(c) => console.log('Selected:', c.hex)}
|
|
66
|
+
/>
|
|
67
|
+
</View>
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
### Lite Version (Zero Dependencies)
|
|
73
|
+
|
|
74
|
+
```tsx
|
|
75
|
+
import React from 'react';
|
|
76
|
+
import { View } from 'react-native';
|
|
77
|
+
import { ColorPicker, useColor } from 'react-native-color-picker-palette/lite';
|
|
78
|
+
|
|
79
|
+
function MyColorPicker() {
|
|
80
|
+
const [color, setColor] = useColor('#FF0000');
|
|
81
|
+
|
|
82
|
+
return (
|
|
83
|
+
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
|
|
84
|
+
<ColorPicker
|
|
85
|
+
color={color}
|
|
86
|
+
onChange={setColor}
|
|
87
|
+
onChangeComplete={(c) => console.log('Selected:', c.hex)}
|
|
88
|
+
/>
|
|
89
|
+
</View>
|
|
90
|
+
);
|
|
91
|
+
}
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
## Picker Variants
|
|
95
|
+
|
|
96
|
+
### Rectangle Variant (default)
|
|
97
|
+
|
|
98
|
+
```tsx
|
|
99
|
+
<ColorPicker variant="rectangle" ... />
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
- Saturation/Value rectangle with current hue
|
|
103
|
+
- **Hue slider** is always visible (required to change hue)
|
|
104
|
+
- **Value slider** is NOT shown (value is controlled in the rectangle)
|
|
105
|
+
|
|
106
|
+
### Circle Variant
|
|
107
|
+
|
|
108
|
+
```tsx
|
|
109
|
+
<ColorPicker variant="circle" ... />
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
- Color wheel with hue around the circle and saturation from center
|
|
113
|
+
- **Value/Brightness slider** is shown (to control brightness)
|
|
114
|
+
- `hideHue` prop works to hide the hue slider
|
|
115
|
+
|
|
116
|
+
## API Reference
|
|
117
|
+
|
|
118
|
+
### ColorPicker Props
|
|
119
|
+
|
|
120
|
+
| Prop | Type | Default | Variant | Description |
|
|
121
|
+
|------|------|---------|---------|-------------|
|
|
122
|
+
| `color` | `IColor` | **required** | Both | Current color value |
|
|
123
|
+
| `onChange` | `(color: IColor) => void` | **required** | Both | Called on every color change during interaction |
|
|
124
|
+
| `onChangeComplete` | `(color: IColor) => void` | - | Both | Called when interaction ends (finger lifted) |
|
|
125
|
+
| `variant` | `'circle' \| 'rectangle'` | `'rectangle'` | - | Picker variant |
|
|
126
|
+
| `width` | `number` | `250` | Both | Width of the picker (diameter for circle) |
|
|
127
|
+
| `barHeight` | `number` | `10` | Both | Height of slider bars |
|
|
128
|
+
| `thumbSize` | `number` | `24` | Both | Size of thumb indicators |
|
|
129
|
+
| `hideHue` | `boolean` | `false` | Circle only | Hide the hue slider (ignored for rectangle) |
|
|
130
|
+
| `hideAlpha` | `boolean` | `false` | Both | Hide the alpha slider |
|
|
131
|
+
| `hidePreview` | `boolean` | `false` | Both | Hide the color preview |
|
|
132
|
+
| `hideInput` | `boolean` | `false` | Both | Hide the input fields |
|
|
133
|
+
| `disabled` | `boolean` | `false` | Both | Disable all interactions |
|
|
134
|
+
|
|
135
|
+
### Variant-Specific Behavior
|
|
136
|
+
|
|
137
|
+
| Feature | Rectangle | Circle |
|
|
138
|
+
|---------|-----------|--------|
|
|
139
|
+
| Hue selection | Via Hue slider (always shown) | Via color wheel angle |
|
|
140
|
+
| Saturation selection | X-axis of rectangle | Distance from center |
|
|
141
|
+
| Value/Brightness selection | Y-axis of rectangle | Via Value slider |
|
|
142
|
+
| Hue slider | Always visible | Optional (use `hideHue`) |
|
|
143
|
+
| Value slider | Not shown | Always shown |
|
|
144
|
+
|
|
145
|
+
### IColor Type
|
|
146
|
+
|
|
147
|
+
```typescript
|
|
148
|
+
interface IColor {
|
|
149
|
+
hex: string; // e.g., "#FF0000" or "#FF0000FF" with alpha
|
|
150
|
+
rgb: IRGB;
|
|
151
|
+
hsv: IHSV;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
interface IRGB {
|
|
155
|
+
r: number; // 0-255
|
|
156
|
+
g: number; // 0-255
|
|
157
|
+
b: number; // 0-255
|
|
158
|
+
a: number; // 0-1
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
interface IHSV {
|
|
162
|
+
h: number; // 0-360
|
|
163
|
+
s: number; // 0-100
|
|
164
|
+
v: number; // 0-100
|
|
165
|
+
a: number; // 0-1
|
|
166
|
+
}
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
### Hooks
|
|
170
|
+
|
|
171
|
+
#### useColor
|
|
172
|
+
|
|
173
|
+
```tsx
|
|
174
|
+
const [color, setColor] = useColor('#FF0000');
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
Manages color state with automatic conversion from hex string.
|
|
178
|
+
|
|
179
|
+
#### useColorWithCallback
|
|
180
|
+
|
|
181
|
+
```tsx
|
|
182
|
+
const {
|
|
183
|
+
color,
|
|
184
|
+
setColor,
|
|
185
|
+
handleChange,
|
|
186
|
+
handleChangeComplete,
|
|
187
|
+
} = useColorWithCallback('#FF0000', (color) => {
|
|
188
|
+
console.log('Color changed:', color.hex);
|
|
189
|
+
});
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
Manages color state with a callback for change completion.
|
|
193
|
+
|
|
194
|
+
### ColorService
|
|
195
|
+
|
|
196
|
+
Utility for color conversions:
|
|
197
|
+
|
|
198
|
+
```tsx
|
|
199
|
+
import { ColorService } from 'react-native-color-picker-palette';
|
|
200
|
+
|
|
201
|
+
// Create color from hex
|
|
202
|
+
const color = ColorService.fromHex('#FF5500');
|
|
203
|
+
|
|
204
|
+
// Create color from RGB
|
|
205
|
+
const color = ColorService.fromRgb({ r: 255, g: 85, b: 0, a: 1 });
|
|
206
|
+
|
|
207
|
+
// Create color from HSV
|
|
208
|
+
const color = ColorService.fromHsv({ h: 20, s: 100, v: 100, a: 1 });
|
|
209
|
+
|
|
210
|
+
// Get string representations
|
|
211
|
+
ColorService.toRgbString(color.rgb); // "rgb(255, 85, 0)"
|
|
212
|
+
ColorService.toRgbaString(color.rgb); // "rgba(255, 85, 0, 1)"
|
|
213
|
+
ColorService.toHslString(color.hsv); // "hsl(20, 100%, 50%)"
|
|
214
|
+
```
|
|
215
|
+
|
|
216
|
+
## Individual Components
|
|
217
|
+
|
|
218
|
+
For custom layouts, you can use individual components:
|
|
219
|
+
|
|
220
|
+
```tsx
|
|
221
|
+
import {
|
|
222
|
+
Saturation, // Circular color wheel (for circle variant)
|
|
223
|
+
RectangleSaturation, // Rectangle saturation/value picker
|
|
224
|
+
Hue, // Hue slider
|
|
225
|
+
Alpha, // Alpha slider
|
|
226
|
+
Value, // Brightness slider (for circle variant)
|
|
227
|
+
Fields, // HEX + RGB inputs
|
|
228
|
+
HexField, // HEX input only
|
|
229
|
+
RgbFields, // RGB inputs only
|
|
230
|
+
Thumb, // Thumb indicator
|
|
231
|
+
} from 'react-native-color-picker-palette';
|
|
232
|
+
```
|
|
233
|
+
|
|
234
|
+
### Example: Custom Layout
|
|
235
|
+
|
|
236
|
+
```tsx
|
|
237
|
+
import React from 'react';
|
|
238
|
+
import { View } from 'react-native';
|
|
239
|
+
import {
|
|
240
|
+
RectangleSaturation,
|
|
241
|
+
Hue,
|
|
242
|
+
Alpha,
|
|
243
|
+
useColor,
|
|
244
|
+
} from 'react-native-color-picker-palette';
|
|
245
|
+
|
|
246
|
+
function CustomColorPicker() {
|
|
247
|
+
const [color, setColor] = useColor('#FF0000');
|
|
248
|
+
|
|
249
|
+
return (
|
|
250
|
+
<View style={{ padding: 20 }}>
|
|
251
|
+
<RectangleSaturation
|
|
252
|
+
color={color}
|
|
253
|
+
onChange={setColor}
|
|
254
|
+
width={300}
|
|
255
|
+
height={200}
|
|
256
|
+
thumbSize={28}
|
|
257
|
+
/>
|
|
258
|
+
<View style={{ height: 20 }} />
|
|
259
|
+
<Hue
|
|
260
|
+
color={color}
|
|
261
|
+
onChange={setColor}
|
|
262
|
+
barHeight={12}
|
|
263
|
+
thumbSize={24}
|
|
264
|
+
/>
|
|
265
|
+
<View style={{ height: 10 }} />
|
|
266
|
+
<Alpha
|
|
267
|
+
color={color}
|
|
268
|
+
onChange={setColor}
|
|
269
|
+
barHeight={12}
|
|
270
|
+
thumbSize={24}
|
|
271
|
+
/>
|
|
272
|
+
</View>
|
|
273
|
+
);
|
|
274
|
+
}
|
|
275
|
+
```
|
|
276
|
+
|
|
277
|
+
## Full vs Lite: When to Use Which?
|
|
278
|
+
|
|
279
|
+
### Use Full Version When:
|
|
280
|
+
- You need the smoothest possible gradients
|
|
281
|
+
- You're already using `react-native-svg` in your project
|
|
282
|
+
- Visual quality is the top priority
|
|
283
|
+
|
|
284
|
+
### Use Lite Version When:
|
|
285
|
+
- You want zero additional dependencies
|
|
286
|
+
- You're using Expo and want to avoid native modules
|
|
287
|
+
- Bundle size is a concern
|
|
288
|
+
- You need a quick setup without pod install
|
|
289
|
+
|
|
290
|
+
## Screenshots
|
|
291
|
+
|
|
292
|
+
### Full Version (with react-native-svg)
|
|
293
|
+
|
|
294
|
+
| Rectangle | Circle |
|
|
295
|
+
|:---------:|:------:|
|
|
296
|
+
| <img src="assets/rectangle-full-version.png" alt="Rectangle Full" width="300" /> | <img src="assets/circle-full-version.png" alt="Circle Full" width="300" /> |
|
|
297
|
+
|
|
298
|
+
### Lite Version (zero dependencies)
|
|
299
|
+
|
|
300
|
+
| Rectangle | Circle |
|
|
301
|
+
|:---------:|:------:|
|
|
302
|
+
| <img src="assets/rectangle-lite-version.png" alt="Rectangle Lite" width="300" /> | <img src="assets/circle-lite-version.png" alt="Circle Lite" width="300" /> |
|
|
303
|
+
|
|
304
|
+
## TypeScript Support
|
|
305
|
+
|
|
306
|
+
This package is written in TypeScript and includes full type definitions:
|
|
307
|
+
|
|
308
|
+
```tsx
|
|
309
|
+
import type {
|
|
310
|
+
IColor,
|
|
311
|
+
IRGB,
|
|
312
|
+
IHSV,
|
|
313
|
+
ColorModel,
|
|
314
|
+
IColorPickerProps,
|
|
315
|
+
ISaturationProps,
|
|
316
|
+
IRectangleSaturationProps,
|
|
317
|
+
IHueProps,
|
|
318
|
+
IAlphaProps,
|
|
319
|
+
IValueProps,
|
|
320
|
+
IThumbProps,
|
|
321
|
+
IFieldsProps,
|
|
322
|
+
ILayout,
|
|
323
|
+
} from 'react-native-color-picker-palette';
|
|
324
|
+
```
|
|
325
|
+
|
|
326
|
+
## Contributing
|
|
327
|
+
|
|
328
|
+
Contributions are welcome! Please feel free to submit a Pull Request.
|
|
329
|
+
|
|
330
|
+
## License
|
|
331
|
+
|
|
332
|
+
MIT License - see the [LICENSE](LICENSE) file for details.
|
|
333
|
+
|
|
334
|
+
## Author
|
|
335
|
+
|
|
336
|
+
Sabri Ghazali
|
|
337
|
+
|
|
338
|
+
---
|
|
339
|
+
|
|
340
|
+
Made with React Native
|
package/package.json
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "react-native-color-picker-palette",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "A versatile color picker for React Native with two flavors: Full (with react-native-svg) and Lite (zero dependencies)",
|
|
5
|
+
"main": "src/index.ts",
|
|
6
|
+
"module": "src/index.ts",
|
|
7
|
+
"types": "src/index.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"import": "./src/index.ts",
|
|
11
|
+
"require": "./src/index.ts",
|
|
12
|
+
"types": "./src/index.ts"
|
|
13
|
+
},
|
|
14
|
+
"./lite": {
|
|
15
|
+
"import": "./src/lite/index.ts",
|
|
16
|
+
"require": "./src/lite/index.ts",
|
|
17
|
+
"types": "./src/lite/index.ts"
|
|
18
|
+
}
|
|
19
|
+
},
|
|
20
|
+
"files": [
|
|
21
|
+
"src",
|
|
22
|
+
"README.md",
|
|
23
|
+
"LICENSE"
|
|
24
|
+
],
|
|
25
|
+
"keywords": [
|
|
26
|
+
"react-native",
|
|
27
|
+
"color-picker",
|
|
28
|
+
"color-wheel",
|
|
29
|
+
"hsv",
|
|
30
|
+
"rgb",
|
|
31
|
+
"hex",
|
|
32
|
+
"color-palette",
|
|
33
|
+
"react-native-svg",
|
|
34
|
+
"zero-dependencies"
|
|
35
|
+
],
|
|
36
|
+
"author": "Sabri Ghazali",
|
|
37
|
+
"license": "MIT",
|
|
38
|
+
"repository": {
|
|
39
|
+
"type": "git",
|
|
40
|
+
"url": "https://github.com/SabriGhazali/react-native-color-palette.git"
|
|
41
|
+
},
|
|
42
|
+
"bugs": {
|
|
43
|
+
"url": "https://github.com/SabriGhazali/react-native-color-palette/issues"
|
|
44
|
+
},
|
|
45
|
+
"homepage": "https://github.com/SabriGhazali/react-native-color-palette#readme",
|
|
46
|
+
"peerDependencies": {
|
|
47
|
+
"react": ">=16.8.0",
|
|
48
|
+
"react-native": ">=0.60.0",
|
|
49
|
+
"react-native-svg": ">=12.0.0"
|
|
50
|
+
},
|
|
51
|
+
"peerDependenciesMeta": {
|
|
52
|
+
"react-native-svg": {
|
|
53
|
+
"optional": true
|
|
54
|
+
}
|
|
55
|
+
},
|
|
56
|
+
"devDependencies": {
|
|
57
|
+
"@types/react": "^18.0.0",
|
|
58
|
+
"@types/react-native": "^0.72.0",
|
|
59
|
+
"react": "^18.2.0",
|
|
60
|
+
"react-native": "^0.72.0",
|
|
61
|
+
"react-native-svg": "^13.0.0",
|
|
62
|
+
"typescript": "^5.0.0"
|
|
63
|
+
},
|
|
64
|
+
"scripts": {
|
|
65
|
+
"typecheck": "tsc --noEmit"
|
|
66
|
+
},
|
|
67
|
+
"engines": {
|
|
68
|
+
"node": ">=16.0.0"
|
|
69
|
+
}
|
|
70
|
+
}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { useState, useEffect, useCallback } from 'react';
|
|
2
|
+
import type { IColor } from '../types';
|
|
3
|
+
import { ColorService } from '../services';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Hook for managing color state
|
|
7
|
+
*
|
|
8
|
+
* @param initialColor - Initial color as hex string (e.g., '#FF0000')
|
|
9
|
+
* @returns Tuple of [color, setColor] - Current color object and setter function
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```tsx
|
|
13
|
+
* const [color, setColor] = useColor('#FF0000');
|
|
14
|
+
*
|
|
15
|
+
* return (
|
|
16
|
+
* <ColorPicker
|
|
17
|
+
* color={color}
|
|
18
|
+
* onChange={setColor}
|
|
19
|
+
* />
|
|
20
|
+
* );
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
export function useColor(
|
|
24
|
+
initialColor: string
|
|
25
|
+
): [IColor, React.Dispatch<React.SetStateAction<IColor>>] {
|
|
26
|
+
const [color, setColor] = useState<IColor>(() =>
|
|
27
|
+
ColorService.convert('hex', initialColor)
|
|
28
|
+
);
|
|
29
|
+
|
|
30
|
+
// Update color when initialColor prop changes
|
|
31
|
+
useEffect(() => {
|
|
32
|
+
setColor(ColorService.convert('hex', initialColor));
|
|
33
|
+
}, [initialColor]);
|
|
34
|
+
|
|
35
|
+
return [color, setColor];
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Hook for managing color state with change complete callback
|
|
40
|
+
*
|
|
41
|
+
* @param initialColor - Initial color as hex string
|
|
42
|
+
* @param onChangeComplete - Callback when color selection is complete
|
|
43
|
+
* @returns Object with color state and handlers
|
|
44
|
+
*
|
|
45
|
+
* @example
|
|
46
|
+
* ```tsx
|
|
47
|
+
* const {
|
|
48
|
+
* color,
|
|
49
|
+
* handleChange,
|
|
50
|
+
* handleChangeComplete
|
|
51
|
+
* } = useColorWithCallback('#FF0000', (color) => {
|
|
52
|
+
* console.log('Final color:', color.hex);
|
|
53
|
+
* });
|
|
54
|
+
*
|
|
55
|
+
* return (
|
|
56
|
+
* <ColorPicker
|
|
57
|
+
* color={color}
|
|
58
|
+
* onChange={handleChange}
|
|
59
|
+
* onChangeComplete={handleChangeComplete}
|
|
60
|
+
* />
|
|
61
|
+
* );
|
|
62
|
+
* ```
|
|
63
|
+
*/
|
|
64
|
+
export function useColorWithCallback(
|
|
65
|
+
initialColor: string,
|
|
66
|
+
onChangeComplete?: (color: IColor) => void
|
|
67
|
+
): {
|
|
68
|
+
color: IColor;
|
|
69
|
+
setColor: React.Dispatch<React.SetStateAction<IColor>>;
|
|
70
|
+
handleChange: (color: IColor) => void;
|
|
71
|
+
handleChangeComplete: (color: IColor) => void;
|
|
72
|
+
} {
|
|
73
|
+
const [color, setColor] = useColor(initialColor);
|
|
74
|
+
|
|
75
|
+
const handleChange = useCallback((newColor: IColor) => {
|
|
76
|
+
setColor(newColor);
|
|
77
|
+
}, []);
|
|
78
|
+
|
|
79
|
+
const handleChangeComplete = useCallback(
|
|
80
|
+
(newColor: IColor) => {
|
|
81
|
+
setColor(newColor);
|
|
82
|
+
onChangeComplete?.(newColor);
|
|
83
|
+
},
|
|
84
|
+
[onChangeComplete]
|
|
85
|
+
);
|
|
86
|
+
|
|
87
|
+
return {
|
|
88
|
+
color,
|
|
89
|
+
setColor,
|
|
90
|
+
handleChange,
|
|
91
|
+
handleChangeComplete,
|
|
92
|
+
};
|
|
93
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { useState, useCallback } from 'react';
|
|
2
|
+
import type { LayoutChangeEvent } from 'react-native';
|
|
3
|
+
import type { ILayout } from '../types';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Hook for tracking component layout dimensions
|
|
7
|
+
*
|
|
8
|
+
* @returns Tuple of [layout, onLayout] - Current layout and handler
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```tsx
|
|
12
|
+
* const [layout, onLayout] = useComponentLayout();
|
|
13
|
+
*
|
|
14
|
+
* return (
|
|
15
|
+
* <View onLayout={onLayout}>
|
|
16
|
+
* <Text>Width: {layout.width}</Text>
|
|
17
|
+
* </View>
|
|
18
|
+
* );
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
export function useComponentLayout(): [
|
|
22
|
+
ILayout,
|
|
23
|
+
(event: LayoutChangeEvent) => void
|
|
24
|
+
] {
|
|
25
|
+
const [layout, setLayout] = useState<ILayout>({
|
|
26
|
+
width: 0,
|
|
27
|
+
height: 0,
|
|
28
|
+
x: 0,
|
|
29
|
+
y: 0,
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
const onLayout = useCallback((event: LayoutChangeEvent) => {
|
|
33
|
+
const { width, height, x, y } = event.nativeEvent.layout;
|
|
34
|
+
setLayout({ width, height, x, y });
|
|
35
|
+
}, []);
|
|
36
|
+
|
|
37
|
+
return [layout, onLayout];
|
|
38
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
// Core exports - shared between full and lite versions
|
|
2
|
+
export { ColorService } from './services';
|
|
3
|
+
export { useColor, useColorWithCallback, useComponentLayout } from './hooks';
|
|
4
|
+
export { clamp, padZero, toHexString, round, safeParseFloat, safeParseInt } from './utils';
|
|
5
|
+
export type {
|
|
6
|
+
IColor,
|
|
7
|
+
IRGB,
|
|
8
|
+
IHSV,
|
|
9
|
+
ColorModel,
|
|
10
|
+
IColorPickerProps,
|
|
11
|
+
ISaturationProps,
|
|
12
|
+
IRectangleSaturationProps,
|
|
13
|
+
IHueProps,
|
|
14
|
+
IAlphaProps,
|
|
15
|
+
IValueProps,
|
|
16
|
+
IThumbProps,
|
|
17
|
+
IFieldsProps,
|
|
18
|
+
ILayout,
|
|
19
|
+
} from './types';
|