+layout.svelte 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <script lang="ts">
  2. import themes from '../data/themes/index.js';
  3. import { switchTheme } from 'stdf/theme';
  4. import Header from '$lib/header/Header.svelte';
  5. import CmdK from '$lib/cmdk/CmdK.svelte';
  6. import Fund from '$lib/fund/Fund.svelte';
  7. import { page } from '$app/stores';
  8. import { isCmdKStore, isShowFundStore } from '../store.js';
  9. import '../app.css';
  10. let { children } = $props();
  11. let showLeftNav = $state(false); //是否显示左侧导航
  12. let isHome = $page.url.pathname === '/' || $page.url.pathname === '';
  13. //路由改变时,更新左侧导航
  14. $effect(() => {
  15. showLeftNav = $page.url.pathname.startsWith('/guide') || $page.url.pathname === '/components';
  16. });
  17. // 点击顶部菜单事件
  18. const headerCmdKFun = () => {
  19. isCmdKStore.set(true);
  20. };
  21. const currentTheme = localStorage.getItem('theme_color') || 'STDF';
  22. // 设置默认主题色
  23. themes.forEach((item) => {
  24. if (item.name === currentTheme) {
  25. const theme = item.theme;
  26. switchTheme(theme);
  27. // 修改 HTML 的 meta name="theme-color" 属性,适配 Safari 的 tab 背景色,需要设置 light 和 dark 两种颜色
  28. const safariLight = theme.color.primaryWhite;
  29. const safariDark = theme.color.darkBlack;
  30. // 查找 meta 标签,name="theme-color" 且 media="(prefers-color-scheme: light)"
  31. const lightMeta = document.querySelector('meta[name="theme-color"][media="(prefers-color-scheme: light)"]');
  32. // 查找 meta 标签,name="theme-color" 且 media="(prefers-color-scheme: dark)"
  33. const darkMeta = document.querySelector('meta[name="theme-color"][media="(prefers-color-scheme: dark)"]');
  34. // 如果找到了,就修改它的 content 属性
  35. if (lightMeta) {
  36. lightMeta.setAttribute('content', safariLight);
  37. }
  38. if (darkMeta) {
  39. darkMeta.setAttribute('content', safariDark);
  40. }
  41. }
  42. });
  43. let urlLang = window.location.href.split('?')[1];
  44. if (!urlLang || (urlLang && !urlLang.includes('lang'))) {
  45. if (!localStorage.getItem('lang')) {
  46. //获取浏览器语言前两位
  47. const lang = navigator.language.substring(0, 2);
  48. if (lang === 'en') {
  49. localStorage.setItem('lang', 'en_US');
  50. } else {
  51. localStorage.setItem('lang', 'zh_CN');
  52. }
  53. }
  54. } else {
  55. let urlParams = new URLSearchParams(urlLang);
  56. localStorage.setItem('lang', urlParams.get('lang') || '');
  57. setTimeout(() => {
  58. // window.location.href = window.location.href.replace(/(\?|#)[^'"]*/, '');//去除参数
  59. const url = new URL(window.location.href);
  60. url.searchParams.delete('lang');
  61. window.history.pushState({}, '', url.toString()); //刷新页面
  62. }, 10);
  63. }
  64. if (localStorage.getItem('theme') === 'dark') {
  65. document.documentElement.classList.add('dark');
  66. } else if (localStorage.getItem('theme') === 'light') {
  67. document.documentElement.classList.remove('dark');
  68. } else {
  69. if (window.matchMedia('(prefers-color-scheme: dark)').matches) {
  70. document.documentElement.classList.add('dark');
  71. } else {
  72. document.documentElement.classList.remove('dark');
  73. }
  74. }
  75. const isZh = localStorage.getItem('lang') === 'zh_CN';
  76. </script>
  77. <svelte:head>
  78. <title>STDF - {isZh ? '移动 web 组件库' : 'Mobile web component library'}</title>
  79. </svelte:head>
  80. <main class="relative min-h-screen bg-primaryWhite text-justify text-black antialiased dark:bg-darkBlack dark:text-white">
  81. <!-- main -> next -->
  82. {#if $page.url.pathname === '' || $page.url.pathname === '/'}
  83. <div class="bg-primary px-2 py-1 text-center text-xs text-white dark:bg-dark dark:text-black">
  84. {#if isZh}
  85. 这是 v1.0.0@next 文档,v0.x 请访问 <a href="https://stdf.design" class="underline" target="_blank">stdf.design</a>
  86. {:else}
  87. This is the v1.0.0@next documentation, please visit <a href="https://stdf.design" class="underline" target="_blank">stdf.design</a> for
  88. v0.x
  89. {/if}
  90. </div>
  91. {/if}
  92. <Header {showLeftNav} showBottonLine={!isHome} onclickCmdK={headerCmdKFun} />
  93. <!-- <Router {routes} /> -->
  94. {@render children()}
  95. <CmdK />
  96. <!-- 赞赏 -->
  97. {#if $isShowFundStore}
  98. <Fund />
  99. {/if}
  100. </main>