Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Switch AccessibleButton and derivatives to using forwardRef #12054

Merged
merged 22 commits into from
Dec 21, 2023

Conversation

t3chguy
Copy link
Member

@t3chguy t3chguy commented Dec 14, 2023

For https://github.com/element-hq/customer-retainer/issues/157
Requires #12053

This is to enable AccessibleTooltipButton using Compound's Tooltip which requires this.

image


This change is marked as an internal change (Task), so will not be included in the changelog.

This prevented us from switching to `forwardRef` in a bunch of places
due to it behaving different with & without strict mode.

Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>
Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>
@t3chguy t3chguy added the T-Task Refactoring, enabling or disabling functionality, other engineering tasks label Dec 14, 2023
@t3chguy t3chguy self-assigned this Dec 14, 2023
Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>
Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>
Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>
Copy link
Member

@richvdh richvdh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there's a lot changing here - much of which, at first glance, doesn't appear to match the PR description (ie, it is not about using forwardRef).

Could you either break up the PR, or help me as a reviewer understand what I'm looking at by making a clear description of it?

@t3chguy
Copy link
Member Author

t3chguy commented Dec 15, 2023

Could you either break up the PR, or help me as a reviewer understand what I'm looking at by making a clear description of it?

This is a broken up PR, this is as small as it can get, given how many places use & extend AccessibleButton

it is not about using forwardRef

Just updated branch which knocked out some bits unrelated to forwardRef from the merged base PR. Everything remaining relates to changing AccessibleButton and derivates from a custom ref prop to forwardRef for compatibility with Compound/Radix - this means updating any call sites rendering those components where a custom ref prop was passed and types to make tsc happier.

@richvdh
Copy link
Member

richvdh commented Dec 15, 2023

This is a broken up PR, this is as small as it can get, given how many places use & extend AccessibleButton

Well, you seem to have found a way to make it smaller since I first looked at it, so thank you for that.

@t3chguy
Copy link
Member Author

t3chguy commented Dec 15, 2023

@richvdh yeah its an unfortunate side effect of squash merging combined with PR stacking =(

Copy link
Member

@richvdh richvdh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks generally plausible, but I have some requests mostly around better documentation.

src/components/views/elements/AccessibleButton.tsx Outdated Show resolved Hide resolved
src/components/views/elements/AccessibleButton.tsx Outdated Show resolved Hide resolved
src/components/views/elements/AccessibleTooltipButton.tsx Outdated Show resolved Hide resolved
src/components/views/elements/AccessibleTooltipButton.tsx Outdated Show resolved Hide resolved
src/components/views/messages/IBodyProps.ts Outdated Show resolved Hide resolved
src/components/views/rooms/ReadReceiptGroup.tsx Outdated Show resolved Hide resolved
src/contexts/MatrixClientContext.tsx Outdated Show resolved Hide resolved
Co-authored-by: Richard van der Hoff <1389908+richvdh@users.noreply.github.com>
@t3chguy
Copy link
Member Author

t3chguy commented Dec 20, 2023

@richvdh I have split things out into #12072

… t3chguy/cr/157-2

# Conflicts:
#	src/components/structures/InteractiveAuth.tsx
#	src/components/views/auth/InteractiveAuthEntryComponents.tsx
#	src/components/views/dialogs/DeactivateAccountDialog.tsx
#	src/components/views/dialogs/InteractiveAuthDialog.tsx
#	src/components/views/elements/AccessibleButton.tsx
#	src/components/views/elements/AccessibleTooltipButton.tsx
#	src/components/views/messages/CallEvent.tsx
#	src/components/views/rooms/ReadReceiptMarker.tsx
@t3chguy
Copy link
Member Author

t3chguy commented Dec 20, 2023

Another split out #12075

… t3chguy/cr/157-2

Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>

# Conflicts:
#	src/accessibility/context_menu/ContextMenuButton.tsx
#	src/accessibility/context_menu/ContextMenuTooltipButton.tsx
#	src/components/views/audio_messages/PlayPauseButton.tsx
#	src/components/views/elements/AccessibleButton.tsx
#	src/components/views/elements/AccessibleTooltipButton.tsx
#	src/components/views/elements/LearnMore.tsx
Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>
Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>
Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>
Copy link
Member

@richvdh richvdh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@@ -101,22 +100,26 @@ interface RenderedElementProps extends React.InputHTMLAttributes<Element> {
* as a button. Identifies the element as a button, setting proper tab
* indexing and keyboard activation behavior.
*
* If a ref is passed, it will be forwarded to the rendered element as specified using the `element` prop.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* If a ref is passed, it will be forwarded to the rendered element as specified using the `element` prop.
* If a ref is provided, it will be forwarded to the rendered element (as specified using the `element` prop).
* If `element` is a DOM element, the ref will be updated to point to the rendered DOM node.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If element is a DOM element, the ref will be updated to point to the rendered DOM node.

element can only be a string though, not a DOM element. It can be a name of a DOM element but it also can't be anything else (other than undefined where the value is div) so not sure what this 2nd line is trying to say.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My understanding is that element can either be an intrinsic DOM element (div, button, or whatever), or a React component (MyFancyButton). (See https://www.typescriptlang.org/docs/handbook/jsx.html#type-checking, etc)

  • If it's a react component, we can't really say much about what happens to the ref other than that it is forwarded to that component for it to do with as it chooses. (It might set it to 42, for all we know.)
  • However, if it's an intrinsic DOM element, then we can be much more helpful: the act of "forwarding" it means that ref.content will be updated to point to the rendered DOM node corresponding to that element. This is what the user of AccessibleButton needs to know, and is the reason that I wanted to add that second sentence.

Now: if you're saying that element can't in fact be a React component and can only ever be an intrinsic DOM element, then we can clarify this further (since "forwarding" is entirely an implementation detail of AccessibleButton). Maybe something like:

Suggested change
* If a ref is passed, it will be forwarded to the rendered element as specified using the `element` prop.
* If a ref is provided, it will be updated to point to the rendered DOM node (which will normally be a `div`,
* unless overridden via the `element` property).

Copy link
Member Author

@t3chguy t3chguy Dec 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

The T type generic can only be one of these keys:

image
(continued...)

So you cannot use a React component here

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yup I misread the typescript. (#include "I shouldn't have to reverse-engineer typescript types.rant")

@t3chguy t3chguy added this pull request to the merge queue Dec 21, 2023
Merged via the queue into develop with commit f632e21 Dec 21, 2023
19 checks passed
@t3chguy t3chguy deleted the t3chguy/cr/157-2 branch December 21, 2023 09:16
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
T-Task Refactoring, enabling or disabling functionality, other engineering tasks
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants