|
1 |
| -import { Stream } from 'xstream'; |
| 1 | +import { Stream, MemoryStream } from 'xstream'; |
2 | 2 | import { DOMSource, VNode, nav, a } from '@cycle/dom';
|
3 | 3 | import isolate from '@cycle/isolate';
|
4 | 4 | import { style } from 'typestyle';
|
5 | 5 | import { rem } from 'csx';
|
| 6 | +import { NavLink } from '../NavLink'; |
| 7 | +import { Location } from '@cycle/history'; |
6 | 8 |
|
7 | 9 | interface Sources {
|
8 | 10 | dom: DOMSource;
|
| 11 | + history: MemoryStream<Location>; |
9 | 12 | }
|
10 | 13 |
|
11 | 14 | interface Sinks {
|
12 | 15 | dom: Stream<VNode>;
|
13 |
| - history: Stream<string>; |
14 | 16 | }
|
15 | 17 |
|
16 | 18 | const navLink = {
|
@@ -43,24 +45,27 @@ const className = style({
|
43 | 45 | });
|
44 | 46 |
|
45 | 47 | const xs = Stream;
|
| 48 | +const menuItems = [ |
| 49 | + { |
| 50 | + href: '/', |
| 51 | + title: 'Home' |
| 52 | + }, |
| 53 | + { |
| 54 | + href: '/commits', |
| 55 | + title: 'Commits' |
| 56 | + }, |
| 57 | + { |
| 58 | + href: '/about', |
| 59 | + title: 'About' |
| 60 | + } |
| 61 | +]; |
46 | 62 |
|
47 |
| -const NavMenuComponent = ({ dom }: Sources): Sinks => { |
48 |
| - const navigateTo$ = |
49 |
| - dom.select('a') |
50 |
| - .events('click', { preventDefault: true }) |
51 |
| - .map(ev => (ev.target as HTMLAnchorElement).href) |
52 |
| - .debug(); |
53 |
| - const vdom$ = |
54 |
| - xs.of( |
55 |
| - nav(`.${className}`, [ |
56 |
| - a({ attrs: { href: '#/', title: 'Home' } }, ['Home']), |
57 |
| - a({ attrs: { href: '#/commits', title: 'Commits' } }, ['Commits']), |
58 |
| - a({ attrs: { href: '#/about', title: 'About' } }, ['About']), |
59 |
| - ]) |
60 |
| - ); |
| 63 | +const NavMenuComponent = ({ dom, history }: Sources): Sinks => { |
| 64 | + const navLinks = menuItems.map(({ href, title }) => NavLink({ dom, history, href$: xs.of(href), title$: xs.of(title) })); |
| 65 | + const navLinksDom$ = xs.combine(...navLinks.map(navLink => navLink.dom)) |
| 66 | + const vdom$ = navLinksDom$.map(navLinks => nav(`.${className}`, navLinks)); |
61 | 67 | return {
|
62 |
| - dom: vdom$, |
63 |
| - history: navigateTo$ |
| 68 | + dom: vdom$ |
64 | 69 | };
|
65 | 70 | };
|
66 | 71 |
|
|
0 commit comments