From 8bb2ccb2dad2bb42790e49e3d8b44f5911accf34 Mon Sep 17 00:00:00 2001 From: jeslage Date: Sun, 13 Aug 2023 10:51:42 +0200 Subject: [PATCH] feat: remove useColor hook --- README.md | 45 ++++++++------------ src/components/ColorPicker/helper.ts | 52 ++++++++++++------------ src/components/Saturation/Saturation.tsx | 36 ++++++++-------- src/hooks/useColor.ts | 16 -------- src/index.tsx | 14 +++---- 5 files changed, 67 insertions(+), 96 deletions(-) delete mode 100644 src/hooks/useColor.ts diff --git a/README.md b/README.md index 6addae1..832fb66 100644 --- a/README.md +++ b/README.md @@ -16,16 +16,14 @@ yarn add react-pick-color ## Usage ```js -import React, { useState } from "react"; -import ColorPicker from "react-pick-color"; +import React, { useState } from 'react' +import ColorPicker from 'react-pick-color' const App = () => { - const [color, setColor] = useState("#fff"); + const [color, setColor] = useState('#fff') - return ( - setColor(color.hex)} /> - ); -}; + return setColor(color.hex)} /> +} ``` ## Options @@ -85,23 +83,23 @@ You can add a custom theme for styling the colorpicker component or choose one f **Custom Theme** ```js -import ColorPicker from "react-pick-color"; +import ColorPicker from 'react-pick-color' const ThemedColorPicker = () => { return ( - ); -}; + ) +} ``` **Predefined Theme** @@ -109,20 +107,11 @@ const ThemedColorPicker = () => { `react-pick-color` exports a `dark` and a `light` theme. ```js -import ColorPicker, { themes } from "react-pick-color"; +import ColorPicker, { themes } from 'react-pick-color' const ThemedColorPicker = () => { - return ; -}; -``` - -## Hooks - -```js -import { useColor } from "react-pick-color"; - -// A color as a hex string or rgba/hsla object. Will return a color object. -const { hex, rgb, hsl, hsv, alpha } = useColor("#fff"); + return +} ``` ## Roadmap diff --git a/src/components/ColorPicker/helper.ts b/src/components/ColorPicker/helper.ts index a1427e5..d92b2d9 100644 --- a/src/components/ColorPicker/helper.ts +++ b/src/components/ColorPicker/helper.ts @@ -1,20 +1,20 @@ -import tinycolor from "tinycolor2"; +import tinycolor from 'tinycolor2' -import { Color, ColorObject, ColorCombination } from "../../types"; +import { Color, ColorObject, ColorCombination } from '../../types' export const initColor = (col?: Color): ColorObject => { - const color = tinycolor(col); + const color = tinycolor(col) - const isValidColor = color.isValid(); + const isValidColor = color.isValid() if (!isValidColor) { return { - hex: "#000000", + hex: '#000000', rgb: { r: 0, g: 0, b: 0, a: 1 }, hsl: { h: 0, s: 0, l: 0, a: 1 }, hsv: { h: 0, s: 0, v: 0, a: 1 }, alpha: 1, - }; + } } return { @@ -23,43 +23,43 @@ export const initColor = (col?: Color): ColorObject => { hsl: color.toHsl(), hsv: color.toHsv(), alpha: color.getAlpha(), - }; -}; + } +} export const getColorCombination = ( color: ColorObject, comb: ColorCombination | ColorCombination[] ): any[] => { - const { hex } = color; + const { hex } = color - const col = tinycolor(hex); + const col = tinycolor(hex) - const all = typeof comb === "string" ? [comb] : comb; - const combs: any[] = []; + const all = typeof comb === 'string' ? [comb] : comb + const combs: any[] = [] all.forEach((item) => { - if (item === "analogous") { - return col.analogous().forEach((t) => combs.push(t.toHexString())); + if (item === 'analogous') { + return col.analogous().forEach((t) => combs.push(t.toHexString())) } - if (item === "monochromatic") { - return col.monochromatic().forEach((t) => combs.push(t.toHexString())); + if (item === 'monochromatic') { + return col.monochromatic().forEach((t) => combs.push(t.toHexString())) } - if (item === "splitcomplement") { - return col.splitcomplement().forEach((t) => combs.push(t.toHexString())); + if (item === 'splitcomplement') { + return col.splitcomplement().forEach((t) => combs.push(t.toHexString())) } - if (item === "tetrad") { - return col.tetrad().forEach((t) => combs.push(t.toHexString())); + if (item === 'tetrad') { + return col.tetrad().forEach((t) => combs.push(t.toHexString())) } - if (item === "triad") { - return col.triad().forEach((t) => combs.push(t.toHexString())); + if (item === 'triad') { + return col.triad().forEach((t) => combs.push(t.toHexString())) } - return combs.push(col.complement().toHexString()); - }); + return combs.push(col.complement().toHexString()) + }) - return combs; -}; + return combs +} diff --git a/src/components/Saturation/Saturation.tsx b/src/components/Saturation/Saturation.tsx index 71dd3c4..e7cfe66 100644 --- a/src/components/Saturation/Saturation.tsx +++ b/src/components/Saturation/Saturation.tsx @@ -1,14 +1,14 @@ -import React, { useCallback } from "react"; +import React, { useCallback } from 'react' -import { HslColor, HsvColor } from "../../types"; -import usePosition, { Position } from "../../hooks/usePosition"; +import { HslColor, HsvColor } from '../../types' +import usePosition, { Position } from '../../hooks/usePosition' -import * as styles from "./Saturation.style"; +import * as styles from './Saturation.style' export type SaturationProps = { - hsl: HslColor; - onChange?: (color: HsvColor) => void; -}; + hsl: HslColor + onChange?: (color: HsvColor) => void +} const Saturation = ({ hsl, onChange }: SaturationProps) => { const handleMove = useCallback( @@ -17,14 +17,14 @@ const Saturation = ({ hsl, onChange }: SaturationProps) => { onChange({ ...hsl, s: left, - v: 1 - top + v: 1 - top, }), [onChange] - ); + ) const { ref, handleStart } = usePosition({ - onMove: handleMove - }); + onMove: handleMove, + }) return (
{ onMouseDown={handleStart} > -
-
+
+
- ); -}; + ) +} -export default React.memo(Saturation) as typeof Saturation; +export default React.memo(Saturation) as typeof Saturation diff --git a/src/hooks/useColor.ts b/src/hooks/useColor.ts deleted file mode 100644 index b980a75..0000000 --- a/src/hooks/useColor.ts +++ /dev/null @@ -1,16 +0,0 @@ -import React from "react"; - -import { Color, ColorObject } from "../types"; -import { initColor } from "../components/ColorPicker/helper"; - -const useColor = (color: Color) => { - const [col, setCol] = React.useState(initColor(color)); - - React.useEffect(() => { - setCol(initColor(color)); - }, [color]); - - return col; -}; - -export default useColor; diff --git a/src/index.tsx b/src/index.tsx index 2e1e7a3..1b341ea 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -1,7 +1,6 @@ -import ColorPicker from "./components/ColorPicker/ColorPicker"; -import themes from "./themes"; -import useColor from "./hooks/useColor"; -import { initColor } from "./components/ColorPicker/helper"; +import ColorPicker from './components/ColorPicker/ColorPicker' +import themes from './themes' +import { initColor } from './components/ColorPicker/helper' import { HslColor, HsvColor, @@ -11,11 +10,10 @@ import { Color, ColorCombination, ColorObject, -} from "./types"; +} from './types' export { themes, - useColor, initColor, ColorPicker, HslColor, @@ -26,5 +24,5 @@ export { Color, ColorCombination, ColorObject, -}; -export default ColorPicker; +} +export default ColorPicker