Skip to content

Commit 851f75a

Browse files
committed
Added scroll into view test.
1 parent 5e5c99b commit 851f75a

File tree

3 files changed

+111
-0
lines changed

3 files changed

+111
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import type { Actions, PageServerLoad } from './$types';
2+
import { message, superValidate } from '$lib/server';
3+
import { schema } from './schema';
4+
import { fail } from '@sveltejs/kit';
5+
6+
export const load = (async () => {
7+
const form = await superValidate(schema);
8+
return { form };
9+
}) satisfies PageServerLoad;
10+
11+
export const actions = {
12+
default: async ({ request }) => {
13+
const formData = await request.formData();
14+
console.log(formData);
15+
16+
const form = await superValidate(formData, schema);
17+
console.log('POST', form);
18+
19+
if (!form.valid) return fail(400, { form });
20+
21+
return message(form, 'Posted OK!');
22+
}
23+
} satisfies Actions;
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<script lang="ts">
2+
import { superForm } from '$lib/client';
3+
import type { PageData } from './$types';
4+
import SuperDebug from '$lib/client/SuperDebug.svelte';
5+
import { schema } from './schema';
6+
7+
export let data: PageData;
8+
9+
const { form, errors, tainted, message, enhance } = superForm(data.form, {
10+
scrollToError: { behavior: 'smooth', block: 'center', inline: 'center' },
11+
taintedMessage: null
12+
});
13+
14+
let nameField: HTMLInputElement;
15+
let isVisible = false;
16+
17+
function checkVisible() {
18+
if (isElementInViewport(nameField)) isVisible = true;
19+
}
20+
21+
// Thanks to https://stackoverflow.com/a/7557433
22+
function isElementInViewport(el: Element) {
23+
var rect = el.getBoundingClientRect();
24+
25+
return (
26+
rect.top >= 0 &&
27+
rect.left >= 0 &&
28+
rect.bottom <=
29+
(window.innerHeight ||
30+
document.documentElement
31+
.clientHeight) /* or $(window).height() */ &&
32+
rect.right <=
33+
(window.innerWidth ||
34+
document.documentElement.clientWidth) /* or $(window).width() */
35+
);
36+
}
37+
</script>
38+
39+
<div>Visible: {isVisible}</div>
40+
41+
<div class="scroller" on:scroll={checkVisible}>
42+
{#if $message}<h4>{$message}</h4>{/if}
43+
44+
<form method="POST" use:enhance>
45+
<div>
46+
<button id="submit">Submit</button>
47+
</div>
48+
<label>
49+
Name: <input
50+
bind:this={nameField}
51+
name="name"
52+
id="name"
53+
aria-invalid={$errors.name ? 'true' : undefined}
54+
bind:value={$form.name}
55+
/>
56+
{#if $errors.name}<span class="invalid">{$errors.name}</span>{/if}
57+
</label>
58+
</form>
59+
</div>
60+
61+
<style lang="scss">
62+
.scroller {
63+
height: 300px;
64+
overflow: scroll;
65+
}
66+
67+
form {
68+
margin: 2rem 0;
69+
70+
label {
71+
margin: 1000px 1000px;
72+
padding: 500px;
73+
}
74+
75+
input {
76+
background-color: #dedede;
77+
}
78+
79+
.invalid {
80+
color: crimson;
81+
}
82+
}
83+
</style>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { z } from 'zod';
2+
3+
export const schema = z.object({
4+
name: z.string().min(2)
5+
});

0 commit comments

Comments
 (0)