-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Allow Icon to receive ref #50816
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/** | ||
* Teleport | ||
* Copyright (C) 2025 Gravitational, Inc. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
import { useEffect, useRef, useState } from 'react'; | ||
import { CSSTransition } from 'react-transition-group'; | ||
import styled from 'styled-components'; | ||
|
||
import { Broadcast } from './Icons/Broadcast'; | ||
|
||
export default { | ||
// Nest stories under Icon/Icon, so that Icon/Icons which lists all icons is the first story. | ||
title: 'Design/Icon/Icon', | ||
}; | ||
|
||
export const WithRef = () => { | ||
const [isShown, setIsShown] = useState(true); | ||
const nodeRef = useRef(null); | ||
|
||
useEffect(() => { | ||
const interval = setInterval(() => { | ||
setIsShown(value => !value); | ||
}, timeoutMs * 3); | ||
|
||
return () => { | ||
clearInterval(interval); | ||
}; | ||
}, []); | ||
|
||
return ( | ||
// This can be done with pure CSS, it's implemented like this just to show how Icon interacts | ||
// with ref. | ||
<CSSTransition | ||
nodeRef={nodeRef} | ||
in={isShown} | ||
timeout={timeoutMs} | ||
classNames="node" | ||
> | ||
<StyledBroadcast ref={nodeRef} /> | ||
</CSSTransition> | ||
); | ||
}; | ||
|
||
const timeoutMs = 200; | ||
|
||
const StyledBroadcast = styled(Broadcast)` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
&.node-enter { | ||
opacity: 0; | ||
} | ||
|
||
&.node-enter-active, | ||
&.node-enter-done { | ||
opacity: 1; | ||
transition: opacity ${timeoutMs}ms; | ||
} | ||
|
||
&.node-exit { | ||
opacity: 1; | ||
} | ||
|
||
&.node-exit-active, | ||
&.node-exit-done { | ||
opacity: 0; | ||
transition: opacity ${timeoutMs}ms; | ||
} | ||
`; |
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.
Should this be configurable via prop?
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.
If it was a real component then sure, but I don't think it matters that much for the story.