sveltacular 0.0.59 → 0.0.61

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.
@@ -0,0 +1,2 @@
1
+ export declare const navigateToAnchor: (anchor: string) => void;
2
+ export declare const getAnchor: () => string | null;
@@ -0,0 +1,17 @@
1
+ import { browser } from '$app/environment';
2
+ export const navigateToAnchor = (anchor) => {
3
+ if (!browser)
4
+ return;
5
+ setTimeout(() => {
6
+ if (window.top)
7
+ window.top.location.hash = anchor;
8
+ else
9
+ window.location.href = anchor;
10
+ }, 0);
11
+ };
12
+ export const getAnchor = () => {
13
+ if (!browser)
14
+ return null;
15
+ const hash = window.top ? window.top.location.hash : window.location.hash;
16
+ return hash ? hash.slice(1) : null;
17
+ };
package/dist/index.d.ts CHANGED
@@ -91,6 +91,7 @@ export * from './types/data.js';
91
91
  export * from './types/date.js';
92
92
  export * from './types/form.js';
93
93
  export * from './helpers/ago.js';
94
+ export * from './helpers/anchor.js';
94
95
  export * from './helpers/capitalize.js';
95
96
  export * from './helpers/date.js';
96
97
  export * from './helpers/debounce.js';
package/dist/index.js CHANGED
@@ -102,6 +102,7 @@ export * from './types/date.js';
102
102
  export * from './types/form.js';
103
103
  // Helpers
104
104
  export * from './helpers/ago.js';
105
+ export * from './helpers/anchor.js';
105
106
  export * from './helpers/capitalize.js';
106
107
  export * from './helpers/date.js';
107
108
  export * from './helpers/debounce.js';
@@ -3,6 +3,7 @@ export type TabStyle = 'traditional' | 'underline' | 'outline' | 'overline';
3
3
  export type TabDefinition = {
4
4
  id: string;
5
5
  name: string;
6
+ defaultActive: boolean;
6
7
  };
7
8
  export interface TabContext {
8
9
  active: Subscribable<string | null>;
@@ -1,17 +1,38 @@
1
1
  <script>import { writable } from "svelte/store";
2
- import { setContext } from "svelte";
2
+ import { createEventDispatcher, setContext } from "svelte";
3
3
  import { tabContext } from "./tab-context.js";
4
- import { subscribable } from "../../index.js";
4
+ import { getAnchor, navigateToAnchor, subscribable } from "../../index.js";
5
5
  export let style = "traditional";
6
+ const dispatch = createEventDispatcher();
6
7
  const tabs = writable([]);
7
8
  const register = (id, name, isActive) => {
8
- tabs.update((value) => [...value, { id, name }]);
9
- if (isActive)
10
- active.set(id);
9
+ tabs.update((value) => [...value, { id, name, defaultActive: isActive }]);
10
+ checkActive();
11
+ };
12
+ const tabExists = (id) => {
13
+ const value = $tabs;
14
+ return value.some((tab) => tab.id === id);
15
+ };
16
+ let timeout = null;
17
+ const checkActive = () => {
18
+ if (timeout)
19
+ clearTimeout(timeout);
20
+ timeout = setTimeout(() => {
21
+ const anchor = getAnchor();
22
+ if (anchor && tabExists(anchor))
23
+ active.set(anchor);
24
+ else {
25
+ const defaultActiveTab = $tabs.find((tab) => tab.defaultActive);
26
+ if (defaultActiveTab)
27
+ active.set(defaultActiveTab.id);
28
+ }
29
+ }, 10);
11
30
  };
12
31
  const active = writable(null);
13
32
  const selectTab = (id) => {
14
33
  active.set(id);
34
+ navigateToAnchor(id);
35
+ dispatch("change", id);
15
36
  };
16
37
  const ctx = {
17
38
  active: subscribable(active),
@@ -5,6 +5,8 @@ declare const __propDef: {
5
5
  style?: TabStyle | undefined;
6
6
  };
7
7
  events: {
8
+ change: CustomEvent<string | null>;
9
+ } & {
8
10
  [evt: string]: CustomEvent<any>;
9
11
  };
10
12
  slots: {
@@ -5,13 +5,17 @@ import Loading from "../../placeholders/loading.svelte";
5
5
  export let title;
6
6
  export let href = void 0;
7
7
  export let active = false;
8
+ export let id = void 0;
9
+ const getId = () => id || title.trim().toLocaleLowerCase().replaceAll(" ", "_") || uniqueId();
8
10
  const dispatch = createEventDispatcher();
9
11
  const ctx = getContext(tabContext);
10
12
  const tabStyle = ctx.style || "traditional";
11
- const id = uniqueId();
12
- ctx.register(id, title, active);
13
+ const _id = getId();
14
+ ctx.register(_id, title, active);
13
15
  const unsubscribe = ctx.active.subscribe((selectedId) => {
14
- active = selectedId === id;
16
+ active = selectedId === _id;
17
+ if (active)
18
+ dispatch("selected", _id);
15
19
  if (active && href) {
16
20
  console.log("Navigating to", href);
17
21
  navigateTo(href);
@@ -20,7 +24,7 @@ const unsubscribe = ctx.active.subscribe((selectedId) => {
20
24
  onDestroy(() => unsubscribe());
21
25
  </script>
22
26
 
23
- <article class:active>
27
+ <article class:active class={tabStyle}>
24
28
  {#if active}
25
29
  {#if $$slots.default}
26
30
  <slot />
@@ -4,9 +4,10 @@ declare const __propDef: {
4
4
  title: string;
5
5
  href?: string | undefined;
6
6
  active?: boolean | undefined;
7
+ id?: string | undefined;
7
8
  };
8
9
  events: {
9
- selectTab: CustomEvent<string>;
10
+ selected: CustomEvent<string>;
10
11
  } & {
11
12
  [evt: string]: CustomEvent<any>;
12
13
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sveltacular",
3
- "version": "0.0.59",
3
+ "version": "0.0.61",
4
4
  "description": "A Svelte component library",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.js",