Skip to content
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

Allow Icon to receive ref #50816

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open

Allow Icon to receive ref #50816

wants to merge 3 commits into from

Conversation

ravicious
Copy link
Member

@ravicious ravicious commented Jan 7, 2025

There's lots of files changed in this PR, but 205 of them are auto-generated.

I wanted to use an icon with CSSTransition, but I discovered that it's impossible without some kind of a wrapper since Icon doesn't accept ref. Using wrappers for icons is rather annoying, especially in the context of a flex parent, so I decided to refactor Icon to accept ref.

First I had to update references of JSX.Element to React.ReactNode. This is because the new version of Icon returns the return value of forwardRef, which is an object that React can render, but it's not a valid JSX element.

JSX.Element and React.ReactElement are functionally the same type. They can be used interchangeably. They represent the thing that a JSX expression creates. They can't be used to represent all the things that React can render, like strings and numbers. For that, use React.ReactNode. In everyday use, you should use React.ReactNode. You rarely need to use the more specific type of JSX.Element.

https://www.totaltypescript.com/jsx-element-vs-react-reactnode

At the time of writing, this change doesn't require an update to teleport.e.

I ended up not needing to pass a ref to an icon, but I think it's worthwhile to keep this in case someone else runs into this.

@ravicious ravicious added no-changelog Indicates that a PR does not require a changelog entry backport/branch/v17 labels Jan 7, 2025
@avatus
Copy link
Contributor

avatus commented Jan 7, 2025

Ill test this out later today, but so far the code (for Icon) looks fine.

this is probably going to be a nightmare to backport

@ravicious
Copy link
Member Author

this is probably going to be a nightmare to backport

Why though? 🌞


const timeoutMs = 200;

const StyledBroadcast = styled(Broadcast)`
Copy link
Member Author

Choose a reason for hiding this comment

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

This is all a little bit too complex, let me know if you have a better idea on how to demonstrate usage of ref. One thing I like about this example is that we're showing that we can just pass a ref through a styled generated component to the final Icon, without having to change the ref prop name along the way.

Copy link
Member Author

Choose a reason for hiding this comment

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

I replaced it with a simpler example based on https://react.dev/reference/react/useRef#scrolling-an-image-into-view.

Copy link
Contributor

@avatus avatus left a comment

Choose a reason for hiding this comment

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

Works fine for me

useEffect(() => {
const interval = setInterval(() => {
setIsShown(value => !value);
}, timeoutMs * 3);
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this be configurable via prop?

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 it was a real component then sure, but I don't think it matters that much for the story.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport/branch/v17 no-changelog Indicates that a PR does not require a changelog entry size/lg ui
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants