@onsvisual/svelte-components 1.0.49 → 1.0.50

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.
@@ -1,9 +1,11 @@
1
1
  <script>
2
- import { onMount, setContext } from "svelte";
2
+ import { onMount, setContext, createEventDispatcher } from "svelte";
3
3
  import Accordion from "./accordion.js";
4
4
  import Details from "./details.js";
5
5
  import { slugify, sleep } from "../../js/utils.js";
6
6
 
7
+ const dispatch = createEventDispatcher();
8
+
7
9
  /**
8
10
  * Optional: Set the unique ID of the accordion
9
11
  * @type {string}
@@ -27,7 +29,9 @@
27
29
  onMount(async () => {
28
30
  await sleep(0);
29
31
  const detailsComponents = [...el.querySelectorAll(".ons-js-details")];
30
- const detailsEls = detailsComponents.map((element) => new Details(element));
32
+ const detailsEls = detailsComponents.map(
33
+ (el) => new Details(el, (data) => dispatch("toggle", { ...data, el }))
34
+ );
31
35
  new Accordion(buttonEl, detailsEls);
32
36
  });
33
37
  </script>
@@ -6,6 +6,8 @@ export default class Accordion extends SvelteComponentTyped<{
6
6
  id?: string | undefined;
7
7
  showToggle?: boolean | undefined;
8
8
  }, {
9
+ toggle: CustomEvent<any>;
10
+ } & {
9
11
  [evt: string]: CustomEvent<any>;
10
12
  }, {
11
13
  default: {};
@@ -22,6 +24,8 @@ declare const __propDef: {
22
24
  showToggle?: boolean | undefined;
23
25
  };
24
26
  events: {
27
+ toggle: CustomEvent<any>;
28
+ } & {
25
29
  [evt: string]: CustomEvent<any>;
26
30
  };
27
31
  slots: {
@@ -1,5 +1,5 @@
1
1
  export default class Details {
2
- constructor(detailsElement: any);
2
+ constructor(detailsElement: any, callback?: null);
3
3
  saveState: boolean;
4
4
  open: boolean;
5
5
  group: any;
@@ -7,6 +7,8 @@ export default class Details {
7
7
  detailsHeader: any;
8
8
  content: any;
9
9
  detailsTitle: any;
10
+ detailsId: any;
11
+ callback: any;
10
12
  toggle(event: any): void;
11
13
  setOpen(open: any): void;
12
14
  isOpen: any;
@@ -1,5 +1,5 @@
1
1
  export default class Details {
2
- constructor(detailsElement) {
2
+ constructor(detailsElement, callback = null) {
3
3
  this.saveState = detailsElement.getAttribute("data-save-state") === "true";
4
4
  this.open = detailsElement.getAttribute("data-open") === "true";
5
5
  this.group = detailsElement.getAttribute("data-group");
@@ -11,14 +11,15 @@ export default class Details {
11
11
  this.detailsTitle = this.details.querySelector(".ons-js-corrections-details-title");
12
12
 
13
13
  // Initialise
14
- const detailsId = detailsElement.getAttribute("id");
14
+ this.detailsId = detailsElement.getAttribute("id");
15
+ this.callback = callback;
15
16
 
16
17
  this.details.setAttribute("role", "group");
17
18
  this.detailsHeader.setAttribute("role", "link");
18
- this.detailsHeader.setAttribute("aria-controls", detailsId);
19
+ this.detailsHeader.setAttribute("aria-controls", this.detailsId);
19
20
  this.detailsHeader.setAttribute("tabindex", 0);
20
21
 
21
- if (localStorage.getItem(detailsId) || this.open) {
22
+ if (localStorage.getItem(this.detailsId) || this.open) {
22
23
  this.setOpen(true);
23
24
  } else {
24
25
  this.setOpen(false);
@@ -32,6 +33,8 @@ export default class Details {
32
33
  toggle(event) {
33
34
  event.preventDefault();
34
35
  this.setOpen(!this.isOpen);
36
+
37
+ if (typeof this.callback === "function") this.callback({ id: this.detailsId, open: this.isOpen });
35
38
  }
36
39
 
37
40
  setOpen(open) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onsvisual/svelte-components",
3
- "version": "1.0.49",
3
+ "version": "1.0.50",
4
4
  "scripts": {
5
5
  "dev": "vite dev",
6
6
  "build": "npm run build:package && npm run build:docs",