Search code examples
reactjsoffice-ui-fabricreact-tsx

Highlight the selected item of a navigation pane


Referring to the example "Nav with nested links" under https://developer.microsoft.com/en-us/fabric#/controls/web/nav , On click of a nav item, I want to highlight this item. I have set url as ' ' so that clicking on an item doesn't do anything. But, I want that item to be highlighted on click. How do I do this? Any pointers would be helpful.

import * as React from 'react';
import { Nav,INavStyles } from 'office-ui-fabric-react/lib/Nav';
import { initializeIcons } from '@uifabric/icons';
initializeIcons();

    const navStyles: INavStyles = {
  root:{
      boxSizing: 'border-box',
      border: '1px solid lightgrey',
      overflowY: 'auto',
      height: 300
  },
  chevronButton: {
      height: 30
  },
  chevronIcon:{
      height: 30,
      lineHeight: 30
  },
  compositeLink: {}, 
  group:{}, 
  groupContent: {},
  link: {},
  linkText:{},
  navItem:{}, 
  navItems:{
    margin: 0
  },
};

export const NavNestedExample1: React.FunctionComponent = () => {
  return (
    <Nav
      styles={navStyles}
      ariaLabel="Nav example with nested links"
      groups={[
        {
          links: [
            {
              name: 'Parent link 1',
              url: '',
              target: '_blank',
              expandAriaLabel: 'Expand Parent link 1',
              collapseAriaLabel: 'Collapse Parent link 1',
              links: [
                {
                  name: 'Child link 1',
                  url: '',
                  target: '_blank'
                },
                {
                  name: 'Child link 2',
                  url: '',
                  target: '_blank',
                  expandAriaLabel: 'Expand Child link 2',
                  collapseAriaLabel: 'Collapse Child link 2',
                  links: [
                    {
                      name: '3rd level link 1',
                      url: '',
                      target: '_blank'
                    },
                    {
                      name: '3rd level link 2',
                      url: '',
                      target: '_blank'
                    }
                  ]
                },
                {
                  name: 'Child link 3',
                  url: '',
                  target: '_blank'
                }
              ]
            },
            {
              name: 'Parent link 2',
              url: '',
              target: '_blank',
              expandAriaLabel: 'Expand Parent link 2',
              collapseAriaLabel: 'Collapse Parent link 2',
              links: [
                {
                  name: 'Child link 4',
                  url: '',
                  target: '_blank'
                }
              ]
            }
          ]
        }
      ]}
    />
  );
};

Solution

  • Use className props to apply an additional CSS class to the Nav,INavProps interface

    export const App: React.FunctionComponent = () => {
      return (
        <Nav
          className='nav' //here
          ariaLabel="Nav example with nested links"
          groups={[
                ....
    


     //App.css
        .nav :focus{ 
          color: brown;
          background-color: darksalmon ;
        }
       .nav :hover{ 
          color: .....;
          background-color: ......;
        }
       .nav :active{ 
          color: .....;
          background-color: ......;
        }
    


    With INavStyles and selectors

    const navStyles: INavStyles = {
      root: {
        boxSizing: 'border-box',
        border: '1px solid lightgrey',
        overflowY: 'auto',
        height: 300
      },
      linkText: {
        color: 'green',
        selectors: { '&:hover': { color: 'red' } }
      },
    compositeLink: {
       selectors: {
          '&:active ,&:focus-within': { backgroundColor: 'orange' }
        }
      },
    ...