-
Section 1: Getting Started
-
Section 2: Core Syntax and Types
-
Section 3: Strings and Numbers in Depth
-
Section 4: Control Flow
-
Section 5: Functions
-
Section 6: Objects and Arrays
-
Section 7: Maps, Sets, and Symbols
-
Section 8: Asynchronous JavaScript
-
Section 9: Object-Oriented and Prototypes
-
Section 10: The DOM
-
Section 11: Browser APIs
-
Section 12: Modern JavaScript (ES2015-ES2025)
-
Section 13: Functional Programming Patterns
-
Section 14: Error Handling and Debugging
-
Section 15: Testing
-
Section 16: Accessibility for JavaScript Developers
-
Section 17: Internationalization and Localization
-
Section 18: Performance
-
Section 19: Node.js Fundamentals
-
Section 20: Regular Expressions
-
Section 21: Design Patterns in JavaScript
-
Section 22: Security Basics
-
Section 23: Data Structures and Algorithms in JavaScript
-
Section 24: Practical Projects
-
Section 25: More Advanced Async Patterns
-
Section 26: More Object and Class Practice
-
Section 27: Working with Dates and Internationalization
-
Section 28: Web Components
-
Section 29: More DOM and Browser Practice
-
Section 30: Build Tooling for Vanilla JavaScript
-
Section 31: More Practice Projects
-
Section 32: Interview and Algorithm Practice
-
Section 33: Error Objects (MDN Reference)
-
Section 34: TypedArrays and Binary Data
-
Section 35: Reflection and Metaprogramming (MDN Reference)
-
Section 36: More Global Functions (MDN Reference)
134: Handling RTL Layouts in JavaScript Apps
Imagine you've spent your whole life driving in the United States. You know instinctively that the passing lane is on the left and you merge from the right. Now, imagine you fly to London. The rules of the road—stopping at red lights, yielding to pedestrians—are exactly the same. But the spatial orientation is mirrored. If you try to apply your "always merge from the right" habit in London, you're going to cause a pile-up.
Handling RTL (Right-to-Left) layouts in JavaScript is exactly like that. The "logic" of your app—how a user logs in, how data is fetched—doesn't change. But the "road" the user travels on flips. If your code explicitly says "move this element 20px to the right," it might work for an English speaker, but for an Arabic or Hebrew speaker, you've just driven them straight into a wall.
Flipping the Global Switch
In a modern app, you don't want to manually flip every single element. Instead, you treat the dir attribute on the html tag as your global "driving side" setting. When a user selects an RTL language, your JavaScript should update this attribute. Everything else should flow from that one source of truth.
// A simple way to handle the direction switch
function setAppDirection(lang) {
const rtlLanguages = ['ar', 'he', 'fa'];
const direction = rtlLanguages.includes(lang) ? 'rtl' : 'ltr';
document.documentElement.dir = direction;
document.documentElement.lang = lang;
// I usually save this to localStorage so the user doesn't
// see a "flash" of LTR layout on the next page load.
localStorage.setItem('preferred-lang', lang);
}
Escaping the "Left" and "Right" Mindset
Here is where most developers mess up: they write JavaScript that manipulates marginLeft or right. Once you move into RTL territory, "left" and "right" are no longer reliable concepts. We need to start thinking in terms of Start and End.
In LTR, "start" is left. In RTL, "start" is right. CSS Logical Properties (like margin-inline-start) handle this automatically, but when you're calculating positions in JS—say, for a custom tooltip or a drag-and-drop library—you have to be explicit.
function getStartOffset(element) {
const isRtl = document.documentElement.dir === 'rtl';
const rect = element.getBoundingClientRect();
// Instead of always returning rect.left, we check the direction
return isRtl ? window.innerWidth - rect.right : rect.left;
}
Handling Mirrored UI Components
Not everything flips automatically. Some elements, like a "Back" arrow, must be mirrored because the concept of "going back" is visually tied to the direction of the text. If the text flows right-to-left, "back" is now a right-pointing arrow.
I've found that the cleanest way to handle this in JS is to toggle a data attribute on the body. This allows you to write specific CSS overrides for icons that shouldn't just move, but actually rotate.
// When switching directions, update a data attribute for CSS targeting
function updateUIMirroring(direction) {
document.body.setAttribute('data-dir', direction);
}
// In your CSS, you can then do:
// [data-dir="rtl"] .back-button { transform: rotate(180deg); }
One last tip: be careful with text-align: left. If you've hardcoded that in your styles or JS, it will override the natural RTL behavior and leave your Arabic text hugging the left wall, which looks broken to a native speaker. Stick to text-align: start.
📋 Practical Task
Exercise: Building a Bi-Directional Language Toggle for a User Dashboard
You are tasked with adding a language switcher to a dashboard. The dashboard contains a sidebar and a main content area. When the user switches from English to Arabic, the entire layout must flip, and a specific "Notification" icon (represented by a div with the class .notif-icon) must be mirrored.
Requirements:
- Create a function
toggleLanguage(lang)that updates thedirattribute of thehtmlelement. - The function must also set a
data-dirattribute on thebodyto'rtl'or'ltr'. - Implement a simple check: if the language is 'ar', the direction is 'rtl'; otherwise, it is 'ltr'.
- Ensure the
localStorageis updated so the preference persists. - Write a small piece of logic that logs to the console whether the "Start" of the page is currently the left or right side of the screen based on the current state.
There are no comments for now.