AppSidebar.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. <script setup>
  2. import { computed, ref } from 'vue';
  3. import { useI18n } from 'vue-i18n';
  4. import {
  5. DashboardOutlined,
  6. UserOutlined,
  7. TeamOutlined,
  8. SettingOutlined,
  9. ToolOutlined,
  10. ClusterOutlined,
  11. LogoutOutlined,
  12. CloseOutlined,
  13. MenuOutlined,
  14. ApiOutlined,
  15. } from '@ant-design/icons-vue';
  16. import { theme, currentTheme, toggleTheme, toggleUltra, pauseAnimationsUntilLeave } from '@/composables/useTheme.js';
  17. import { HttpUtil } from '@/utils';
  18. const { t } = useI18n();
  19. const SIDEBAR_COLLAPSED_KEY = 'isSidebarCollapsed';
  20. const props = defineProps({
  21. basePath: { type: String, default: '' },
  22. // Current request URI so the matching menu item highlights.
  23. requestUri: { type: String, default: '' },
  24. });
  25. const iconByName = {
  26. dashboard: DashboardOutlined,
  27. user: UserOutlined,
  28. team: TeamOutlined,
  29. setting: SettingOutlined,
  30. tool: ToolOutlined,
  31. cluster: ClusterOutlined,
  32. logout: LogoutOutlined,
  33. apidocs: ApiOutlined,
  34. };
  35. const prefix = props.basePath?.startsWith('/') ? props.basePath : `/${props.basePath || ''}`;
  36. const tabs = computed(() => [
  37. { key: `${prefix}panel/`, icon: 'dashboard', title: t('menu.dashboard') },
  38. { key: `${prefix}panel/inbounds`, icon: 'user', title: t('menu.inbounds') },
  39. { key: `${prefix}panel/clients`, icon: 'team', title: t('menu.clients') },
  40. { key: `${prefix}panel/nodes`, icon: 'cluster', title: t('menu.nodes') },
  41. { key: `${prefix}panel/settings`, icon: 'setting', title: t('menu.settings') },
  42. { key: `${prefix}panel/xray`, icon: 'tool', title: t('menu.xray') },
  43. { key: `${prefix}panel/api-docs`, icon: 'apidocs', title: t('menu.apiDocs') },
  44. { key: 'logout', icon: 'logout', title: t('logout') },
  45. ]);
  46. const navTabs = computed(() => tabs.value.filter((tab) => tab.icon !== 'logout'));
  47. const utilTabs = computed(() => tabs.value.filter((tab) => tab.icon === 'logout'));
  48. const activeTab = ref([props.requestUri]);
  49. const drawerOpen = ref(false);
  50. const collapsed = ref(JSON.parse(localStorage.getItem(SIDEBAR_COLLAPSED_KEY) || 'false'));
  51. const drawerWidth = 'min(82vw, 320px)';
  52. async function openLink(key) {
  53. if (key === 'logout') {
  54. await HttpUtil.post('/logout');
  55. window.location.href = props.basePath || '/';
  56. return;
  57. }
  58. if (key.startsWith('http')) {
  59. window.open(key);
  60. } else {
  61. window.location.href = key;
  62. }
  63. }
  64. function onCollapse(isCollapsed, type) {
  65. // Only persist explicit toggle clicks, not breakpoint-triggered collapses.
  66. if (type === 'clickTrigger') {
  67. localStorage.setItem(SIDEBAR_COLLAPSED_KEY, isCollapsed);
  68. collapsed.value = isCollapsed;
  69. }
  70. }
  71. function toggleDrawer() {
  72. drawerOpen.value = !drawerOpen.value;
  73. }
  74. function closeDrawer() {
  75. drawerOpen.value = false;
  76. }
  77. function cycleTheme() {
  78. pauseAnimationsUntilLeave('theme-cycle');
  79. if (!theme.isDark) {
  80. toggleTheme();
  81. if (theme.isUltra) toggleUltra();
  82. } else if (!theme.isUltra) {
  83. toggleUltra();
  84. } else {
  85. toggleUltra();
  86. toggleTheme();
  87. }
  88. }
  89. </script>
  90. <template>
  91. <div class="ant-sidebar">
  92. <a-layout-sider :theme="currentTheme" collapsible :collapsed="collapsed" breakpoint="md" @collapse="onCollapse">
  93. <div class="sider-brand" :class="{ 'sider-brand-collapsed': collapsed }">
  94. <span class="brand-text">{{ collapsed ? '3X' : '3X-UI' }}</span>
  95. <button v-if="!collapsed" id="theme-cycle" type="button" class="theme-cycle" :aria-label="t('menu.theme')"
  96. :title="t('menu.theme')" @click="cycleTheme">
  97. <svg v-if="!theme.isDark" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"
  98. stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">
  99. <circle cx="12" cy="12" r="4" />
  100. <path
  101. 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" />
  102. </svg>
  103. <svg v-else-if="!theme.isUltra" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"
  104. stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">
  105. <path d="M21 12.79A9 9 0 1 1 11.21 3 7 7 0 0 0 21 12.79z" />
  106. </svg>
  107. <svg v-else viewBox="0 0 24 24" fill="currentColor" stroke="currentColor" stroke-width="1.5"
  108. stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">
  109. <path d="M21 12.79A9 9 0 1 1 11.21 3 7 7 0 0 0 21 12.79z" />
  110. <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" />
  111. </svg>
  112. </button>
  113. </div>
  114. <a-menu :theme="currentTheme" mode="inline" :selected-keys="activeTab" class="sider-nav"
  115. @click="({ key }) => openLink(key)">
  116. <a-menu-item v-for="tab in navTabs" :key="tab.key">
  117. <component :is="iconByName[tab.icon]" />
  118. <span>{{ tab.title }}</span>
  119. </a-menu-item>
  120. </a-menu>
  121. <a-menu :theme="currentTheme" mode="inline" :selected-keys="activeTab" class="sider-utility"
  122. @click="({ key }) => openLink(key)">
  123. <a-menu-item v-for="tab in utilTabs" :key="tab.key">
  124. <component :is="iconByName[tab.icon]" />
  125. <span>{{ tab.title }}</span>
  126. </a-menu-item>
  127. </a-menu>
  128. </a-layout-sider>
  129. <a-drawer placement="left" :closable="false" :open="drawerOpen" :wrap-class-name="currentTheme"
  130. :wrap-style="{ padding: 0 }" :width="drawerWidth"
  131. :body-style="{ padding: 0, display: 'flex', flexDirection: 'column', height: '100%' }"
  132. :header-style="{ display: 'none' }" @close="closeDrawer">
  133. <div class="drawer-header">
  134. <span class="drawer-brand">3X-UI</span>
  135. <div class="drawer-header-actions">
  136. <button id="theme-cycle-drawer" type="button" class="theme-cycle" :aria-label="t('menu.theme')"
  137. :title="t('menu.theme')" @click="cycleTheme">
  138. <svg v-if="!theme.isDark" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"
  139. stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">
  140. <circle cx="12" cy="12" r="4" />
  141. <path
  142. 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" />
  143. </svg>
  144. <svg v-else-if="!theme.isUltra" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"
  145. stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">
  146. <path d="M21 12.79A9 9 0 1 1 11.21 3 7 7 0 0 0 21 12.79z" />
  147. </svg>
  148. <svg v-else viewBox="0 0 24 24" fill="currentColor" stroke="currentColor" stroke-width="1.5"
  149. stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">
  150. <path d="M21 12.79A9 9 0 1 1 11.21 3 7 7 0 0 0 21 12.79z" />
  151. <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" />
  152. </svg>
  153. </button>
  154. <button class="drawer-close" type="button" :aria-label="t('close')" @click="closeDrawer">
  155. <CloseOutlined />
  156. </button>
  157. </div>
  158. </div>
  159. <a-menu :theme="currentTheme" mode="inline" :selected-keys="activeTab" class="drawer-menu drawer-nav"
  160. @click="({ key }) => openLink(key)">
  161. <a-menu-item v-for="tab in navTabs" :key="tab.key">
  162. <component :is="iconByName[tab.icon]" />
  163. <span>{{ tab.title }}</span>
  164. </a-menu-item>
  165. </a-menu>
  166. <a-menu :theme="currentTheme" mode="inline" :selected-keys="activeTab" class="drawer-menu drawer-utility"
  167. @click="({ key }) => openLink(key)">
  168. <a-menu-item v-for="tab in utilTabs" :key="tab.key">
  169. <component :is="iconByName[tab.icon]" />
  170. <span>{{ tab.title }}</span>
  171. </a-menu-item>
  172. </a-menu>
  173. </a-drawer>
  174. <button v-show="!drawerOpen" class="drawer-handle" type="button" :aria-label="t('menu.dashboard')"
  175. @click="toggleDrawer">
  176. <MenuOutlined />
  177. </button>
  178. </div>
  179. </template>
  180. <style scoped>
  181. .ant-sidebar>.ant-layout-sider {
  182. position: sticky;
  183. top: 0;
  184. height: 100vh;
  185. align-self: flex-start;
  186. }
  187. .sider-brand,
  188. .drawer-brand {
  189. font-weight: 600;
  190. font-size: 18px;
  191. letter-spacing: 0.5px;
  192. color: rgba(0, 0, 0, 0.88);
  193. }
  194. .sider-brand {
  195. display: flex;
  196. align-items: center;
  197. justify-content: space-between;
  198. gap: 8px;
  199. padding: 14px 14px;
  200. border-bottom: 1px solid rgba(128, 128, 128, 0.15);
  201. user-select: none;
  202. }
  203. /* Collapsed sider only has room for the '3X' brand — center it and
  204. * hide the theme cycle button (which is `v-if`-ed out in template). */
  205. .sider-brand-collapsed {
  206. justify-content: center;
  207. font-size: 16px;
  208. padding: 14px 4px;
  209. letter-spacing: 0;
  210. }
  211. .brand-text {
  212. flex: 1 1 auto;
  213. }
  214. .sider-brand-collapsed .brand-text {
  215. flex: 0 0 auto;
  216. }
  217. .theme-cycle {
  218. background: transparent;
  219. border: none;
  220. width: 30px;
  221. height: 30px;
  222. border-radius: 50%;
  223. display: inline-flex;
  224. align-items: center;
  225. justify-content: center;
  226. cursor: pointer;
  227. color: rgba(0, 0, 0, 0.75);
  228. padding: 0;
  229. flex-shrink: 0;
  230. transition: background-color 0.2s, transform 0.15s, color 0.2s;
  231. }
  232. .theme-cycle:hover,
  233. .theme-cycle:focus-visible {
  234. background-color: rgba(64, 150, 255, 0.1);
  235. color: #4096ff;
  236. transform: scale(1.08);
  237. outline: none;
  238. }
  239. .theme-cycle svg {
  240. width: 16px;
  241. height: 16px;
  242. }
  243. .drawer-header-actions {
  244. display: inline-flex;
  245. align-items: center;
  246. gap: 4px;
  247. }
  248. .drawer-handle {
  249. position: fixed;
  250. top: 12px;
  251. left: 12px;
  252. z-index: 1100;
  253. background: rgba(0, 0, 0, 0.55);
  254. color: #fff;
  255. border: none;
  256. width: 40px;
  257. height: 40px;
  258. border-radius: 50%;
  259. cursor: pointer;
  260. display: none;
  261. align-items: center;
  262. justify-content: center;
  263. font-size: 18px;
  264. box-shadow: 0 2px 8px rgba(0, 0, 0, 0.25);
  265. }
  266. .drawer-header {
  267. display: flex;
  268. align-items: center;
  269. justify-content: space-between;
  270. padding: 14px 16px;
  271. border-bottom: 1px solid rgba(128, 128, 128, 0.15);
  272. }
  273. .drawer-close {
  274. background: transparent;
  275. border: none;
  276. width: 32px;
  277. height: 32px;
  278. border-radius: 50%;
  279. display: inline-flex;
  280. align-items: center;
  281. justify-content: center;
  282. cursor: pointer;
  283. font-size: 16px;
  284. color: rgba(0, 0, 0, 0.65);
  285. }
  286. .drawer-close:hover,
  287. .drawer-close:focus-visible {
  288. background: rgba(128, 128, 128, 0.18);
  289. }
  290. .drawer-menu :deep(.ant-menu-item) {
  291. height: 48px;
  292. line-height: 48px;
  293. margin: 0;
  294. border-radius: 0;
  295. }
  296. .drawer-menu :deep(.ant-menu-item .anticon) {
  297. font-size: 16px;
  298. }
  299. .drawer-utility {
  300. margin-top: auto;
  301. border-top: 1px solid rgba(128, 128, 128, 0.15);
  302. }
  303. .ant-sidebar>.ant-layout-sider :deep(.ant-layout-sider-children) {
  304. display: flex;
  305. flex-direction: column;
  306. height: 100%;
  307. }
  308. .sider-brand {
  309. flex: 0 0 auto;
  310. }
  311. .sider-nav {
  312. flex: 1 1 auto;
  313. overflow-y: auto;
  314. overflow-x: hidden;
  315. min-height: 0;
  316. }
  317. .sider-utility {
  318. flex: 0 0 auto;
  319. border-top: 1px solid rgba(128, 128, 128, 0.15);
  320. }
  321. @media (max-width: 768px) {
  322. .drawer-handle {
  323. display: inline-flex;
  324. }
  325. .ant-sidebar>.ant-layout-sider :deep(.ant-layout-sider-children),
  326. .ant-sidebar>.ant-layout-sider :deep(.ant-layout-sider-trigger) {
  327. display: none;
  328. }
  329. .ant-sidebar>.ant-layout-sider {
  330. flex: 0 0 0 !important;
  331. max-width: 0 !important;
  332. min-width: 0 !important;
  333. width: 0 !important;
  334. }
  335. }
  336. </style>
  337. <style>
  338. body.dark .drawer-brand,
  339. body.dark .sider-brand {
  340. color: rgba(255, 255, 255, 0.92);
  341. }
  342. html[data-theme='ultra-dark'] .drawer-brand,
  343. html[data-theme='ultra-dark'] .sider-brand {
  344. color: #ffffff;
  345. }
  346. body.dark .drawer-close {
  347. color: rgba(255, 255, 255, 0.75);
  348. }
  349. html[data-theme='ultra-dark'] .drawer-close {
  350. color: rgba(255, 255, 255, 0.85);
  351. }
  352. body.dark .theme-cycle {
  353. color: rgba(255, 255, 255, 0.85);
  354. }
  355. html[data-theme='ultra-dark'] .theme-cycle {
  356. color: rgba(255, 255, 255, 0.92);
  357. }
  358. body.dark .ant-drawer .ant-drawer-content,
  359. body.dark .ant-drawer .ant-drawer-body {
  360. background: #252526 !important;
  361. }
  362. html[data-theme='ultra-dark'] .ant-drawer .ant-drawer-content,
  363. html[data-theme='ultra-dark'] .ant-drawer .ant-drawer-body {
  364. background: #0a0a0a !important;
  365. }
  366. .sider-nav .ant-menu-item-selected,
  367. .sider-utility .ant-menu-item-selected,
  368. .drawer-menu .ant-menu-item-selected {
  369. background-color: rgba(64, 150, 255, 0.2) !important;
  370. color: #4096ff !important;
  371. }
  372. .sider-nav .ant-menu-item-active:not(.ant-menu-item-selected),
  373. .sider-utility .ant-menu-item-active:not(.ant-menu-item-selected),
  374. .drawer-menu .ant-menu-item-active:not(.ant-menu-item-selected),
  375. .sider-nav .ant-menu-item:not(.ant-menu-item-selected):not(.ant-menu-item-disabled):hover,
  376. .sider-utility .ant-menu-item:not(.ant-menu-item-selected):not(.ant-menu-item-disabled):hover,
  377. .drawer-menu .ant-menu-item:not(.ant-menu-item-selected):not(.ant-menu-item-disabled):hover {
  378. background-color: rgba(64, 150, 255, 0.1) !important;
  379. color: #4096ff !important;
  380. }
  381. </style>