diff --git a/docs/resources/changelog.md b/docs/resources/changelog.md index 55f26523..8eca210c 100644 --- a/docs/resources/changelog.md +++ b/docs/resources/changelog.md @@ -8,6 +8,10 @@ New versions of Shoelace are released as-needed and generally occur when a criti _During the beta period, these restrictions may be relaxed in the event of a mission-critical bug._ 🐛 +## Next + +- Improved RTL support for `` + ## 2.0.0-beta.81 - 🚨 BREAKING: removed the `base` part from `` and removed an unnecessary `
` that made styling more difficult diff --git a/src/components/image-comparer/image-comparer.ts b/src/components/image-comparer/image-comparer.ts index d3dadac3..40c3c1b3 100644 --- a/src/components/image-comparer/image-comparer.ts +++ b/src/components/image-comparer/image-comparer.ts @@ -1,10 +1,12 @@ import { html, LitElement } from 'lit'; import { customElement, property, query } from 'lit/decorators.js'; +import { classMap } from 'lit/directives/class-map.js'; import { styleMap } from 'lit/directives/style-map.js'; import { drag } from '../../internal/drag'; import { emit } from '../../internal/event'; import { clamp } from '../../internal/math'; import { watch } from '../../internal/watch'; +import { LocalizeController } from '../../utilities/localize'; import '../icon/icon'; import styles from './image-comparer.styles'; import type { CSSResultGroup } from 'lit'; @@ -37,33 +39,40 @@ export default class SlImageComparer extends LitElement { @query('.image-comparer') base: HTMLElement; @query('.image-comparer__handle') handle: HTMLElement; + private readonly localize = new LocalizeController(this); + /** The position of the divider as a percentage. */ @property({ type: Number, reflect: true }) position = 50; handleDrag(event: PointerEvent) { const { width } = this.base.getBoundingClientRect(); + const isRtl = this.localize.dir() === 'rtl'; event.preventDefault(); drag(this.base, { onMove: x => { this.position = parseFloat(clamp((x / width) * 100, 0, 100).toFixed(2)); + if (isRtl) this.position = 100 - this.position; }, initialEvent: event }); } handleKeyDown(event: KeyboardEvent) { + const isLtr = this.localize.dir() === 'ltr'; + const isRtl = this.localize.dir() === 'rtl'; + if (['ArrowLeft', 'ArrowRight', 'Home', 'End'].includes(event.key)) { const incr = event.shiftKey ? 10 : 1; let newPosition = this.position; event.preventDefault(); - if (event.key === 'ArrowLeft') { + if ((isLtr && event.key === 'ArrowLeft') || (isRtl && event.key === 'ArrowRight')) { newPosition -= incr; } - if (event.key === 'ArrowRight') { + if ((isLtr && event.key === 'ArrowRight') || (isRtl && event.key === 'ArrowLeft')) { newPosition += incr; } if (event.key === 'Home') { @@ -84,8 +93,18 @@ export default class SlImageComparer extends LitElement { } render() { + const isRtl = this.localize.dir() === 'rtl'; + return html` -
+
@@ -94,7 +113,9 @@ export default class SlImageComparer extends LitElement {
@@ -103,7 +124,9 @@ export default class SlImageComparer extends LitElement {