Skip to content

Commit 1b29f1f

Browse files
committed
3.0.12
1 parent 0abb3f6 commit 1b29f1f

File tree

9 files changed

+58
-18
lines changed

9 files changed

+58
-18
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-best-gradient-color-picker",
3-
"version": "3.0.11-beta.4",
3+
"version": "3.0.12",
44
"description": "An easy to use color/gradient picker for React.js",
55
"type": "module",
66
"sideEffects": [

src/components/Controls.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ const Controls = ({
185185
}
186186
} else {
187187
return (
188-
<div style={{ paddingTop: 12, paddingBottom: 4 }}>
188+
<div style={{ paddingBottom: 4 }}>
189189
<div
190190
style={{
191191
width: '100%',

src/components/Picker.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@ const Picker = ({
1313
locales,
1414
presets,
1515
hideHue,
16+
pickerId,
1617
hideInputs,
1718
hidePresets,
1819
hideOpacity,
1920
hideEyeDrop,
2021
hideControls,
2122
hideInputType,
2223
hideColorGuide,
24+
hidePickerSquare,
2325
hideGradientType,
2426
hideGradientStop,
2527
hideGradientAngle,
@@ -30,8 +32,8 @@ const Picker = ({
3032
const { isGradient } = usePicker()
3133

3234
return (
33-
<div style={{ userSelect: 'none' }} id="rbgcp-wrapper">
34-
<Square />
35+
<div style={{ userSelect: 'none' }} id={pickerId ?? 'rbgcp-color-picker'}>
36+
{!hidePickerSquare && <Square />}
3537
{!hideControls && (
3638
<Controls
3739
locales={locales}
@@ -74,4 +76,6 @@ type PickerProps = {
7476
hideGradientStop?: boolean
7577
hideGradientControls?: boolean
7678
locales?: LocalesProps
79+
pickerId?: string
80+
hidePickerSquare?: boolean
7781
}

src/components/Presets.tsx

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { usePicker } from '../context.js'
55
import { fakePresets } from '../constants.js'
66

77
const Presets = ({ presets = [] }: { presets?: string[] }) => {
8-
const { value, onChange, handleChange, squareWidth } = usePicker()
8+
const { value, onChange, handleChange, squareWidth, isDarkMode } = usePicker()
99

1010
const getPresets = () => {
1111
if (presets?.length > 0) {
@@ -23,6 +23,15 @@ const Presets = ({ presets = [] }: { presets?: string[] }) => {
2323
}
2424
}
2525

26+
const getBorder = (p: string) => {
27+
if (!p || isDarkMode) return ''
28+
const c = p?.replace(' ', '');
29+
if (c === 'rgba(255,255,255,1)') {
30+
return '1px solid #96959c'
31+
}
32+
return ''
33+
}
34+
2635
return (
2736
<div
2837
style={{
@@ -39,6 +48,7 @@ const Presets = ({ presets = [] }: { presets?: string[] }) => {
3948
background: value,
4049
borderRadius: 6,
4150
flexShrink: 0,
51+
border: getBorder(value),
4252
}}
4353
// className="rbgcp-preset-color-preview"
4454
/>
@@ -60,7 +70,7 @@ const Presets = ({ presets = [] }: { presets?: string[] }) => {
6070
borderRadius: 4,
6171
background: p,
6272
marginBottom: 2,
63-
border: p === 'rgba(255,255,255,1)' ? '1px solid #96959c' : '',
73+
border: getBorder(p),
6474
}}
6575
// className="rbgcp-preset-color"
6676
onClick={() => handlePresetClick(p)}

src/components/Square.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ const Square = () => {
8888
}, [])
8989

9090
return (
91-
<div style={{ position: 'relative' }}>
91+
<div style={{ position: 'relative', marginBottom: 12 }}>
9292
<div
9393
onMouseUp={stopDragging}
9494
onTouchEnd={stopDragging}

src/components/index.tsx

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { objectToString } from '../utils/utils.js'
88
import { getStyles } from '../styles/styles.js'
99

1010
export function ColorPicker({
11+
id,
1112
value = 'rgba(175, 51, 242, 1)',
1213
onChange,
1314
hideControls = false,
@@ -32,10 +33,25 @@ export function ColorPicker({
3233
className,
3334
disableDarkMode = false,
3435
disableLightMode = false,
36+
hidePickerSquare = false,
3537
}: ColorPickerProps) {
3638
const safeValue = objectToString(value)
39+
const isDarkMode =
40+
typeof window === 'undefined' || disableDarkMode
41+
? false
42+
: window.matchMedia('(prefers-color-scheme: dark)').matches ||
43+
disableLightMode
44+
? true
45+
: false
3746
// const contRef = useRef<HTMLDivElement>(null)
38-
const defaultStyles = getStyles(disableDarkMode, disableLightMode)
47+
const defaultStyles = getStyles(isDarkMode)
48+
const pickerId = id
49+
? id
50+
: !disableDarkMode && !disableLightMode
51+
? 'rbgcp-color-picker'
52+
: disableDarkMode
53+
? 'rbgcp-color-picker-light'
54+
: 'rbgcp-color-picker-dark'
3955

4056
return (
4157
<div
@@ -48,10 +64,12 @@ export function ColorPicker({
4864
onChange={onChange}
4965
squareWidth={width}
5066
squareHeight={height}
67+
isDarkMode={isDarkMode}
5168
hideOpacity={hideOpacity}
5269
defaultStyles={defaultStyles}
5370
>
5471
<Picker
72+
pickerId={pickerId}
5573
hideControls={hideControls}
5674
hideInputs={hideInputs}
5775
hidePresets={hidePresets}
@@ -67,6 +85,7 @@ export function ColorPicker({
6785
hideGradientAngle={hideGradientAngle}
6886
hideGradientStop={hideGradientStop}
6987
hideGradientControls={hideGradientControls}
88+
hidePickerSquare={hidePickerSquare}
7089
locales={locales}
7190
/>
7291
</PickerContextWrapper>

src/context.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export default function PickerContextWrapper({
1616
value,
1717
children,
1818
onChange,
19+
isDarkMode,
1920
squareWidth,
2021
hideOpacity,
2122
squareHeight,
@@ -91,6 +92,7 @@ export default function PickerContextWrapper({
9192
previous,
9293
inputType,
9394
tinyColor,
95+
isDarkMode,
9496
isGradient,
9597
squareWidth,
9698
hideOpacity,
@@ -132,6 +134,7 @@ type PCWProps = {
132134
hideOpacity: boolean
133135
onChange: (arg0: string) => void
134136
defaultStyles: Styles
137+
isDarkMode: boolean
135138
}
136139

137140
export type PickerContextProps = {
@@ -161,4 +164,5 @@ export type PickerContextProps = {
161164
color?: string
162165
gradient?: string
163166
}
167+
isDarkMode: boolean
164168
}

src/shared/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export type ColorPickerProps = {
2+
id?: string
23
value?: string
34
onChange: (value: string) => void
45
hideControls?: boolean
@@ -23,6 +24,7 @@ export type ColorPickerProps = {
2324
locales?: LocalesProps
2425
disableDarkMode?: boolean
2526
disableLightMode?: boolean
27+
hidePickerSquare?: boolean
2628
}
2729

2830
export type ColorsProps = {

src/styles/styles.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -179,23 +179,24 @@ const styles: Styles = {
179179
}
180180
};
181181

182-
export const getStyles = (disableDarkMode: boolean, disableLightMode: boolean) => {
183-
if (typeof window === 'undefined' || disableDarkMode) return styles;
184-
if (window.matchMedia("(prefers-color-scheme: dark)").matches || disableLightMode) {
182+
export const getStyles = (isDarkMode: boolean) => {
183+
if (isDarkMode) {
185184
const mergedStyles = { ...styles }
186185
for (const key in darkStyles) {
187186
if (Object.prototype.hasOwnProperty.call(darkStyles, key)) {
188-
(mergedStyles as Record<string, any>)[key] = {
189-
...(Object.prototype.hasOwnProperty.call(mergedStyles, key) ? (mergedStyles as Record<string, any>)[key] : {}),
187+
;(mergedStyles as Record<string, any>)[key] = {
188+
...(Object.prototype.hasOwnProperty.call(mergedStyles, key)
189+
? (mergedStyles as Record<string, any>)[key]
190+
: {}),
190191
...(darkStyles as Record<string, any>)[key],
191-
};
192+
}
192193
}
193194
}
194195

195-
return mergedStyles;
196-
}
197-
return styles;
198-
};
196+
return mergedStyles
197+
}
198+
return styles
199+
}
199200

200201
export const colorTypeBtnStyles = (selected: boolean, styles: Styles): React.CSSProperties => {
201202
if (selected) {

0 commit comments

Comments
 (0)