Skip to content

Commit

Permalink
feat: new TrackFocus component with active status
Browse files Browse the repository at this point in the history
  • Loading branch information
Wagner3UB committed Mar 5, 2024
1 parent 02df644 commit 83e7296
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 10 deletions.
22 changes: 15 additions & 7 deletions src/components/ItaliaTheme/AppExtras/TrackFocus.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ const CLASS_NAME_MOUSE_ACTIVE = 'active';

const TrackFocus = () => {
const [usingMouse, setUsingMouse] = useState(false);
const [activeElement, setActiveElement] = useState(null);

useEffect(() => {
const handleEvent = (e) => {
setUsingMouse(e.type === 'mousedown' ? true : false);
setUsingMouse(e.type === 'mousedown');
};

document.addEventListener('keydown', handleEvent);
Expand All @@ -24,23 +25,30 @@ const TrackFocus = () => {
useEffect(() => {
const handleFocusChange = (e) => {
if (e.target) {
const isLinkOrButton =
e.target.tagName === 'A' || e.target.tagName === 'BUTTON';

if (usingMouse) {
e.target.classList.add(CLASS_NAME_MOUSE_FOCUS);
e.target.classList.add(CLASS_NAME_MOUSE_ACTIVE);
if (isLinkOrButton) {
if (activeElement && activeElement !== e.target) {
activeElement.classList.remove(CLASS_NAME_MOUSE_ACTIVE);
}
e.target.classList.add(CLASS_NAME_MOUSE_ACTIVE);
setActiveElement(e.target);
}
e.target.setAttribute(DATA_FOCUS_MOUSE, true);
} else {
e.target.classList.remove(CLASS_NAME_MOUSE_FOCUS);
e.target.classList.remove(CLASS_NAME_MOUSE_ACTIVE);
e.target.setAttribute(DATA_FOCUS_MOUSE, false);
e.target.removeAttribute(DATA_FOCUS_MOUSE);
}
}
};

const handleFocusOut = (e) => {
if (e.target) {
e.target.classList.remove(CLASS_NAME_MOUSE_FOCUS);
e.target.classList.remove(CLASS_NAME_MOUSE_ACTIVE);
e.target.setAttribute(DATA_FOCUS_MOUSE, false);
e.target.removeAttribute(DATA_FOCUS_MOUSE);
}
};

Expand All @@ -51,7 +59,7 @@ const TrackFocus = () => {
document.removeEventListener('focusin', handleFocusChange);
document.removeEventListener('focusout', handleFocusOut);
};
}, [usingMouse]);
}, [usingMouse, activeElement]);

return null;
};
Expand Down
3 changes: 1 addition & 2 deletions src/components/ItaliaTheme/MegaMenu/MegaMenu.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@ const MegaMenu = ({ item, pathname }) => {
href={item.linkUrl === '' ? '/' : null}
item={item.linkUrl[0]?.['@id'] ? item.linkUrl[0] : '#'}
tag={UniversalLink}
active={isItemActive}
data-element={item.id_lighthouse}
role="menuitem"
>
Expand Down Expand Up @@ -239,7 +238,7 @@ const MegaMenu = ({ item, pathname }) => {
}

return (
<NavItem tag="li" className="megamenu" active={isItemActive} role="none">
<NavItem tag="li" className="megamenu" role="none">
<UncontrolledDropdown
nav
inNavbar
Expand Down
1 change: 0 additions & 1 deletion src/components/ItaliaTheme/MenuSecondary/MenuSecondary.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ const MenuSecondary = ({ pathname }) => {
<NavLink
href={url === '' ? '/' : flattenToAppURL(url)}
tag={UniversalLink}
active={isMenuActive(url)}
data-element={item.id_lighthouse}
role="menuitem"
>
Expand Down
15 changes: 15 additions & 0 deletions src/theme/ItaliaTheme/Components/_navigation.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
.public-ui {
.navbar {
.navbar-collapsable {
.navbar-nav {
li {
a.nav-link {
&.focus--mouse {
border-color: unset !important;
}
}
}
}
}
}
}
1 change: 1 addition & 0 deletions src/theme/site.scss
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
@import 'ItaliaTheme/Components/sideMenu';
@import 'ItaliaTheme/Components/loginAgid';
@import 'ItaliaTheme/Components/pager';
@import 'ItaliaTheme/Components/navigation';
@import 'ItaliaTheme/Blocks/subblocks-edit';
@import 'ItaliaTheme/Blocks/listing';
@import 'ItaliaTheme/Blocks/tableOfContents';
Expand Down

0 comments on commit 83e7296

Please sign in to comment.