From 5589b2244d89389085c91ce984b947c5927ede92 Mon Sep 17 00:00:00 2001 From: olayway Date: Wed, 18 May 2022 12:57:53 +0200 Subject: [PATCH] [site/components][f]: tooltip with clipped content --- site/components/Anchor.js | 55 +++++++++++++++++------------------ site/components/Tooltip.js | 26 +++++++++++------ site/config/siteConfig.js | 1 + site/styles/global.css | 12 ++++++++ site/utils/absolutePath.js | 9 ------ site/utils/documentExtract.js | 14 ++++----- 6 files changed, 62 insertions(+), 55 deletions(-) delete mode 100644 site/utils/absolutePath.js diff --git a/site/components/Anchor.js b/site/components/Anchor.js index 4abc02c..59137aa 100644 --- a/site/components/Anchor.js +++ b/site/components/Anchor.js @@ -15,7 +15,7 @@ import { useRole, } from '@floating-ui/react-dom-interactions' -import getAbsolutePath from '../utils/absolutePath' +import siteConfig from '../config/siteConfig.js' import documentExtract from '../utils/documentExtract' import { Tooltip } from './Tooltip' @@ -52,7 +52,7 @@ export const Anchor = (props) => { ] }); const { getReferenceProps, getFloatingProps } = useInteractions([ - useHover(context, { delay: { open: 100, close: 0 } }), + useHover(context, { delay: 100 }), useRole(context, { role: 'tooltip' }), useDismiss(context, { ancestorScroll: true }) ]); @@ -64,21 +64,17 @@ export const Anchor = (props) => { }, [showTooltip]) const fetchPreview = async () => { - console.log("Fetching...") setPreviewLoaded(false); - const basePath = "http://localhost:3000"; // TODO - const currentPath = router.asPath; - const relativePath = props.href.split(".")[0]; // TBD temp remove .md - const absolutePath = getAbsolutePath({ currentPath, basePath, relativePath }); - - const response = await fetch(absolutePath); + const path = new URL(props.href, document.baseURI).pathname; + const rawContentPath = [siteConfig.rawContentBaseUrl, path].join("") + const response = await fetch(rawContentPath); if (response.status !== 200) { // TODO console.log(`Looks like there was a problem. Status Code: ${response.status}`) return } - const html = await response.text(); - const extract = documentExtract(html); + const md = await response.text(); + const extract = documentExtract(md); setPreview(extract); setPreviewLoaded(true); @@ -93,24 +89,25 @@ export const Anchor = (props) => { return - - { preview } - + { showTooltip && previewLoaded && + + { preview } + + } ; } diff --git a/site/components/Tooltip.js b/site/components/Tooltip.js index d63e2eb..188e131 100644 --- a/site/components/Tooltip.js +++ b/site/components/Tooltip.js @@ -1,18 +1,24 @@ import React from 'react'; -const tooltipStyles = (theme) => ({ +const tooltipBoxStyle = (theme) => ({ height: 'auto', - maxWidth: '80vw', - padding: '1rem 2rem', + maxWidth: '60vw', + padding: '1rem', background: theme === 'light' ? '#fff' : '#000', color: theme === 'light' ? 'rgb(99, 98, 98)' : '#A8A8A8', borderRadius: '4px', boxShadow: 'rgba(0, 0, 0, 0.55) 0px 0px 16px -3px', - fontSize: '0.9em' }) -const tooltipArrowStyles = ({ theme, x, y, side }) => ({ +const tooltipBodyStyle = () => ({ + maxHeight: '3.6rem', + position: 'relative', + lineHeight: '1.2rem', + overflow: 'hidden', +}) + +const tooltipArrowStyle = ({ theme, x, y, side }) => ({ position: "absolute", left: x != null ? `${x}px` : '', top: y != null ? `${y}px` : '', @@ -36,11 +42,13 @@ export const Tooltip = React.forwardRef((props, ref) => { }[placement.split('-')[0]]; return ( -
-
- { children } +
+
+
+ { children } +
-
{ - const absolutePath = currentPath.slice(1).split("/") - absolutePath.pop(); // remove current page name - absolutePath.unshift(basePath); - absolutePath.push(relativePath); - return absolutePath.join("/"); -}; - -export default absolutePath; diff --git a/site/utils/documentExtract.js b/site/utils/documentExtract.js index 8f6ab55..f6462dc 100644 --- a/site/utils/documentExtract.js +++ b/site/utils/documentExtract.js @@ -1,15 +1,13 @@ import { unified } from 'unified' -import rehypeParse from 'rehype-parse' +import remarkParse from 'remark-parse' import find from 'unist-util-find' -import { toString } from 'hast-util-to-string' +import { toString } from 'mdast-util-to-string' -// get first paragraph inside article's main tag -const documentExtract = (htmlString) => { - const hast = unified().use(rehypeParse).parse(htmlString); - const article = find(hast, (node) => node.tagName === "article"); - const main = find(article, (node) => node.tagName === "main"); - const paragraph = find(main, (node) => node.tagName === "p"); +// get first paragraph +const documentExtract = (md) => { + const mdast = unified().use(remarkParse).parse(md); + let paragraph = find(mdast, (node) => node.type === "paragraph"); return toString(paragraph); }