kopia lustrzana https://github.com/shoelace-style/shoelace
improve RTL
rodzic
81620f0199
commit
0163edd8a3
|
|
@ -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._ 🐛
|
_During the beta period, these restrictions may be relaxed in the event of a mission-critical bug._ 🐛
|
||||||
|
|
||||||
|
## Next
|
||||||
|
|
||||||
|
- Improved RTL support for `<sl-image-comparer>`
|
||||||
|
|
||||||
## 2.0.0-beta.81
|
## 2.0.0-beta.81
|
||||||
|
|
||||||
- 🚨 BREAKING: removed the `base` part from `<sl-menu>` and removed an unnecessary `<div>` that made styling more difficult
|
- 🚨 BREAKING: removed the `base` part from `<sl-menu>` and removed an unnecessary `<div>` that made styling more difficult
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,12 @@
|
||||||
import { html, LitElement } from 'lit';
|
import { html, LitElement } from 'lit';
|
||||||
import { customElement, property, query } from 'lit/decorators.js';
|
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 { styleMap } from 'lit/directives/style-map.js';
|
||||||
import { drag } from '../../internal/drag';
|
import { drag } from '../../internal/drag';
|
||||||
import { emit } from '../../internal/event';
|
import { emit } from '../../internal/event';
|
||||||
import { clamp } from '../../internal/math';
|
import { clamp } from '../../internal/math';
|
||||||
import { watch } from '../../internal/watch';
|
import { watch } from '../../internal/watch';
|
||||||
|
import { LocalizeController } from '../../utilities/localize';
|
||||||
import '../icon/icon';
|
import '../icon/icon';
|
||||||
import styles from './image-comparer.styles';
|
import styles from './image-comparer.styles';
|
||||||
import type { CSSResultGroup } from 'lit';
|
import type { CSSResultGroup } from 'lit';
|
||||||
|
|
@ -37,33 +39,40 @@ export default class SlImageComparer extends LitElement {
|
||||||
@query('.image-comparer') base: HTMLElement;
|
@query('.image-comparer') base: HTMLElement;
|
||||||
@query('.image-comparer__handle') handle: HTMLElement;
|
@query('.image-comparer__handle') handle: HTMLElement;
|
||||||
|
|
||||||
|
private readonly localize = new LocalizeController(this);
|
||||||
|
|
||||||
/** The position of the divider as a percentage. */
|
/** The position of the divider as a percentage. */
|
||||||
@property({ type: Number, reflect: true }) position = 50;
|
@property({ type: Number, reflect: true }) position = 50;
|
||||||
|
|
||||||
handleDrag(event: PointerEvent) {
|
handleDrag(event: PointerEvent) {
|
||||||
const { width } = this.base.getBoundingClientRect();
|
const { width } = this.base.getBoundingClientRect();
|
||||||
|
const isRtl = this.localize.dir() === 'rtl';
|
||||||
|
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
|
|
||||||
drag(this.base, {
|
drag(this.base, {
|
||||||
onMove: x => {
|
onMove: x => {
|
||||||
this.position = parseFloat(clamp((x / width) * 100, 0, 100).toFixed(2));
|
this.position = parseFloat(clamp((x / width) * 100, 0, 100).toFixed(2));
|
||||||
|
if (isRtl) this.position = 100 - this.position;
|
||||||
},
|
},
|
||||||
initialEvent: event
|
initialEvent: event
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
handleKeyDown(event: KeyboardEvent) {
|
handleKeyDown(event: KeyboardEvent) {
|
||||||
|
const isLtr = this.localize.dir() === 'ltr';
|
||||||
|
const isRtl = this.localize.dir() === 'rtl';
|
||||||
|
|
||||||
if (['ArrowLeft', 'ArrowRight', 'Home', 'End'].includes(event.key)) {
|
if (['ArrowLeft', 'ArrowRight', 'Home', 'End'].includes(event.key)) {
|
||||||
const incr = event.shiftKey ? 10 : 1;
|
const incr = event.shiftKey ? 10 : 1;
|
||||||
let newPosition = this.position;
|
let newPosition = this.position;
|
||||||
|
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
|
|
||||||
if (event.key === 'ArrowLeft') {
|
if ((isLtr && event.key === 'ArrowLeft') || (isRtl && event.key === 'ArrowRight')) {
|
||||||
newPosition -= incr;
|
newPosition -= incr;
|
||||||
}
|
}
|
||||||
if (event.key === 'ArrowRight') {
|
if ((isLtr && event.key === 'ArrowRight') || (isRtl && event.key === 'ArrowLeft')) {
|
||||||
newPosition += incr;
|
newPosition += incr;
|
||||||
}
|
}
|
||||||
if (event.key === 'Home') {
|
if (event.key === 'Home') {
|
||||||
|
|
@ -84,8 +93,18 @@ export default class SlImageComparer extends LitElement {
|
||||||
}
|
}
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
|
const isRtl = this.localize.dir() === 'rtl';
|
||||||
|
|
||||||
return html`
|
return html`
|
||||||
<div part="base" id="image-comparer" class="image-comparer" @keydown=${this.handleKeyDown}>
|
<div
|
||||||
|
part="base"
|
||||||
|
id="image-comparer"
|
||||||
|
class=${classMap({
|
||||||
|
'image-comparer': true,
|
||||||
|
'image-comparer--rtl': isRtl
|
||||||
|
})}
|
||||||
|
@keydown=${this.handleKeyDown}
|
||||||
|
>
|
||||||
<div class="image-comparer__image">
|
<div class="image-comparer__image">
|
||||||
<div part="before" class="image-comparer__before">
|
<div part="before" class="image-comparer__before">
|
||||||
<slot name="before"></slot>
|
<slot name="before"></slot>
|
||||||
|
|
@ -94,7 +113,9 @@ export default class SlImageComparer extends LitElement {
|
||||||
<div
|
<div
|
||||||
part="after"
|
part="after"
|
||||||
class="image-comparer__after"
|
class="image-comparer__after"
|
||||||
style=${styleMap({ clipPath: `inset(0 ${100 - this.position}% 0 0)` })}
|
style=${styleMap({
|
||||||
|
clipPath: isRtl ? `inset(0 0 0 ${100 - this.position}%)` : `inset(0 ${100 - this.position}% 0 0)`
|
||||||
|
})}
|
||||||
>
|
>
|
||||||
<slot name="after"></slot>
|
<slot name="after"></slot>
|
||||||
</div>
|
</div>
|
||||||
|
|
@ -103,7 +124,9 @@ export default class SlImageComparer extends LitElement {
|
||||||
<div
|
<div
|
||||||
part="divider"
|
part="divider"
|
||||||
class="image-comparer__divider"
|
class="image-comparer__divider"
|
||||||
style=${styleMap({ left: `${this.position}%` })}
|
style=${styleMap({
|
||||||
|
left: isRtl ? `${100 - this.position}%` : `${this.position}%`
|
||||||
|
})}
|
||||||
@mousedown=${this.handleDrag}
|
@mousedown=${this.handleDrag}
|
||||||
@touchstart=${this.handleDrag}
|
@touchstart=${this.handleDrag}
|
||||||
>
|
>
|
||||||
|
|
|
||||||
Ładowanie…
Reference in New Issue