Skip to content

Conversation

@julesyoungberg
Copy link

Summary

  • Added comprehensive RTL (Right-to-Left) support for proper handling of RTL languages
  • Enhanced useMeasurement hook to calculate negative scroll offsets for RTL layouts
  • Added isRTL utility function to detect text direction from element or document

Description

This PR adds comprehensive RTL (Right-to-Left) language support to the Carousel component, enabling proper functionality for languages like Arabic and Hebrew.

Changes include:

  • RTL Support in useMeasurement: Enhanced the hook to detect RTL direction and calculate negative scroll offsets for RTL layouts. In RTL mode, scrollLeft uses negative values to scroll right, so all offsets (except the first page at 0) are negated
  • New isRTL utility: Added utility function in browser.ts that detects RTL mode from element computed styles or document direction attribute
  • RTL Storybook story: Added new story demonstrating RTL functionality with dir="rtl" wrapper and navigation buttons
  • Comprehensive tests: Added test coverage for both the isRTL utility and RTL-specific behavior in useMeasurement

These changes ensure the carousel works correctly for international users using RTL languages without modifying any existing carousel behavior.

Type of Change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How Has This Been Tested?

Unit Tests:

  • Added comprehensive test coverage for isRTL utility function (59 lines in browser.test.ts)
    • Tests document-level RTL detection
    • Tests element-level RTL detection via computed styles
    • Tests RTL inheritance from parent elements
  • Added RTL-specific tests for useMeasurement hook (62 lines)
    • Verifies negative scroll offsets in RTL mode for 'screen', 'slide', and numeric scroll distances
    • Verifies positive scroll offsets remain in LTR mode
  • All existing tests pass

Manual Testing Scenarios:

  1. RTL Story: View the new RTL story in Storybook to verify RTL carousel behavior
  2. RTL navigation: Test that navigation buttons work correctly in RTL mode
  3. Different scroll modes: Verify RTL works with scrollDistance set to 'screen', 'slide', and numeric values
  4. Cross-browser compatibility: Test in major browsers (Chrome, Firefox, Safari, Edge)

Checklist

  • My code follows the style guidelines of this project (I have run pnpm run lint)
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes (I have run pnpm run test:ci-with-server/pnpm run test)
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • I have recorded any user-facing fixes or changes with pnpm changeset.
  • My changes generate no new warnings
  • Any dependent changes have been merged and published in downstream modules

- Updated Carousel component to handle smooth scrolling behavior based on initial load state.
- Introduced RTL support in useMeasurement hook for accurate scroll offsets.
- Added isRTL utility function to determine text direction for proper layout handling.
- Improved Carousel component to prevent unnecessary scroll animations during initial load.
- Updated useMeasurement hook to correctly handle negative scroll offsets in RTL mode.
- Added utility function to detect RTL layout for better scroll behavior management.
- Consolidated RTL direction tests in browser utility to use parameterized tests for better readability and maintainability.
- Simplified RTL offset tests in useMeasurement by utilizing a mock element and parameterized test cases for various scroll distances.
- Streamlined the parameterized test for RTL direction in browser utility by consolidating the test structure, enhancing clarity and maintainability.
- Introduced a new story for the Carousel component that demonstrates its functionality in a right-to-left (RTL) layout.
- Added buttons for navigating between slides in the RTL context, enhancing the component's usability for RTL users.
- Updated the isRTL function to accept an optional HTMLElement parameter, allowing detection of RTL mode based on computed styles.
- Enhanced the useMeasurement hook to utilize the updated isRTL function with the container element.
- Added tests to verify RTL detection from element styles and inheritance from parent elements.
- Added JSDoc comments to the isRTL function to clarify its purpose and usage for detecting right-to-left (RTL) layouts.
- Enhanced code readability and maintainability by providing detailed descriptions of the function's behavior.
- Simplified the scrolling logic in the Carousel component to enhance smooth scrolling after the initial load.
- Removed unnecessary checks and streamlined the handling of the initial page load to prevent unwanted animations.
- Ensured that the scroll behavior is consistent and responsive to the current page state.
@changeset-bot
Copy link

changeset-bot bot commented Dec 3, 2025

🦋 Changeset detected

Latest commit: 5642edf

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
nuka-carousel Patch

Not sure what this means? Click here to learn what changesets are.

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

@vercel
Copy link

vercel bot commented Dec 3, 2025

@julesyoungberg is attempting to deploy a commit to the formidable-labs Team on Vercel.

A member of the Team first needs to authorize it.

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.

1 participant