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(SideNav): add screen size check for aria-hidden value #10087

Merged
merged 17 commits into from
Jan 27, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 5 additions & 8 deletions packages/react/src/components/UIShell/SideNav.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/

import React, { useState, useRef } from 'react';
import cx from 'classnames';
import PropTypes from 'prop-types';
import { AriaLabelPropType } from '../../prop-types/AriaPropTypes';
import { CARBON_SIDENAV_ITEMS } from './_utils';
import { usePrefix } from '../../internal/usePrefix';
import { useMatchMedia } from '../../internal/useMatchMedia';
// TO-DO: comment back in when footer is added for rails
// import SideNavFooter from './SideNavFooter';

Expand Down Expand Up @@ -121,12 +121,9 @@ const SideNav = React.forwardRef(function SideNav(props, ref) {
eventHandlers.onMouseLeave = () => handleToggle(false, false);
}

function getAriaHiddenValue() {
if (expanded === false && window.innerWidth < 1056) {
return true;
}
return false;
}
const isCollapsedWidth = useMatchMedia(`(max-width: 1055px)`);
dakahn marked this conversation as resolved.
Show resolved Hide resolved
const ariaHidden = expanded === false && isCollapsedWidth;
console.log(ariaHidden);
dakahn marked this conversation as resolved.
Show resolved Hide resolved

return (
<>
Expand All @@ -135,7 +132,7 @@ const SideNav = React.forwardRef(function SideNav(props, ref) {
<div className={overlayClassName} onClick={onOverlayClick} />
)}
<nav
aria-hidden={getAriaHiddenValue()}
aria-hidden={ariaHidden}
ref={ref}
className={`${prefix}--side-nav__navigation ${className}`}
{...accessibilityLabel}
Expand Down
37 changes: 37 additions & 0 deletions packages/react/src/internal/useMatchMedia.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* Copyright IBM Corp. 2016, 2018
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/

import { useState, useEffect } from 'react';
import { canUseDOM } from '../../internal/environment';

export function useMatchMedia(mediaQueryString) {
dakahn marked this conversation as resolved.
Show resolved Hide resolved
const [matches, setMatches] = useState(() => {
if (canUseDOM) {
const mql = window.matchMedia(mediaQueryString);
return mql.matches;
}
return false;
});

useEffect(() => {
function listener() {
setMatches(mediaQueryList.matches);
dakahn marked this conversation as resolved.
Show resolved Hide resolved
}

const mediaQueryList = window.matchMedia(mediaQueryString);
mediaQueryList.addEventListener('change', listener);

// make sure media query list and matches state are synced
dakahn marked this conversation as resolved.
Show resolved Hide resolved
setMatches(mediaQueryList.matches);

return () => {
mediaQueryList.removeEventListener('change', listener);
};
dakahn marked this conversation as resolved.
Show resolved Hide resolved
}, [mediaQueryString]);

return matches;
}