This repository has been archived on 2022-10-07. You can view files and clone it, but cannot push or open issues or pull requests.
skynet-webportal/packages/dashboard-v2/src/components/Tabs/ActiveTabIndicator.js

41 lines
1.1 KiB
JavaScript
Raw Normal View History

2022-02-18 08:20:47 +00:00
import { useEffect, useState } from "react";
import PropTypes from "prop-types";
import styled from "styled-components";
import { useWindowSize } from "react-use";
const Wrapper = styled.div.attrs({
2022-02-18 08:20:47 +00:00
className: "absolute left-0 bottom-0 w-full h-0.5 bg-palette-200",
})``;
const Indicator = styled.div.attrs({
2022-02-18 08:20:47 +00:00
className: "absolute h-0.5 bottom-0 bg-primary duration-200 ease-in-out",
})`
will-change: left, width;
2022-02-18 08:20:47 +00:00
`;
export const ActiveTabIndicator = ({ tabRef }) => {
2022-02-18 08:20:47 +00:00
const [position, setPosition] = useState(0);
const [width, setWidth] = useState(0);
const { width: windowWidth } = useWindowSize();
useEffect(() => {
if (!tabRef?.current) {
2022-02-18 08:20:47 +00:00
return;
}
2022-02-18 08:20:47 +00:00
const { offsetLeft, offsetWidth } = tabRef.current;
setPosition(offsetLeft);
setWidth(offsetWidth);
}, [tabRef, windowWidth]);
return (
<Wrapper>
<Indicator style={{ left: position, width: `${width}px` }} />
</Wrapper>
2022-02-18 08:20:47 +00:00
);
};
ActiveTabIndicator.propTypes = {
tabRef: PropTypes.oneOfType([PropTypes.func, PropTypes.shape({ current: PropTypes.elementType })]),
};