Debian Codegraph @

src/modules/Classes.ts

raw
1 import type Swup from '../Swup.js';
2 import { queryAll } from '../utils.js';
3
4 export class Classes {
5 protected swup: Swup;
6 protected swupClasses = [
7 'to-',
8 'is-changing',
9 'is-rendering',
10 'is-popstate',
11 'is-animating',
12 'is-leaving'
13 ];
14
15 constructor(swup: Swup) {
16 this.swup = swup;
17 }
18
19 protected get selectors(): string[] {
20 const { scope } = this.swup.visit.animation;
21 if (scope === 'containers') return this.swup.visit.containers;
22 if (scope === 'html') return ['html'];
23 if (Array.isArray(scope)) return scope;
24 return [];
25 }
26
27 protected get selector(): string {
28 return this.selectors.join(',');
29 }
30
31 protected get targets(): HTMLElement[] {
32 if (!this.selector.trim()) return [];
33 return queryAll(this.selector);
34 }
35
36 add(...classes: string[]): void {
37 this.targets.forEach((target) => target.classList.add(...classes));
38 }
39
40 remove(...classes: string[]): void {
41 this.targets.forEach((target) => target.classList.remove(...classes));
42 }
43
44 clear(): void {
45 this.targets.forEach((target) => {
46 const remove = target.className.split(' ').filter((c) => this.isSwupClass(c));
47 target.classList.remove(...remove);
48 });
49 }
50
51 protected isSwupClass(className: string): boolean {
52 return this.swupClasses.some((c) => className.startsWith(c));
53 }
54 }