And so useTabs is born.
I took the already existing code and transformed it into a hook that returns a function to set the highlight to the current ref or event, and returns an object containing the styling for the wrapper element.
const wrapperRef = useRef(null);
const { highlightStyles, setHighlight } = useTabs({
container: wrapperRef,
});
By passing a event or a ref to the setHightlight()
function, the hightlightStyles
object gets updated with the height, width and position of the referenced element. You can then pass the highlightStyles to any HTML element to add some styling and have it automatically move to the ref element. In this example I am using TailwindCSS.<div
style={highlightStyles}
className="bg-gray-500 bg-opacity-10 rounded-sm"
/>
And that’s it. You can view the full example on the Github page.