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

Add OrientationLock parameter to BitSwipeTrap (#9662) #9663

Merged

Conversation

msynk
Copy link
Member

@msynk msynk commented Jan 12, 2025

closes #9662

Summary by CodeRabbit

Release Notes

  • New Features

    • Introduced a new swipe orientation lock feature for navigation panels
    • Added ability to restrict swipe actions to horizontal or vertical directions
  • Improvements

    • Enhanced swipe trap component with more granular control over gesture handling
    • Implemented orientation-specific swipe behavior
  • Technical Updates

    • Added new BitSwipeOrientation enumeration to support orientation locking

The changes provide more precise control over swipe interactions in the user interface, allowing developers to define specific swipe movement constraints.

Copy link

coderabbitai bot commented Jan 12, 2025

Important

Review skipped

Auto incremental reviews are disabled on this repository.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Walkthrough

The pull request introduces a new feature to the BitSwipeTrap component by adding a LockOrientation property that allows controlling swipe gesture behavior. This enhancement provides more granular control over swipe interactions by enabling horizontal or vertical orientation locking. The changes span multiple files across the Blazor UI component, including TypeScript, C#, and Razor files, to implement a comprehensive orientation locking mechanism for swipe gestures.

Changes

File Change Summary
src/BlazorUI/.../BitNavPanel.razor Added LockOrientation="BitSwipeOrientation.Horizontal" to BitSwipeTrap component
src/BlazorUI/.../BitNavPanel.razor.cs Commented out _js.BitExtrasScrollBy method call in HandleOnSwipeMove
src/BlazorUI/.../BitSwipeOrientation.cs New enum added with None, Horizontal, and Vertical values
src/BlazorUI/.../BitSwipeTrap.razor.cs Added new LockOrientation parameter with nullable BitSwipeOrientation type
src/BlazorUI/.../BitSwipeTrap.ts Updated swipe handling logic to respect orientation locking
src/BlazorUI/.../BitSwipeTrapJsRuntimeExtensions.cs Updated method signature to include lockOrientation parameter

Sequence Diagram

sequenceDiagram
    participant User
    participant SwipeTrap
    participant JavaScript
    User->>SwipeTrap: Perform Swipe Gesture
    SwipeTrap->>JavaScript: Invoke Setup with LockOrientation
    JavaScript->>JavaScript: Determine Swipe Orientation
    alt Horizontal Locked
        JavaScript->>JavaScript: Prevent Vertical Movement
    else Vertical Locked
        JavaScript->>JavaScript: Prevent Horizontal Movement
    end
    JavaScript->>SwipeTrap: Return Swipe Result
Loading

Assessment against linked issues

Objective Addressed Explanation
Add LockDirection parameter to BitSwipeTrap

Poem

🐰 Swipe, swipe, with direction tight,
Horizontal or vertical, locked just right!
No more wandering gestures astray,
Our trap now guides the swiper's way.
Code magic, rabbit-style delight! 🌈


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. (Beta)
  • @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.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

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.

@msynk msynk marked this pull request as draft January 12, 2025 18:07
Copy link

@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: 1

🧹 Nitpick comments (8)
src/BlazorUI/Bit.BlazorUI/Components/Utilities/SwipeTrap/BitSwipeOrientation.cs (3)

4-4: Fix typo in documentation

There's a typo in the documentation: "swipte" should be "swipe".

-/// The lock orientation of the swipte trap component.
+/// The lock orientation of the swipe trap component.

9-10: Fix grammar in documentation

The documentation for None value needs grammatical correction.

-/// Not orientation lock for swipe trap.
+/// No orientation lock for swipe trap.

14-15: Improve documentation clarity

The documentation for Horizontal and Vertical values could be clearer.

-/// Horizontal orientation lock of traping the swipe action.
+/// Locks swipe actions to horizontal orientation only.

-/// Vertical orientation lock of traping the swipe action.
+/// Locks swipe actions to vertical orientation only.

Also applies to: 19-20

src/BlazorUI/Bit.BlazorUI/Components/Utilities/SwipeTrap/BitSwipeTrap.razor.cs (1)

104-111: Consider adding parameter validation

The LockOrientation parameter is used directly in the setup call. Consider adding validation to ensure the value is within the enum range.

 protected override async Task OnAfterRenderAsync(bool firstRender)
 {
     if (firstRender)
     {
+        var lockOrientation = LockOrientation ?? BitSwipeOrientation.None;
+        if (!Enum.IsDefined(typeof(BitSwipeOrientation), lockOrientation))
+        {
+            throw new ArgumentException($"Invalid orientation value: {lockOrientation}", nameof(LockOrientation));
+        }
         var dotnetObj = DotNetObjectReference.Create(this);
         await _js.BitSwipeTrapSetup(
             UniqueId, 
             RootElement, 
             Trigger ?? 0.25m, 
             Threshold ?? 0, 
             Throttle ?? 0, 
-            LockOrientation ?? BitSwipeOrientation.None,
+            lockOrientation,
             dotnetObj);
     }
src/BlazorUI/Bit.BlazorUI.Extras/Components/NavPanel/BitNavPanel.razor (1)

Line range hint 17-25: Consider documenting the swipe behavior

The nav panel now uses horizontal-only swipes. Consider adding a comment explaining this UX decision for future maintainers.

 <BitSwipeTrap Style="width:100%; height:100%"
               Threshold="5"
               Throttle="10"
               OnMove="HandleOnSwipeMove"
               OnEnd="HandleOnSwipeEnd"
               OnTrigger="HandleOnSwipeTrigger"
+              @* Horizontal-only swipes for consistent navigation panel interaction *@
               LockOrientation="BitSwipeOrientation.Horizontal">
src/BlazorUI/Bit.BlazorUI/Components/Utilities/SwipeTrap/BitSwipeTrap.ts (3)

39-47: Consider using angle-based orientation detection.

The current orientation detection assumes perfect horizontal/vertical movements, which is rare in real-world touch interactions. A more robust approach would be to calculate the angle of movement.

 if (orientation === BitSwipeOrientation.None) {
-    if (diffX !== 0 && diffY === 0) {
+    const angle = Math.abs(Math.atan2(diffY, diffX) * 180 / Math.PI);
+    if (angle <= 45 || angle >= 135) {
         orientation = BitSwipeOrientation.Horizontal;
-    }
-
-    if (diffX === 0 && diffY !== 0) {
+    } else {
         orientation = BitSwipeOrientation.Vertical;
     }
 }

49-65: Consider extracting repeated preventDefault/stopPropagation calls.

The event prevention logic is correct but contains duplicated code. Consider extracting the prevention logic into a helper function.

+const preventEvent = (e: TouchEvent | PointerEvent) => {
+    e.preventDefault();
+    e.stopPropagation();
+};

 if (e.cancelable) {
     if (lockOrientation === BitSwipeOrientation.Horizontal) {
         if (orientation === BitSwipeOrientation.Horizontal && Math.abs(diffX) > threshold) {
-            e.preventDefault();
-            e.stopPropagation();
+            preventEvent(e);
         }
     }
     else if (lockOrientation === BitSwipeOrientation.Vertical) {
         if (orientation === BitSwipeOrientation.Vertical && Math.abs(diffY) > threshold) {
-            e.preventDefault();
-            e.stopPropagation();
+            preventEvent(e);
         }
     }
     else if ((Math.abs(diffX) > threshold || Math.abs(diffY) > threshold)) {
-        e.preventDefault();
-        e.stopPropagation();
+        preventEvent(e);
     }
 }

161-165: Add JSDoc documentation for the BitSwipeOrientation enum.

Consider adding documentation to improve code maintainability.

+/**
+ * Defines the allowed orientations for swipe gestures.
+ * @enum {number}
+ */
 enum BitSwipeOrientation {
+    /** No specific orientation constraint */
     None,
+    /** Constrain swipes to horizontal movement */
     Horizontal,
+    /** Constrain swipes to vertical movement */
     Vertical
 }
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 8d6a51c and 8dac5c3.

📒 Files selected for processing (6)
  • src/BlazorUI/Bit.BlazorUI.Extras/Components/NavPanel/BitNavPanel.razor (1 hunks)
  • src/BlazorUI/Bit.BlazorUI.Extras/Components/NavPanel/BitNavPanel.razor.cs (1 hunks)
  • src/BlazorUI/Bit.BlazorUI/Components/Utilities/SwipeTrap/BitSwipeOrientation.cs (1 hunks)
  • src/BlazorUI/Bit.BlazorUI/Components/Utilities/SwipeTrap/BitSwipeTrap.razor.cs (2 hunks)
  • src/BlazorUI/Bit.BlazorUI/Components/Utilities/SwipeTrap/BitSwipeTrap.ts (4 hunks)
  • src/BlazorUI/Bit.BlazorUI/Components/Utilities/SwipeTrap/BitSwipeTrapJsRuntimeExtensions.cs (1 hunks)
✅ Files skipped from review due to trivial changes (1)
  • src/BlazorUI/Bit.BlazorUI.Extras/Components/NavPanel/BitNavPanel.razor.cs
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: build and test
🔇 Additional comments (5)
src/BlazorUI/Bit.BlazorUI/Components/Utilities/SwipeTrap/BitSwipeTrapJsRuntimeExtensions.cs (1)

11-14: Verify JavaScript method signature

The lockOrientation parameter has been added to the C# method. Please ensure that the JavaScript BitBlazorUI.SwipeTrap.setup method's signature has been updated to handle this parameter.

✅ Verification successful

JavaScript implementation verified

The BitBlazorUI.SwipeTrap.setup method signature in TypeScript matches the C# invocation perfectly, including the lockOrientation parameter. The implementation properly handles the orientation locking functionality.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for the setup method definition in TypeScript files
rg -t ts "setup.*=.*function.*\(.*id.*element.*trigger.*threshold.*throttle.*lockOrientation.*dotnetObjectReference" --multiline

Length of output: 130


Script:

#!/bin/bash
# First locate SwipeTrap related files
fd -e ts -e js "SwipeTrap"

# Then look for setup method with less strict pattern
rg -t ts -t js "SwipeTrap.*setup.*=.*function" -B 2 -A 10

Length of output: 159


Script:

#!/bin/bash
# View the TypeScript file content
cat "src/BlazorUI/Bit.BlazorUI/Components/Utilities/SwipeTrap/BitSwipeTrap.ts"

Length of output: 6742

src/BlazorUI/Bit.BlazorUI/Components/Utilities/SwipeTrap/BitSwipeTrap.razor.cs (1)

23-26: LGTM! Parameter documentation is clear and accurate.

src/BlazorUI/Bit.BlazorUI.Extras/Components/NavPanel/BitNavPanel.razor (1)

24-25: Verify OnTrigger handler implementation

The HandleOnSwipeTrigger event handler has been added, but its implementation is not visible in the current changes.

✅ Verification successful

Handler implementation verified

The HandleOnSwipeTrigger implementation is complete and correctly handles swipe gestures for both LTR and RTL layouts, with proper panel state management.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for HandleOnSwipeTrigger implementation
rg -t cs "HandleOnSwipeTrigger.*\(.*BitSwipeTrapTriggerArgs" --multiline

Length of output: 222


Script:

#!/bin/bash
# Get the complete implementation of HandleOnSwipeTrigger
ast-grep --pattern 'private async Task HandleOnSwipeTrigger(BitSwipeTrapTriggerArgs $_) {
  $$$
}'

Length of output: 100


Script:

#!/bin/bash
# Get the complete implementation with context
rg -t cs -B 2 -A 10 "private async Task HandleOnSwipeTrigger\(BitSwipeTrapTriggerArgs"

Length of output: 1469

src/BlazorUI/Bit.BlazorUI/Components/Utilities/SwipeTrap/BitSwipeTrap.ts (2)

11-11: LGTM! Clean parameter and state management additions.

The new lockOrientation parameter and orientation state variable are well-typed and properly initialized.

Also applies to: 19-19


11-11: Verify naming consistency with PR objectives.

The PR objective mentions "LockDirection" but the implementation uses "lockOrientation". Consider aligning the naming for consistency.

Also applies to: 161-165

@msynk msynk marked this pull request as ready for review January 12, 2025 19:42
@msynk msynk changed the title Add LockDirection parameter to BitSwipeTrap (#9662) Add OrientationLock parameter to BitSwipeTrap (#9662) Jan 13, 2025
@msynk msynk merged commit 606d220 into bitfoundation:develop Jan 13, 2025
3 checks passed
@msynk msynk deleted the 9662-blazorui-swipetrap-lockdirection branch January 13, 2025 15:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Missing OrientationLock parameter from the BitSwipeTrap component
3 participants