Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved Theme selector that outlines the current theme #57

Open
wants to merge 34 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
f6249e3
Adding theme picker
manuelernestog Oct 7, 2022
3004175
Adding theme picker
manuelernestog Oct 7, 2022
21eb3f4
Merge branch 'demo' of github.com:manuelernestog/astro-modern-persona…
manuelernestog Oct 23, 2022
31ae8fc
Adding theme picker
manuelernestog Oct 7, 2022
423a1b0
Merge branch 'demo' of github.com:manuelernestog/astro-modern-persona…
manuelernestog Oct 26, 2022
62733d1
Adding theme picker
manuelernestog Oct 7, 2022
7e099dc
Merge branch 'demo' of github.com:manuelernestog/astro-modern-persona…
manuelernestog Oct 26, 2022
8e24ca2
Merge branch 'main' into demo
manuelernestog Feb 2, 2023
7c53d77
Merge branch 'main' into demo
manuelernestog Feb 2, 2023
8a8adea
v2.1.1
manuelernestog Feb 2, 2023
7ebbbba
Adding theme picker
manuelernestog Oct 7, 2022
a182fe3
Merge branch 'demo' of github.com:manuelernestog/astro-modern-persona…
manuelernestog Mar 10, 2023
3c931c3
Adding theme picker
manuelernestog Oct 7, 2022
de08876
Merge branch 'demo' of github.com:manuelernestog/astro-modern-persona…
manuelernestog Mar 10, 2023
93aa698
Adding theme picker
manuelernestog Oct 7, 2022
c67dff9
Merge branch 'demo' of github.com:manuelernestog/astro-modern-persona…
manuelernestog Mar 21, 2023
e42e69e
Adding theme picker
manuelernestog Oct 7, 2022
ea7e885
Merge branch 'demo' of github.com:manuelernestog/astro-modern-persona…
manuelernestog Apr 26, 2023
62e537e
improving theme picker with theme creation
manuelernestog Apr 26, 2023
9d3d6b2
adding netlify.tom file to cache imgs
manuelernestog Apr 26, 2023
e24a7ce
Adding theme picker
manuelernestog Oct 7, 2022
6155129
improving theme picker with theme creation
manuelernestog Apr 26, 2023
5f181a9
adding netlify.tom file to cache imgs
manuelernestog Apr 26, 2023
4e938fe
Merge branch 'demo' of github.com:manuelernestog/astro-modern-persona…
manuelernestog Apr 26, 2023
2976436
Merge branch 'main' into demo
manuelernestog Apr 26, 2023
3d6fc2a
Merge branch 'main' into demo
manuelernestog Apr 26, 2023
c575e87
Merge branch 'main' into demo
manuelernestog Jun 8, 2023
a32f0e1
Merge branch 'main' into demo
manuelernestog Jun 8, 2023
f6cd46b
Merge branch 'main' into demo
manuelernestog Jul 4, 2023
5a32f97
Merge branch 'main' into demo
manuelernestog Jul 10, 2023
8f927a6
adding mt in lg for demo
manuelernestog Jul 10, 2023
b4b3d8a
Merge branch 'main' into demo
manuelernestog Jan 8, 2024
a530cfd
improving theme selector for transition API
manuelernestog Jan 8, 2024
14a4c72
Improved Theme selector that outlines the current theme
hansaskov Jan 17, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions netlify.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[build]
command = "npm run build"
publish = "dist"
[[headers]]
for = "*.webp"
[headers.values]
Cache-Control = "public, max-age=604800"
13 changes: 9 additions & 4 deletions src/components/Header.astro
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
---
import ThemeSelector from "./ThemeSelector.astro";
---
<div
class="sticky lg:hidden top-0 z-30 flex h-16 w-full justify-center bg-opacity-90 backdrop-blur transition-all duration-100 bg-base-100 text-base-content shadow-sm"
class="sticky lg:shadow-none lg:backdrop-blur-none lg:bg-opacity-0 top-0 z-30 flex h-16 w-full justify-center bg-opacity-90 backdrop-blur transition-all duration-100 bg-base-100 text-base-content shadow-sm"
>
<div class="navbar">
<div class="navbar-start">
<label for="my-drawer" class="btn btn-square btn-ghost">
<label for="my-drawer" class="btn btn-square btn-ghost lg:hidden">
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" class="inline-block w-5 h-5 stroke-current"
><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 6h16M4 12h16M4 18h16"></path>
</svg>
</label>
</div>
<div class="navbar-center">
<a class="btn btn-ghost normal-case text-xl" href="/">Manuel Ernesto ⚡️</a>
<a class="btn btn-ghost normal-case text-xl lg:hidden" href="/">Manuel Ernesto ⚡️</a>
</div>
<div class="navbar-end">
<ThemeSelector/>
</div>
<div class="navbar-end"></div>
</div>
</div>
158 changes: 158 additions & 0 deletions src/components/ThemeSelector.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
---

const themes = [
"light",
"dark",
"cupcake",
"bumblebee",
"emerald",
"corporate",
"synthwave",
"retro",
"cyberpunk",
"valentine",
"halloween",
"garden",
"forest",
"aqua",
"lofi",
"pastel",
"fantasy",
"wireframe",
"black",
"luxury",
"dracula",
"cmyk",
"autumn",
"business",
"acid",
"lemonade",
"night",
"coffee",
"winter",
"dim",
"nord",
"sunset",
];
---

<div title="Change Theme" class="dropdown dropdown-end" id="theme-selector">
<div tabindex="0" class="btn gap-1 normal-case btn-ghost">
<svg
width="20"
height="20"
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 24 24"
class="inline-block h-5 w-5 stroke-current md:h-6 md:w-6"
fill="oklch(var(--b1))"
>
<path
fill-rule="evenodd"
clip-rule="evenodd"
stroke-linecap="round"
stroke-linejoin="round"
stroke-width="2"
d="M7 21a4 4 0 01-4-4V5a2 2 0 012-2h4a2 2 0 012 2v12a4 4 0 01-4 4zm0 0h12a2 2 0 002-2v-4a2 2 0 00-2-2h-2.343M11 7.343l1.657-1.657a2 2 0 012.828 0l2.829 2.829a2 2 0 010 2.828l-8.486 8.485M7 17h.01"
/>
</svg>
<span class="hidden xl:inline">Theme</span>
<svg
width="12px"
height="12px"
class="ml-1 h-3 w-3 fill-current opacity-60 sm:inline-block"
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 2048 2048"
><path
d="M1799 349l242 241-1017 1017L7 590l242-241 775 775 775-775z"
></path>
</svg>
</div>
<div
class="dropdown-content bg-base-200 text-base-content rounded-t-box rounded-b-box top-px max-h-48 h-[70vh] w-52 overflow-y-auto shadow-2xl mt-16"
>
<div class="grid grid-cols-1 gap-3 p-3" tabindex="0">
{
themes.map((theme) => (
<div
class="outline-base-content overflow-hidden rounded-lg outline-2 outline-offset-2"
data-set-theme={theme}
data-act-class="outline"
>
<div
data-theme={theme}
class="bg-base-100 text-base-content w-full cursor-pointer font-sans"
>
<div class="grid grid-cols-5 grid-rows-3">
<div class="col-span-5 row-span-3 row-start-1 flex gap-1 py-3 px-4">
<div class="flex-grow text-sm">
{theme}
</div>
<div class="flex flex-shrink-0 flex-wrap gap-1">
<div class="bg-primary w-2 rounded" />
<div class="bg-secondary w-2 rounded" />
<div class="bg-accent w-2 rounded" />
<div class="bg-neutral w-2 rounded" />
</div>
</div>
</div>
</div>
</div>
))
}
</div>
</div>
</div>

<script is:inline>
function getSavedTheme() {
return window.localStorage.getItem("data-theme") || "lofi";
}

function applyOutlineClass(doc, theme) {
const themeDivs = doc.querySelectorAll(
"#theme-selector div[data-set-theme]",
);
themeDivs.forEach((div) => {
if (div.dataset.setTheme === theme) {
div.classList.add("outline");
} else {
div.classList.remove("outline");
}
});
}

function setTheme(doc) {
const theme = getSavedTheme();
updateTheme(doc, theme);
applyOutlineClass(doc, theme);
}

function updateTheme(doc, newTheme) {
doc.querySelector("html").setAttribute("data-theme", newTheme);
window.localStorage.setItem("data-theme", newTheme);
}

function changeThemeBehavior(doc) {
const theme_selectors = doc.querySelectorAll(
"#theme-selector div[data-theme]",
);
theme_selectors.forEach((selector) => {
selector.addEventListener("click", () => {
const selectedTheme = selector.getAttribute("data-theme");
updateTheme(doc, selectedTheme);
applyOutlineClass(doc, selectedTheme);
});
});
}

setTheme(document);
changeThemeBehavior(document);

document.addEventListener("astro:before-swap", (ev) => {
setTheme(ev.newDocument);
});

document.addEventListener("astro:after-swap", () =>
changeThemeBehavior(document),
);
</script>
2 changes: 1 addition & 1 deletion src/layouts/BaseLayout.astro
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const {
<div class="drawer-content bg-base-100">
<Header title={SITE_TITLE} />
<div class="md:flex md:justify-center">
<main class="p-6 pt-10 lg:max-w-[900px] max-w-[100vw]">
<main class="p-6 pt-10 lg:pt-0 lg:max-w-[900px] max-w-[100vw]">
<slot />
</main>
</div>
Expand Down