-
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
CustomGradientPicker: Refactor to TypeScript #48929
Conversation
Size Change: +655 B (0%) Total Size: 1.34 MB
ℹ️ View Unchanged
|
packages/components/src/custom-gradient-picker/gradient-bar/index.tsx
Outdated
Show resolved
Hide resolved
5b0663c
to
58661fd
Compare
packages/components/src/custom-gradient-picker/gradient-bar/control-points.tsx
Outdated
Show resolved
Hide resolved
packages/components/src/custom-gradient-picker/gradient-bar/control-points.tsx
Outdated
Show resolved
Hide resolved
packages/components/src/custom-gradient-picker/gradient-bar/control-points.tsx
Outdated
Show resolved
Hide resolved
packages/components/src/custom-gradient-picker/gradient-bar/control-points.tsx
Show resolved
Hide resolved
packages/components/src/custom-gradient-picker/gradient-bar/control-points.tsx
Outdated
Show resolved
Hide resolved
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.
Great job so far, @chad1008 !
This is not an easy one
packages/components/src/custom-gradient-picker/gradient-bar/utils.ts
Outdated
Show resolved
Hide resolved
packages/components/src/custom-gradient-picker/gradient-bar/utils.ts
Outdated
Show resolved
Hide resolved
packages/components/src/custom-gradient-picker/gradient-bar/utils.ts
Outdated
Show resolved
Hide resolved
packages/components/src/custom-gradient-picker/stories/index.tsx
Outdated
Show resolved
Hide resolved
56b15f2
to
a33606e
Compare
Whew, thanks for all of the detailed feedback, as always @ciampo. I think I've addressed everything. There are a couple of unresolved conversations in the thread I had questions or needed feedback on. Beyond that, the conflicts listed are updating the CHANGELOG and addressing some added |
…lPoints` declaration
21dcf47
to
7595d97
Compare
Rebased and incorporated changes. I did need to make one small change (7595d97) to the new types in |
packages/components/src/custom-gradient-picker/gradient-bar/utils.ts
Outdated
Show resolved
Hide resolved
export function getHorizontalRelativeGradientPosition( | ||
mouseXcoordinate: number, | ||
containerElement: HTMLDivElement | null | ||
): number | undefined; |
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 think that your initial implementation was correct — we don't really need to advertise this type for the getHorizontalRelativeGradientPosition
function
export function getHorizontalRelativeGradientPosition( | |
mouseXcoordinate: number, | |
containerElement: HTMLDivElement | null | |
): number | undefined; |
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.
This comment probably got lost in the mountain of threads we've accumulated, but I think we do actually need the union override.
If you don't get the same error, or if there's a better solution, I'm happy to investigate further!
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 definitely missed it 😓
Anyway, I took a look and I see why we'd need the third overload. Although I also looked at the TS error, and I think that a better solution would be to tweak the early return check in the GradientBar
, which would then allows us to remove that third function overload:
diff --git a/packages/components/src/custom-gradient-picker/gradient-bar/index.tsx b/packages/components/src/custom-gradient-picker/gradient-bar/index.tsx
index 526ee53bff..225a5fb757 100644
--- a/packages/components/src/custom-gradient-picker/gradient-bar/index.tsx
+++ b/packages/components/src/custom-gradient-picker/gradient-bar/index.tsx
@@ -96,15 +96,15 @@ export default function CustomGradientBar( {
const onMouseEnterAndMove: MouseEventHandler< HTMLDivElement > = (
event
) => {
+ if ( ! gradientMarkersContainerDomRef.current ) {
+ return;
+ }
+
const insertPosition = getHorizontalRelativeGradientPosition(
event.clientX,
gradientMarkersContainerDomRef.current
);
- if ( insertPosition === undefined ) {
- return;
- }
-
// If the insert point is close to an existing control point don't show it.
if (
controlPoints.some( ( { position } ) => {
diff --git a/packages/components/src/custom-gradient-picker/gradient-bar/utils.ts b/packages/components/src/custom-gradient-picker/gradient-bar/utils.ts
index 6e9417328c..88daf2b2c1 100644
--- a/packages/components/src/custom-gradient-picker/gradient-bar/utils.ts
+++ b/packages/components/src/custom-gradient-picker/gradient-bar/utils.ts
@@ -178,10 +178,6 @@ export function getHorizontalRelativeGradientPosition(
mouseXcoordinate: number,
containerElement: null
): undefined;
-export function getHorizontalRelativeGradientPosition(
- mouseXcoordinate: number,
- containerElement: HTMLDivElement | null
-): number | undefined;
export function getHorizontalRelativeGradientPosition(
mouseXCoordinate: number,
containerElement: HTMLDivElement | null
What do you think?
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.
Co-authored-by: Marco Ciampini <marco.ciampo@gmail.com>
…l with `false` default
Flaky tests detected in 465a59d. 🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/4448835977
|
… to better align with `ColorPickerPopover` types
Applied most recent feedback. I believe there is only one remaining open item. I did introduce one additional small change. tl;dr: |
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.
This was a tough one! Thank you @chad1008 for the patience in addressing all the feedback. I think we did a good job and introduced very few runtime changes 🚀
Good to merge once this last comment is addressed
Last change implemented, waiting on CI before merging... |
What?
Refactor
CustomGradientPicker
component to TypeScriptPart of #35744
Why?
The refactor to TypeScript has many benefits (auto-generated docs, static linting and error prevention, better IDE experience). See #35744 for more details
How?
Followed the steps in the TypeScript migration guide
Testing Instructions
npm ci
, as a newDefinitelyTyped
package has been added for thegradientParser
library this component relies on.Notes:
DefinitelyTyped
values for thegradientParser
library. This led to what felt like pretty liberal use of type assertions. Many of these were to avoid conflicts between union members. Anyway, I know there's a bunch of them and I'm very open to feedback them... so anywhere you see me using anas
, please take it with a grain of salt and don't hesitate to redirect me if you see a better option.