Skip to content

Commit 04ea8ee

Browse files
committed
feat: add basic email box
1 parent e72da0b commit 04ea8ee

File tree

5 files changed

+84
-21
lines changed

5 files changed

+84
-21
lines changed

astro.config.mjs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { defineConfig } from 'astro/config';
22
import tailwind from '@astrojs/tailwind';
33
import image from '@astrojs/image';
44
import react from '@astrojs/react';
5-
65
import prefetch from '@astrojs/prefetch';
76

87
// https://astro.build/config

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,13 @@
1616
"@astrojs/tailwind": "^3.1.2",
1717
"@types/react": "^18.0.21",
1818
"@types/react-dom": "^18.0.6",
19-
"astro": "^2.4.5",
19+
"astro": "^2.5.1",
2020
"prettier": "^2.8.8",
2121
"prettier-plugin-tailwindcss": "^0.3.0",
2222
"react": "^18.0.0",
2323
"react-dom": "^18.0.0",
2424
"sharp": "^0.32.1",
25-
"tailwindcss": "^3.0.24"
25+
"tailwindcss": "^3.0.24",
26+
"zod": "^3.21.4"
2627
}
2728
}

pnpm-lock.yaml

Lines changed: 34 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/components/EmailSub.astro

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
---
2+
---
3+
4+
<div class="mx-auto w-96 py-4">
5+
<label class="mb-2 block text-lg font-bold text-white" for="email"
6+
>Subscribe to our newsletter</label
7+
>
8+
<form data-email-subscription class="flex place-content-between">
9+
<input
10+
class="w-full rounded-lg bg-slate-900 p-2 text-white"
11+
type="email"
12+
name="email"
13+
id="email"
14+
placeholder="Enter your email"
15+
required
16+
/>
17+
<button class="ml-2 rounded-lg bg-slate-900 p-2 text-white" type="submit"
18+
>Subscribe</button
19+
>
20+
</form>
21+
</div>
22+
23+
<script>
24+
import { z } from 'zod';
25+
26+
const forms = document.querySelectorAll('[data-email-subscription]');
27+
forms.forEach((form) => {
28+
form.addEventListener('submit', (e) => {
29+
e.preventDefault();
30+
// TODO: Send email to some service
31+
const email = z
32+
.string()
33+
.email()
34+
.safeParse((e.target as any)?.email?.value);
35+
36+
if (!email.success) {
37+
alert('Please enter a valid email address');
38+
return;
39+
}
40+
41+
console.log(email.data);
42+
form.innerHTML = 'Thank you for subscribing!';
43+
});
44+
});
45+
</script>

src/components/Layout.astro

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
---
22
import { Image } from '@astrojs/image/components';
3+
import EmailSub from '../components/EmailSub.astro';
34
45
interface Props {
56
title: string;
@@ -35,5 +36,6 @@ const { title } = Astro.props;
3536
>
3637
</header>
3738
<slot />
39+
<EmailSub />
3840
</body>
3941
</html>

0 commit comments

Comments
 (0)