UNPKG

@wener/console

Version:

Base console UI toolkit

33 lines (32 loc) 1.22 kB
import React, { forwardRef, useEffect, useRef, useState } from "react"; /** * 不会添加 basename */ export const StaticNavLink = /* @__PURE__ */ /*#__PURE__*/ forwardRef(({ href, children, className, ...rest }, ref)=>{ const [isActive, setActive] = useState(()=>new URL(globalThis.location?.href || 'http://127.0.0.1:3000').pathname.startsWith(href)); const onUrl = (s)=>{ new URL(s).pathname.startsWith(href) ? setActive(true) : setActive(false); }; const cbRef = useRef(onUrl); cbRef.current = onUrl; useEffect(()=>{ const navigation = globalThis.navigation; const callback = (e)=>{ cbRef.current(e.destination.url); }; navigation?.addEventListener('navigate', callback); return ()=>{ navigation?.removeEventListener('navigate', callback); }; }, []); return /*#__PURE__*/ React.createElement("a", { href: href, ref: ref, className: typeof className === 'function' ? className({ isActive }) : className, ...rest }, typeof children === 'function' ? children({ isActive }) : children); }); //# sourceMappingURL=StaticNavLink.js.map