Currently, I am utilizing React and JavaScript for styling on my website. One feature I have implemented is a skip link in the navigation bar that allows users to jump further down the page with a simple click. The challenge I'm facing is that this skip link only becomes noticeable when the tab key is pressed. It is positioned above the site logo in the navbar and requires more space when it appears. To address this, I aim to increase the height of the navbar when the link is visible. My approach involves using a boolean variable named linkHasFocus for conditional CSS styling. Additionally, I have created a function called checkFocus:
const TopNavbar = styled.div`
...
height: ${props => (props.linkHasFocus ? '9rem' : '4rem')};
...
`;
Here is an outline of the checkFocus function:
const checkFocus = () => {
const elem = document.getElementById('skip-link');
if (elem === document.activeElement) {
this.linkHasFocus = true;
}
this.linkHasFocus = false;
return this.linkHasFocus;
};
I pass the checkFocus function to my TopNavbar component (the parent of the skip link component) in the return statement like this:
<TopNavbar linkHasFocus={checkFocus}>
However, I have encountered an issue where the checkFocus function does not update the linkHasFocus variable correctly. My grasp of props and JavaScript concepts may be lacking, so I apologize if there are glaring errors in my implementation.