AppSidebar.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. <script setup>
  2. import { computed, ref } from 'vue';
  3. import { useI18n } from 'vue-i18n';
  4. import {
  5. DashboardOutlined,
  6. UserOutlined,
  7. SettingOutlined,
  8. ToolOutlined,
  9. ClusterOutlined,
  10. LogoutOutlined,
  11. CloseOutlined,
  12. MenuOutlined,
  13. } from '@ant-design/icons-vue';
  14. import { theme, currentTheme, toggleTheme, toggleUltra, pauseAnimationsUntilLeave } from '@/composables/useTheme.js';
  15. const { t } = useI18n();
  16. const SIDEBAR_COLLAPSED_KEY = 'isSidebarCollapsed';
  17. const props = defineProps({
  18. // Path prefix (e.g. /custom-base/) the panel is served under. Defaults
  19. // to '' which means tab keys end up as '/panel/...'. Pages pass the
  20. // value the Go backend gave them (in production via a meta tag).
  21. basePath: { type: String, default: '' },
  22. // Current request URI so the matching menu item highlights.
  23. requestUri: { type: String, default: '' },
  24. });
  25. // AD-Vue 4 dropped <a-icon :type="x"> in favor of explicit icon
  26. // imports — keep a small name-to-component map so tab definitions stay
  27. // declarative.
  28. const iconByName = {
  29. dashboard: DashboardOutlined,
  30. user: UserOutlined,
  31. setting: SettingOutlined,
  32. tool: ToolOutlined,
  33. cluster: ClusterOutlined,
  34. logout: LogoutOutlined,
  35. };
  36. // basePath comes from Go (`/` by default, `/myprefix/` when configured) so
  37. // these concatenations land on absolute paths. In dev we synthesize the prop
  38. // from a window global which can be empty — force a leading slash so the
  39. // browser doesn't resolve the link relative to the current pathname (which
  40. // would turn /panel/settings + 'panel/...' into /panel/panel/...).
  41. const prefix = props.basePath?.startsWith('/') ? props.basePath : `/${props.basePath || ''}`;
  42. // Labels are i18n-driven so the sidebar matches the locale picked
  43. // in panel settings without a page reload of the sidebar component.
  44. const tabs = computed(() => [
  45. { key: `${prefix}panel/`, icon: 'dashboard', title: t('menu.dashboard') },
  46. { key: `${prefix}panel/inbounds`, icon: 'user', title: t('menu.inbounds') },
  47. { key: `${prefix}panel/nodes`, icon: 'cluster', title: t('menu.nodes') },
  48. { key: `${prefix}panel/settings`, icon: 'setting', title: t('menu.settings') },
  49. { key: `${prefix}panel/xray`, icon: 'tool', title: t('menu.xray') },
  50. { key: `${prefix}logout`, icon: 'logout', title: t('logout') },
  51. ]);
  52. // Logout sits in its own pinned-to-bottom block on the drawer; the
  53. // remaining items are the navigation proper. The full-height sider on
  54. // desktop still uses `tabs` as-is so the desktop look is unchanged.
  55. const navTabs = computed(() => tabs.value.filter((tab) => tab.icon !== 'logout'));
  56. const utilTabs = computed(() => tabs.value.filter((tab) => tab.icon === 'logout'));
  57. const activeTab = ref([props.requestUri]);
  58. const drawerOpen = ref(false);
  59. const collapsed = ref(JSON.parse(localStorage.getItem(SIDEBAR_COLLAPSED_KEY) || 'false'));
  60. // Drawer width is capped against the viewport — AD-Vue's default 378px
  61. // overflows on narrow phones (e.g. 360px portrait), leaving the page
  62. // hidden behind the mask. `min()` keeps it sane on both phones and
  63. // tablets while never exceeding 320px on larger displays.
  64. const drawerWidth = 'min(82vw, 320px)';
  65. function openLink(key) {
  66. if (key.startsWith('http')) {
  67. window.open(key);
  68. } else {
  69. window.location.href = key;
  70. }
  71. }
  72. function onCollapse(isCollapsed, type) {
  73. // Only persist explicit toggle clicks, not breakpoint-triggered collapses.
  74. if (type === 'clickTrigger') {
  75. localStorage.setItem(SIDEBAR_COLLAPSED_KEY, isCollapsed);
  76. collapsed.value = isCollapsed;
  77. }
  78. }
  79. function toggleDrawer() {
  80. drawerOpen.value = !drawerOpen.value;
  81. }
  82. function closeDrawer() {
  83. drawerOpen.value = false;
  84. }
  85. /* 3-state theme cycle driven by the brand-row icon button.
  86. * Light → Dark (turn dark on, ensure ultra off)
  87. * Dark → Ultra (turn ultra on)
  88. * Ultra → Light (turn ultra off, turn dark off)
  89. * Using a single button keeps the sider header clean — the old
  90. * ThemeSwitch a-sub-menu plus its expandable items lived here. */
  91. function cycleTheme() {
  92. pauseAnimationsUntilLeave('theme-cycle');
  93. if (!theme.isDark) {
  94. toggleTheme();
  95. if (theme.isUltra) toggleUltra();
  96. } else if (!theme.isUltra) {
  97. toggleUltra();
  98. } else {
  99. toggleUltra();
  100. toggleTheme();
  101. }
  102. }
  103. </script>
  104. <template>
  105. <div class="ant-sidebar">
  106. <a-layout-sider :theme="currentTheme" collapsible :collapsed="collapsed" breakpoint="md" @collapse="onCollapse">
  107. <div class="sider-brand" :class="{ 'sider-brand-collapsed': collapsed }">
  108. <span class="brand-text">{{ collapsed ? '3X' : '3X-UI' }}</span>
  109. <button v-if="!collapsed" id="theme-cycle" type="button" class="theme-cycle" :aria-label="t('menu.theme')"
  110. :title="t('menu.theme')" @click="cycleTheme">
  111. <svg v-if="!theme.isDark" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"
  112. stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">
  113. <circle cx="12" cy="12" r="4" />
  114. <path d="M12 2v2M12 20v2M4.93 4.93l1.41 1.41M17.66 17.66l1.41 1.41M2 12h2M20 12h2M6.34 17.66l-1.41 1.41M19.07 4.93l-1.41 1.41" />
  115. </svg>
  116. <svg v-else-if="!theme.isUltra" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"
  117. stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">
  118. <path d="M21 12.79A9 9 0 1 1 11.21 3 7 7 0 0 0 21 12.79z" />
  119. </svg>
  120. <svg v-else viewBox="0 0 24 24" fill="currentColor" stroke="currentColor" stroke-width="1.5"
  121. stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">
  122. <path d="M21 12.79A9 9 0 1 1 11.21 3 7 7 0 0 0 21 12.79z" />
  123. <path fill="none" d="M19 3l0.7 1.4 1.4 0.7-1.4 0.7L19 7.2l-0.7-1.4-1.4-0.7 1.4-0.7z" />
  124. </svg>
  125. </button>
  126. </div>
  127. <a-menu :theme="currentTheme" mode="inline" :selected-keys="activeTab" class="sider-nav"
  128. @click="({ key }) => openLink(key)">
  129. <a-menu-item v-for="tab in navTabs" :key="tab.key">
  130. <component :is="iconByName[tab.icon]" />
  131. <span>{{ tab.title }}</span>
  132. </a-menu-item>
  133. </a-menu>
  134. <a-menu :theme="currentTheme" mode="inline" :selected-keys="activeTab" class="sider-utility"
  135. @click="({ key }) => openLink(key)">
  136. <a-menu-item v-for="tab in utilTabs" :key="tab.key">
  137. <component :is="iconByName[tab.icon]" />
  138. <span>{{ tab.title }}</span>
  139. </a-menu-item>
  140. </a-menu>
  141. </a-layout-sider>
  142. <a-drawer placement="left" :closable="false" :open="drawerOpen" :wrap-class-name="currentTheme"
  143. :wrap-style="{ padding: 0 }" :width="drawerWidth"
  144. :body-style="{ padding: 0, display: 'flex', flexDirection: 'column', height: '100%' }"
  145. :header-style="{ display: 'none' }" @close="closeDrawer">
  146. <div class="drawer-header">
  147. <span class="drawer-brand">3X-UI</span>
  148. <div class="drawer-header-actions">
  149. <button id="theme-cycle-drawer" type="button" class="theme-cycle" :aria-label="t('menu.theme')"
  150. :title="t('menu.theme')" @click="cycleTheme">
  151. <svg v-if="!theme.isDark" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"
  152. stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">
  153. <circle cx="12" cy="12" r="4" />
  154. <path d="M12 2v2M12 20v2M4.93 4.93l1.41 1.41M17.66 17.66l1.41 1.41M2 12h2M20 12h2M6.34 17.66l-1.41 1.41M19.07 4.93l-1.41 1.41" />
  155. </svg>
  156. <svg v-else-if="!theme.isUltra" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"
  157. stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">
  158. <path d="M21 12.79A9 9 0 1 1 11.21 3 7 7 0 0 0 21 12.79z" />
  159. </svg>
  160. <svg v-else viewBox="0 0 24 24" fill="currentColor" stroke="currentColor" stroke-width="1.5"
  161. stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">
  162. <path d="M21 12.79A9 9 0 1 1 11.21 3 7 7 0 0 0 21 12.79z" />
  163. <path fill="none" d="M19 3l0.7 1.4 1.4 0.7-1.4 0.7L19 7.2l-0.7-1.4-1.4-0.7 1.4-0.7z" />
  164. </svg>
  165. </button>
  166. <button class="drawer-close" type="button" :aria-label="t('close')" @click="closeDrawer">
  167. <CloseOutlined />
  168. </button>
  169. </div>
  170. </div>
  171. <a-menu :theme="currentTheme" mode="inline" :selected-keys="activeTab" class="drawer-menu drawer-nav"
  172. @click="({ key }) => openLink(key)">
  173. <a-menu-item v-for="tab in navTabs" :key="tab.key">
  174. <component :is="iconByName[tab.icon]" />
  175. <span>{{ tab.title }}</span>
  176. </a-menu-item>
  177. </a-menu>
  178. <a-menu :theme="currentTheme" mode="inline" :selected-keys="activeTab" class="drawer-menu drawer-utility"
  179. @click="({ key }) => openLink(key)">
  180. <a-menu-item v-for="tab in utilTabs" :key="tab.key">
  181. <component :is="iconByName[tab.icon]" />
  182. <span>{{ tab.title }}</span>
  183. </a-menu-item>
  184. </a-menu>
  185. </a-drawer>
  186. <button v-show="!drawerOpen" class="drawer-handle" type="button" :aria-label="t('menu.dashboard')"
  187. @click="toggleDrawer">
  188. <MenuOutlined />
  189. </button>
  190. </div>
  191. </template>
  192. <style scoped>
  193. /* Pin the desktop sider to the viewport. Without this, AD-Vue's
  194. * `<a-layout-sider>` stretches to match the flex row's height — which
  195. * equals the page height on tall dashboards (cards stack into one
  196. * column below `lg` = 992px), so the bottom-anchored
  197. * `.ant-layout-sider-trigger` (and Logout right above it) slide off
  198. * the screen. Sticky + 100vh keeps the sider exactly viewport-tall;
  199. * `align-self: flex-start` stops the flex row from re-stretching it. */
  200. .ant-sidebar>.ant-layout-sider {
  201. position: sticky;
  202. top: 0;
  203. height: 100vh;
  204. align-self: flex-start;
  205. }
  206. /* `.sider-brand` and `.drawer-brand` share the same light-theme colour
  207. * but differ in layout — the sider one is centered with its own
  208. * top-of-sidebar padding + border, the drawer one sits inside a flex
  209. * header next to the close button. Dark/ultra colour overrides live
  210. * in the non-scoped block at the bottom (theme classes attach to
  211. * body / html). */
  212. .sider-brand,
  213. .drawer-brand {
  214. font-weight: 600;
  215. font-size: 18px;
  216. letter-spacing: 0.5px;
  217. color: rgba(0, 0, 0, 0.88);
  218. }
  219. .sider-brand {
  220. display: flex;
  221. align-items: center;
  222. justify-content: space-between;
  223. gap: 8px;
  224. padding: 14px 14px;
  225. border-bottom: 1px solid rgba(128, 128, 128, 0.15);
  226. user-select: none;
  227. }
  228. /* Collapsed sider only has room for the '3X' brand — center it and
  229. * hide the theme cycle button (which is `v-if`-ed out in template). */
  230. .sider-brand-collapsed {
  231. justify-content: center;
  232. font-size: 16px;
  233. padding: 14px 4px;
  234. letter-spacing: 0;
  235. }
  236. .brand-text {
  237. flex: 1 1 auto;
  238. }
  239. .sider-brand-collapsed .brand-text {
  240. flex: 0 0 auto;
  241. }
  242. .theme-cycle {
  243. background: transparent;
  244. border: none;
  245. width: 30px;
  246. height: 30px;
  247. border-radius: 50%;
  248. display: inline-flex;
  249. align-items: center;
  250. justify-content: center;
  251. cursor: pointer;
  252. color: inherit;
  253. padding: 0;
  254. flex-shrink: 0;
  255. transition: background-color 0.2s, transform 0.15s;
  256. }
  257. .theme-cycle:hover,
  258. .theme-cycle:focus-visible {
  259. background: rgba(128, 128, 128, 0.18);
  260. transform: scale(1.08);
  261. }
  262. .theme-cycle svg {
  263. width: 16px;
  264. height: 16px;
  265. }
  266. .drawer-header-actions {
  267. display: inline-flex;
  268. align-items: center;
  269. gap: 4px;
  270. }
  271. .drawer-handle {
  272. position: fixed;
  273. top: 12px;
  274. left: 12px;
  275. z-index: 1100;
  276. background: rgba(0, 0, 0, 0.55);
  277. color: #fff;
  278. border: none;
  279. width: 40px;
  280. height: 40px;
  281. border-radius: 50%;
  282. cursor: pointer;
  283. display: none;
  284. align-items: center;
  285. justify-content: center;
  286. font-size: 18px;
  287. box-shadow: 0 2px 8px rgba(0, 0, 0, 0.25);
  288. }
  289. .drawer-header {
  290. display: flex;
  291. align-items: center;
  292. justify-content: space-between;
  293. padding: 14px 16px;
  294. border-bottom: 1px solid rgba(128, 128, 128, 0.15);
  295. }
  296. .drawer-close {
  297. background: transparent;
  298. border: none;
  299. width: 32px;
  300. height: 32px;
  301. border-radius: 50%;
  302. display: inline-flex;
  303. align-items: center;
  304. justify-content: center;
  305. cursor: pointer;
  306. font-size: 16px;
  307. color: rgba(0, 0, 0, 0.65);
  308. }
  309. .drawer-close:hover,
  310. .drawer-close:focus-visible {
  311. background: rgba(128, 128, 128, 0.18);
  312. }
  313. .drawer-menu :deep(.ant-menu-item) {
  314. height: 48px;
  315. line-height: 48px;
  316. margin: 0;
  317. border-radius: 0;
  318. }
  319. .drawer-menu :deep(.ant-menu-item .anticon) {
  320. font-size: 16px;
  321. }
  322. /* Push the utility (Logout) block to the bottom of the flex-column
  323. * drawer body and separate it from the nav block with a hairline. The
  324. * border colour is theme-neutral so it reads on both light and dark. */
  325. .drawer-utility {
  326. margin-top: auto;
  327. border-top: 1px solid rgba(128, 128, 128, 0.15);
  328. }
  329. /* Pin Logout exactly above AD-Vue's `.ant-layout-sider-trigger` (the
  330. * collapse bar at the bottom, position: absolute; height: 48px). The
  331. * old `margin-top: auto` approach only pushed the utility down when the
  332. * content was shorter than the container — on short viewports the
  333. * Logout got hidden behind the trigger. Switching to a flex layout
  334. * where `.sider-nav` consumes all spare space (flex: 1) and
  335. * `.sider-utility` stays at content height pins it consistently. The
  336. * padding-bottom: 48px on the parent reserves the trigger's strip so
  337. * Logout sits directly above it.
  338. *
  339. * The mobile @media rule below still hides the whole sider on phones;
  340. * this block only kicks in once that override no longer matches. */
  341. .ant-sidebar>.ant-layout-sider :deep(.ant-layout-sider-children) {
  342. display: flex;
  343. flex-direction: column;
  344. height: 100%;
  345. padding-bottom: 48px;
  346. }
  347. .sider-brand {
  348. flex: 0 0 auto;
  349. }
  350. .sider-nav {
  351. flex: 1 1 auto;
  352. overflow-y: auto;
  353. overflow-x: hidden;
  354. min-height: 0;
  355. }
  356. .sider-utility {
  357. flex: 0 0 auto;
  358. border-top: 1px solid rgba(128, 128, 128, 0.15);
  359. }
  360. @media (max-width: 768px) {
  361. .drawer-handle {
  362. display: inline-flex;
  363. }
  364. /* On mobile the drawer is the menu — hide the inline sider's content
  365. * + the collapse trigger so the sider stops taking layout space and
  366. * leaves no remnant button next to the page. */
  367. .ant-sidebar>.ant-layout-sider :deep(.ant-layout-sider-children),
  368. .ant-sidebar>.ant-layout-sider :deep(.ant-layout-sider-trigger) {
  369. display: none;
  370. }
  371. .ant-sidebar>.ant-layout-sider {
  372. flex: 0 0 0 !important;
  373. max-width: 0 !important;
  374. min-width: 0 !important;
  375. width: 0 !important;
  376. }
  377. }
  378. </style>
  379. <style>
  380. /* Non-scoped so the rules survive AD-Vue teleporting the drawer body
  381. * outside the AppSidebar element's scope id. Without this the Vue
  382. * `:global(body.dark) .drawer-brand` form did not produce the expected
  383. * `body.dark .drawer-brand[data-v-xxx]` selector reliably, and the
  384. * drawer brand stayed at the light-theme dark colour on the navy
  385. * drawer surface. Class names are specific enough that no collision is
  386. * expected; AppSidebar owns the only drawer in the app. */
  387. body.dark .drawer-brand,
  388. body.dark .sider-brand {
  389. color: rgba(255, 255, 255, 0.92);
  390. }
  391. html[data-theme='ultra-dark'] .drawer-brand,
  392. html[data-theme='ultra-dark'] .sider-brand {
  393. color: #ffffff;
  394. }
  395. body.dark .drawer-close {
  396. color: rgba(255, 255, 255, 0.75);
  397. }
  398. html[data-theme='ultra-dark'] .drawer-close {
  399. color: rgba(255, 255, 255, 0.85);
  400. }
  401. /* Pin the drawer surface to the same colour the desktop sider uses
  402. * (Layout.colorBgHeader / Menu.colorItemBg from useTheme.js) so the
  403. * header, empty body region, and menu items read as one continuous
  404. * panel. AD-Vue's CSS-in-JS tokens otherwise leave the drawer at
  405. * colorBgElevated (#2d2d30 in dark) which clashes with the #252526
  406. * menu rows. `!important` is required to beat the CSS-in-JS rule
  407. * specificity; AppSidebar owns the only drawer in the app so this
  408. * doesn't collide with anything else. */
  409. body.dark .ant-drawer .ant-drawer-content,
  410. body.dark .ant-drawer .ant-drawer-body {
  411. background: #252526 !important;
  412. }
  413. html[data-theme='ultra-dark'] .ant-drawer .ant-drawer-content,
  414. html[data-theme='ultra-dark'] .ant-drawer .ant-drawer-body {
  415. background: #0a0a0a !important;
  416. }
  417. /* Force the same light-blue tint on selected + hover/active across
  418. * all three themes. AD-Vue's defaults read too subtle on the dark
  419. * sider, and the light-theme variant looked inconsistent vs. dark —
  420. * applying the same RGBA tint over all backgrounds gives the active
  421. * page the same visual weight everywhere. `!important` is required to
  422. * beat AD-Vue's CSS-in-JS specificity; scoped to .sider-nav /
  423. * .sider-utility / .drawer-menu so only the navigation menus pick up
  424. * the override (other a-menu instances keep AD-Vue defaults). */
  425. .sider-nav .ant-menu-item-selected,
  426. .sider-utility .ant-menu-item-selected,
  427. .drawer-menu .ant-menu-item-selected {
  428. background-color: rgba(64, 150, 255, 0.2) !important;
  429. color: #4096ff !important;
  430. }
  431. .sider-nav .ant-menu-item-active:not(.ant-menu-item-selected),
  432. .sider-utility .ant-menu-item-active:not(.ant-menu-item-selected),
  433. .drawer-menu .ant-menu-item-active:not(.ant-menu-item-selected),
  434. .sider-nav .ant-menu-item:not(.ant-menu-item-selected):not(.ant-menu-item-disabled):hover,
  435. .sider-utility .ant-menu-item:not(.ant-menu-item-selected):not(.ant-menu-item-disabled):hover,
  436. .drawer-menu .ant-menu-item:not(.ant-menu-item-selected):not(.ant-menu-item-disabled):hover {
  437. background-color: rgba(64, 150, 255, 0.1) !important;
  438. color: #4096ff !important;
  439. }
  440. </style>