Skip to content

Commit

Permalink
fix(Topbar): remove previous context api, add examples
Browse files Browse the repository at this point in the history
  • Loading branch information
bfbiggs authored and pauljeter committed Sep 24, 2019
1 parent 0714150 commit 09c5867
Show file tree
Hide file tree
Showing 21 changed files with 477 additions and 219 deletions.
58 changes: 58 additions & 0 deletions react/src/lib/Topbar/examples/Blue.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/* eslint-disable jsx-a11y/anchor-is-valid */
import React from 'react';
import {
Button,
Icon,
ListItem,
Topbar,
TopbarMobile,
TopbarNav,
TopbarRight,
} from '@momentum-ui/react';

export default class TopbarBlue extends React.Component {
render() {
return (
<Topbar
color='blue'
brandAnchorElement={
<a
href='javascript:void(0)'
title='Momentum UI'
>
<Icon name='cisco-logo' />
</a>
}
>
<TopbarNav>
<ListItem>Develop</ListItem>
<ListItem>Styles</ListItem>
<ListItem>Layout</ListItem>
<ListItem>Navigation</ListItem>
</TopbarNav>
<TopbarRight>
<div className='md-top-bar__user' />
<div className='md-top-bar__logged-out'>
<a href='javascript:void(0)'>Log In</a>
<Button color='blue' ariaLabel='myAriaLabel'>
Button
</Button>
</div>
</TopbarRight>
<TopbarMobile shouldCloseOnClick={false}>
<a
href='javascript:void(0)'
title='Momentum UI'
>
<Icon name='cisco-logo' />
</a>
<ListItem>Develop</ListItem>
<ListItem>Styles</ListItem>
<ListItem>Layout</ListItem>
<ListItem>Navigation</ListItem>
</TopbarMobile>
</Topbar>
);
}
}
/* eslint-enable jsx-a11y/anchor-is-valid */
58 changes: 58 additions & 0 deletions react/src/lib/Topbar/examples/Dark.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/* eslint-disable jsx-a11y/anchor-is-valid */
import React from 'react';
import {
Button,
Icon,
ListItem,
Topbar,
TopbarMobile,
TopbarNav,
TopbarRight,
} from '@momentum-ui/react';

export default class TopbarDark extends React.Component {
render() {
return (
<Topbar
color='dark'
brandAnchorElement={
<a
href='javascript:void(0)'
title='Momentum UI'
>
<Icon name='cisco-logo' />
</a>
}
>
<TopbarNav>
<ListItem>Develop</ListItem>
<ListItem>Styles</ListItem>
<ListItem>Layout</ListItem>
<ListItem>Navigation</ListItem>
</TopbarNav>
<TopbarRight>
<div className='md-top-bar__user' />
<div className='md-top-bar__logged-out'>
<a href='javascript:void(0)'>Log In</a>
<Button color='blue' ariaLabel='myAriaLabel'>
Button
</Button>
</div>
</TopbarRight>
<TopbarMobile shouldCloseOnClick={false}>
<a
href='javascript:void(0)'
title='Momentum UI'
>
<Icon name='cisco-logo' />
</a>
<ListItem>Develop</ListItem>
<ListItem>Styles</ListItem>
<ListItem>Layout</ListItem>
<ListItem>Navigation</ListItem>
</TopbarMobile>
</Topbar>
);
}
}
/* eslint-enable jsx-a11y/anchor-is-valid */
22 changes: 22 additions & 0 deletions react/src/lib/Topbar/examples/KitchenSink.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/* eslint-disable jsx-a11y/anchor-is-valid */
import React from 'react';
import {
TopbarBlue,
TopbarDark,
TopbarLight,
TopbarNoMiddle
} from './index';

export default class TopbarKitchenSink extends React.Component {
render() {
return (
<React.Fragment>
<TopbarBlue />
<TopbarDark />
<TopbarLight />
<TopbarNoMiddle />
</React.Fragment>
);
}
}
/* eslint-enable jsx-a11y/anchor-is-valid */
58 changes: 58 additions & 0 deletions react/src/lib/Topbar/examples/Light.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/* eslint-disable jsx-a11y/anchor-is-valid */
import React from 'react';
import {
Button,
Icon,
ListItem,
Topbar,
TopbarMobile,
TopbarNav,
TopbarRight,
} from '@momentum-ui/react';

export default class TopbarLight extends React.Component {
render() {
return (
<Topbar
color='light'
brandAnchorElement={
<a
href='javascript:void(0)'
title='Momentum UI'
>
<Icon name='cisco-logo' />
</a>
}
>
<TopbarNav>
<ListItem>Develop</ListItem>
<ListItem>Styles</ListItem>
<ListItem>Layout</ListItem>
<ListItem>Navigation</ListItem>
</TopbarNav>
<TopbarRight>
<div className='md-top-bar__user' />
<div className='md-top-bar__logged-out'>
<a href='javascript:void(0)'>Log In</a>
<Button color='blue' ariaLabel='myAriaLabel'>
Button
</Button>
</div>
</TopbarRight>
<TopbarMobile shouldCloseOnClick={false}>
<a
href='javascript:void(0)'
title='Momentum UI'
>
<Icon name='cisco-logo' />
</a>
<ListItem>Develop</ListItem>
<ListItem>Styles</ListItem>
<ListItem>Layout</ListItem>
<ListItem>Navigation</ListItem>
</TopbarMobile>
</Topbar>
);
}
}
/* eslint-enable jsx-a11y/anchor-is-valid */
46 changes: 46 additions & 0 deletions react/src/lib/Topbar/examples/NoMiddle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/* eslint-disable jsx-a11y/anchor-is-valid */
import React from 'react';
import {
Button,
Icon,
Topbar,
TopbarMobile,
TopbarRight,
} from '@momentum-ui/react';

export default class TopbarNoMiddle extends React.Component {
render() {
return (
<Topbar
color='light'
brandAnchorElement={
<a
href='javascript:void(0)'
title='Momentum UI'
>
<Icon name='cisco-logo' />
</a>
}
>
<TopbarRight>
<div className='md-top-bar__user' />
<div className='md-top-bar__logged-out'>
<a href='javascript:void(0)'>Log In</a>
<Button color='blue' ariaLabel='myAriaLabel'>
Button
</Button>
</div>
</TopbarRight>
<TopbarMobile shouldCloseOnClick={false}>
<a
href='javascript:void(0)'
title='Momentum UI'
>
<Icon name='cisco-logo' />
</a>
</TopbarMobile>
</Topbar>
);
}
}
/* eslint-enable jsx-a11y/anchor-is-valid */
6 changes: 6 additions & 0 deletions react/src/lib/Topbar/examples/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export { default as TopbarLight } from './Light';
export { default as TopbarDark } from './Dark';
export { default as TopbarBlue } from './Blue';
export { default as TopbarNoMiddle } from './NoMiddle';
export { default as TopbarKitchenSink } from './KitchenSink';

74 changes: 24 additions & 50 deletions react/src/lib/Topbar/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,54 +2,35 @@

import React from 'react';
import PropTypes from 'prop-types';
import { prefix } from '../utils/index';

class Topbar extends React.Component {
static displayName = 'Topbar';

state = {
isMobileOpen: false,
activeIndex: null,
focus: 0,
};

getChildContext = () => {
return {
activeIndex: this.state.activeIndex,
onActivate: index => this.setSelected(index),
onFocus: index => this.setState(() => ({ focus: index })),
focus: this.state.focus,
};
};

setSelected = index => {
if (index === this.state.activeIndex) return;
this.setState(() => ({ activeIndex: index }));
};

render() {
const {
anchor,
brandAnchorElement,
color,
children,
className,
color,
fixed,
icon,
image,
title
title,
...otherProps
} = this.props;

const mdTopBarClass = 'md-top-bar';
const mdBrandClass = 'md-brand';
const topBarClass = `${prefix}-top-bar`;
const brandClass = `${prefix}-brand`;

const brandNodeChildren = ([
<div className={`${mdBrandClass}__logo`} key={`${mdBrandClass}__logo`}>
<div className={`${brandClass}__logo`} key={`${brandClass}__logo`}>
{
image
? image
: <i className={`icon ${icon}`} />
}
</div>,
<div className={`${mdBrandClass}__title`} key={`${mdBrandClass}__title`}>
<div className={`${brandClass}__title`} key={`${brandClass}__title`}>
{title}
</div>
]);
Expand All @@ -59,24 +40,24 @@ class Topbar extends React.Component {
? React.cloneElement(
brandAnchorElement,
{
className: `${mdBrandClass}` +
className:
`${brandClass}` +
`${(brandAnchorElement.props.className && ` ${brandAnchorElement.props.className}`) || ''}`,
},
brandNodeChildren
)
: <a className={mdBrandClass} href={anchor}>
: <a className={brandClass} href={anchor}>
{brandNodeChildren}
</a>
);

const brandNode = (
<div className={`${mdTopBarClass}__brand`}>
<div className={`${topBarClass}__brand`}>
{getBrandAnchor()}
</div>
);

const injectChildren = React.Children.map(this.props.children, child => {
if (!child) return;
const injectChildren = React.Children.map(children, child => {
if ((child.type.displayName === 'TopbarMobile') && (!child.props.brandNode)) {
return React.cloneElement(child, {
brandNode
Expand All @@ -88,16 +69,16 @@ class Topbar extends React.Component {

return (
<div
className={`${mdTopBarClass}` +
`${(fixed && ` ${mdTopBarClass}--fixed`) || ''}` +
`${(className && ` ${className}`) || ''}` +
` ${mdTopBarClass}--${color}`
className={
`${topBarClass}` +
`${fixed && ` ${topBarClass}--fixed` || ''}` +
`${color && ` ${topBarClass}--${color}` || ''}` +
`${className && ` ${className}` || ''}`
}
role="navigation"
ref={ref => {
this.parentContainer = ref;
}}>
<div className={`${mdTopBarClass}__container`}>
role='navigation'
{...otherProps}
>
<div className={`${topBarClass}__container`}>
{brandNode}
{injectChildren}
</div>
Expand All @@ -121,7 +102,7 @@ Topbar.propTypes = {
fixed: PropTypes.bool,
/** @prop Icon class name | 'icon-cisco-logo' */
icon: PropTypes.string,
/** @prop Image source URL | null */
/** @prop Image node | null */
image: PropTypes.node,
/** @prop Topbar title text | '' */
title: PropTypes.string,
Expand All @@ -139,13 +120,6 @@ Topbar.defaultProps = {
title: '',
};

Topbar.childContextTypes = {
focus: PropTypes.number,
activeIndex: PropTypes.number,
onActivate: PropTypes.func,
onFocus: PropTypes.func,
};

Topbar.displayName = 'Topbar';

export default Topbar;
Loading

0 comments on commit 09c5867

Please sign in to comment.