-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
ColorPalette: Ensure text label contrast checking works with CSS variables #47373
Merged
Merged
Changes from 3 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
0c5c13b
ColorPalette: Ensure text label contrast checking works with CSS vari…
t-hamano c4cd604
use `useEffect` to get normalized color value
t-hamano ebae2d7
Update changelog
t-hamano 9327bc9
Try to detect actual color from rgba
t-hamano 2eec7a6
Resolve conflict
t-hamano 98bf1dc
Use function to get the composite background color
t-hamano 75f9c62
Rewrite useEffect with useCallback
t-hamano 0489602
Merge branch 'trunk' into fix/colorpalette-contrast-check
t-hamano 30ae6bc
Don't consider transparent color
t-hamano 011e099
Don't use ref, simplify normalizeColorValue() function
t-hamano dc62b0f
Add JSDoc
t-hamano 2b845f7
Add unit tests
t-hamano b020ea2
Fix conflicts
t-hamano e225bf5
Refactor unit tests
t-hamano 2945982
Refactor unit test
t-hamano 969bf03
Merge branch 'trunk' into fix/colorpalette-contrast-check
t-hamano File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Since this effect runs only when
value
changes and has an early return whencustomColorPaletteRef.current
, I think that there could be an issue with this scenario:disableCustomColors
prop set totrue
CustomColorPickerDropdown
is not rendered, which means thatcustomColorPaletteRef.current
isnull
setNormalizedColorValue
doesn't get called, because of the early return on the ref checkdisableCustomColors
gets set tofalse
customColorPaletteRef.current
gets updated to reference the HTML element, but that doesn't cause the component to re-render (that's how a React ref works)normalizedColorValue
at this point could be out of sync, because theuseEffect
hook that is in charge of calculating thenormalizedColorValue
won't be called until thevalue
prop changesWe could do some manual testing (or unit testing) to check against this behaviour. A couple of approaches to fix this potential issue could be to:
disableCustomColors
as a dependency of theuseEffect
hook (and usedisableCustomColors
in the hook internal check) — this is more of an indirect check, and it's not my favourite option because it's not clear why we'd need to check for itcustomColorPaletteRef
in the component state, by using a ref callback +useState
. This method would ensure that, when the ref updates, the component re-renders — thus forcing theuseEffect
hook to run and keepnormalizeColorValue
in syncThere 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.
Thank you, I have confirmed that the re-rendering will not take place even if
disableCustomColors
is changed. Below is an example where the text labels remain in an unintended color becausenormalizedColorValue
is not executed whendisableCustomColors
is switched:I may not have understood your advice correctly, but in 75f9c62, it appears to have worked correctly by rewriting
useEffect
withuseCallback
. Is this implementation appropriate?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.
I can't see any example, maybe the attachment didn't upload correctly?
Excuses if I wasn't clear enough with my advice.
Your implementation looks good and works well in my tests — but I think we can clean it up further:
normalizeColorValue
color value to accept directly an element (HTMLElement | null
), instead of aref
customColorPaletteRef
variableWhat do you think?
It would also be great if we added a JSDoc comment to the
normalizeColorValue
utility function, to specify that currently its purpose is to "transform" a CSS variable used as background color into the color value itself.