diff --git a/js/src/common/compat.ts b/js/src/common/compat.ts index 771bc5617..bf952abd2 100644 --- a/js/src/common/compat.ts +++ b/js/src/common/compat.ts @@ -43,7 +43,7 @@ import SplitDropdown from './components/SplitDropdown'; import RequestErrorModal from './components/RequestErrorModal'; import FieldSet from './components/FieldSet'; // import Select from './components/Select'; -// import Navigation from './components/Navigation'; +import Navigation from './components/Navigation'; import Alert from './components/Alert'; import LinkButton from './components/LinkButton'; import Checkbox from './components/Checkbox'; @@ -107,7 +107,7 @@ export default { 'components/RequestErrorModal': RequestErrorModal, 'components/FieldSet': FieldSet, // 'components/Select': Select, - // 'components/Navigation': Navigation, + 'components/Navigation': Navigation, 'components/Alert': Alert, 'components/LinkButton': LinkButton, 'components/Checkbox': Checkbox, diff --git a/js/src/common/components/Navigation.tsx b/js/src/common/components/Navigation.tsx new file mode 100644 index 000000000..726df3cec --- /dev/null +++ b/js/src/common/components/Navigation.tsx @@ -0,0 +1,113 @@ +import Component, { ComponentProps } from '../Component'; +import Button from './Button'; +import LinkButton from './LinkButton'; + +import History from '../../forum/utils/History'; + +export interface NavigationProps extends ComponentProps { + /** + * The name of a class to set on the root element. + */ + className?: string; + + /** + * Whether or not to show a button to toggle the app's drawer if + * there is no more history to pop. + */ + drawer?: boolean; +} + +/** + * The `Navigation` component displays a set of navigation buttons. Typically + * this is just a back button which pops the app's History. If the user is on + * the root page and there is no history to pop, then in some instances it may + * show a button that toggles the app's drawer. + * + * If the app has a pane, it will also include a 'pin' button which toggles the + * pinned state of the pane. + */ +export default class Navigation extends Component { + className?: string; + drawer: boolean; + + constructor(options: { className?: string; drawer?: boolean } = {}) { + super(); + + this.className = options.className; + this.drawer = !!options.drawer; + } + + view() { + const { history, pane }: { history: History; pane: any } = app; + + return ( +
+ {history.canGoBack() ? [this.getBackButton(), this.getPaneButton()] : this.getDrawerButton()} +
+ ); + } + + /** + * Get the back button. + */ + protected getBackButton() { + const { history } = app; + const previous = history.getPrevious() || {}; + + return ( + { + if (e.shiftKey || e.ctrlKey || e.metaKey || e.which === 2) return; + e.preventDefault(); + history.back(); + }} + /> + ); + } + + /** + * Get the pane pinned toggle button. + */ + protected getPaneButton() { + const { pane } = app; + + if (!pane || !pane.active) return ''; + + return ( +