-
-
Notifications
You must be signed in to change notification settings - Fork 200
docs: Split runtime hook docs into dedicated UI hook pages #2885
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
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 |
|---|---|---|
| @@ -1 +1,8 @@ | ||
| ["use-i18n"] | ||
| [ | ||
| "use-lang", | ||
| "use-version", | ||
| "use-dark", | ||
| "router-hooks", | ||
| "use-i18n", | ||
| "use-page-data" | ||
| ] |
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,4 @@ | ||
| --- | ||
| overview: true | ||
| title: Built-in hooks | ||
| --- |
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 @@ | ||
| # Router hooks | ||
|
|
||
| Rspress re-exports the routing utilities from `react-router-dom`, letting you access navigation and location data without adding extra dependencies. | ||
|
|
||
| - **Type:** the same signatures as the corresponding `react-router-dom` hooks | ||
|
|
||
| Commonly used hooks include `useLocation`, `useNavigate`, `useParams`, `useSearchParams`, and `useMatches`. | ||
|
|
||
| ```tsx | ||
| import { useLocation, useNavigate } from '@rspress/core/runtime'; | ||
|
|
||
| export default function LocationDebugger() { | ||
| const location = useLocation(); | ||
| const navigate = useNavigate(); | ||
|
|
||
| return ( | ||
| <div> | ||
| <div>Current path: {location.pathname}</div> | ||
| <button type="button" onClick={() => navigate('/')}> | ||
| Back to home | ||
| </button> | ||
| </div> | ||
| ); | ||
| } | ||
| ``` |
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,40 @@ | ||
| # useDark | ||
|
|
||
| `useDark` reports whether the current theme is dark mode. | ||
|
|
||
| - **Type:** `() => boolean` | ||
|
|
||
| ```tsx preview | ||
| import { useDark } from '@rspress/core/runtime'; | ||
|
|
||
| export default function ThemeSwitchHint() { | ||
| const isDark = useDark(); | ||
| return <div>{isDark ? 'Dark theme enabled' : 'Light theme enabled'}</div>; | ||
| } | ||
| ``` | ||
|
|
||
| :::warning Note | ||
|
|
||
| During SSG, `useDark` cannot accurately reflect the user's browser theme setting because SSG is executed at build time. This hook will only return the correct theme value after client-side hydration is complete. | ||
|
|
||
| If you need to apply dark theme styles during SSG, it's recommended to use the CSS selector `.dark` for styling. Rspress adds the `dark` class name to the document root element, which works correctly in both SSG and client-side rendering: | ||
|
|
||
| ```css | ||
| /* Light mode */ | ||
| .my-component { | ||
| color: black; | ||
| background-color: white; | ||
| } | ||
|
|
||
| /* Dark mode */ | ||
| .dark .my-component { | ||
| color: white; | ||
| background-color: #1a1a1a; | ||
| } | ||
| ``` | ||
|
|
||
| ::: | ||
|
|
||
| ``` | ||
SoonIter marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| ``` | ||
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,16 @@ | ||
| # useLang | ||
|
|
||
| `useLang` returns the current locale code so you can render language-specific content or route users accordingly. | ||
|
|
||
| - **Type:** `() => string` | ||
|
|
||
| The hook reads the active language from the runtime context and stays in sync when users switch locales. | ||
|
|
||
| ```tsx preview | ||
| import { useLang } from '@rspress/core/runtime'; | ||
|
|
||
| export default function Comp() { | ||
| const lang = useLang(); | ||
| return <span>Current language: {lang}</span>; | ||
| } | ||
| ``` |
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,22 @@ | ||
| --- | ||
| tag: deprecated | ||
| --- | ||
|
|
||
| # usePageData | ||
|
|
||
| `usePageData` exposes the metadata of the current page so you can render it inside custom components or utilities. | ||
|
|
||
| - **Type:** `() => PageData` | ||
|
|
||
| The returned `PageData` object includes information such as the page title, route path, frontmatter, and site-wide data. This hook is available on both the server and client, making it suitable for SSR and SSG-safe rendering. | ||
|
|
||
| ```tsx | ||
| import { usePageData } from '@rspress/core/runtime'; | ||
|
|
||
| export default function PageTitle() { | ||
| const { page } = usePageData(); | ||
| return <h1>{page.title}</h1>; | ||
| } | ||
| ``` | ||
|
|
||
| > Related: you can pair this hook with [`useLang`](./use-lang) or [`useVersion`](./use-version) to tailor content by locale or documentation version. |
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,16 @@ | ||
| # useVersion | ||
|
|
||
| `useVersion` provides the current documentation version when multi-version docs are enabled. | ||
|
|
||
| - **Type:** `() => string` | ||
|
|
||
| Use it to surface the active version in UI or to branch logic for versioned content. | ||
|
|
||
| ```tsx | ||
| import { useVersion } from '@rspress/core/runtime'; | ||
|
|
||
| export default function VersionTag() { | ||
| const version = useVersion(); | ||
| return <div>Viewing docs for version {version}</div>; | ||
| } | ||
| ``` |
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.