@14ch/svelte-ui 0.0.19 → 0.0.20

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.
@@ -3,7 +3,7 @@
3
3
  <script lang="ts">
4
4
  import TabItem from './TabItem.svelte';
5
5
  import type { MenuItem } from '../types/menuItem';
6
- import { afterNavigate } from '$app/navigation';
6
+ import { subscribeUrlChange } from '../utils/urlChange';
7
7
 
8
8
  // =========================================================================
9
9
  // Props, States & Constants
@@ -44,6 +44,21 @@
44
44
 
45
45
  let resolvedCurrentPath = $state('');
46
46
 
47
+ // =========================================================================
48
+ // Effects
49
+ // =========================================================================
50
+ $effect(() => {
51
+ // props の currentPath が変更されたとき
52
+ resolvedCurrentPath = getCurrentPath();
53
+ });
54
+
55
+ $effect(() => {
56
+ // URL の変更を subscribe
57
+ return subscribeUrlChange(() => {
58
+ resolvedCurrentPath = getCurrentPath();
59
+ });
60
+ });
61
+
47
62
  // =========================================================================
48
63
  // Methods
49
64
  // =========================================================================
@@ -57,18 +72,10 @@
57
72
  if (typeof window !== 'undefined') {
58
73
  return window.location.pathname;
59
74
  }
75
+
60
76
  return '';
61
77
  };
62
78
 
63
- // currentPath が渡されたときやマウント時に選択状態を反映
64
- $effect(() => {
65
- resolvedCurrentPath = getCurrentPath();
66
- });
67
-
68
- afterNavigate(() => {
69
- resolvedCurrentPath = getCurrentPath();
70
- });
71
-
72
79
  // パスの正規化処理
73
80
  const normalizePath = (path: string): string => {
74
81
  if (!pathPrefix) return path;
@@ -109,11 +116,6 @@
109
116
  return normalizedCurrentPath !== '' && normalizedCurrentPath.startsWith(itemHref);
110
117
  };
111
118
 
112
- // 有効なタブのインデックス一覧(disabled を除く)
113
- const enabledIndices = $derived(
114
- tabItems.map((item, i) => (item.disabled ? -1 : i)).filter((i) => i >= 0)
115
- );
116
-
117
119
  // シンプルなキーボードナビゲーション(disabled タブはスキップ)
118
120
  const handleKeyDown = (event: KeyboardEvent) => {
119
121
  if (tabItems.length === 0 || enabledIndices.length === 0) return;
@@ -171,6 +173,11 @@
171
173
  }
172
174
  return -1;
173
175
  });
176
+
177
+ // 有効なタブのインデックス一覧(disabled を除く)
178
+ const enabledIndices = $derived(
179
+ tabItems.map((item, i) => (item.disabled ? -1 : i)).filter((i) => i >= 0)
180
+ );
174
181
  </script>
175
182
 
176
183
  <div
@@ -0,0 +1 @@
1
+ export declare const subscribeUrlChange: (handler: () => void) => () => void;
@@ -0,0 +1,29 @@
1
+ const URL_CHANGE_EVENT = 'svelte-ui:urlchange';
2
+ let historyPatched = false;
3
+ const patchHistory = () => {
4
+ if (historyPatched || typeof window === 'undefined')
5
+ return;
6
+ historyPatched = true;
7
+ const originalPushState = history.pushState.bind(history);
8
+ const originalReplaceState = history.replaceState.bind(history);
9
+ history.pushState = (...args) => {
10
+ originalPushState(...args);
11
+ window.dispatchEvent(new Event(URL_CHANGE_EVENT));
12
+ };
13
+ history.replaceState = (...args) => {
14
+ originalReplaceState(...args);
15
+ window.dispatchEvent(new Event(URL_CHANGE_EVENT));
16
+ };
17
+ };
18
+ export const subscribeUrlChange = (handler) => {
19
+ if (typeof window === 'undefined') {
20
+ return () => { };
21
+ }
22
+ patchHistory();
23
+ window.addEventListener('popstate', handler);
24
+ window.addEventListener(URL_CHANGE_EVENT, handler);
25
+ return () => {
26
+ window.removeEventListener('popstate', handler);
27
+ window.removeEventListener(URL_CHANGE_EVENT, handler);
28
+ };
29
+ };
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@14ch/svelte-ui",
3
3
  "description": "Modern Svelte UI components library with TypeScript support",
4
4
  "private": false,
5
- "version": "0.0.19",
5
+ "version": "0.0.20",
6
6
  "type": "module",
7
7
  "keywords": [
8
8
  "svelte",
@@ -106,7 +106,6 @@
106
106
  "sass": "^1.89.2"
107
107
  },
108
108
  "peerDependencies": {
109
- "@sveltejs/kit": "^2.0.0",
110
109
  "svelte": "^5.0.0"
111
110
  }
112
111
  }