Skip to content

Commit a7fb749

Browse files
committed
themes
1 parent 478909b commit a7fb749

34 files changed

Lines changed: 841 additions & 0 deletions
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
name: Deploy to GitHub Pages
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
8+
permissions:
9+
contents: read
10+
pages: write
11+
id-token: write
12+
13+
jobs:
14+
build:
15+
runs-on: ubuntu-latest
16+
steps:
17+
- name: Check out the code
18+
uses: actions/checkout@v3
19+
20+
- name: Set up Node.js
21+
uses: actions/setup-node@v3
22+
with:
23+
node-version: 20
24+
25+
- name: Install dependencies
26+
run: npm install
27+
28+
- name: Build the project
29+
run: npm run build
30+
31+
- name: Upload to GitHub Pages
32+
uses: actions/upload-pages-artifact@v1
33+
with:
34+
path: ./dist
35+
36+
deploy:
37+
needs: build
38+
runs-on: ubuntu-latest
39+
permissions:
40+
pages: write
41+
id-token: write
42+
steps:
43+
- name: Deploy to GitHub Pages
44+
uses: actions/deploy-pages@v1
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# build output
2+
dist/
3+
# generated types
4+
.astro/
5+
6+
# dependencies
7+
node_modules/
8+
9+
# logs
10+
npm-debug.log*
11+
yarn-debug.log*
12+
yarn-error.log*
13+
pnpm-debug.log*
14+
15+
16+
# environment variables
17+
.env
18+
.env.production
19+
20+
# macOS-specific files
21+
.DS_Store
22+
23+
# jetbrains setting folder
24+
.idea/
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
plugins: [require("prettier-plugin-astro")],
3+
};
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"plugins": ["prettier-plugin-astro"],
3+
"overrides": [
4+
{
5+
"files": "*.astro",
6+
"options": {
7+
"parser": "astro"
8+
}
9+
}
10+
]
11+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// @ts-check
2+
import { defineConfig } from 'astro/config';
3+
4+
import preact from '@astrojs/preact';
5+
6+
// https://astro.build/config
7+
export default defineConfig({
8+
integrations: [preact()]
9+
});
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "ejemplo",
3+
"type": "module",
4+
"version": "0.0.1",
5+
"scripts": {
6+
"dev": "astro dev",
7+
"start": "astro dev",
8+
"build": "astro check && astro build",
9+
"preview": "astro preview",
10+
"astro": "astro"
11+
},
12+
"dependencies": {
13+
"@astrojs/check": "^0.9.4",
14+
"@astrojs/preact": "^3.5.4",
15+
"astro": "^4.16.13",
16+
"preact": "^10.25.0",
17+
"typescript": "^5.6.3"
18+
},
19+
"devDependencies": {
20+
"prettier-plugin-astro": "^0.14.1"
21+
}
22+
}
Lines changed: 9 additions & 0 deletions
Loading

04-frameworks/15-astro/17-temas/readme.md

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,124 @@
22

33
No siempre tenemos que tirar de islas para todo, en este caso vamos a ver como tener tema claro y oscuro en nuestro sitio sin tener que tirar de islas.
44

5+
En esta caso con JavaScript y CSS lo vamos a dejar listo :).
6+
7+
# Manos a la obra
8+
9+
Vamos a añadir un componente para elegir el tema:
10+
11+
_./src/components/theme-icon.astro_
12+
13+
```tsx
14+
---
15+
---
16+
<button id="themeToggle">
17+
<svg width="30px" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">
18+
<path class="sun" fill-rule="evenodd" d="M12 17.5a5.5 5.5 0 1 0 0-11 5.5 5.5 0 0 0 0 11zm0 1.5a7 7 0 1 0 0-14 7 7 0 0 0 0 14zm12-7a.8.8 0 0 1-.8.8h-2.4a.8.8 0 0 1 0-1.6h2.4a.8.8 0 0 1 .8.8zM4 12a.8.8 0 0 1-.8.8H.8a.8.8 0 0 1 0-1.6h2.5a.8.8 0 0 1 .8.8zm16.5-8.5a.8.8 0 0 1 0 1l-1.8 1.8a.8.8 0 0 1-1-1l1.7-1.8a.8.8 0 0 1 1 0zM6.3 17.7a.8.8 0 0 1 0 1l-1.7 1.8a.8.8 0 1 1-1-1l1.7-1.8a.8.8 0 0 1 1 0zM12 0a.8.8 0 0 1 .8.8v2.5a.8.8 0 0 1-1.6 0V.8A.8.8 0 0 1 12 0zm0 20a.8.8 0 0 1 .8.8v2.4a.8.8 0 0 1-1.6 0v-2.4a.8.8 0 0 1 .8-.8zM3.5 3.5a.8.8 0 0 1 1 0l1.8 1.8a.8.8 0 1 1-1 1L3.5 4.6a.8.8 0 0 1 0-1zm14.2 14.2a.8.8 0 0 1 1 0l1.8 1.7a.8.8 0 0 1-1 1l-1.8-1.7a.8.8 0 0 1 0-1z"/>
19+
<path class="moon" fill-rule="evenodd" d="M16.5 6A10.5 10.5 0 0 1 4.7 16.4 8.5 8.5 0 1 0 16.4 4.7l.1 1.3zm-1.7-2a9 9 0 0 1 .2 2 9 9 0 0 1-11 8.8 9.4 9.4 0 0 1-.8-.3c-.4 0-.8.3-.7.7a10 10 0 0 0 .3.8 10 10 0 0 0 9.2 6 10 10 0 0 0 4-19.2 9.7 9.7 0 0 0-.9-.3c-.3-.1-.7.3-.6.7a9 9 0 0 1 .3.8z"/>
20+
</svg>
21+
</button>
22+
23+
<style>
24+
#themeToggle {
25+
border: 0;
26+
background: none;
27+
}
28+
.sun { fill: black; }
29+
.moon { fill: transparent; }
30+
31+
:global(.dark) .sun { fill: transparent; }
32+
:global(.dark) .moon { fill: white; }
33+
</style>
34+
```
35+
36+
Vamos añadir este componente en el _header_ de la página:
37+
38+
_./src/components/header.astro_
39+
40+
```diff
41+
---
42+
import Hamburger from './hamburger.astro';
43+
+ import ThemeIcon from './theme-icon.astro';
44+
import Navigation from './navigation.astro';
45+
---
46+
<header>
47+
<nav>
48+
<Hamburger />
49+
+ <ThemeIcon />
50+
<Navigation />
51+
</nav>
52+
</header>
53+
```
54+
55+
Y ahora vamos a añadir CSS global para definir los temas oscuros y claros:
56+
57+
_./src/styles/global.css_
58+
59+
````diff
60+
html {
61+
background-color: #f1f5f9;
62+
font-family: sans-serif;
63+
}
64+
65+
+ html.dark {
66+
+ background-color: #0d0950;
67+
+ color: #fff;
68+
+ }
69+
+
70+
+ .dark .nav-links a {
71+
+ color: #fff;
72+
+ }
73+
``
74+
75+
Y volvemos al _header_ para añadir la lógica de cambio de tema (esta vez la vamos a hacer en JavaScript en línea, lo podríamos sacar a un fichero ts).
76+
77+
78+
_./src/components/theme-icon.astro_
79+
80+
```diff
81+
</button>
82+
83+
<style>
84+
#themeToggle {
85+
border: 0;
86+
background: none;
87+
}
88+
.sun { fill: black; }
89+
.moon { fill: transparent; }
90+
91+
:global(.dark) .sun { fill: transparent; }
92+
:global(.dark) .moon { fill: white; }
93+
</style>
94+
95+
96+
+ <script is:inline>
97+
+ const theme = (() => {
98+
+ if (typeof localStorage !== 'undefined' && localStorage.getItem('theme')) {
99+
+ return localStorage.getItem('theme');
100+
+ }
101+
+ if (window.matchMedia('(prefers-color-scheme: dark)').matches) {
102+
+ return 'dark';
103+
+ }
104+
+ return 'light';
105+
+ })();
106+
+
107+
+ if (theme === 'light') {
108+
+ document.documentElement.classList.remove('dark');
109+
+ } else {
110+
+ document.documentElement.classList.add('dark');
111+
+ }
112+
+
113+
+ window.localStorage.setItem('theme', theme);
114+
+
115+
+ const handleToggleClick = () => {
116+
+ const element = document.documentElement;
117+
+ element.classList.toggle("dark");
118+
+
119+
+ const isDark = element.classList.contains("dark");
120+
+ localStorage.setItem("theme", isDark ? "dark" : "light");
121+
+ }
122+
+
123+
+ document.getElementById("themeToggle").addEventListener("click", handleToggleClick);
124+
+ </script>
125+
````
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
import Social from "./social.astro";
3+
---
4+
5+
<footer>
6+
<Social platform="twitter" username="lemoncoders" />
7+
<Social platform="github" username="lemoncode" />
8+
<Social platform="youtube" username="lemoncoders" />
9+
</footer>
10+
11+
<style>
12+
footer {
13+
display: flex;
14+
gap: 1rem;
15+
margin-top: 2rem;
16+
}
17+
</style>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import type { FunctionComponent, h } from "preact";
2+
import { useState } from "preact/hooks";
3+
4+
interface GreetingProps {
5+
messages: string[];
6+
}
7+
8+
const Greeting: FunctionComponent<GreetingProps> = ({ messages }) => {
9+
const randomMessage = (): string =>
10+
messages[Math.floor(Math.random() * messages.length)];
11+
12+
const [greeting, setGreeting] = useState<string>(messages[0]);
13+
14+
return (
15+
<div>
16+
<h3>{greeting}! Thank you for visiting!</h3>
17+
<button onClick={() => setGreeting(randomMessage())}>New Greeting</button>
18+
</div>
19+
);
20+
};
21+
22+
export default Greeting;

0 commit comments

Comments
 (0)