diff --git a/components/tinaMarkdownComponents/docAndBlogComponents.tsx b/components/tinaMarkdownComponents/docAndBlogComponents.tsx index b653e55df..305852258 100644 --- a/components/tinaMarkdownComponents/docAndBlogComponents.tsx +++ b/components/tinaMarkdownComponents/docAndBlogComponents.tsx @@ -1,9 +1,10 @@ +import { CheckIcon, ClipboardIcon } from '@heroicons/react/24/outline'; import { CardGrid } from 'components/blocks/CardGrid'; import RecipeBlock from 'components/blocks/Recipe'; import { GraphQLQueryResponseTabs } from 'components/ui/GraphQLQueryResponseTabs'; import dynamic from 'next/dynamic'; import Image from 'next/image'; -import { useState } from 'react'; +import React, { useEffect, useState } from 'react'; import { BiRightArrowAlt } from 'react-icons/bi'; import { FaMinus, FaPlus } from 'react-icons/fa'; import { FiLink } from 'react-icons/fi'; @@ -11,8 +12,6 @@ import { Components, TinaMarkdown } from 'tinacms/dist/rich-text'; import { getDocId } from 'utils/docs/getDocIds'; import { WarningCallout } from 'utils/shortcodes'; import { Prism } from '../styles/Prism'; -import React from 'react'; -import { CheckIcon, ClipboardIcon } from '@heroicons/react/24/outline'; const ScrollBasedShowcase = dynamic( () => import('./templateComponents/scrollBasedShowcase'), { @@ -316,31 +315,35 @@ export const docAndBlogComponents: Components<{ // @ts-ignore TODO: fix this in TinaCMS code_block: ({ value, lang, children }) => { const [hasCopied, setHasCopied] = React.useState(false); - + const handleCopy = () => { - navigator.clipboard.writeText(children || value || ""); + navigator.clipboard.writeText(children || value || ''); setHasCopied(true); setTimeout(() => setHasCopied(false), 2000); }; - + return (
); }, - + GraphQLCodeBlock: ({ query, response, preselectResponse }) => { return ( { + event.preventDefault(); + scrollToElement(id); + window.history.pushState(null, '', linkHref); + }; + + const scrollToElement = (elementId) => { + const element = document.getElementById(elementId); + if (element) { + const offset = 130; //offset in pixels + const elementPosition = + element.getBoundingClientRect().top + window.scrollY; + const offsetPosition = elementPosition - offset; + + window.scrollTo({ + top: offsetPosition, + behavior: 'smooth', + }); + } + }; + + useEffect(() => { + if (window.location.hash) { + const hash = window.location.hash.substring(1); + scrollToElement(hash); + } + //this is used for when you get sent a link with a hash (i.e link to a header) + }, []); + return ( navigator.clipboard.writeText(`tina.io${linkHref}`)} + onClick={handleHeaderClick} > {' '} {children}