Skip to content
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

fix(docs): horizontal scroll triggering intersection observer #4341

Merged
merged 1 commit into from
Dec 12, 2024

Conversation

Peterl561
Copy link
Contributor

@Peterl561 Peterl561 commented Dec 12, 2024

Closes #

📝 Description

  • Codeblock disappears when out of view for performance reasons
  • horizontal scrolling in Codeblock triggers this as well due to weird display: contents behavior
  • fix by setting width: fit-content so Codeblock intersection observer ref remains in view when scrolled horizontally

⛳️ Current behavior (updates)

white border added to wrapper for reference

before.mp4

🚀 New behavior

white border added to wrapper for reference

after.mp4

💣 Is this a breaking change (Yes/No):

No

📝 Additional Information

I have no idea why display: contents is used here. Was added in this commit.

Summary by CodeRabbit

Summary by CodeRabbit

  • New Features

    • Enhanced visual representation of code blocks with improved highlighting for inserted and deleted lines.
  • Bug Fixes

    • Resolved scrolling issues by adjusting the width of the outer div to fit content.

Copy link

changeset-bot bot commented Dec 12, 2024

⚠️ No Changeset found

Latest commit: 7742902

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Copy link

vercel bot commented Dec 12, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
nextui-docs-v2 ✅ Ready (Inspect) Visit Preview 💬 Add feedback Dec 12, 2024 6:49am
nextui-storybook-v2 ✅ Ready (Inspect) Visit Preview 💬 Add feedback Dec 12, 2024 6:49am

Copy link
Contributor

coderabbitai bot commented Dec 12, 2024

Walkthrough

The pull request introduces modifications to the CodeBlockHighlight component in the specified file. Key changes include adding a width: "fit-content" style to the outer div to improve scrolling behavior and refining the logic for handling highlighted lines based on the presence of "diff" in the language. The highlightStyle array is now populated to categorize lines as "inserted" or "deleted" based on their prefixes. The useEffect for selection changes remains unchanged, maintaining the component's overall structure and functionality.

Changes

File Path Change Summary
apps/docs/components/docs/components/codeblock.tsx Added width: "fit-content" style to CodeBlockHighlight, refined logic for highlighted lines, updated codeString processing.

Possibly related PRs

  • fix(docs): avoid translating the code block #3878: This PR modifies the Codeblock component in the same file, specifically addressing the translation behavior of code snippets, which is closely related to the visual representation and functionality of the CodeBlockHighlight component.
  • refactor(docs): revise code block #3922: This PR also involves changes to the Codeblock component, enhancing its internal logic and structure, which directly relates to the modifications made in the main PR regarding the handling of highlighted lines and overall component functionality.

Suggested reviewers

  • jrgarciadev
  • wingkwong

📜 Recent review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between f16a82a and 7742902.

📒 Files selected for processing (1)
  • apps/docs/components/docs/components/codeblock.tsx (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • apps/docs/components/docs/components/codeblock.tsx

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Experiment)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Outside diff range and nitpick comments (2)
apps/docs/components/docs/components/codeblock.tsx (2)

102-105: LGTM! Consider extracting the style to a constant.

The fix for the horizontal scrolling issue is elegant and well-documented. The comments clearly explain the problem and solution.

Consider extracting the inline styles to a constant or CSS class for better maintainability:

+const CODE_BLOCK_WRAPPER_STYLES = {
+  width: "fit-content",
+} as const;

 <div
   ref={intersectionRef}
   style={{
     height: isVisible ? "auto" : `${height}px`,
-    width: "fit-content",
+    ...CODE_BLOCK_WRAPPER_STYLES,
   }}
 >

Line range hint 1-300: Consider architectural improvements for better performance.

Here are some suggestions to enhance the component's architecture:

  1. Extract the intersection observer logic into a custom hook for better reusability and separation of concerns.
  2. Optimize the selection change handler by using useCallback and moving it outside the effect.
  3. Consider memoizing the CodeBlockHighlight component to prevent unnecessary re-renders.

Here's an example of how to implement these improvements:

// Custom hook for handling code block visibility
const useCodeBlockVisibility = (height: number) => {
  const [intersectionRef, isVisible] = useIntersectionObserver({
    threshold: 0,
  });

  return {
    ref: intersectionRef,
    style: {
      height: isVisible ? "auto" : `${height}px`,
      width: "fit-content",
    },
    isVisible,
  };
};

// Optimized selection change handler
const useSelectionChangeHandler = (codeString: string) => {
  const lastSelectionText = useRef<string | null>(null);

  const handleSelectionChange = useCallback(() => {
    if (!window.getSelection) return;
    // ... rest of the handler logic
  }, [codeString]);

  useEffect(() => {
    const debouncedHandler = debounce(handleSelectionChange, 1000);
    document.addEventListener("selectionchange", debouncedHandler);
    return () => document.removeEventListener("selectionchange", debouncedHandler);
  }, [handleSelectionChange]);
};

// Memoized CodeBlockHighlight
const MemoizedCodeBlockHighlight = memo(CodeBlockHighlight);
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 77206bc and f16a82a.

📒 Files selected for processing (1)
  • apps/docs/components/docs/components/codeblock.tsx (1 hunks)

@wingkwong wingkwong added the 📋 Scope : Docs Related to the documentation label Dec 12, 2024
@wingkwong wingkwong changed the base branch from canary to main December 12, 2024 06:42
@Peterl561 Peterl561 force-pushed the fix/codeblock-disappear-on-horizontal-scroll branch from f16a82a to 7742902 Compare December 12, 2024 06:44
Copy link
Member

@wingkwong wingkwong left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Peterl561 Next time please create the branch from main instead of canary for doc changes (fixes only) so that the PR won't include unrelated changes. You may reset the branch to main and add back the changes, or you can recreate a new PR.

@jrgarciadev jrgarciadev merged commit c8a576e into main Dec 12, 2024
3 checks passed
@jrgarciadev jrgarciadev deleted the fix/codeblock-disappear-on-horizontal-scroll branch December 12, 2024 12:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
📋 Scope : Docs Related to the documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants