diff --git a/docs/src/components/navigation/hash_observer.ts b/docs/src/components/navigation/hash_observer.ts new file mode 100644 index 0000000..dca4d1a --- /dev/null +++ b/docs/src/components/navigation/hash_observer.ts @@ -0,0 +1,69 @@ +import { createSignal, onCleanup, onMount } from "solid-js"; + +export const createHashObserver = (baseUrl: string) => { + let isUserNavigation = false; + let timerId: number | undefined; + const [hash, setHash] = createSignal(""); + + const isHash = (...args: string[]) => { + return args.some((arg) => hash() === arg); + }; + + const onNavigation = (event: MouseEvent) => { + const target = event.target as HTMLAnchorElement; + const href = target.href; + const hash = new URL(href).hash; + + isUserNavigation = true; + setHash(hash); + clearTimeout(timerId); + + timerId = setTimeout(() => { + isUserNavigation = false; + }, 1000); + }; + + onMount(() => { + const sections = document.querySelectorAll("section[id]"); + const options: IntersectionObserverInit = { + threshold: 0.6 + }; + + const observer = new IntersectionObserver((entries) => { + if (isUserNavigation) { + return; + } + + const visibleEntries = entries.filter((entry) => entry.isIntersecting); + + if (visibleEntries.length > 0) { + const visibleEntry = visibleEntries[0]; + // const visibleRatio = visibleEntry.intersectionRatio; + const sectionId = visibleEntry.target.getAttribute("id"); + + if (!sectionId) { + return; + } + + const newHash = `#${sectionId}`; + + setHash(newHash); + history.replaceState(null, "", baseUrl + newHash); + } + }, options); + + sections.forEach((section) => { + observer.observe(section); + }); + + onCleanup(() => { + observer.disconnect(); + clearTimeout(timerId); + }); + }); + + return { + isHash, + onNavigation + }; +}; diff --git a/docs/src/routes/get-started/index.tsx b/docs/src/routes/get-started/index.tsx index 745da70..749ca83 100644 --- a/docs/src/routes/get-started/index.tsx +++ b/docs/src/routes/get-started/index.tsx @@ -3,31 +3,58 @@ import { Link } from "solid-uix"; import { Code } from "~/components/code/code"; import { Container } from "~/components/container/container"; import { Footer } from "~/components/footer/footer"; +import { createHashObserver } from "~/components/navigation/hash_observer"; import { Navigation } from "~/components/navigation/navigation"; const GetStarted = () => { + const { isHash, onNavigation } = createHashObserver("/get-started"); + return (
Get started - + Install - + Register fields - + Apply validation - + Controlled Inputs - + Handle errors - + TypeScript @@ -35,7 +62,7 @@ const GetStarted = () => {

Get started

-
+

Install @@ -43,9 +70,9 @@ const GetStarted = () => {

Install Solid Hook Form package.

npm install solid-hook-form -
+ -
+

Example @@ -74,11 +101,11 @@ export const ExampleForm = () => { ) }`} -

+ -
+

- + Register fields

@@ -116,11 +143,11 @@ export const ExampleForm = () => { ) }`} -
+ -
+

- + Apply validation

@@ -173,9 +200,9 @@ export const ExampleForm = () => { ) }`} -
+ -
+

Controlled Inputs @@ -274,11 +301,11 @@ export const ExampleForm = () => { ) }`} -

+ -
+

- + Handle errors

@@ -320,9 +347,9 @@ export const ExampleForm = () => { ) }`} -
+ -
+

TypeScript @@ -360,7 +387,7 @@ export const ExampleForm = () => { ) }`} -

+