@creative-web-solution/front-library 7.1.48 → 7.1.49

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 7.1.49
4
+
5
+ - [Accordion]: Fix bug when a tab is opened at start
6
+
3
7
  ## 7.1.48
4
8
 
5
9
  - [MediaQueriesEvents]: Add position check function
@@ -1,13 +1,12 @@
1
- import { next } from '../../DOM/Traversing';
2
-
1
+ import { next } from "../../DOM/Traversing";
3
2
 
4
3
  /**
5
4
  * Tab of an accordion
6
5
  */
7
6
  export default class Tab {
8
- #isOpen: boolean;
7
+ #isOpen: boolean;
9
8
  #originalOpenedState: boolean;
10
- #$TAB_PANNEL: HTMLElement | null;
9
+ #$TAB_PANNEL: HTMLElement | null;
11
10
  #options;
12
11
  #$TAB;
13
12
 
@@ -15,75 +14,82 @@ export default class Tab {
15
14
  return this.#isOpen;
16
15
  }
17
16
 
18
- constructor( $TAB: HTMLElement, options: FLib.Accordion.TabOptions ) {
19
- this.#options = options;
20
- this.#$TAB = $TAB ;
21
-
22
- const ID = $TAB.getAttribute( 'aria-controls' );
23
- this.#$TAB_PANNEL = ID ? document.getElementById( ID ) : next( $TAB ) as HTMLElement;
17
+ constructor($TAB: HTMLElement, options: FLib.Accordion.TabOptions) {
18
+ this.#options = options;
19
+ this.#$TAB = $TAB;
24
20
 
25
- this.#isOpen = this.#originalOpenedState = $TAB.getAttribute( 'aria-expanded' ) === 'true';
21
+ const ID = $TAB.getAttribute("aria-controls");
22
+ this.#$TAB_PANNEL = ID
23
+ ? document.getElementById(ID)
24
+ : (next($TAB) as HTMLElement);
26
25
 
26
+ this.#isOpen = this.#originalOpenedState =
27
+ $TAB.getAttribute("aria-expanded") === "true";
27
28
 
28
- if ( this.#isOpen ) {
29
- this.#openTab( true );
29
+ if (this.#isOpen) {
30
+ this.#openTab(true);
30
31
  }
31
32
  }
32
33
 
33
-
34
34
  #changeTabState = (): void => {
35
- this.#$TAB.setAttribute( 'aria-expanded', this.#isOpen ? 'true' : 'false' );
36
- }
37
-
35
+ this.#$TAB.setAttribute(
36
+ "aria-expanded",
37
+ this.#isOpen ? "true" : "false",
38
+ );
39
+ };
38
40
 
39
- #openTab = ( isOpenAtStart?: boolean ): void => {
41
+ #openTab = (isOpenAtStart?: boolean): void => {
40
42
  this.#options.animations
41
- .open( this.#$TAB, this.#$TAB_PANNEL )
42
- .then( () => {
43
-
44
- if ( isOpenAtStart && this.#options.onOpenAtStart ) {
45
- this.#options.onOpenAtStart( this.#$TAB, this.#$TAB_PANNEL );
46
- }
47
- else if ( !isOpenAtStart && this.#options.onOpen ) {
48
- this.#options.onOpen( this.#$TAB, this.#$TAB_PANNEL );
49
- }
50
- } );
43
+ .open(this.#$TAB, this.#$TAB_PANNEL)
44
+ .then(() => {
45
+ if (isOpenAtStart && this.#options.onOpenAtStart) {
46
+ this.#options.onOpenAtStart(this.#$TAB, this.#$TAB_PANNEL);
47
+ } else if (!isOpenAtStart && this.#options.onOpen) {
48
+ this.#options.onOpen(this.#$TAB, this.#$TAB_PANNEL);
49
+ }
50
+ });
51
51
 
52
52
  this.#isOpen = true;
53
53
  this.#changeTabState();
54
- }
54
+ };
55
55
 
56
- #closeTab = ( autoClose?: boolean ): void => {
56
+ #closeTab = (autoClose?: boolean): void => {
57
57
  this.#options.animations
58
- .close( this.#$TAB, this.#$TAB_PANNEL )
59
- .then( () => {
60
- this.#options.onClose?.( this.#$TAB, this.#$TAB_PANNEL, autoClose );
61
- } );
58
+ .close(this.#$TAB, this.#$TAB_PANNEL)
59
+ .then(() => {
60
+ this.#options.onClose?.(
61
+ this.#$TAB,
62
+ this.#$TAB_PANNEL,
63
+ autoClose,
64
+ );
65
+ });
62
66
 
63
67
  this.#isOpen = false;
64
68
  this.#changeTabState();
65
- }
69
+ };
66
70
 
67
71
  open(): this {
68
- if( !this.#isOpen ) {
69
- this.#openTab( false );
72
+ if (!this.#isOpen) {
73
+ this.#openTab(false);
70
74
  }
71
75
  return this;
72
76
  }
73
77
 
74
- close( autoClose?: boolean ): this {
75
- if( this.#isOpen ) {
76
- this.#closeTab( autoClose );
78
+ close(autoClose?: boolean): this {
79
+ if (this.#isOpen) {
80
+ this.#closeTab(autoClose);
77
81
  }
78
82
 
79
83
  return this;
80
84
  }
81
85
 
82
-
83
86
  destroy(): this {
84
- this.#options.animations.destroy( this.#$TAB, this.#$TAB_PANNEL );
87
+ this.#options.animations.destroy(this.#$TAB, this.#$TAB_PANNEL);
85
88
 
86
- this.#$TAB.setAttribute( 'aria-expanded', this.#originalOpenedState ? 'true' : 'false' );
89
+ this.#$TAB.setAttribute(
90
+ "aria-expanded",
91
+ this.#originalOpenedState ? "true" : "false",
92
+ );
87
93
 
88
94
  return this;
89
95
  }
@@ -1,33 +1,31 @@
1
- import { aClass, rClass } from '../../DOM/Class';
2
- import { extend } from '../../Helpers/Extend';
3
- import { on, off } from '../../Events/EventsManager';
4
- import Tab from './Tab';
5
-
1
+ import { aClass, rClass } from "../../DOM/Class";
2
+ import { extend } from "../../Helpers/Extend";
3
+ import { on, off } from "../../Events/EventsManager";
4
+ import Tab from "./Tab";
6
5
 
7
6
  const DEFAULT_OPTIONS = {
8
- "tabSelector": "button[aria-expanded]",
9
- "allowMultipleTab": false,
10
- "atLeastOneOpen": false,
11
- "animations": {
12
- "open": function( $TAB, $TAB_PANNEL ) {
13
- aClass( [ $TAB, $TAB_PANNEL ], 'on' );
7
+ tabSelector: "button[aria-expanded]",
8
+ allowMultipleTab: false,
9
+ atLeastOneOpen: false,
10
+ animations: {
11
+ open: function ($TAB, $TAB_PANNEL) {
12
+ aClass([$TAB, $TAB_PANNEL], "on");
14
13
 
15
14
  return Promise.resolve();
16
15
  },
17
- "close": function( $TAB, $TAB_PANNEL ) {
18
- rClass( [ $TAB, $TAB_PANNEL ], 'on' );
16
+ close: function ($TAB, $TAB_PANNEL) {
17
+ rClass([$TAB, $TAB_PANNEL], "on");
19
18
 
20
19
  return Promise.resolve();
21
20
  },
22
- "destroy": function( $TAB, $TAB_PANNEL ) {
23
- rClass( [ $TAB, $TAB_PANNEL ], 'on' );
21
+ destroy: function ($TAB, $TAB_PANNEL) {
22
+ rClass([$TAB, $TAB_PANNEL], "on");
24
23
 
25
24
  return Promise.resolve();
26
- }
27
- }
25
+ },
26
+ },
28
27
  };
29
28
 
30
-
31
29
  /**
32
30
  * Accordion
33
31
  *
@@ -86,70 +84,76 @@ const DEFAULT_OPTIONS = {
86
84
  */
87
85
  export default class Accordion {
88
86
  #$accordionWrapper: HTMLElement;
89
- #options: FLib.Accordion.Options;
90
- #$tabs: NodeListOf<HTMLElement>;
91
- #tablist = new Map<HTMLElement, Tab>();
92
- #status: string;
87
+ #options: FLib.Accordion.Options;
88
+ #$tabs: NodeListOf<HTMLElement>;
89
+ #tablist = new Map<HTMLElement, Tab>();
90
+ #status: string;
93
91
  #lastOpenedTabs = new Set<Tab>();
94
92
 
93
+ #STATUS_ON = "STATUS_ON";
94
+ #STATUS_OFF = "STATUS_OFF";
95
95
 
96
- #STATUS_ON = 'STATUS_ON';
97
- #STATUS_OFF = 'STATUS_OFF';
98
-
99
-
100
- constructor( $accordionWrapper: HTMLElement, userOptions: FLib.Accordion.OptionsInit ) {
96
+ constructor(
97
+ $accordionWrapper: HTMLElement,
98
+ userOptions: FLib.Accordion.OptionsInit,
99
+ ) {
101
100
  this.#$accordionWrapper = $accordionWrapper;
102
101
 
103
- this.#options = extend( DEFAULT_OPTIONS, userOptions );
102
+ this.#options = extend(DEFAULT_OPTIONS, userOptions);
104
103
 
105
- this.#$tabs = $accordionWrapper.querySelectorAll( this.#options.tabSelector );
106
- this.#status = this.#STATUS_OFF;
104
+ this.#$tabs = $accordionWrapper.querySelectorAll(
105
+ this.#options.tabSelector,
106
+ );
107
+ this.#status = this.#STATUS_OFF;
107
108
 
108
109
  this.#on();
109
110
  }
110
111
 
111
112
  #on = (): void => {
112
- if( this.#status === this.#STATUS_ON ){
113
+ if (this.#status === this.#STATUS_ON) {
113
114
  return;
114
115
  }
115
116
 
116
117
  this.#status = this.#STATUS_ON;
117
118
 
118
- this.#$tabs.forEach( ( $tab, index ) => {
119
- this.#tablist.set($tab, new Tab( $tab, {
119
+ this.#$tabs.forEach(($tab, index) => {
120
+ const tab = new Tab($tab, {
120
121
  ...this.#options,
121
122
  index,
122
- } ) );
123
- } );
124
-
125
- on( this.#$accordionWrapper, {
126
- "eventsName": "click",
127
- "selector": this.#options.tabSelector,
128
- "callback": this.#toggleTab
129
- } );
130
- }
131
-
123
+ });
124
+ this.#tablist.set($tab, tab);
125
+ if (tab.isOpen) {
126
+ this.#lastOpenedTabs.add(tab);
127
+ }
128
+ });
129
+
130
+ on(this.#$accordionWrapper, {
131
+ eventsName: "click",
132
+ selector: this.#options.tabSelector,
133
+ callback: this.#toggleTab,
134
+ });
135
+ };
132
136
 
133
137
  #off = (): void => {
134
- if( this.#status === this.#STATUS_OFF ){
138
+ if (this.#status === this.#STATUS_OFF) {
135
139
  return;
136
140
  }
137
141
 
138
142
  this.#status = this.#STATUS_OFF;
139
143
 
140
- this.#tablist.forEach( tab => {
144
+ this.#tablist.forEach((tab) => {
141
145
  tab.destroy();
142
- } );
146
+ });
143
147
 
144
148
  this.#tablist.clear();
145
149
 
146
- off( this.#$accordionWrapper, {
147
- "eventsName": "click",
148
- "callback": this.#toggleTab
149
- } );
150
- }
150
+ off(this.#$accordionWrapper, {
151
+ eventsName: "click",
152
+ callback: this.#toggleTab,
153
+ });
154
+ };
151
155
 
152
- #toggleTab = ( e: Event, $target: HTMLElement ): void => {
156
+ #toggleTab = (e: Event, $target: HTMLElement): void => {
153
157
  e.preventDefault();
154
158
 
155
159
  const tab = this.#tablist.get($target);
@@ -159,12 +163,12 @@ export default class Accordion {
159
163
  }
160
164
 
161
165
  if (tab.isOpen) {
162
- this.closeTab(tab)
166
+ this.closeTab(tab);
163
167
  return;
164
168
  }
165
169
 
166
- this.openTab(tab)
167
- }
170
+ this.openTab(tab);
171
+ };
168
172
 
169
173
  closeTab(tab: Tab): this {
170
174
  if (this.#options.atLeastOneOpen && this.#lastOpenedTabs.size < 2) {
@@ -179,7 +183,7 @@ export default class Accordion {
179
183
 
180
184
  openTab(tab: Tab): this {
181
185
  if (!this.#options.allowMultipleTab && this.#lastOpenedTabs.size > 0) {
182
- this.#lastOpenedTabs.forEach(tab => {
186
+ this.#lastOpenedTabs.forEach((tab) => {
183
187
  tab.close(true);
184
188
  });
185
189
  this.#lastOpenedTabs.clear();
@@ -210,7 +214,6 @@ export default class Accordion {
210
214
  return this;
211
215
  }
212
216
 
213
-
214
217
  /**
215
218
  * Restart the module
216
219
  */
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Front Library
2
2
 
3
- @version: 7.1.48
3
+ @version: 7.1.49
4
4
 
5
5
 
6
6
  ## Use
@@ -1,35 +1,41 @@
1
1
  declare namespace FLib {
2
2
  namespace Accordion {
3
-
4
- type AnimationFunction = ( $tab: HTMLElement, $panel: HTMLElement ) => Promise<void>;
5
- type Callback = ( $tab: HTMLElement, $panel: HTMLElement ) => void;
6
- type CloseCallback = ( $tab: HTMLElement, $panel: HTMLElement, autoclose: boolean ) => void;
3
+ type AnimationFunction = (
4
+ $tab: HTMLElement,
5
+ $panel: HTMLElement,
6
+ ) => Promise<void>;
7
+ type Callback = ($tab: HTMLElement, $panel: HTMLElement) => void;
8
+ type CloseCallback = (
9
+ $tab: HTMLElement,
10
+ $panel: HTMLElement,
11
+ autoclose: boolean,
12
+ ) => void;
7
13
 
8
14
  interface AnimationOptions {
9
- open: AnimationFunction;
10
- close: AnimationFunction;
11
- destroy: AnimationFunction;
15
+ open: AnimationFunction;
16
+ close: AnimationFunction;
17
+ destroy: AnimationFunction;
12
18
  }
13
19
 
14
20
  interface Options {
15
21
  /** @defaultValue 'button[aria-expanded]' */
16
- tabSelector: string;
22
+ tabSelector: string;
17
23
  /** @defaultValue false */
18
- allowMultipleTab: boolean;
24
+ allowMultipleTab: boolean;
19
25
  /** @defaultValue false */
20
- atLeastOneOpen: boolean;
21
- onOpenAtStart: Callback;
22
- onOpen: Callback;
23
- onClose: CloseCallback;
24
- animations: Partial<AnimationOptions>;
26
+ atLeastOneOpen: boolean;
27
+ onOpenAtStart: Callback;
28
+ onOpen: Callback;
29
+ onClose: CloseCallback;
30
+ animations: Partial<AnimationOptions>;
25
31
  }
26
32
 
27
33
  interface OptionsInit extends Partial<Options> {
28
- animations?: Partial<AnimationOptions>;
34
+ animations?: Partial<AnimationOptions>;
29
35
  }
30
36
 
31
37
  interface TabOptions extends Options {
32
- index: number;
38
+ index: number;
33
39
  }
34
40
  }
35
41
  }
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@creative-web-solution/front-library",
3
3
  "title": "Frontend library",
4
4
  "description": "Frontend functions and modules",
5
- "version": "7.1.48",
5
+ "version": "7.1.49",
6
6
  "homepage": "https://github.com/creative-web-solution/front-library",
7
7
  "author": "Creative Web Solution <contact@cws-studio.com> (https://www.cws-studio.com)",
8
8
  "keywords": [],