-
Notifications
You must be signed in to change notification settings - Fork 4.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix unexpected movements, and closing on first click in the color picker #35670
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -11,7 +11,6 @@ import namesPlugin from 'colord/plugins/names'; | |||||||||
*/ | ||||||||||
import { useState, useMemo } from '@wordpress/element'; | ||||||||||
import { settings } from '@wordpress/icons'; | ||||||||||
import { useDebounce } from '@wordpress/compose'; | ||||||||||
import { __ } from '@wordpress/i18n'; | ||||||||||
|
||||||||||
/** | ||||||||||
|
@@ -33,7 +32,6 @@ import { | |||||||||
import { ColorDisplay } from './color-display'; | ||||||||||
import { ColorInput } from './color-input'; | ||||||||||
import { Picker } from './picker'; | ||||||||||
import { useControlledValue } from '../utils/hooks'; | ||||||||||
|
||||||||||
import type { ColorType } from './types'; | ||||||||||
|
||||||||||
|
@@ -59,32 +57,23 @@ const ColorPicker = ( | |||||||||
) => { | ||||||||||
const { | ||||||||||
enableAlpha = false, | ||||||||||
color: colorProp, | ||||||||||
color, | ||||||||||
onChange, | ||||||||||
defaultValue, | ||||||||||
defaultValue = '#fff', | ||||||||||
copyFormat, | ||||||||||
...divProps | ||||||||||
} = useContextSystem( props, 'ColorPicker' ); | ||||||||||
|
||||||||||
const [ color, setColor ] = useControlledValue( { | ||||||||||
onChange, | ||||||||||
value: colorProp, | ||||||||||
defaultValue, | ||||||||||
} ); | ||||||||||
Comment on lines
-69
to
-73
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure if moving away from There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm! I don't understand why we need to move away from I wonder if the fact that we're not memoizing the state updater here is causing any problem since the original updater from gutenberg/packages/components/src/utils/hooks/use-controlled-value.ts Lines 35 to 38 in 8ccd5e1
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The only reason I removed the useControlledValue call was that everything seemed to be working as before without it. It seems on this case it is not needed. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just cc'ing @sarayourfriend who wrote this part of the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The idea behind using <ColorPicker onChange={setColor} /> Should work even though the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This makes sense, thank you Sara! @jorgefilipecosta , would it possible to add the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hey @jorgefilipecosta , just wondering if you've had any time to work on this, or if you plan to anytime soon? Thank you! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @ciampo and @jorgefilipecosta, I was investigating an issue with the color picker and gradients (reported in #36899) and it looks like re-instating There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you @andrewserong , I'll definitely have a look |
||||||||||
|
||||||||||
// Use a safe default value for the color and remove the possibility of `undefined`. | ||||||||||
const safeColordColor = useMemo( () => { | ||||||||||
return color ? colord( color ) : colord( '#fff' ); | ||||||||||
}, [ color ] ); | ||||||||||
|
||||||||||
// Debounce to prevent rapid changes from conflicting with one another. | ||||||||||
const debouncedSetColor = useDebounce( setColor ); | ||||||||||
return color ? colord( color ) : colord( defaultValue ); | ||||||||||
}, [ color, defaultValue ] ); | ||||||||||
|
||||||||||
const handleChange = useCallback( | ||||||||||
( nextValue: Colord ) => { | ||||||||||
debouncedSetColor( nextValue.toHex() ); | ||||||||||
onChange( nextValue.toHex() ); | ||||||||||
}, | ||||||||||
[ debouncedSetColor ] | ||||||||||
[ onChange ] | ||||||||||
); | ||||||||||
|
||||||||||
const [ showInputs, setShowInputs ] = useState< boolean >( false ); | ||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { HslColorPicker, HslaColorPicker } from 'react-colorful'; | ||
import { RgbStringColorPicker, RgbaStringColorPicker } from 'react-colorful'; | ||
import { colord, Colord } from 'colord'; | ||
|
||
/** | ||
|
@@ -15,12 +15,14 @@ interface PickerProps { | |
} | ||
|
||
export const Picker = ( { color, enableAlpha, onChange }: PickerProps ) => { | ||
const Component = enableAlpha ? HslaColorPicker : HslColorPicker; | ||
const hslColor = useMemo( () => color.toHsl(), [ color ] ); | ||
const Component = enableAlpha | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. While I understand why we switched from an object-based to a string-based picker, I don't understand why we switched also from an HSL-based to an RBG-based picker — could you explain why? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The format we use outside the component is hex code which maps directly to RGB just by converting hex to decimal. Since we are changing to a string-based picker why not use the RGB one when it maps more directly? What advantage do we get in using an HSL string-based picker? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, it looks like the only difference would be about the input string format — while the underlying Thank you for explaining! |
||
? RgbaStringColorPicker | ||
: RgbStringColorPicker; | ||
const rgbColor = useMemo( () => color.toRgbString(), [ color ] ); | ||
|
||
return ( | ||
<Component | ||
color={ hslColor } | ||
color={ rgbColor } | ||
onChange={ ( nextColor ) => { | ||
onChange( colord( nextColor ) ); | ||
} } | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should update the README and any potential JSDocs types to add the Default value of the
defaultValue
prop — @jorgefilipecosta would you be able to follow up with this small change?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure I will work on a PR with this change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @jorgefilipecosta , have you had any time yet to work on a PR with this change ? Thank you!