Download web/src/shell/railHeader.tsx from fsanyoto/loopable: direct link, hf CLI and curl.
- Browser
- Download file 4.03 kB
-
https://huggingface.co/spaces/fsanyoto/loopable/resolve/main/web/src/shell/railHeader.tsx
- Command line
-
hf download hf://spaces/fsanyoto/loopable/web/src/shell/railHeader.tsx
-
curl -L -o railHeader.tsx https://huggingface.co/spaces/fsanyoto/loopable/resolve/main/web/src/shell/railHeader.tsx
4.03 kB
| import { useLayoutEffect, useRef, useSyncExternalStore } from "react"; | |
| import { createPortal } from "react-dom"; | |
| import PanelToggleIcon from "./PanelToggleIcon"; | |
| export type RailHeaderOwner = "data" | "sheet" | "record"; | |
| export const NAV_PANEL_EVENT = "aios:nav-panel"; | |
| export type NavPanelDetail = { open: boolean }; | |
| const slots = new Map<RailHeaderOwner, Set<HTMLElement>>(); | |
| const listeners = new Set<() => void>(); | |
| let version = 0; | |
| function subscribe(listener: () => void) { | |
| listeners.add(listener); | |
| return () => { listeners.delete(listener); }; | |
| } | |
| function snapshot() { | |
| return version; | |
| } | |
| function changed() { | |
| version += 1; | |
| listeners.forEach((listener) => listener()); | |
| } | |
| export function RailHeaderSlot({ owner, className }: { | |
| owner: RailHeaderOwner; | |
| className?: string; | |
| }) { | |
| const ref = useRef<HTMLSpanElement>(null); | |
| useLayoutEffect(() => { | |
| const element = ref.current; | |
| if (!element) return; | |
| const owned = slots.get(owner) ?? new Set<HTMLElement>(); | |
| slots.set(owner, owned); | |
| owned.add(element); | |
| changed(); | |
| return () => { | |
| owned.delete(element); | |
| if (!owned.size) slots.delete(owner); | |
| changed(); | |
| }; | |
| }, [owner]); | |
| return ( | |
| <span | |
| className={"rail-header-slot" + (className ? " " + className : "")} | |
| data-rail-header-slot={owner} | |
| ref={ref} | |
| /> | |
| ); | |
| } | |
| export function useRailHeaderSlot( | |
| owner: RailHeaderOwner, | |
| anchor?: HTMLElement | null, | |
| ): HTMLElement | null { | |
| useSyncExternalStore(subscribe, snapshot, snapshot); | |
| const candidates = [...(slots.get(owner) ?? [])] | |
| .filter((element) => element.isConnected) | |
| .sort((a, b) => a.compareDocumentPosition(b) & 4 ? -1 : 1); | |
| for (let ancestor = anchor?.parentElement; ancestor; ancestor = ancestor.parentElement) { | |
| const parent = ancestor; | |
| const local = candidates.find((element) => parent.contains(element)); | |
| if (local) return local; | |
| } | |
| return candidates[0] ?? null; | |
| } | |
| export function RailHeaderIcons({ | |
| owner, onExpand, chatOpen = false, onToggleChat, anchor, expandLabel = "Expand views", | |
| }: { | |
| owner: RailHeaderOwner; | |
| onExpand: () => void; | |
| chatOpen?: boolean; | |
| onToggleChat?: () => void; | |
| anchor?: HTMLElement | null; | |
| expandLabel?: string; | |
| }) { | |
| const slot = useRailHeaderSlot(owner, anchor); | |
| if (!slot) return null; | |
| const chatLabel = chatOpen ? "Close the chat about this data" : "Ask about this data"; | |
| return createPortal( | |
| <> | |
| <button | |
| type="button" | |
| className="cg-tb-btn rail-header-btn" | |
| aria-label={expandLabel} | |
| title={expandLabel} | |
| aria-expanded={false} | |
| onClick={onExpand} | |
| > | |
| <PanelToggleIcon open={false} /> | |
| </button> | |
| {onToggleChat && ( | |
| <button | |
| type="button" | |
| className={"cg-tb-btn rail-header-btn" + (chatOpen ? " is-active" : "")} | |
| aria-label={chatLabel} | |
| title={chatLabel} | |
| aria-expanded={chatOpen} | |
| onClick={onToggleChat} | |
| > | |
| <svg width="16" height="16" viewBox="0 0 16 16" fill="none" aria-hidden="true"> | |
| <path d="M13.4 9.2c0 1.5-1.3 2.7-2.9 2.7H6.9l-3 2.1v-2.3a2.8 2.8 0 0 1-1.3-2.3V5.1c0-1.5 1.3-2.7 2.9-2.7h5c1.6 0 2.9 1.2 2.9 2.7z" | |
| stroke="currentColor" strokeWidth="1.3" strokeLinejoin="round" /> | |
| <path d="M5.9 7.2h4.2" stroke="currentColor" strokeWidth="1.3" | |
| strokeLinecap="round" /> | |
| </svg> | |
| </button> | |
| )} | |
| </>, | |
| slot, | |
| ); | |
| } | |
| const narrowQuery = "(max-width: 1023px)"; | |
| function subscribeNarrow(listener: () => void) { | |
| if (typeof window === "undefined") return () => {}; | |
| const media = window.matchMedia(narrowQuery); | |
| media.addEventListener("change", listener); | |
| return () => media.removeEventListener("change", listener); | |
| } | |
| function narrowSnapshot() { | |
| return typeof window !== "undefined" && window.matchMedia(narrowQuery).matches; | |
| } | |
| export function useNarrowRailFold(): boolean { | |
| return useSyncExternalStore(subscribeNarrow, narrowSnapshot, () => false); | |
| } | |