Skip to content

Commit 58c9ada

Browse files
committed
chore: replace isLTR instances
1 parent 2ba0190 commit 58c9ada

File tree

9 files changed

+28
-13
lines changed

9 files changed

+28
-13
lines changed

1st-gen/packages/color-area/src/ColorArea.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,7 @@ export class ColorArea extends SpectrumElement {
466466
color=${this.colorController.getHslString()}
467467
?disabled=${this.disabled}
468468
style=${`transform: translate(${
469-
(this.dir !== 'rtl' ? this.x : 1 - this.x) * width
469+
(isLTR(this) ? this.x : 1 - this.x) * width
470470
}px, ${height - this.y * height}px);`}
471471
${streamingListener({
472472
start: ['pointerdown', this.handlePointerdown],

1st-gen/packages/color-slider/src/ColorSlider.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -270,9 +270,7 @@ export class ColorSlider extends Focusable {
270270

271271
const percent = Math.max(0, Math.min(1, (offset - minOffset) / size));
272272
const sliderHandlePosition =
273-
this.vertical || this.dir === 'rtl'
274-
? 100 - 100 * percent
275-
: 100 * percent;
273+
this.vertical || !isLTR(this) ? 100 - 100 * percent : 100 * percent;
276274

277275
return sliderHandlePosition;
278276
}

1st-gen/packages/color-wheel/src/ColorWheel.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ export class ColorWheel extends Focusable {
308308

309309
// Calculate handle position on the wheel.
310310
const translateX =
311-
(this.dir !== 'rtl' ? 1 : -1) *
311+
(isLTR(this) ? 1 : -1) *
312312
(radius - trackWidth / 2) *
313313
Math.cos((this.value * Math.PI) / 180);
314314
const translateY =

1st-gen/packages/menu/src/Menu.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -832,11 +832,11 @@ export class Menu extends SizedMixin(SpectrumElement, { noDefaultSize: true }) {
832832
protected navigateBetweenRelatedMenus(event: MenuItemKeydownEvent): void {
833833
const { key, root } = event;
834834
const shouldOpenSubmenu =
835-
(this.dir !== 'rtl' && key === 'ArrowRight') ||
836-
(this.dir === 'rtl' && key === 'ArrowLeft');
835+
(isLTR(this) && key === 'ArrowRight') ||
836+
(!isLTR(this) && key === 'ArrowLeft');
837837
const shouldCloseSelfAsSubmenu =
838-
(this.dir !== 'rtl' && key === 'ArrowLeft') ||
839-
(this.dir === 'rtl' && key === 'ArrowRight') ||
838+
(isLTR(this) && key === 'ArrowLeft') ||
839+
(!isLTR(this) && key === 'ArrowRight') ||
840840
key === 'Escape';
841841
const lastFocusedItem = root as MenuItem;
842842
if (shouldOpenSubmenu) {

1st-gen/packages/menu/src/MenuItem.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ export class MenuItem extends LikeAnchor(
394394
?open=${this.hasSubmenu &&
395395
this.open &&
396396
this.dependencyManager.loaded}
397-
.placement=${this.dir !== 'rtl' ? 'right-start' : 'left-start'}
397+
.placement=${isLTR(this) ? 'right-start' : 'left-start'}
398398
receives-focus="false"
399399
.offset=${[-10, -5] as [number, number]}
400400
.type=${'auto'}

1st-gen/packages/slider/src/Slider.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import {
2828
repeat,
2929
styleMap,
3030
} from '@spectrum-web-components/base/src/directives.js';
31+
import { isLTR } from '@spectrum-web-components/base/src/direction.js';
3132

3233
import sliderStyles from './slider.css.js';
3334
import { ObserveSlotText } from '@spectrum-web-components/shared/src/observe-slot-text.js';
@@ -392,7 +393,7 @@ export class Slider extends SizedMixin(ObserveSlotText(SliderHandle, ''), {
392393
this.min,
393394
this.max
394395
);
395-
const position = this.dir === 'rtl' ? 'right' : 'left';
396+
const position = isLTR(this) ? 'left' : 'right';
396397
const offsetPosition =
397398
(this.value > centerPoint
398399
? centerPointNormalized

1st-gen/packages/split-view/src/SplitView.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ export class SplitView extends SpectrumElement {
314314
return;
315315
}
316316
let direction = 0;
317-
const isLTRorVertical = this.dir !== 'rtl' || this.vertical;
317+
const isLTRorVertical = isLTR(this) || this.vertical;
318318
switch (event.key) {
319319
case 'Home':
320320
event.preventDefault();

1st-gen/tools/base/README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,21 @@ export MyElement extends SpectrumMixin(HTMLElement) {}
3030

3131
### Features
3232

33+
#### `isLTR` utility
34+
35+
While CSS offers many powerful solutions for styling content in various directions, sometimes JS functionality depends on the specific direction. Use the `isLTR` utility function to check if an element's computed direction is left-to-right:
36+
37+
```ts
38+
import { isLTR } from '@spectrum-web-components/base/src/direction.js';
39+
40+
// In a keyboard handler
41+
if (isLTR(this) ? key === 'ArrowRight' : key === 'ArrowLeft') {
42+
this.openSubmenu();
43+
}
44+
```
45+
46+
The `isLTR` function uses `getComputedStyle` to read the browser's resolved direction, which respects CSS `direction` property, `dir` attributes, and inherited values from ancestors.
47+
3348
#### public shadowRoot!: ShadowRoot;
3449

3550
Elements built from `SpectrumMixin` assume that you will be using shadow DOM in the resulting custom element. To simplify TypeScript usage the presence of `this.shadowRoot` is asserted as non-null so that you have direct access to it without extended type checking.

1st-gen/tools/base/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@
9393
"./streaming-listener.js": {
9494
"development": "./streaming-listener.dev.js",
9595
"default": "./streaming-listener.js"
96-
}
96+
},
97+
"./src/direction.js": "./src/direction.js"
9798
},
9899
"scripts": {
99100
"test": "karma start --coverage"

0 commit comments

Comments
 (0)