Skip to content

Commit

Permalink
3.0.12
Browse files Browse the repository at this point in the history
  • Loading branch information
hxf31891 committed Dec 30, 2024
1 parent 0abb3f6 commit 1b29f1f
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 18 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-best-gradient-color-picker",
"version": "3.0.11-beta.4",
"version": "3.0.12",
"description": "An easy to use color/gradient picker for React.js",
"type": "module",
"sideEffects": [
Expand Down
2 changes: 1 addition & 1 deletion src/components/Controls.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ const Controls = ({
}
} else {
return (
<div style={{ paddingTop: 12, paddingBottom: 4 }}>
<div style={{ paddingBottom: 4 }}>
<div
style={{
width: '100%',
Expand Down
8 changes: 6 additions & 2 deletions src/components/Picker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ const Picker = ({
locales,
presets,
hideHue,
pickerId,
hideInputs,
hidePresets,
hideOpacity,
hideEyeDrop,
hideControls,
hideInputType,
hideColorGuide,
hidePickerSquare,
hideGradientType,
hideGradientStop,
hideGradientAngle,
Expand All @@ -30,8 +32,8 @@ const Picker = ({
const { isGradient } = usePicker()

return (
<div style={{ userSelect: 'none' }} id="rbgcp-wrapper">
<Square />
<div style={{ userSelect: 'none' }} id={pickerId ?? 'rbgcp-color-picker'}>
{!hidePickerSquare && <Square />}
{!hideControls && (
<Controls
locales={locales}
Expand Down Expand Up @@ -74,4 +76,6 @@ type PickerProps = {
hideGradientStop?: boolean
hideGradientControls?: boolean
locales?: LocalesProps
pickerId?: string
hidePickerSquare?: boolean
}
14 changes: 12 additions & 2 deletions src/components/Presets.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { usePicker } from '../context.js'
import { fakePresets } from '../constants.js'

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

const getPresets = () => {
if (presets?.length > 0) {
Expand All @@ -23,6 +23,15 @@ const Presets = ({ presets = [] }: { presets?: string[] }) => {
}
}

const getBorder = (p: string) => {
if (!p || isDarkMode) return ''
const c = p?.replace(' ', '');
if (c === 'rgba(255,255,255,1)') {
return '1px solid #96959c'
}
return ''
}

return (
<div
style={{
Expand All @@ -39,6 +48,7 @@ const Presets = ({ presets = [] }: { presets?: string[] }) => {
background: value,
borderRadius: 6,
flexShrink: 0,
border: getBorder(value),
}}
// className="rbgcp-preset-color-preview"
/>
Expand All @@ -60,7 +70,7 @@ const Presets = ({ presets = [] }: { presets?: string[] }) => {
borderRadius: 4,
background: p,
marginBottom: 2,
border: p === 'rgba(255,255,255,1)' ? '1px solid #96959c' : '',
border: getBorder(p),
}}
// className="rbgcp-preset-color"
onClick={() => handlePresetClick(p)}
Expand Down
2 changes: 1 addition & 1 deletion src/components/Square.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ const Square = () => {
}, [])

return (
<div style={{ position: 'relative' }}>
<div style={{ position: 'relative', marginBottom: 12 }}>
<div
onMouseUp={stopDragging}
onTouchEnd={stopDragging}
Expand Down
21 changes: 20 additions & 1 deletion src/components/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { objectToString } from '../utils/utils.js'
import { getStyles } from '../styles/styles.js'

export function ColorPicker({
id,
value = 'rgba(175, 51, 242, 1)',
onChange,
hideControls = false,
Expand All @@ -32,10 +33,25 @@ export function ColorPicker({
className,
disableDarkMode = false,
disableLightMode = false,
hidePickerSquare = false,
}: ColorPickerProps) {
const safeValue = objectToString(value)
const isDarkMode =
typeof window === 'undefined' || disableDarkMode
? false
: window.matchMedia('(prefers-color-scheme: dark)').matches ||
disableLightMode
? true
: false
// const contRef = useRef<HTMLDivElement>(null)
const defaultStyles = getStyles(disableDarkMode, disableLightMode)
const defaultStyles = getStyles(isDarkMode)
const pickerId = id
? id
: !disableDarkMode && !disableLightMode
? 'rbgcp-color-picker'
: disableDarkMode
? 'rbgcp-color-picker-light'
: 'rbgcp-color-picker-dark'

return (
<div
Expand All @@ -48,10 +64,12 @@ export function ColorPicker({
onChange={onChange}
squareWidth={width}
squareHeight={height}
isDarkMode={isDarkMode}
hideOpacity={hideOpacity}
defaultStyles={defaultStyles}
>
<Picker
pickerId={pickerId}
hideControls={hideControls}
hideInputs={hideInputs}
hidePresets={hidePresets}
Expand All @@ -67,6 +85,7 @@ export function ColorPicker({
hideGradientAngle={hideGradientAngle}
hideGradientStop={hideGradientStop}
hideGradientControls={hideGradientControls}
hidePickerSquare={hidePickerSquare}
locales={locales}
/>
</PickerContextWrapper>
Expand Down
4 changes: 4 additions & 0 deletions src/context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export default function PickerContextWrapper({
value,
children,
onChange,
isDarkMode,
squareWidth,
hideOpacity,
squareHeight,
Expand Down Expand Up @@ -91,6 +92,7 @@ export default function PickerContextWrapper({
previous,
inputType,
tinyColor,
isDarkMode,
isGradient,
squareWidth,
hideOpacity,
Expand Down Expand Up @@ -132,6 +134,7 @@ type PCWProps = {
hideOpacity: boolean
onChange: (arg0: string) => void
defaultStyles: Styles
isDarkMode: boolean
}

export type PickerContextProps = {
Expand Down Expand Up @@ -161,4 +164,5 @@ export type PickerContextProps = {
color?: string
gradient?: string
}
isDarkMode: boolean
}
2 changes: 2 additions & 0 deletions src/shared/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export type ColorPickerProps = {
id?: string
value?: string
onChange: (value: string) => void
hideControls?: boolean
Expand All @@ -23,6 +24,7 @@ export type ColorPickerProps = {
locales?: LocalesProps
disableDarkMode?: boolean
disableLightMode?: boolean
hidePickerSquare?: boolean
}

export type ColorsProps = {
Expand Down
21 changes: 11 additions & 10 deletions src/styles/styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,23 +179,24 @@ const styles: Styles = {
}
};

export const getStyles = (disableDarkMode: boolean, disableLightMode: boolean) => {
if (typeof window === 'undefined' || disableDarkMode) return styles;
if (window.matchMedia("(prefers-color-scheme: dark)").matches || disableLightMode) {
export const getStyles = (isDarkMode: boolean) => {
if (isDarkMode) {
const mergedStyles = { ...styles }
for (const key in darkStyles) {
if (Object.prototype.hasOwnProperty.call(darkStyles, key)) {
(mergedStyles as Record<string, any>)[key] = {
...(Object.prototype.hasOwnProperty.call(mergedStyles, key) ? (mergedStyles as Record<string, any>)[key] : {}),
;(mergedStyles as Record<string, any>)[key] = {
...(Object.prototype.hasOwnProperty.call(mergedStyles, key)
? (mergedStyles as Record<string, any>)[key]
: {}),
...(darkStyles as Record<string, any>)[key],
};
}
}
}

return mergedStyles;
}
return styles;
};
return mergedStyles
}
return styles
}

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

0 comments on commit 1b29f1f

Please sign in to comment.