@typescript-calendar-lib/svelte 0.5.4 → 0.6.1

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/README.md CHANGED
@@ -71,7 +71,7 @@ Run the component through a Svelte compiler such as Vite or SvelteKit; the packa
71
71
  | `year` | `number` | — (required) | Calendar year |
72
72
  | `month` | `number` | — (required) | Month, 1-indexed (`1`–`12`) |
73
73
  | `locale` | `"en" \| "ja" \| "es" \| "de" \| "fr" \| "ko" \| "zh"` | `"en"` | Language |
74
- | `holidayLocale` | `"en" \| "ja" \| "es" \| "de" \| "fr" \| "ko" \| "zh"` | `"ja"` | Holiday rules; currently only `"ja"` is provided |
74
+ | `holidayLocale` | `"en" \| "ja" \| "es" \| "de" \| "fr" \| "ko" \| "zh"` | `"ja"` | Holiday rules (fixed-date holidays for every locale; moving holidays such as Easter are not included) |
75
75
  | `weekStart` | `"sunday" \| "monday"` | `"sunday"` | First day of the week |
76
76
  | `highlight` | `Date` | — | Date to highlight |
77
77
  | `range` | `{ from: Date; to: Date }` | — | Dates to emphasize |
@@ -1,5 +1,6 @@
1
1
  <script lang="ts">
2
2
  import type { CalendarCellState, CalendarOptions } from "@typescript-calendar-lib/core";
3
+ import { createDate, lastDayOfMonth } from "@typescript-calendar-lib/core";
3
4
  import type { SelectionMode } from "@typescript-calendar-lib/tui";
4
5
  import { keyToAction } from "@typescript-calendar-lib/tui";
5
6
  import type { Snippet } from "svelte";
@@ -22,6 +23,10 @@
22
23
  initialYear?: number;
23
24
  /** 表示開始月 (1-12)。範囲外は正規化される */
24
25
  initialMonth?: number;
26
+ /** 制御モードの表示年。指定すると initialYear より優先され、月移動は onMonthChange に委譲される */
27
+ year?: number;
28
+ /** 制御モードの表示月 (1-12) */
29
+ month?: number;
25
30
  locale?: CalendarOptions["locale"];
26
31
  holidayLocale?: CalendarOptions["holidayLocale"];
27
32
  weekStart?: CalendarOptions["weekStart"];
@@ -80,6 +85,8 @@
80
85
  selectionMode,
81
86
  today,
82
87
  isDateDisabled,
88
+ year,
89
+ month,
83
90
  onMonthChange,
84
91
  theme,
85
92
  colorScheme,
@@ -96,6 +103,8 @@
96
103
  const cal = useCalendarState(() => ({
97
104
  initialYear,
98
105
  initialMonth,
106
+ year,
107
+ month,
99
108
  locale,
100
109
  holidayLocale,
101
110
  weekStart,
@@ -142,7 +151,7 @@
142
151
  });
143
152
 
144
153
  const handleKeyDown = (event: KeyboardEvent) => {
145
- const action = keyToAction(event.key);
154
+ const action = keyToAction(event.key, event);
146
155
  if (action === undefined) return;
147
156
  event.preventDefault();
148
157
  switch (action) {
@@ -152,6 +161,21 @@
152
161
  case "next":
153
162
  cal.goNext();
154
163
  break;
164
+ case "prevYear":
165
+ cal.navigateYear("prev");
166
+ break;
167
+ case "nextYear":
168
+ cal.navigateYear("next");
169
+ break;
170
+ case "firstDay":
171
+ cal.setCursorToDate(createDate(cal.state.year, cal.state.month - 1, 1));
172
+ break;
173
+ case "lastDay":
174
+ cal.setCursorToDate(lastDayOfMonth(cal.state.year, cal.state.month));
175
+ break;
176
+ case "clear":
177
+ cal.clearSelection();
178
+ break;
155
179
  default:
156
180
  cal.moveCursor(action);
157
181
  }
@@ -9,6 +9,10 @@ interface InteractiveCalendarProps {
9
9
  initialYear?: number;
10
10
  /** 表示開始月 (1-12)。範囲外は正規化される */
11
11
  initialMonth?: number;
12
+ /** 制御モードの表示年。指定すると initialYear より優先され、月移動は onMonthChange に委譲される */
13
+ year?: number;
14
+ /** 制御モードの表示月 (1-12) */
15
+ month?: number;
12
16
  locale?: CalendarOptions["locale"];
13
17
  holidayLocale?: CalendarOptions["holidayLocale"];
14
18
  weekStart?: CalendarOptions["weekStart"];
@@ -3,6 +3,13 @@ import type { CalendarState, CalendarStateOptions, Direction, MonthDirection } f
3
3
  export interface UseCalendarStateOptions extends Pick<CalendarStateOptions, "locale" | "holidayLocale" | "weekStart" | "today" | "highlight" | "range" | "isDateDisabled" | "selectionMode"> {
4
4
  initialYear?: number;
5
5
  initialMonth?: number;
6
+ /**
7
+ * 制御モードの表示年。指定すると `initialYear` より優先され、値が変わるたびに
8
+ * その年月へ再構築される。月移動は内部状態を変えず `onMonthChange` に通知する。
9
+ */
10
+ year?: number;
11
+ /** 制御モードの表示月 (1–12)。`year` と併用する */
12
+ month?: number;
6
13
  /** 表示中の年月が変わったときに呼ばれる */
7
14
  onMonthChange?: (year: number, month: number) => void;
8
15
  }
@@ -50,16 +50,36 @@ export function useCalendarState(options = {}) {
50
50
  prev = next;
51
51
  state = updateStateOptions(state, next, previous);
52
52
  });
53
- // 表示月が変わったら onMonthChange を通知し、古い月を指すホバーをクリアする
53
+ // 表示月が変わったら onMonthChange を通知し、古い月を指すホバーをクリアする。
54
+ // 制御モードでは props 側が月を決めているので再通知しない(二重通知の抑止)。
54
55
  let prevMonthKey = null;
55
56
  $effect(() => {
56
57
  const key = `${state.year}-${state.month}`;
57
58
  if (prevMonthKey !== null && key !== prevMonthKey) {
58
59
  hoveredDate = null;
59
- getter().onMonthChange?.(state.year, state.month);
60
+ const current = getter();
61
+ if (current.year === undefined && current.month === undefined) {
62
+ current.onMonthChange?.(state.year, state.month);
63
+ }
60
64
  }
61
65
  prevMonthKey = key;
62
66
  });
67
+ /** 制御モードか(year/month のどちらかが指定されている) */
68
+ const isControlled = () => {
69
+ const current = getter();
70
+ return current.year !== undefined || current.month !== undefined;
71
+ };
72
+ /** 制御モードなら onMonthChange に委譲し、そうでなければ状態を更新する */
73
+ const applyNavigation = (next) => {
74
+ if (isControlled()) {
75
+ const current = getter();
76
+ if (next.year !== state.year || next.month !== state.month) {
77
+ current.onMonthChange?.(next.year, next.month);
78
+ }
79
+ return;
80
+ }
81
+ state = next;
82
+ };
63
83
  return {
64
84
  get state() {
65
85
  return state;
@@ -80,22 +100,22 @@ export function useCalendarState(options = {}) {
80
100
  state = moveCursor(state, direction);
81
101
  },
82
102
  goNext: () => {
83
- state = navigateMonth(state, "next");
103
+ applyNavigation(navigateMonth(state, "next"));
84
104
  },
85
105
  goPrev: () => {
86
- state = navigateMonth(state, "prev");
106
+ applyNavigation(navigateMonth(state, "prev"));
87
107
  },
88
108
  navigateYear: (direction) => {
89
- state = navigateYear(state, direction);
109
+ applyNavigation(navigateYear(state, direction));
90
110
  },
91
111
  goToMonth: (year, month) => {
92
- state = goToMonth(state, year, month);
112
+ applyNavigation(goToMonth(state, year, month));
93
113
  },
94
114
  goToDate: (date) => {
95
- state = goToDate(state, date);
115
+ applyNavigation(goToDate(state, date));
96
116
  },
97
117
  goToday: () => {
98
- state = goToToday(state);
118
+ applyNavigation(goToToday(state));
99
119
  },
100
120
  selectDate: () => {
101
121
  state = selectDate(state);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@typescript-calendar-lib/svelte",
3
- "version": "0.5.4",
3
+ "version": "0.6.1",
4
4
  "description": "Svelte 5 components and reactive state for typescript-calendar-lib",
5
5
  "type": "module",
6
6
  "repository": {
@@ -34,13 +34,13 @@
34
34
  "svelte": "^5"
35
35
  },
36
36
  "dependencies": {
37
- "@typescript-calendar-lib/core": "^0.5.4",
38
- "@typescript-calendar-lib/tui": "^0.5.4",
39
- "@typescript-calendar-lib/web": "^0.5.4"
37
+ "@typescript-calendar-lib/core": "^0.6.1",
38
+ "@typescript-calendar-lib/tui": "^0.6.1",
39
+ "@typescript-calendar-lib/web": "^0.6.1"
40
40
  },
41
41
  "devDependencies": {
42
42
  "@sveltejs/package": "^2.5.8",
43
- "@sveltejs/vite-plugin-svelte": "^7.3.0",
43
+ "@sveltejs/vite-plugin-svelte": "^7.3.1",
44
44
  "@testing-library/jest-dom": "^7.0.1",
45
45
  "@testing-library/svelte": "^5",
46
46
  "jsdom": "^30",
@@ -48,6 +48,17 @@
48
48
  "typescript": "^5.9.0",
49
49
  "vitest": "^5"
50
50
  },
51
+ "author": "nazozokc",
52
+ "homepage": "https://github.com/nazozokc/typescript-calendar-lib#readme",
53
+ "bugs": {
54
+ "url": "https://github.com/nazozokc/typescript-calendar-lib/issues"
55
+ },
56
+ "engines": {
57
+ "node": ">=22"
58
+ },
59
+ "sideEffects": [
60
+ "**/*.css"
61
+ ],
51
62
  "scripts": {
52
63
  "build": "svelte-package -i src && cp ../web/dist/calendar.css dist/calendar.css",
53
64
  "typecheck": "svelte-check --tsconfig ./tsconfig.json --fail-on-warnings"