shoelace/src/components/carousel/autoplay-controller.ts

69 wiersze
1.8 KiB
TypeScript
Czysty Zwykły widok Historia

feat: add carousel component feat: add nav indicators wip wip wip fix: minor fixes fix: minor fixes fix: some refactor chore: update docs chore: update docs fix: remove slide component feat: create sl-carousel-item feat: code refactoring and improvements chore: update docs with more examples chore: fix docs feat: add autoplay feat: implement accessibility fix: change icons for rtl chore: minor change feat: improve accessibility fix: minor regression fix: minor regression chore: fix docs fix: improve accessibility and minor fixes fix: remove heading and refactor component chore: add custom style exmaple fix: address review commnets * Removed header from carousel * Added `ArrowUp` and `ArrowDown` in keyboard navigation * Added `--scroll-hint-margin` css property * Added an example with customized carousel layout * Fixed thumbnails navigation in demo * Renamed show-controls to show-arrows and updated the corresponding parts/css accordingly * Changed `activeSlideElement` getter to a private method * Changed pagination colors * Added `--slide-width` and `--slide-height` css properties chore: update docs fix: integrate latest repo changes fix: add aspect ratio and rebase chore: remove ignore path feat: multiple slides per page feat: multiple slide per page fix: various improvements chore: minor changes chore: minor changes chore: add bit of documentation chore: improve documentation fix: add unit tests and fix minor issues chore: update documentation and unit tests chore: update tests
2022-08-06 08:45:45 +00:00
import type { ReactiveController, ReactiveElement } from 'lit';
/**
* A controller that repeatedly calls the specified callback with the provided interval time.
* The timer is automatically paused while the user is interacting with the component.
*/
export class AutoplayController implements ReactiveController {
private host: ReactiveElement;
private timerId = 0;
private tickCallback: () => void;
paused = false;
stopped = true;
constructor(host: ReactiveElement, tickCallback: () => void) {
host.addController(this);
this.host = host;
this.tickCallback = tickCallback;
}
hostConnected(): void {
this.host.addEventListener('mouseenter', this.pause);
this.host.addEventListener('mouseleave', this.resume);
this.host.addEventListener('focusin', this.pause);
this.host.addEventListener('focusout', this.resume);
this.host.addEventListener('touchstart', this.pause, { passive: true });
this.host.addEventListener('touchend', this.resume);
}
hostDisconnected(): void {
this.stop();
this.host.removeEventListener('mouseenter', this.pause);
this.host.removeEventListener('mouseleave', this.resume);
this.host.removeEventListener('focusin', this.pause);
this.host.removeEventListener('focusout', this.resume);
this.host.removeEventListener('touchstart', this.pause);
this.host.removeEventListener('touchend', this.resume);
}
start(interval: number) {
this.stop();
this.stopped = false;
this.timerId = window.setInterval(() => {
if (!this.paused) {
this.tickCallback();
}
}, interval);
}
stop() {
clearInterval(this.timerId);
this.stopped = true;
this.host.requestUpdate();
}
pause = () => {
this.paused = true;
this.host.requestUpdate();
};
resume = () => {
this.paused = false;
this.host.requestUpdate();
};
}