-
Notifications
You must be signed in to change notification settings - Fork 666
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: mark active or ancestor links (#1840)
- Loading branch information
1 parent
d638c52
commit 4295dbe
Showing
11 changed files
with
304 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { VNode } from "preact"; | ||
|
||
/** | ||
* Mark active or ancestor link | ||
* Note: This function is used both on the server and the client | ||
*/ | ||
export function setActiveUrl(vnode: VNode, pathname: string) { | ||
const props = vnode.props as Record<string, unknown>; | ||
const hrefProp = props.href; | ||
if (typeof hrefProp === "string" && hrefProp.startsWith("/")) { | ||
let href = new URL(hrefProp, "http://localhost").pathname; | ||
if (href !== "/" && href.endsWith("/")) { | ||
href = href.slice(0, -1); | ||
} | ||
|
||
if (pathname !== "/" && pathname.endsWith("/")) { | ||
pathname = pathname.slice(0, -1); | ||
} | ||
|
||
if (pathname === href) { | ||
props["data-current"] = "true"; | ||
} else if (pathname.startsWith(href + "/") || href === "/") { | ||
props["data-ancestor"] = "true"; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.