Skip to content

Commit

Permalink
feat(Nav): Updated to add wrapper for nav link text (#9740)
Browse files Browse the repository at this point in the history
* Allows NavExpandable to accept ReactNode as a title prop

* feat(Navigation): Updated to add nav link text wrappper

* fixed md file

* revert changes in NavItem

* add opt in prop for NavItem link text wrapper

* updates from comments

---------

Co-authored-by: David Vail <dcvail17@gmail.com>
  • Loading branch information
tlabaj and dvail authored Oct 23, 2023
1 parent c1f452f commit 32655ae
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 7 deletions.
10 changes: 5 additions & 5 deletions packages/react-core/src/components/Nav/NavExpandable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import { PickOptional } from '../../helpers/typeUtils';
import { getOUIAProps, OUIAProps, getDefaultOUIAId } from '../../helpers';

export interface NavExpandableProps
extends React.DetailedHTMLProps<React.LiHTMLAttributes<HTMLLIElement>, HTMLLIElement>,
extends Omit<React.DetailedHTMLProps<React.LiHTMLAttributes<HTMLLIElement>, HTMLLIElement>, 'title'>,
OUIAProps {
/** Title shown for the expandable list */
title: string;
/** Title content shown for the expandable list */
title: React.ReactNode;
/** If defined, screen readers will read this text instead of the list title */
srText?: string;
/** Boolean to programatically expand or collapse section */
Expand Down Expand Up @@ -126,14 +126,14 @@ class NavExpandable extends React.Component<NavExpandableProps, NavExpandableSta
<PageSidebarContext.Consumer>
{({ isSidebarOpen }) => (
<button
className={styles.navLink}
className={css(styles.navLink)}
id={srText ? null : this.id}
onClick={(event) => this.onExpand(event, context.onToggle)}
aria-expanded={expandedState}
tabIndex={isSidebarOpen ? null : -1}
{...buttonProps}
>
{title}
{typeof title !== 'string' ? <span className={css(`${styles.nav}__link-text`)}>{title}</span> : title}
<span className={css(styles.navToggle)}>
<span className={css(styles.navToggleIcon)}>
<AngleRightIcon aria-hidden="true" />
Expand Down
5 changes: 4 additions & 1 deletion packages/react-core/src/components/Nav/NavItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ export interface NavItemProps extends Omit<React.HTMLProps<HTMLAnchorElement>, '
ouiaId?: number | string;
/** Set the value of data-ouia-safe. Only set to true when the component is in a static state, i.e. no animations are occurring. At all other times, this value must be false. */
ouiaSafe?: boolean;
/** @beta Adds a wrapper around the nav link text. Improves the layout when the text is a react node. */
hasNavLinkWrapper?: boolean;
}

export const NavItem: React.FunctionComponent<NavItemProps> = ({
Expand All @@ -56,6 +58,7 @@ export const NavItem: React.FunctionComponent<NavItemProps> = ({
ouiaId,
ouiaSafe,
zIndex = 9999,
hasNavLinkWrapper,
...props
}: NavItemProps) => {
const { flyoutRef, setFlyoutRef, navRef } = React.useContext(NavContext);
Expand Down Expand Up @@ -188,7 +191,7 @@ export const NavItem: React.FunctionComponent<NavItemProps> = ({
{...(hasFlyout && { ...ariaFlyoutProps })}
{...props}
>
{children}
{hasNavLinkWrapper ? <span className={css(`${styles.nav}__link-text`)}>{children}</span> : children}
{flyout && flyoutButton}
</Component>
);
Expand Down
8 changes: 8 additions & 0 deletions packages/react-core/src/components/Nav/examples/Nav.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ ouia: true
---

import './nav.css';
import ArrowRightIcon from '@patternfly/react-icons/dist/esm/icons/arrow-right-icon';
import UserIcon from '@patternfly/react-icons/dist/esm/icons/user-icon';

## Examples

Expand Down Expand Up @@ -68,6 +70,12 @@ A flyout should be a `Menu` component. Press `space` or `right arrow` to open a

```

### Link text

```ts isBeta file="./NavLinkText.tsx"

```

## Types

### NavSelectClickHandler
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export const NavExpandableThirdLevel: React.FunctionComponent = () => {
</NavItem>
</NavExpandable>
<NavExpandable
title="Expandable section title 2"
title={'Expandable section title 2'}
groupId="nav-expand3rd-group-2"
isActive={activeGroup === 'nav-expand3rd-group-2'}
isExpanded
Expand Down
109 changes: 109 additions & 0 deletions packages/react-core/src/components/Nav/examples/NavLinkText.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import React from 'react';
import { Nav, NavExpandable, NavItem, NavList } from '@patternfly/react-core';
import ArrowRightIcon from '@patternfly/react-icons/dist/esm/icons/arrow-right-icon';
import UserIcon from '@patternfly/react-icons/dist/esm/icons/user-icon';

export const NavLinkText: React.FunctionComponent = () => {
const [activeGroup, setActiveGroup] = React.useState('nav-expand3rd-group-1');
const [activeItem, setActiveItem] = React.useState('nav-expand3rd-group-1_item-1');

const onSelect = (
_event: React.FormEvent<HTMLInputElement>,
result: { itemId: number | string; groupId: number | string }
) => {
setActiveGroup(result.groupId as string);
setActiveItem(result.itemId as string);
};

const onToggle = (
_event: React.MouseEvent<HTMLButtonElement>,
result: { groupId: number | string; isExpanded: boolean }
) => {
// eslint-disable-next-line no-console
console.log(`Group ${result.groupId} expanded? ${result.isExpanded}`);
};

return (
<Nav onSelect={onSelect} onToggle={onToggle} aria-label="Nav link text">
<NavList>
<NavItem
preventDefault
id="link-text-link-1"
to="#link-text-link-1"
itemId="link-text-1-item-1"
isActive={activeItem === 'link-text-1-item-1'}
hasNavLinkWrapper
>
Link 1
<ArrowRightIcon />
</NavItem>
<NavExpandable
title={
<>
Link 2 <small>(small text)</small>
</>
}
groupId="link-text-group-1"
isActive={activeGroup === 'link-text-group-1'}
isExpanded
>
<NavItem
preventDefault
id="link-text-link-2"
to="#link-text-link-2"
groupId="link-text-group-1"
itemId="link-text-group-1_item-1"
isActive={activeItem === 'link-text-group-1_item-1'}
hasNavLinkWrapper
>
<UserIcon />
Subnav link 1
</NavItem>
<NavItem
preventDefault
id="link-text-link-3"
to="#link-text-link-3"
groupId="link-text-group-1"
itemId="link-text-group-1_item-2"
isActive={activeItem === 'link-text-group-1_item-2'}
hasNavLinkWrapper
>
<UserIcon />
Subnav link 2
</NavItem>
</NavExpandable>
<NavExpandable
title={
<>
Link 3 <strong>(strong text)</strong>
</>
}
groupId="link-text-group-2"
isActive={activeGroup === 'nav-expand3rd-group-2'}
isExpanded
>
<NavItem
preventDefault
id="link-text-link-4"
to="link-text-link-4"
groupId="link-text-group-2"
itemId="link-text-group-2_item-1"
isActive={activeItem === 'link-text-group-2_item-1'}
>
Subnav link 1
</NavItem>
<NavItem
preventDefault
id="link-text-link-5"
to="link-text-link-5"
groupId="link-text-group-2"
itemId="link-text-group-2_item-2"
isActive={activeItem === 'link-text-group-2_item-2'}
>
Subnav link 2
</NavItem>
</NavExpandable>
</NavList>
</Nav>
);
};
1 change: 1 addition & 0 deletions packages/react-core/src/components/Nav/examples/nav.css
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#ws-react-c-navigation-expandable-wsubnavigation-titles,
#ws-react-c-navigation-mixed,
#ws-react-c-navigation-expandable-third-level,
#ws-react-c-navigation-link-text,
#ws-react-c-navigation-horizontal-subnav,
#ws-react-c-navigation-flyout,
#ws-react-c-navigation-drilldown {
Expand Down

0 comments on commit 32655ae

Please sign in to comment.