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

Fix tabs.mdx animated example #597

Merged
merged 1 commit into from May 24, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
39 changes: 25 additions & 14 deletions website/src/pages/tabs.mdx
Expand Up @@ -763,28 +763,39 @@ With a little composition we can animate the selected tab bar.
const HORIZONTAL_PADDING = 8;
const AnimatedContext = React.createContext();

function AnimatedTabs({ color, ...rest }) {
function AnimatedTabs({ color, children, ...rest }) {
// some state to store the position we want to animate to
const [activeRect, setActiveRect] = useState(null);
const ref = useRef();
const rect = useRect(ref);

return (
// put the function to change the styles on context so an active Tab
// can call it, then style it up
<AnimatedContext.Provider value={setActiveRect}>
{/* make sure to forward props since we're wrapping Tabs */}
<Tabs {...rest} style={{ ...rest.style, position: "relative" }} />
<div
style={{
position: "absolute",
height: 2,
background: color,
transition: "all 300ms ease",
left: activeRect && activeRect.left,
// subtract both sides of horizontal padding to center the div
width: activeRect && activeRect.width - HORIZONTAL_PADDING * 2,
top: activeRect && activeRect.bottom - 2,
}}
/>
<Tabs
{...rest}
ref={ref}
style={{ ...rest.style, position: "relative" }}
>
<div
style={{
position: "absolute",
height: 2,
background: color,
transition: "all 300ms ease",
left:
(activeRect && activeRect.left) -
(rect && rect.left) +
HORIZONTAL_PADDING,
top: (activeRect && activeRect.bottom) - (rect && rect.top),
// subtract both sides of horizontal padding to center the div
width: activeRect && activeRect.width - HORIZONTAL_PADDING * 2,
}}
/>
{children}
</Tabs>
</AnimatedContext.Provider>
);
}
Expand Down