-
Notifications
You must be signed in to change notification settings - Fork 6.5k
feat(ui): implement deep linking in CodeTabs via URL hash #9159
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
moshams272
wants to merge
8
commits into
nodejs:main
Choose a base branch
from
moshams272:feat/codetabs-deep-linking
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
0f7aec8
feat(ui): implement deep linking in CodeTabs via URL hash
moshams272 97b39de
feat(site,ui): add HashContext and HashProvider for global URL state
moshams272 07251e6
refactor(ui): isolate window hash logic from CodeTabs
moshams272 d4ed698
chore: improve code documentation for useHash hook
moshams272 c79ec05
feat(ui): use native DOM id for CodeTabs scrolling
moshams272 7bc608a
fix(site): dispatch global hashchange event on hash update
moshams272 de1d09c
refactor(ui-components): revert CodeTabs and use derived state in Cod…
moshams272 e902106
feat(rehype-shiki): route to CodeTabsWithHash if groupId exists
moshams272 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,25 @@ | ||
| 'use client'; | ||
|
|
||
| import { useSyncExternalStore, useCallback } from 'react'; | ||
|
|
||
| const subscribe = (callback: () => void) => { | ||
| window.addEventListener('hashchange', callback); | ||
| return () => window.removeEventListener('hashchange', callback); | ||
| }; | ||
|
|
||
| const getSnapshot = () => window.location.hash.slice(1); | ||
| const getServerSnapshot = () => ''; | ||
|
|
||
| const useHash = () => { | ||
| // Works in both client and server environments, provides the initial value for the store during server rendering and hydration, avoiding hydration mismatches | ||
| const hash = useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot); | ||
|
|
||
| const setHash = useCallback((newHash: string) => { | ||
| window.history.replaceState(null, '', `#${newHash}`); | ||
| window.dispatchEvent(new Event('hashchange')); | ||
| }, []); | ||
|
|
||
| return [hash, setHash] as const; | ||
| }; | ||
|
|
||
| export default useHash; |
This file contains hidden or 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 hidden or 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,13 @@ | ||
| 'use client'; | ||
|
|
||
| import { HashContext } from '@node-core/ui-components/contexts/HashContext'; | ||
|
|
||
| import useHash from '#site/hooks/useHash'; | ||
|
|
||
| import type { FC, PropsWithChildren } from 'react'; | ||
|
|
||
| export const HashProvider: FC<PropsWithChildren> = ({ children }) => { | ||
| const [hash, setHash] = useHash(); | ||
|
|
||
| return <HashContext value={{ hash, setHash }}>{children}</HashContext>; | ||
| }; | ||
This file contains hidden or 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 hidden or 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 hidden or 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
89 changes: 89 additions & 0 deletions
89
packages/ui-components/src/MDX/CodeTabs/CodeTabsWithHash.tsx
This file contains hidden or 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,89 @@ | ||
| 'use client'; | ||
|
|
||
| import * as TabsPrimitive from '@radix-ui/react-tabs'; | ||
| import { useCallback, useMemo } from 'react'; | ||
|
|
||
| import CodeTabs from '#ui/Common/CodeTabs'; | ||
| import { useHashContext } from '#ui/contexts/HashContext'; | ||
|
|
||
| import type { ComponentProps, FC } from 'react'; | ||
|
|
||
| import MDXCodeTabs from './index'; | ||
|
|
||
| type CodeTabsWithHashProps = ComponentProps<typeof MDXCodeTabs> & { | ||
| groupId: string; | ||
| }; | ||
|
|
||
| const NAME_OVERRIDES: Record<string, string | undefined> = { | ||
| mjs: 'ESM', | ||
| }; | ||
|
|
||
| const CodeTabsWithHash: FC<CodeTabsWithHashProps> = ({ | ||
| languages: rawLanguages, | ||
| displayNames: rawDisplayNames, | ||
| children: codes, | ||
| defaultTab = '0', | ||
| groupId, | ||
| ...props | ||
| }) => { | ||
| const { hash, setHash } = useHashContext(); | ||
|
|
||
| const { tabs, languages } = useMemo(() => { | ||
| const occurrences: Record<string, number> = {}; | ||
| const languages = rawLanguages.split('|'); | ||
| const displayNames = rawDisplayNames?.split('|') ?? []; | ||
|
|
||
| const tabs = languages.map((language, index) => { | ||
| const base = | ||
| displayNames[index]?.trim() || | ||
| NAME_OVERRIDES[language] || | ||
| language.toUpperCase(); | ||
|
|
||
| const count = occurrences[base] ?? 0; | ||
| occurrences[base] = count + 1; | ||
|
|
||
| const label = count > 0 ? `${base} (${count + 1})` : base; | ||
|
|
||
| return { | ||
| key: `${language}-${index}`, | ||
| label, | ||
| id: `${groupId}-${language}-${index}`.replace(/[^a-zA-Z0-9-_]/g, '-'), | ||
| }; | ||
| }); | ||
|
|
||
| return { tabs, languages }; | ||
| }, [rawLanguages, rawDisplayNames, groupId]); | ||
|
|
||
| const activeTab = | ||
| tabs.find(t => t.id === hash)?.key ?? tabs[Number(defaultTab)].key; | ||
|
|
||
| const handleValueChange = useCallback( | ||
| (value: string) => { | ||
| const matched = tabs.find(t => t.key === value); | ||
| if (matched?.id) { | ||
| setHash(matched.id); | ||
| } | ||
| }, | ||
| [tabs, setHash] | ||
| ); | ||
|
|
||
| return ( | ||
| <CodeTabs | ||
| tabs={tabs} | ||
| value={activeTab} | ||
| onValueChange={handleValueChange} | ||
| {...props} | ||
| > | ||
| {languages.map((_, index) => ( | ||
| <TabsPrimitive.Content | ||
| forceMount | ||
| key={tabs[index].key} | ||
| value={tabs[index].key} | ||
| > | ||
| {codes[index]} | ||
| </TabsPrimitive.Content> | ||
| ))} | ||
| </CodeTabs> | ||
| ); | ||
| }; | ||
| export default CodeTabsWithHash; |
File renamed without changes.
This file contains hidden or 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,15 @@ | ||
| 'use client'; | ||
|
|
||
| import { createContext, use } from 'react'; | ||
|
|
||
| type HashContextType = { | ||
| hash: string; | ||
| setHash: (newHash: string) => void; | ||
| }; | ||
|
|
||
| export const HashContext = createContext<HashContextType>({ | ||
| hash: '', | ||
| setHash: () => {}, | ||
| }); | ||
|
|
||
| export const useHashContext = () => use(HashContext); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: Have Location be passed to the HashProvider ;)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Replied your other comment