Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 7 additions & 41 deletions lib/code/header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,12 @@ interface HeaderProps {
}

class Header extends Component<HeaderProps> {
private ticking = false;
private tippyCopyTimeout: ReturnType<typeof setTimeout> | undefined;
private readonly guid = runtime.guid();
private readonly headerRef = React.createRef<HTMLDivElement>();
private readonly tippyFullScreenRef = React.createRef<TooltipHandle>();
private readonly tippyCopyContentRef = React.createRef<TooltipHandle>();
private stopWatchingScroll: (() => void) | undefined;
private stickyHeader: runtime.StickyHeaderWatch | undefined;

constructor(props: HeaderProps) {
super(props);
Expand All @@ -41,9 +40,10 @@ class Header extends Component<HeaderProps> {

override componentDidMount() {
const vm = this;
vm.updateHeaderScrollClass();
vm.stopWatchingScroll = runtime.onViewportScroll(
vm.updateHeaderScrollClass,
vm.stickyHeader = runtime.watchStickyHeader(
() => vm.props.wrapperRef.current,
() => vm.headerRef.current,
() => vm.props.fullscreen === true,
);
}

Expand Down Expand Up @@ -171,41 +171,7 @@ class Header extends Component<HeaderProps> {
}

updateHeaderScrollClass() {
const vm = this;

if (vm.ticking !== true) {
requestAnimationFrame(() => {
let rec;
let top;
let height;

const wrapper = vm.props.wrapperRef?.current;

if (vm.props.fullscreen === true) {
vm.headerRef?.current?.classList?.remove("scroll");
} else {
if (wrapper) {
rec = wrapper.getBoundingClientRect();
top = rec?.top || 0;
height = rec?.height || 0;

if (top >= 56) {
vm.headerRef?.current?.classList?.remove("scroll");
} else {
if (height + top > 106) {
vm.headerRef?.current?.classList?.add("scroll");
} else {
vm.headerRef?.current?.classList?.remove("scroll");
}
}
}
}

vm.ticking = false;
});

vm.ticking = true;
}
this.stickyHeader?.update();
}

previewButtonComponent(props: HeaderProps) {
Expand Down Expand Up @@ -248,7 +214,7 @@ class Header extends Component<HeaderProps> {

override componentWillUnmount() {
const vm = this;
vm.stopWatchingScroll?.();
vm.stickyHeader?.stop();
}

override render() {
Expand Down
48 changes: 7 additions & 41 deletions lib/mermaid/header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,11 @@ interface HeaderProps {
}

class Header extends Component<HeaderProps> {
private ticking = false;
private tippyCopyTimeout: ReturnType<typeof setTimeout> | undefined;
private readonly headerRef = React.createRef<HTMLDivElement>();
private readonly tippyFullScreenRef = React.createRef<TooltipHandle>();
private readonly tippyCopyContentRef = React.createRef<TooltipHandle>();
private stopWatchingScroll: (() => void) | undefined;
private stickyHeader: runtime.StickyHeaderWatch | undefined;

constructor(props: HeaderProps) {
super(props);
Expand All @@ -35,9 +34,10 @@ class Header extends Component<HeaderProps> {

override componentDidMount() {
const vm = this;
vm.updateHeaderScrollClass();
vm.stopWatchingScroll = runtime.onViewportScroll(
vm.updateHeaderScrollClass,
vm.stickyHeader = runtime.watchStickyHeader(
() => vm.props.wrapperRef.current,
() => vm.headerRef.current,
() => vm.props.fullscreen === true,
);
}

Expand Down Expand Up @@ -106,46 +106,12 @@ class Header extends Component<HeaderProps> {
}

updateHeaderScrollClass() {
const vm = this;

if (vm.ticking !== true) {
requestAnimationFrame(() => {
let rec;
let top;
let height;

const wrapper = vm.props?.wrapperRef?.current;

if (vm.props.fullscreen === true) {
vm.headerRef?.current?.classList?.remove("scroll");
} else {
if (wrapper) {
rec = wrapper.getBoundingClientRect();
top = rec?.top || 0;
height = rec?.height || 0;

if (top >= 56) {
vm.headerRef?.current?.classList?.remove("scroll");
} else {
if (height + top > 106) {
vm.headerRef?.current?.classList?.add("scroll");
} else {
vm.headerRef?.current?.classList?.remove("scroll");
}
}
}
}

vm.ticking = false;
});

vm.ticking = true;
}
this.stickyHeader?.update();
}

override componentWillUnmount() {
const vm = this;
vm.stopWatchingScroll?.();
vm.stickyHeader?.stop();
}

override render() {
Expand Down
145 changes: 145 additions & 0 deletions lib/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,3 +148,148 @@ export function onViewportScroll(handler: () => void): () => void {
window.removeEventListener("resize", handler);
};
}

/** Distance from the host's sticky chat header at which a block toolbar goes flat. */
const STICKY_HEADER_OFFSET = 56;

/** Below this the block is nearly scrolled past, so the toolbar drops away. */
const STICKY_HEADER_MIN_VISIBLE = 106;

interface StickyHeader {
readonly wrapper: () => HTMLElement | null;
readonly header: () => HTMLElement | null;
readonly fullscreen: () => boolean;
target: HTMLElement | null;
stuck: boolean;
}

/** Handle for one block toolbar registered with {@link watchStickyHeader}. */
export interface StickyHeaderWatch {
/** Recompute this toolbar now, for changes no scroll reports (fullscreen). */
update(): void;
/** Stop watching and clear the class, so a later registration starts from the element's real state. */
stop(): void;
}

const stickyPending = new Set<StickyHeader>();
const stickyOnScreen = new Set<StickyHeader>();
const stickyByTarget = new Map<Element, StickyHeader>();
let stickyObserver: IntersectionObserver | undefined;
let stopStickyScroll: (() => void) | undefined;

function isStuck(entry: StickyHeader): boolean {
if (entry.target === null || entry.fullscreen()) return false;
const rect = entry.target.getBoundingClientRect();
const top = rect.top || 0;
return top < STICKY_HEADER_OFFSET && top + (rect.height || 0) > STICKY_HEADER_MIN_VISIBLE;
}

function setStuck(entry: StickyHeader, stuck: boolean): void {
if (entry.stuck === stuck) return;
entry.stuck = stuck;
entry.header()?.classList.toggle("scroll", stuck);
}

/** Reads every on-screen block before writing any class, so one pass lays out once. */
function updateOnScreen(): void {
const next = [...stickyOnScreen].map((entry) => [entry, isStuck(entry)] as const);
for (const [entry, stuck] of next) setStuck(entry, stuck);
}

/** Scroll and resize are watched only while some block is on screen. */
function syncStickyScroll(): void {
if (stickyOnScreen.size > 0) {
stopStickyScroll ??= onViewportScroll(updateOnScreen);
} else {
stopStickyScroll?.();
stopStickyScroll = undefined;
}
}

function onStickyIntersection(entries: IntersectionObserverEntry[]): void {
for (const change of entries) {
const entry = stickyByTarget.get(change.target);
if (entry === undefined) continue;
if (change.isIntersecting) {
stickyOnScreen.add(entry);
setStuck(entry, isStuck(entry));
} else {
// Off screen the block cannot span the toolbar band.
stickyOnScreen.delete(entry);
setStuck(entry, false);
}
}
syncStickyScroll();
}

/** Attach registrations whose wrapper ref has committed since they were made. */
function attachPending(): void {
for (const entry of stickyPending) {
const target = entry.wrapper();
if (target === null) continue;
stickyPending.delete(entry);
entry.target = target;
stickyByTarget.set(target, entry);
if (typeof IntersectionObserver === "undefined") {
stickyOnScreen.add(entry);
setStuck(entry, isStuck(entry));
} else {
stickyObserver ??= new IntersectionObserver(onStickyIntersection);
stickyObserver.observe(target);
}
}
syncStickyScroll();
}

/**
* Keep a block toolbar's "scroll" class in step with its block's position:
* set while the block spans the band below the host's sticky header, cleared
* otherwise and in fullscreen.
*
* One IntersectionObserver and one capturing scroll listener serve every
* block on the page, and the listener measures only blocks on screen, so an
* idle transcript with hundreds of blocks costs nothing per scroll frame. The
* class is written only when the state changes: `classList` add and remove
* rewrite the attribute, and so queue a mutation record, even when the token
* is already in the requested state.
*
* The wrapper is read on the next frame, after the host commits its ref;
* without IntersectionObserver every block counts as on screen.
* @param wrapper - reads the block element whose position decides the state.
* @param header - reads the toolbar element that carries the class.
* @param fullscreen - reads whether the block is fullscreen.
* @returns the registration's update and stop handle.
*/
export function watchStickyHeader(
wrapper: () => HTMLElement | null,
header: () => HTMLElement | null,
fullscreen: () => boolean,
): StickyHeaderWatch {
const entry: StickyHeader = { wrapper, header, fullscreen, target: null, stuck: false };

if (typeof window === "undefined") {
return { update() {}, stop() {} };
}

stickyPending.add(entry);
requestAnimationFrame(attachPending);

return {
update() {
if (entry.target !== null && (fullscreen() || stickyOnScreen.has(entry))) {
setStuck(entry, isStuck(entry));
}
},
stop() {
setStuck(entry, false);
stickyPending.delete(entry);
stickyOnScreen.delete(entry);
if (entry.target !== null) {
stickyByTarget.delete(entry.target);
stickyObserver?.unobserve(entry.target);
entry.target = null;
}
syncStickyScroll();
},
};
}
64 changes: 19 additions & 45 deletions lib/table/header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,6 @@ import { defaultUi } from "../config";
import type { UiConfig } from "../config";
import Tooltip, { type TooltipHandle } from "../tooltip";

/** Distance from the sticky chat header at which the toolbar goes flat. */
const HEADER_OFFSET = 56;

/** Below this the table is nearly scrolled past, so the toolbar drops away. */
const HEADER_MIN_VISIBLE = 106;

interface TableHeaderProps {
/** This renderer's own bus, so blocks report only to the renderer that made them. */
events?: Emitter | undefined;
Expand All @@ -42,51 +36,31 @@ function TableHeaderComponent(props: TableHeaderProps) {
const headerRef = useRef<HTMLDivElement | null>(null);
const tippyCopyRef = useRef<TooltipHandle | null>(null);

// The scroll handler is registered once but reads the current fullscreen
// value, so it goes through a ref rather than the closed-over state.
const tickingRef = useRef(false);
// The watch is registered once but reads the current fullscreen value, so
// it goes through a ref rather than the closed-over prop.
const fullscreenRef = useRef(fullscreen);
fullscreenRef.current = fullscreen;
const stickyHeaderRef = useRef<runtime.StickyHeaderWatch | null>(null);
const copyTimeoutRef = useRef<ReturnType<typeof setTimeout> | undefined>(
undefined,
);

const updateHeaderScrollClass = useCallback(() => {
if (tickingRef.current === true) {
return;
}

tickingRef.current = true;

requestAnimationFrame(() => {
const header = headerRef.current;
const wrapper = wrapperRef.current;

tickingRef.current = false;

if (!header) {
return;
}

if (fullscreen === true || !wrapper) {
header.classList.remove("scroll");
return;
}

const rect = wrapper.getBoundingClientRect();
const top = rect?.top || 0;
const height = rect?.height || 0;

if (top < HEADER_OFFSET && height + top > HEADER_MIN_VISIBLE) {
header.classList.add("scroll");
} else {
header.classList.remove("scroll");
}
});
}, [fullscreen, wrapperRef]);
useEffect(() => {
const watch = runtime.watchStickyHeader(
() => wrapperRef.current,
() => headerRef.current,
() => fullscreenRef.current === true,
);
stickyHeaderRef.current = watch;
return () => {
stickyHeaderRef.current = null;
watch.stop();
};
}, [wrapperRef]);

useEffect(() => {
updateHeaderScrollClass();
return runtime.onViewportScroll(updateHeaderScrollClass);
}, [updateHeaderScrollClass]);
stickyHeaderRef.current?.update();
}, [fullscreen]);

useEffect(() => {
return () => {
Expand Down
Loading