-
Notifications
You must be signed in to change notification settings - Fork 26
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
feat(icon-button): exposes ref prop #199
Conversation
if (typeof ref === 'function') { | ||
ref(buttonRef.current); | ||
} |
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 we may want to use useImperativeHandle
here:
useImperativeHandle(ref, () => buttonRef.current)
That way the ref
passed as props below can continue to use the internal ref value, whether it was passed from the parent as a ref or functional ref, or not.
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.
What is the difference between the imperativeHandle and what is implemented in the PR? The TS compiler really REALLY does not like: useImperativeHandle(ref, () => buttonRef.current)
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.
From what i see useImperativeHandle
is more suited to provide a subset of (in this case) <HTMLButton>
properties to the forwardRef like "focus". I would think we want to expose the full ref, which is what is implemented.
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.
ah, nevermind... i got this to compile, changes inbound and down.
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.
FWIW, I was able to get it working with this as well:
function IconButton({
icon,
label,
tooltipPlacement = 'auto',
className,
...other
}: IconButtonProps, ref: React.Ref<HTMLButtonElement>) {
const buttonRef = useRef<HTMLButtonElement>(null);
useImperativeHandle(ref, () => buttonRef.current as HTMLButtonElement)
return (...)
}
What you have is probably fine too.
IconButton.propTypes = { | ||
icon: PropTypes.string, | ||
label: PropTypes.string, | ||
tooltipPlayemnt: PropTypes.string, | ||
buttonRef: PropTypes.any | ||
}; |
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 don't care about propTypes
here as I find them pretty useless, but others have complained when they were removed. cc @schne324
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.
buttonRef was actually not exposed properly w/ prop types and they were being problematic with forwardRef + Typescript, they become essentially extraneous with TS and interfaces
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.
also: tooltipPlayemnt
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 put them back, however they were not even properly enforcing required props.
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.
Put them back, the rest of the library has them so for now (even though I think they're pointless) let's keep them
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.
word. i'll fix them up to enforce the required props n stuff
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.
Added the PropTypes object back.
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.
Approving, but I'd like @schne324's to weigh in on removing the propTypes
as well.
@stephenmathieson iirc you had some concerns with removing them. Thoughts here? |
Only reason I suggest we keep them around is for non-TypeScript users. I agree that they're pointless if you're using TS, but for JS apps, they're pretty handy. Not a big deal to me either way tho 🤷 |
valid point. |
update: icon button ref was just internal for the tooltip, adds ability to use ref freely