update /www and tailwind-plugin to support tailwind v4. Thanks to the merge of #3034, the work of integrating and advancing #2903 is promoted. related #3034 related #2903 close #3056 close #2819 --patch-1 based on review, 1. del compiler and make it more clean and simple, 2. fix init tailwind part, 3. fix test tailwind part --patch-2 1. fix the docs 2. fix the docs of plugin and version --patch-3 1. Addressed various maintenance tasks (chores) 2. Updated the Tailwind CSS plugin to align with this release. Generated a fresh project using the updated init configuration and 3. deployed it via Deno Deploy EA. Deployment is functioning correctly. --patch-4 Move postcss imports to Tailwind section in init script --patch-5 Refactored Tailwind plugin type definitions by copying PluginOptions properties from @tailwindcss/postcss and adding Fresh-specific exclude option, while exporting types from package entrypoint for better accessibility. Test Project: https://fresh.fresh-press.deno.net/ Test Project Repository: https://github.com/SisyphusZheng/fresh-tailwindv4 Test Plugin: https://jsr.io/@freshpress/fresh-plugin-tailwind@1.0.0 --------- Co-authored-by: 李嘉图·M·路 <146103794+Ricardo-M-Zheng@users.noreply.github.com> Co-authored-by: Asher Gomez <ashersaupingomez@gmail.com> Co-authored-by: Marvin Hagemeister <marvin@deno.com>
186 lines
6.1 KiB
TypeScript
186 lines
6.1 KiB
TypeScript
import { useEffect, useRef } from "preact/hooks";
|
|
import type { MarkdownHeading } from "../utils/markdown.ts";
|
|
import { useSignal } from "@preact/signals";
|
|
|
|
export interface TableOfContentsProps {
|
|
headings: MarkdownHeading[];
|
|
}
|
|
|
|
function setActiveLink(
|
|
container: HTMLElement,
|
|
marker: HTMLElement,
|
|
id: string,
|
|
) {
|
|
container.querySelectorAll(`a`).forEach((link) =>
|
|
link.classList.remove("active")
|
|
);
|
|
const tocLink = container.querySelector(
|
|
`a[href="#${id}"]`,
|
|
) as HTMLElement;
|
|
|
|
if (tocLink === null) return;
|
|
|
|
tocLink.classList.add("active");
|
|
|
|
const rect = tocLink
|
|
.getBoundingClientRect();
|
|
const markerRect = marker.getBoundingClientRect();
|
|
|
|
const top = tocLink.offsetTop + (rect.height / 2) -
|
|
(markerRect.height / 2);
|
|
marker.style.cssText = `transform: translate3d(0, ${top}px, 0); opacity: 1`;
|
|
}
|
|
|
|
export function TableOfContents({ headings }: TableOfContentsProps) {
|
|
const ref = useRef<HTMLDivElement>(null);
|
|
const refMarker = useRef<HTMLDivElement>(null);
|
|
const isOpen = useSignal(false);
|
|
|
|
useEffect(() => {
|
|
if (!ref.current) return;
|
|
const container = ref.current;
|
|
|
|
const activeList = new Array(headings.length).fill(false);
|
|
const visibleList = new Array(headings.length).fill(false);
|
|
|
|
const marker = refMarker.current!;
|
|
const observer = new IntersectionObserver((entries) => {
|
|
for (let i = 0; i < entries.length; i++) {
|
|
const entry = entries[i];
|
|
const target = entry.target;
|
|
|
|
for (let j = 0; j < headings.length; j++) {
|
|
const heading = headings[j];
|
|
if (heading.id === target.id) {
|
|
const active = entry.isIntersecting ||
|
|
entry.boundingClientRect.top < 0;
|
|
activeList[j] = active;
|
|
visibleList[j] = entry.isIntersecting;
|
|
}
|
|
}
|
|
}
|
|
|
|
// Reset links
|
|
for (let i = 0; i < headings.length; i++) {
|
|
const id = headings[i].id;
|
|
const tocLink = container.querySelector(
|
|
`a[href="#${id}"]`,
|
|
);
|
|
if (tocLink !== null) {
|
|
tocLink.classList.remove("active");
|
|
}
|
|
}
|
|
|
|
let activeIdx = visibleList.indexOf(true);
|
|
if (activeIdx < 0) {
|
|
activeIdx = activeList.lastIndexOf(true);
|
|
}
|
|
|
|
if (activeIdx > -1) {
|
|
const id = headings[activeIdx].id;
|
|
setActiveLink(container, marker, id);
|
|
} else {
|
|
marker.style.cssText = `transform: translate3d(0, 0, 0); opacity: 0`;
|
|
}
|
|
});
|
|
|
|
document.querySelectorAll(
|
|
".markdown-body h2, .markdown-body h3, .markdown-body h4, .markdown-body h5, .markdown-body h6",
|
|
).forEach((elem) => {
|
|
observer.observe(elem);
|
|
});
|
|
|
|
return () => {
|
|
observer.disconnect();
|
|
};
|
|
}, [headings]);
|
|
|
|
return (
|
|
<div
|
|
ref={ref}
|
|
class="relative xl:order-2 w-56 xl:max-w-xs xl:top-14 shrink-0"
|
|
>
|
|
{headings.length > 0 && (
|
|
<>
|
|
<div class="xl:hidden mx-4 md:mx-0 mt-4 md:mt-0">
|
|
<button
|
|
type="button"
|
|
id="toc-outline-btn"
|
|
onClick={() => isOpen.value = !isOpen.value}
|
|
class="bg-background-primary py-2 px-4 rounded-sm border border-foreground-secondary/30 flex items-center hover:border-fresh-green/80 transition-colors text-sm"
|
|
>
|
|
On this page
|
|
<svg
|
|
class={`w-4 h-4 inline-block ml-2 -rotate-90 [&.active]:rotate-0 ${
|
|
isOpen ? "active" : ""
|
|
}`}
|
|
>
|
|
<use href="/icons.svg#arrow-down" />
|
|
</svg>
|
|
</button>
|
|
{isOpen.value && (
|
|
<div class="mt-2 pl-4 border-l border-foreground-primary/20 text-[13px] leading-7">
|
|
<nav aria-labelledby="toc-outline-btn">
|
|
<ul>
|
|
{headings.map((heading) => {
|
|
return (
|
|
<li key={heading.id}>
|
|
<a
|
|
href={`#${heading.id}`}
|
|
class="block truncatetext-gray-600 dark:text-gray-400"
|
|
// deno-lint-ignore react-no-danger
|
|
dangerouslySetInnerHTML={{ __html: heading.html }}
|
|
/>
|
|
</li>
|
|
);
|
|
})}
|
|
</ul>
|
|
</nav>
|
|
</div>
|
|
)}
|
|
</div>
|
|
<div class="hidden xl:block xl:sticky xl:top-40">
|
|
<div class="relative">
|
|
<div
|
|
ref={refMarker}
|
|
class="marker w-[2px] bg-green-400 h-5 absolute top-0 opacity-0 transition-all"
|
|
/>
|
|
<div class="pl-4 border-l border-foreground-secondary/20 text-[13px] leading-7">
|
|
<div role="heading" aria-level={2} class="font-semibold">
|
|
On this page
|
|
</div>
|
|
<nav aria-labelledby="doc-outline-aria-label">
|
|
<span id="doc-outline-aria-label" class="sr-only">
|
|
Table of Contents for current page
|
|
</span>
|
|
<ul>
|
|
{headings.map((heading) => {
|
|
return (
|
|
<li key={heading.id}>
|
|
<a
|
|
href={`#${heading.id}`}
|
|
class="block truncate transition-colors text-gray-600 dark:text-gray-400 [&.active]:text-green-600 dark:[&.active]:text-green-300"
|
|
onClick={() => {
|
|
setActiveLink(
|
|
ref.current!,
|
|
refMarker.current!,
|
|
heading.id,
|
|
);
|
|
}}
|
|
// deno-lint-ignore react-no-danger
|
|
dangerouslySetInnerHTML={{ __html: heading.html }}
|
|
/>
|
|
</li>
|
|
);
|
|
})}
|
|
</ul>
|
|
</nav>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</>
|
|
)}
|
|
</div>
|
|
);
|
|
}
|