import * as React from "react"; import { useEffect, useRef, useState } from "react"; import { tinaField } from "tinacms/dist/react"; import { ObjectField } from "@tinacms/schema-tools/dist/types"; interface AnchoringProps { text?: string, // Default: Découvrez-en plus ! linkTo?: string // Default: #main-page } export const Anchoring = (props: AnchoringProps) => { const [opacity, setOpacity] = useState(1); const anchoringRef = useRef(null); const handleScrollOpacity = () => { const scrollTop = window.scrollY || window.pageYOffset; const elementHeight = anchoringRef.current?.clientHeight ? anchoringRef.current.clientHeight : 1; const newOpacity = (elementHeight - scrollTop) / elementHeight; setOpacity(newOpacity); }; const handleScrollClick = () => { const nextSection = anchoringRef?.current?.closest("section")?.parentElement?.nextElementSibling; nextSection?.scrollIntoView({ behavior: "smooth", block: "start" }); }; useEffect(() => { handleScrollOpacity(); // Initialize opacity on mount window.addEventListener("scroll", handleScrollOpacity); return () => { window.removeEventListener("scroll", handleScrollOpacity); }; }, []); return (

{ props.text }

); }; export const anchoringSchema: ObjectField = { type: "object", label: "Lien", name: "link", ui: { defaultItem: { text: "Découvrez-en plus !", linkTo: "#main-page", enabled: true } }, fields: [ { type: "boolean", label: "Active", name: "enabled" }, { type: "string", label: "Texte", name: "text" }, { type: "string", label: "Lien", name: "linkTo" } ] };