Outlets

An outlet represents a visual location of an application that renderers different content depending on which route has been matched. Using outlets reduces boilerplate required compared to using routes, multiple routes can be associated to the same outlet to more naturally and accurately structure the application output.

Consider a typical application layout which includes a left side menu and a main content view that depending on the route has a right hand side bar:

  1. -------------------------------------------------------------------
  2. | | | |
  3. | | | |
  4. | | | |
  5. | | | |
  6. | | | |
  7. | menu | main | side-menu |
  8. | | | |
  9. | | | |
  10. | | | |
  11. | | | |
  12. | | | |
  13. -------------------------------------------------------------------

The route configuration below specifies all the main pages to the main content outlet, but the widget to a side-menu outlet. This enables building an application that constantly renders the main content depending on route, but also include a right hand side menu for all children routes of the widget route.

  1. const routes = [
  2. {
  3. id: 'landing',
  4. path: '/',
  5. outlet: 'main',
  6. defaultRoute: true
  7. },
  8. {
  9. id: 'widget',
  10. path: 'widget/{widget}',
  11. outlet: 'side-menu',
  12. children: [
  13. {
  14. id: 'tests',
  15. path: 'tests',
  16. outlet: 'main'
  17. },
  18. {
  19. id: 'overview',
  20. path: 'overview',
  21. outlet: 'main'
  22. },
  23. {
  24. id: 'example'
  25. path: 'example/{example}',
  26. outlet: 'main'
  27. }
  28. ]
  29. }
  30. ];

In the routing configuration above, there are two outlets defined, main and side-menu, and a simplified application layout using outlets is shown below. By default the Outlet will render any of the keys that equal a route id that has been matched for the outlet, in this case main. If a function is passed to the Outlet then it will render whenever any route is matched for the outlet specified.

  1. import { create, tsx } from '@dojo/framework/core/vdom';
  2. import Outlet from '@dojo/framework/routing/Outlet';
  3. import Menu from './Menu';
  4. import SideMenu from './SideMenu';
  5. import Landing from './Landing';
  6. import Tests from './Tests';
  7. import Example from './Example';
  8. const factory = create();
  9. const App = factory(function App() {
  10. return (
  11. <div>
  12. <Menu />
  13. <main>
  14. <div>
  15. <Outlet id="main">
  16. {{
  17. landing: <Landing />,
  18. tests: <Tests />,
  19. example: ({ params: { example }}) => <Example example={example}/>,
  20. overview: <Example example="overview"/>
  21. }}
  22. </Outlet>
  23. </div>
  24. <div>
  25. <Outlet id="side-menu">
  26. {({ params: { widget }}) => <SideMenu widget={widget}>}
  27. </Outlet>
  28. </div>
  29. </main>
  30. </div>
  31. );
  32. });

The node structure of the App looks good and succinctly represents the actual visual output for the user with minimal duplication, there still is a need to duplicate the usage of the Example widget across to different routes. This can be solved by using the matcher property to override the default route matching rules. The matcher receives the defaultMatches and a matchDetailsMap in order to make custom matching decisions. In the final example below the usage of Example has been combined into a new key, details that does not exist as a route. This will never match for the outlet unless we override the default matches to set it to true when either the example or overview route has matched. Finally in the details renderer the example property has been defaulted to overview to maintain the same behavior as before.

  1. import { create, tsx } from '@dojo/framework/core/vdom';
  2. import Outlet from '@dojo/framework/routing/Outlet';
  3. import Menu from './Menu';
  4. import SideMenu from './SideMenu';
  5. import Landing from './Landing';
  6. import Tests from './Tests';
  7. import Example from './Example';
  8. const factory = create();
  9. const App = factory(function App() {
  10. return (
  11. <div>
  12. <Menu />
  13. <main>
  14. <div>
  15. <Outlet id="main" matcher={(defaultMatches, matchDetailsMap) => {
  16. defaultMatches.details = matchDetailsMap.has('example') || matchDetailsMap.has('overview');
  17. return defaultMatches;
  18. }}>
  19. {{
  20. landing: <Landing />,
  21. tests: <Tests />,
  22. details: ({ params: { example = "overview" }}) => <Example example={example}/>,
  23. }}
  24. </Outlet>
  25. </div>
  26. <div>
  27. <Outlet id="side-menu">
  28. {({ params: { widget }}) => <SideMenu widget={widget}>}
  29. </Outlet>
  30. </div>
  31. </main>
  32. </div>
  33. );
  34. });