-
Notifications
You must be signed in to change notification settings - Fork 4k
RND-3113: expand when expandable anchor #2435
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 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5ab0a29
Scroll to element in expandable when hash matches the id and expand t…
BrettJephson 7637891
Rethink of logic to find an element for a hash (using dom)
BrettJephson e0125e3
Formatting
BrettJephson 54870cb
Lint imports
BrettJephson e0d8464
Consolidate `useScrollToHash` with details implementation
BrettJephson a0cfc9e
Formatting and lint
BrettJephson b06a6ab
Tiny refactor activeElement -> openFromHash
BrettJephson 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| 'gitbook': patch | ||
| --- | ||
|
|
||
| Use url hash to open Expandable and scroll to anchor |
84 changes: 84 additions & 0 deletions
84
packages/gitbook/src/components/DocumentView/Expandable/Details.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,84 @@ | ||
| 'use client'; | ||
|
|
||
| import React from 'react'; | ||
|
|
||
| import { useHash } from '@/components/hooks'; | ||
| import { ClassValue, tcls } from '@/lib/tailwind'; | ||
|
|
||
| /** | ||
| * Details component rendered on client so it can expand dependent on url hash changes. | ||
| */ | ||
| export function Details(props: { | ||
| children: React.ReactNode; | ||
| id: string; | ||
| contentIds?: string[]; | ||
| open?: boolean; | ||
| className?: ClassValue; | ||
| }) { | ||
| const { children, id, open, className } = props; | ||
|
|
||
| const detailsRef = React.useRef<HTMLDetailsElement>(null); | ||
|
|
||
| const [anchorElement, setAnchorElement] = React.useState<Element | null | undefined>(); | ||
|
|
||
| const hash = useHash(); | ||
| /** | ||
| * Open the details element if the url hash refers to the id of the details element | ||
| * or the id of some element contained within the details element. | ||
| */ | ||
| React.useEffect(() => { | ||
| if (!hash || !detailsRef.current) { | ||
| return; | ||
| } | ||
| if (hash === id) { | ||
| setAnchorElement(detailsRef.current); | ||
| } else { | ||
| const activeElement = document.getElementById(hash); | ||
| if (activeElement && detailsRef.current?.contains(activeElement)) { | ||
| setAnchorElement(activeElement); | ||
| } | ||
| } | ||
| }, [hash, id]); | ||
|
|
||
| return ( | ||
| <details | ||
| ref={detailsRef} | ||
| id={id} | ||
| open={open || Boolean(anchorElement)} | ||
| className={tcls( | ||
| className, | ||
| 'group/expandable', | ||
| 'shadow-dark/1', | ||
| 'bg-gradient-to-t', | ||
| 'from-light-1', | ||
| 'to-light-1', | ||
| 'border', | ||
| 'border-b-0', | ||
| 'border-dark-3/3', | ||
| //all | ||
| '[&]:mt-[0px]', | ||
| //select first child | ||
| '[&:first-child]:mt-5', | ||
| '[&:first-child]:rounded-t-lg', | ||
| //select first in group | ||
| '[:not(&)_+&]:mt-5', | ||
| '[:not(&)_+&]:rounded-t-lg', | ||
| //select last in group | ||
| '[&:not(:has(+_&))]:mb-5', | ||
| '[&:not(:has(+_&))]:rounded-b-lg', | ||
| '[&:not(:has(+_&))]:border-b', | ||
| /* '[&:not(:has(+_&))]:shadow-1xs', */ | ||
|
|
||
| 'dark:border-light-2/[0.06]', | ||
| 'dark:from-dark-2', | ||
| 'dark:to-dark-2', | ||
| 'dark:shadow-none', | ||
|
|
||
| 'group open:dark:to-dark-2/8', | ||
| 'group open:to-light-1/6', | ||
| )} | ||
| > | ||
| {children} | ||
| </details> | ||
| ); | ||
| } | ||
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,2 +1,3 @@ | ||
| export * from './useScrollActiveId'; | ||
| export * from './useScrollToHash'; | ||
| export * from './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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| import { useParams } from 'next/navigation'; | ||
| import React from 'react'; | ||
|
|
||
| export function useHash() { | ||
| const params = useParams(); | ||
| const [hash, setHash] = React.useState<string>(global.location?.hash?.slice(1)); | ||
| React.useEffect(() => { | ||
| function updateHash() { | ||
| setHash(global.location?.hash?.slice(1)); | ||
| } | ||
| global.addEventListener('hashchange', updateHash); | ||
| updateHash(); | ||
| return () => global.removeEventListener('hashchange', updateHash); | ||
| // With next.js, the hashchange event is not triggered when the hash changes | ||
| // Instead a hack is to use the `useParams` hook to listen to changes in the hash | ||
| // https://github.com/vercel/next.js/discussions/49465#discussioncomment-5845312 | ||
| }, [params]); | ||
| return hash; | ||
| } |
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,29 +1,21 @@ | ||
| import { useParams } from 'next/navigation'; | ||
| import React from 'react'; | ||
|
|
||
| import { useHash } from './useHash'; | ||
|
|
||
| /** | ||
| * Scroll to the current URL hash everytime the URL changes. | ||
| */ | ||
| export function useScrollToHash() { | ||
| const params = useParams(); | ||
|
|
||
| const scrollToHash = React.useCallback(() => { | ||
| const hash = window.location.hash; | ||
| const hash = useHash(); | ||
| React.useLayoutEffect(() => { | ||
| if (hash) { | ||
| const element = document.getElementById(hash.slice(1)); | ||
| const element = document.getElementById(hash); | ||
| if (element) { | ||
| element.scrollIntoView({ | ||
| block: 'start', | ||
| behavior: 'smooth', | ||
| }); | ||
| } | ||
| } | ||
| }, []); | ||
|
|
||
| // With next.js, the hashchange event is not triggered when the hash changes | ||
| // Instead a hack is to use the `useParams` hook to listen to changes in the hash | ||
| // https://github.com/vercel/next.js/discussions/49465#discussioncomment-5845312 | ||
| React.useEffect(() => { | ||
| scrollToHash(); | ||
| }, [params, scrollToHash]); | ||
| }, [hash]); | ||
| } |
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.