@onsvisual/svelte-components 0.1.4 → 0.1.6

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.
@@ -14,6 +14,8 @@ export default class Scroller extends SvelteComponentTyped<{
14
14
  visible?: boolean;
15
15
  }, {
16
16
  change: CustomEvent<any>;
17
+ enter: CustomEvent<any>;
18
+ exit: CustomEvent<any>;
17
19
  } & {
18
20
  [evt: string]: CustomEvent<any>;
19
21
  }, {
@@ -40,6 +42,8 @@ declare const __propDef: {
40
42
  };
41
43
  events: {
42
44
  change: CustomEvent<any>;
45
+ enter: CustomEvent<any>;
46
+ exit: CustomEvent<any>;
43
47
  } & {
44
48
  [evt: string]: CustomEvent<any>;
45
49
  };
@@ -45,11 +45,13 @@
45
45
  },
46
46
 
47
47
  remove: ({ outer, update }) => {
48
- const index = handlers.indexOf(update);
49
- if (index !== -1) handlers.splice(index, 1);
48
+ if (map.get(outer)) {
49
+ const index = handlers.indexOf(update);
50
+ if (index !== -1) handlers.splice(index, 1);
50
51
 
51
- map.delete(outer);
52
- observer.unobserve(outer);
52
+ map.delete(outer);
53
+ observer.unobserve(outer);
54
+ }
53
55
  },
54
56
  };
55
57
  } else {
@@ -67,7 +69,9 @@
67
69
  </script>
68
70
 
69
71
  <script>
70
- import { onMount, createEventDispatcher } from "svelte";
72
+ import { onMount, setContext, createEventDispatcher } from "svelte";
73
+ import { writable } from "svelte/store";
74
+
71
75
  const dispatch = createEventDispatcher();
72
76
 
73
77
  // config
@@ -94,7 +98,6 @@
94
98
 
95
99
  let top = 0;
96
100
  let bottom = 1;
97
- let query = "section";
98
101
  let parallax = false;
99
102
 
100
103
  // bindings
@@ -129,11 +132,14 @@
129
132
  */
130
133
  export let visible = false;
131
134
 
135
+ const sections = writable([]);
136
+ setContext("sections", sections);
137
+
138
+ let scroller;
132
139
  let outer;
133
140
  let foreground;
134
141
  let background;
135
142
  let left;
136
- let sections;
137
143
  let wh = 0;
138
144
  let fixed;
139
145
  let offset_top = 0;
@@ -156,17 +162,19 @@
156
162
 
157
163
  $: widthStyle = fixed ? `width:${width}px;` : "";
158
164
 
159
- onMount(() => {
160
- sections = foreground.querySelectorAll(query);
165
+ function initSections(sections) {
166
+ if (scroller) manager.remove(scroller);
167
+
161
168
  count = sections.length;
162
169
 
163
170
  update();
164
171
 
165
- const scroller = { outer, update };
172
+ scroller = { outer, update };
166
173
 
167
174
  manager.add(scroller);
168
175
  return () => manager.remove(scroller);
169
- });
176
+ }
177
+ $: outer && initSections($sections);
170
178
 
171
179
  function update() {
172
180
  if (!foreground) return;
@@ -180,7 +188,10 @@
180
188
  const fg = foreground.getBoundingClientRect();
181
189
  const bg = background.getBoundingClientRect();
182
190
 
183
- visible = fg.top < wh && fg.bottom > 0;
191
+ const visible_new = fg.top < wh && fg.bottom > 0;
192
+ const entered = visible_new && !visible;
193
+ const exited = !visible_new && visible;
194
+ visible = visible_new;
184
195
 
185
196
  const foreground_height = fg.bottom - fg.top;
186
197
  const background_height = bg.bottom - bg.top;
@@ -203,16 +214,16 @@
203
214
  fixed = true;
204
215
  }
205
216
 
206
- for (let i = 0; i < sections.length; i++) {
207
- const section = sections[i];
217
+ for (let i = 0; i < $sections.length; i++) {
218
+ const section = $sections[i];
208
219
  const { top } = section.getBoundingClientRect();
209
220
 
210
- const next = sections[i + 1];
221
+ const next = $sections[i + 1];
211
222
  const bottom = next ? next.getBoundingClientRect().top : fg.bottom;
212
223
 
213
224
  offset = (threshold_px - top) / (bottom - top);
214
225
  if (bottom >= threshold_px) {
215
- if (index !== i) {
226
+ if (index !== i || entered) {
216
227
  index = i;
217
228
  sectionId = section.dataset.id ? section.dataset.id : null;
218
229
  dispatch("change", { id, index, sectionId });
@@ -220,6 +231,9 @@
220
231
  break;
221
232
  }
222
233
  }
234
+
235
+ if (entered) dispatch("enter", { id, index, sectionId });
236
+ if (exited) dispatch("exit", { id, index, sectionId });
223
237
  }
224
238
  </script>
225
239
 
@@ -1,5 +1,5 @@
1
1
  <script>
2
- // import { onMount } from "svelte";
2
+ import { onMount, onDestroy, getContext } from "svelte";
3
3
  import Container from "../../wrappers/Container/Container.svelte";
4
4
 
5
5
  /**
@@ -24,12 +24,20 @@
24
24
  export let hideTitle = false;
25
25
 
26
26
  let section;
27
- // let background;
28
27
 
29
- // onMount(() => {
30
- // background = getComputedStyle(section).getPropertyValue("--background").replaceAll('"', "");
31
- // console.log(section, getComputedStyle(section), background);
32
- // });
28
+ const sections = getContext("sections");
29
+
30
+ onMount(() => {
31
+ if (sections) {
32
+ $sections = [...$sections, section];
33
+ }
34
+ });
35
+
36
+ onDestroy(() => {
37
+ if (sections) {
38
+ $sections = $sections.filter((s) => s !== section);
39
+ }
40
+ });
33
41
  </script>
34
42
 
35
43
  <section data-id="{id}" bind:this="{section}">
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onsvisual/svelte-components",
3
- "version": "0.1.4",
3
+ "version": "0.1.6",
4
4
  "type": "module",
5
5
  "private": false,
6
6
  "homepage": "https://onsvisual.github.io/svelte-components",