Skip to content
Source
Sign inSign up
TanStack/cli
Open History to see the last commit

Header.tsx.ejs

179 lines6.7 KB

1<% if (addOns.length === 0 && integrations.length === 0 && routes.length === 0) { ignoreFile() } %>import { Link } from '@tanstack/react-router'
2<% for(const integration of integrations.filter(i => i.type === 'header-user')) { %>
3import <%= integration.jsName %> from "<%= relativePath(integration.path) %>";
4<% } %><% if (tailwind) {
5const icons = new Set([
6 "Menu",
7 "X",
8 "Home",
9])
10​
11const hasRoutesWithChildren = addOns.some(a => a.routes?.some(r => r.children));
12​
13if (hasRoutesWithChildren) {
14 icons.add("ChevronDown");
15 icons.add("ChevronRight");
17​
18for(const addOn of addOns) {
19 for(const route of (addOn?.routes||[])?.filter(r => r.url && r.name)) {
20 icons.add( route.icon || "Globe");
21 }
23%>
24import { useState } from 'react';
25import {
26 <%= Array.from(icons).sort().join(", ") %>
27} from "lucide-react";
28<% } %>
29​
30export default function Header() {
31<% if (tailwind) {
32const menusWithChildren = addOns.filter(a => a.routes?.some(r => r.children));
33const userHeaders = integrations.filter(i => i.type === 'header-user');
34%>
35 const [isOpen, setIsOpen] = useState(false);
36 <% if (menusWithChildren.length > 0) { %>const [groupedExpanded, setGroupedExpanded] = useState<Record<string, boolean>>({});
37 <% } %>
38​
39 return (
40 <>
41 <header className="p-4 flex items-center bg-gray-800 text-white shadow-lg">
42 <button
43 onClick={() => setIsOpen(true)}
44 className="p-2 hover:bg-gray-700 rounded-lg transition-colors"
45 aria-label="Open menu"
46 >
47 <Menu size={24} />
48 </button>
49 <h1 className="ml-4 text-xl font-semibold">
50 <Link to="/">
51 <img
52 src="/tanstack-word-logo-white.svg"
53 alt="TanStack Logo"
54 className="h-10"
55 />
56 </Link>
57 </h1>
58 </header>
59​
60 <aside
61 className={`fixed top-0 left-0 h-full w-80 bg-gray-900 text-white shadow-2xl z-50 transform transition-transform duration-300 ease-in-out flex flex-col ${
62 isOpen ? "translate-x-0" : "-translate-x-full"
63 }`}
64 >
65 <div className="flex items-center justify-between p-4 border-b border-gray-700">
66 <h2 className="text-xl font-bold">Navigation</h2>
67 <button
68 onClick={() => setIsOpen(false)}
69 className="p-2 hover:bg-gray-800 rounded-lg transition-colors"
70 aria-label="Close menu"
71 >
72 <X size={24} />
73 </button>
74 </div>
75​
76 <nav className="flex-1 p-4 overflow-y-auto">
77 <Link
78 to="/"
79 onClick={() => setIsOpen(false)}
80 className="flex items-center gap-3 p-3 rounded-lg hover:bg-gray-800 transition-colors mb-2"
81 activeProps={{
82 className:
83 "flex items-center gap-3 p-3 rounded-lg bg-cyan-600 hover:bg-cyan-700 transition-colors mb-2",
84 }}
85 >
86 <Home size={20} />
87 <span className="font-medium">Home</span>
88 </Link>
89​
90 {/* Demo Links Start */}
91 <% for(const addOn of addOns) {
92 for(const route of (addOn?.routes||[])?.filter(r => r.url && r.name)) {
93 if (route.children) { %>
94 <div className="flex flex-row justify-between">
95 <Link to="<%= route.url %>"
96 onClick={() => setIsOpen(false)}
97 className="flex-1 flex items-center gap-3 p-3 rounded-lg hover:bg-gray-800 transition-colors mb-2"
98 activeProps={{
99 className:
100 "flex-1 flex items-center gap-3 p-3 rounded-lg bg-cyan-600 hover:bg-cyan-700 transition-colors mb-2",
101 }}
102 >
103 <<%= route.icon || "Globe" %> size={20} />
104 <span className="font-medium"><%= route.name %></span>
105 </Link>
106 <button
107 className="p-2 hover:bg-gray-800 rounded-lg transition-colors"
108 onClick={() => setGroupedExpanded(prev => ({ ...prev, <%= route.jsName %>: !prev.<%= route.jsName %> }))}>
109 {groupedExpanded.<%= route.jsName %> ? <ChevronDown size={20} /> : <ChevronRight size={20} />}
110 </button>
111 </div>
112 {groupedExpanded.<%= route.jsName %> && (
113 <div className="flex flex-col ml-4">
114 <% for(const child of route.children) { %>
115 <Link
116 to="<%= child.url %>"
117 onClick={() => setIsOpen(false)}
118 className="flex items-center gap-3 p-3 rounded-lg hover:bg-gray-800 transition-colors mb-2"
119 activeProps={{
120 className:
121 "flex items-center gap-3 p-3 rounded-lg bg-cyan-600 hover:bg-cyan-700 transition-colors mb-2",
122 }}
123 >
124 <<%= child.icon || "Globe" %> size={20} />
125 <span className="font-medium"><%= child.name %></span>
126 </Link>
127 <% } %>
128 </div>
129 )}
130 <% } else { %>
131 <Link
132 to="<%= route.url %>"
133 onClick={() => setIsOpen(false)}
134 className="flex items-center gap-3 p-3 rounded-lg hover:bg-gray-800 transition-colors mb-2"
135 activeProps={{
136 className:
137 "flex items-center gap-3 p-3 rounded-lg bg-cyan-600 hover:bg-cyan-700 transition-colors mb-2",
138 }}
139 >
140 <<%= route.icon || "Globe" %> size={20} />
141 <span className="font-medium"><%= route.name %></span>
142 </Link>
143 <% } } } %>
144 {/* Demo Links End */}
145 </nav>
146​
147 <% if (userHeaders.length > 0) { %>
148 <div className="p-4 border-t border-gray-700 bg-gray-800 flex flex-col gap-2">
149 <% for(const integration of userHeaders) { %>
150 <<%= integration.jsName %> />
151 <% } %>
152 </div>
153 <% } %>
154 </aside>
155 </>
156 );
157<% } else { %>
158 return (
159 <header className="p-2 flex gap-2 bg-white text-black justify-between">
160 <nav className="flex flex-row">
161 <div className="px-2 font-bold">
162 <Link to="/">Home</Link>
163 </div>
164<% for(const addOn of addOns) {
165 for(const route of (addOn?.routes||[])?.filter(r => r.url && r.name)) { %>
166<div className="px-2 font-bold"><Link to="<%= route.url %>"><%= route.name %></Link></div>
167<% } } %>
168 </nav>
169<% if (integrations.filter(i => i.type === 'header-user').length > 0) { %>
170 <div>
171<% for(const integration of integrations.filter(i => i.type === 'header-user')) { %>
172 <<%= integration.jsName %> />
173<% } %>
174 </div>
175<% } %>
176 </header>
177 )
178<% } %>