|
| 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> |
0 commit comments