import { html, LitElement, nothing } from "lit"; import { customElement, state } from "lit/decorators.js"; import Subscriber from "@supersoniks/concorde/core/mixins/Subscriber"; import { tailwind } from "../tailwind"; import { animate } from "@lit-labs/motion"; import { Theme } from "@supersoniks/concorde/core/components/ui/theme/theme"; const tagName = "sonic-docs-header"; // For Astro.build @customElement(tagName) export class SonicComponent extends Subscriber(LitElement) { static styles = [tailwind]; @state() theme: "dark" | "light" = "light"; @state() searchVisible = false; connectedCallback() { this.handleColorScheme(); super.connectedCallback(); // if escape key is pressed and search is visible, hide search document.addEventListener("keydown", (e) => { if (e.key === "Escape" && this.searchVisible) { this.hideSearch(); } }); // ctrl + k to toggle search document.addEventListener("keydown", (e) => { if (e.ctrlKey && e.key === "k") { this.toggleSearch(); e.preventDefault(); } // command + k to toggle search if (e.metaKey && e.key === "k") { this.toggleSearch(); e.preventDefault(); } }); // on hash change, hide search window.addEventListener("hashchange", () => { this.hideSearch(); }); } handleColorScheme() { if ( localStorage.sonicTheme === "dark" || (!("sonicTheme" in localStorage) && window.matchMedia("(prefers-color-scheme: dark)").matches) ) { document.documentElement.setAttribute("data-concorde-theme", "dark"); document.documentElement.classList.add("dark"); this.theme = "dark"; } else { document.documentElement.classList.remove("dark"); document.documentElement.setAttribute("data-concorde-theme", "light"); this.theme = "light"; } } setMode(theme: "dark" | "light") { localStorage.sonicTheme = theme; this.handleColorScheme(); } hideSearch() { this.searchVisible = false; } toggleSearch() { this.searchVisible = !this.searchVisible; } render() { return html`
concorde
(this.searchVisible = !this.searchVisible)} class="lg:min-w-[19rem]" align="left" > Quick search... ${ this.theme == "light" ? html`` : html`` } this.setMode("light")} > light this.setMode("dark")} > dark
${ this.searchVisible ? html`
this.hideSearch()} class="bg-neutral-0 dark:bg-neutral-300 opacity-90 fixed top-0 left-0 w-full h-full " >
` : nothing } `; } }