For AI agents: the complete documentation index is available at https://a3s-lab.github.io/UI/llms.txt, the full documentation bundle is available at https://a3s-lab.github.io/UI/llms-full.txt, and this page is available as Markdown at https://a3s-lab.github.io/UI/components/theme-switcher.md.
  • 简体中文
  • next
  • 主题切换器

    Live previewHTML · CSS · JavaScript

    用途

    Include CSS

    导入 Tailwind 和一个完整的 A3S UI 样式包。

    @import "tailwindcss";
    @import "@a3s-lab/ui/a3s.css";

    或者只导入基本CSS、其组成的组件CSS文件和一个样式包。

    @import "tailwindcss";
    @import "@a3s-lab/ui/base.css";
    @import "@a3s-lab/ui/components/button.css";
    @import "@a3s-lab/ui/components/tooltip.css";
    @import "@a3s-lab/ui/styles/a3s.css";

    Theme Switcher 由 Button 和 Tooltip 组件 CSS 组成。

    使用CDN或批量程序导入?请参见安装页

    Include JavaScript

    复制或提供完整的 A3S UI JavaScript 包。

    <script src="/assets/js/all.min.js" defer></script>

    或者仅复制或提供A3S UI运行时。

    <script src="/assets/js/runtime.min.js" defer></script>

    使用CDN或批量程序导入?请参见安装页

    Avoid the initial flash

    在加载样式之前添加此内联脚本,以便在页面呈现之前应用存储模式。

    <script>
      (() => {
        try {
          const stored = localStorage.getItem("themeMode");
          if (stored ? stored === "dark" : matchMedia("(prefers-color-scheme: dark)").matches) {
            document.documentElement.classList.add("dark");
          }
        } catch (_) {}
      })();
    </script>

    Add your switcher HTML

    <button type="button" aria-label="Toggle dark mode" data-tooltip="Toggle dark mode" data-side="bottom" onclick="window.a3sUI.theme.toggle()" class="btn size-8" data-variant="outline" data-size="icon">
      <span class="hidden dark:block"><svg class="lucide lucide-sun" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="4" /><path d="M12 2v2" /><path d="M12 20v2" /><path d="m4.93 4.93 1.41 1.41" /><path d="m17.66 17.66 1.41 1.41" /><path d="M2 12h2" /><path d="M20 12h2" /><path d="m6.34 17.66-1.41 1.41" /><path d="m19.07 4.93-1.41 1.41" /></svg></span>
      <span class="block dark:hidden"><svg class="lucide lucide-moon" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M20.985 12.486a9 9 0 1 1-9.473-9.472c.405-.022.617.46.402.803a6 6 0 0 0 8.268 8.268c.344-.215.825-.004.803.401" /></svg></span>
    </button>

    JavaScript API

    应用程序编程接口类型描述
    window.a3sUI.theme.get()方法返回 "dark""light"
    window.a3sUI.theme.set(mode)方法将模式设置为 "dark""light" 并将其存储在 localStorage.themeMode 中。
    window.a3sUI.theme.toggle()方法在深色和浅色模式之间切换。