@typescript-calendar-lib/svelte 0.5.2 → 0.5.4

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
@@ -8,6 +8,8 @@ A `<Calendar />` Svelte 5 component with CSS-based themes and color schemes, plu
8
8
  > npm install svelte @typescript-calendar-lib/svelte
9
9
  > ```
10
10
 
11
+ > **Note: Build tooling** — This package ships Svelte component sources (`.svelte`), so your app must have a Svelte compiler wired up, e.g. Vite with [`@sveltejs/vite-plugin-svelte`](https://github.com/sveltejs/vite-plugin-svelte) or SvelteKit. It is not designed to be imported directly in Node.js (SSR without a compiler fails with `ERR_UNKNOWN_FILE_EXTENSION`).
12
+
11
13
  ## Installation
12
14
 
13
15
  ```sh
@@ -29,6 +31,33 @@ bun add @typescript-calendar-lib/svelte
29
31
  <Calendar year={2026} month={9} colorScheme="ocean" theme="modern" />
30
32
  ```
31
33
 
34
+ ## Server-side rendering
35
+
36
+ SvelteKit enables SSR for `+page.svelte` by default. Render the component normally and SvelteKit will produce the initial HTML before hydrating it in the browser:
37
+
38
+ ```svelte
39
+ <!-- src/routes/+page.svelte -->
40
+ <script lang="ts">
41
+ import Calendar from "@typescript-calendar-lib/svelte";
42
+ import "@typescript-calendar-lib/svelte/calendar.css";
43
+ </script>
44
+
45
+ <Calendar year={2026} month={9} today={new Date(2026, 8, 15)} />
46
+ ```
47
+
48
+ For a standalone Svelte 5 server render, use `render` from `svelte/server`:
49
+
50
+ ```ts
51
+ import { render } from "svelte/server";
52
+ import Calendar from "@typescript-calendar-lib/svelte";
53
+
54
+ const { body, head } = render(Calendar, {
55
+ props: { year: 2026, month: 9, today: new Date(2026, 8, 15) },
56
+ });
57
+ ```
58
+
59
+ Run the component through a Svelte compiler such as Vite or SvelteKit; the package ships `.svelte` sources and is not a bare-Node component entry.
60
+
32
61
  ## Component API
33
62
 
34
63
  ### `Calendar`
@@ -42,23 +71,27 @@ bun add @typescript-calendar-lib/svelte
42
71
  | `year` | `number` | — (required) | Calendar year |
43
72
  | `month` | `number` | — (required) | Month, 1-indexed (`1`–`12`) |
44
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 |
45
75
  | `weekStart` | `"sunday" \| "monday"` | `"sunday"` | First day of the week |
46
76
  | `highlight` | `Date` | — | Date to highlight |
47
77
  | `range` | `{ from: Date; to: Date }` | — | Dates to emphasize |
48
78
  | `rangePreview` | `{ from: Date; to: Date }` | — | Candidate range preview (hover preview), rendered with the `is-in-range-preview` class |
79
+ | `isDateDisabled` | `(date: Date) => boolean` | — | Mark dates as non-selectable in interactive mode (`is-disabled` class) |
49
80
  | `today` | `Date` | — | Reference date for "today" styling |
50
- | `selected` | `Date \| null` | — | Selected date (`.is-selected` class, `aria-pressed` in interactive mode) |
81
+ | `selected` | `Date \| null` | — | Selected date (`.is-selected` class, `aria-pressed="true"` in interactive mode; other buttons are `"false"`) |
51
82
  | `cursorDate` | `Date \| null` | — | Cursor position (`.is-cursor` class) |
52
83
  | `hoveredDate` | `Date \| null` | — | Hovered date (`.is-hovered` class) |
53
84
  | `theme` | `ThemeName \| SvelteTheme` | `"default"` | CSS class-based theme |
54
85
  | `colorScheme` | `ColorSchemeName \| SvelteColorScheme` | `"default"` | CSS variable-based colors |
55
86
  | `size` | `CalendarSize` | `"md"` | Cell size |
56
87
  | `responsive` | `boolean` | `false` | Shrink cells & padding below `480px` so the grid fits phone screens |
88
+ | `showWeekNumbers` | `boolean` | `false` | Render a leading week-number column (`th[scope="row"]` with class `calendar-week`); the number follows `weekStart` (`"monday"` → ISO week, `"sunday"` → week-containing-Jan-1 week) |
57
89
  | `style` | `CSSProperties` | — | Extra styles for the root element |
58
90
  | `interactive` | `boolean` | `false` | Enable cell click/hover/keyboard selection |
59
91
  | `onDateClick` | `(date: Date, data?: unknown) => void` | — | Called when a day cell is clicked (or Enter/Space pressed); `data` is that cell's `cellData` value (`undefined` when none) |
60
92
  | `onDateHover` | `(date: Date) => void` | — | Called when a day cell is hovered |
61
93
  | `onDateLeave` | `() => void` | — | Called when the mouse leaves the calendar |
94
+ | `onkeydown` | `(event: KeyboardEvent) => void` | — | Keyboard handler fired on the root element (arrow keys etc.) |
62
95
  | `cellData` | `(date: Date) => unknown` | — | Resolve per-cell data; passed to `renderCell` and `onDateClick` |
63
96
  | `renderCell` | `(day, date, state, data?) => string` | — | Custom cell content; the returned string is inserted as plain text (XSS-safe) |
64
97
 
@@ -93,6 +126,18 @@ A turnkey component that wires `useCalendarState` and `Calendar` together with b
93
126
  - **Arrow keys** — moves the cursor with focus tracking
94
127
  - **PageUp/PageDown** — navigates to the previous/next month
95
128
 
129
+ ### Disabled Dates
130
+
131
+ Pass `isDateDisabled` to mark dates as non-selectable. Disabled cells get the `is-disabled` class, cannot receive keyboard focus or be clicked, and `Arrow*` navigation skips over them:
132
+
133
+ ```svelte
134
+ <InteractiveCalendar
135
+ initialYear={2026}
136
+ initialMonth={9}
137
+ isDateDisabled={(date) => date.getDay() === 0} // disable Sundays
138
+ />
139
+ ```
140
+
96
141
  ### Props
97
142
 
98
143
  Combines `useCalendarState` options, `Calendar` visual props, and event callbacks:
@@ -102,24 +147,39 @@ Combines `useCalendarState` options, `Calendar` visual props, and event callback
102
147
  | `initialYear` | `number` | Starting year (defaults to today's year) |
103
148
  | `initialMonth` | `number` | Starting month `1`–`12` (normalized if out of range) |
104
149
  | `locale` | `Locale` | Language |
150
+ | `holidayLocale` | `Locale` | Locale used for holiday detection (defaults to `ja`) |
105
151
  | `weekStart` | `WeekStart` | First day of the week |
152
+ | `showWeekNumbers` | `boolean` | Render the leading week-number column |
153
+ | `selectionMode` | `SelectionMode` | Selection mode: `"single"` \| `"range"` (default `"single"`; range commits on the 2nd pick) |
106
154
  | `highlight` | `Date` | Date to highlight |
107
155
  | `range` | `{ from: Date; to: Date }` | Dates to emphasize |
108
156
  | `today` | `Date` | Reference date for "today" styling |
157
+ | `isDateDisabled` | `(date: Date) => boolean` | Mark dates as non-selectable |
158
+ | `onkeydown` | `(event: KeyboardEvent) => void` | Keyboard handler fired on the root element (arrow keys etc.) |
159
+ | `onMonthChange` | `(year: number, month: number) => void` | Called when the displayed month changes |
109
160
  | `theme` | `ThemeName \| SvelteTheme` | CSS class-based theme |
110
161
  | `colorScheme` | `ColorSchemeName \| SvelteColorScheme` | CSS variable-based colors |
111
162
  | `size` | `CalendarSize` | Cell size |
112
163
  | `responsive` | `boolean` | Shrink cells & padding below `480px` so the grid fits phone screens |
113
164
  | `style` | `CSSProperties` | Extra styles for the root element |
114
165
  | `cellData` | `(date: Date) => unknown` | Resolve per-cell data; passed to `renderCell` and `onDateClick` |
115
- | `renderCell` | `(day, date, state, data?) => string` | Custom cell content (plain text, XSS-safe) |
166
+ | `renderCell` | `(day, date, state, data?) => string \| Snippet` | Custom cell content (plain text is XSS-safe; a `Snippet` renders markup) |
116
167
  | `onDateClick` | `(date: Date, data?: unknown) => void` | Called when a date is selected; `data` is the cell's `cellData` value |
117
168
  | `onDateHover` | `(date: Date) => void` | Called on cell hover |
118
169
  | `onDateLeave` | `() => void` | Called when the mouse leaves the calendar |
119
170
 
171
+ ## Accessibility notes
172
+
173
+ - The `<table>` is labelled by the visible `<h2>` heading through `aria-labelledby`, so screen readers announce the same month text you see - exactly once, instead of a duplicated `aria-label`.
174
+ - In interactive mode the heading itself is the live region (`aria-live="polite"` + `aria-atomic="true"`), so navigating to another month is announced (WAI-ARIA APG Date Picker Dialog). The month name is **not** copied into a separate hidden element, which would put it into the accessibility tree twice.
175
+ - In interactive mode the table becomes `role="grid"` with `role="gridcell"` cells; the non-interactive table keeps native table semantics.
176
+ - Padding cells carry the `is-blank` class instead of relying on `td:empty`, so grid lines do not break when a renderer emits stray whitespace or comments inside a cell.
177
+ - Heading `id` values are allocated per component instance, so several calendars of the same month can coexist on one page without duplicate ids.
178
+ - All colour schemes keep text at 4.5:1 or better against its background; grid borders target 2:1 (decorative), and `prefers-reduced-motion` / `forced-colors` are honoured.
179
+
120
180
  ## Interactive Mode
121
181
 
122
- Set `interactive` to make day cells clickable. Each cell becomes a `<button class="calendar-day-btn">` — clickable, hoverable, and keyboard-accessible (Enter / Space). Buttons are announced with a localized `aria-label`; today gets `aria-current="date"` and a `selected` date gets `aria-pressed="true"`:
182
+ Set `interactive` to make day cells clickable. Each cell becomes a `<button class="calendar-day-btn">` — clickable, hoverable, and keyboard-accessible (Enter / Space). Buttons are announced with a localized `aria-label` and an explicit `aria-pressed="true"` or `"false"`; today's cell gets `aria-current="date"`. The table is labelled by the visible `<h2>` heading via `aria-labelledby`, weekday headers use `scope="col"`, and week-number cells use `th[scope="row"]`. In interactive mode it is exposed as a `role="grid"` table with `role="gridcell"` cells (WAI-ARIA APG calendar pattern), today's cell gets `aria-current="date"`, the selected date gets `aria-selected`, and only the cursor cell is tabbable (roving tabindex):
123
183
 
124
184
  ```svelte
125
185
  <Calendar
@@ -193,16 +253,52 @@ For full interactivity (cursor movement, month navigation, selection), use the `
193
253
  | `goNext` | `() => void` | Move to next month |
194
254
  | `goPrev` | `() => void` | Move to previous month |
195
255
  | `goToday` | `() => void` | Jump to today's month |
256
+ | `navigateYear` | `(direction: MonthDirection) => void` | Move to the previous/next year (`"prev"` / `"next"`) |
257
+ | `goToMonth` | `(year: number, month: number) => void` | Jump to a specific year and month |
258
+ | `goToDate` | `(date: Date) => void` | Jump to a date's month and put the cursor on that date |
196
259
  | `moveCursor` | `(direction: Direction) => void` | Move cursor: `"up"` / `"down"` / `"left"` / `"right"` |
197
260
  | `selectDate` | `() => void` | Select the date under the cursor |
198
261
  | `selectDateAt` | `(date: Date) => void` | Move cursor to a date and select it (useful for mouse click) |
262
+ | `selectRange` | `(from: Date, to: Date) => void` | Set a committed range directly (range mode) |
199
263
  | `setCursorToDate` | `(date: Date) => void` | Move the cursor to a specific date (no-op if outside current month) |
200
264
  | `clearSelection` | `() => void` | Clear the selection |
201
265
  | `cursorDate` | `Date \| null` | Date under the cursor |
202
266
  | `selectedDate` | `Date \| null` | Currently selected date |
267
+ | `selectedRange` | `DateRange \| null` | Committed selection range (range mode) |
203
268
  | `hoveredDate` | `Date \| null` | Currently hovered date |
204
269
  | `setHoveredDate` | `(date: Date \| null) => void` | Set or clear the hovered date |
205
270
 
271
+ ## Range selection
272
+
273
+ Pass `selectionMode="range"` to select a date range instead of a single date. Clicking alternates between **anchor → range → reset**: the 1st click sets an anchor, the 2nd commits the range, and the 3rd starts a new anchor. The committed range stays visible as the preview until the next pick begins.
274
+
275
+ ```svelte
276
+ <InteractiveCalendar
277
+ initialYear={2026}
278
+ initialMonth={9}
279
+ selectionMode="range"
280
+ />
281
+ ```
282
+
283
+ With the raw hook, set the range explicitly via `selectRange(from, to)` and read it back from the `selectedRange` return value:
284
+
285
+ ```svelte
286
+ <script lang="ts">
287
+ import { useCalendarState } from "@typescript-calendar-lib/svelte";
288
+
289
+ const cal = useCalendarState({
290
+ initialYear: 2026,
291
+ initialMonth: 9,
292
+ selectionMode: "range",
293
+ });
294
+
295
+ function pick() {
296
+ cal.selectRange(new Date(2026, 8, 1), new Date(2026, 8, 15));
297
+ console.log(cal.selectedRange); // { from, to } | null
298
+ }
299
+ </script>
300
+ ```
301
+
206
302
  ## Themes
207
303
 
208
304
  ### `ThemeName`
@@ -263,6 +359,7 @@ Available variables:
263
359
  | `--cal-fg` | Foreground |
264
360
  | `--cal-accent` | Accent (highlight) |
265
361
  | `--cal-weekend-fg` | Weekend foreground |
362
+ | `--cal-holiday-fg` | Holiday foreground |
266
363
  | `--cal-border` | Borders |
267
364
  | `--cal-header-bg` | Header background |
268
365
  | `--cal-highlight-bg` | Highlight background |
@@ -271,8 +368,11 @@ Available variables:
271
368
  | `--cal-range-preview-bg` | Range preview background (falls back to `--cal-range-bg`) |
272
369
  | `--cal-today-bg` | Today background |
273
370
  | `--cal-today-fg` | Today foreground |
274
- | `--cal-selected-bg` | Selected background |
275
- | `--cal-selected-fg` | Selected foreground |
371
+ | `--cal-selected-bg` | Selected cell background (interactive mode) |
372
+ | `--cal-selected-fg` | Selected cell foreground (interactive mode) |
373
+ | `--cal-disabled-fg` | Disabled cell foreground (interactive mode) |
374
+
375
+ `calendar.css` also uses a CSS-only `--cal-hover-bg` (falls back to `--cal-range-bg`) for the interactive hover state; set it with `style` or your own CSS.
276
376
 
277
377
  Pass a custom scheme directly:
278
378
 
@@ -334,16 +434,18 @@ It's **off by default** to keep existing layouts unchanged. The calendar root ge
334
434
 
335
435
  ## Cell Classes
336
436
 
337
- Each day cell gets semantic classes you can target with CSS:
437
+ Each day cell gets semantic classes you can target with CSS. The table is labelled by the visible `<h2>` heading via `aria-labelledby`, weekday headers use `scope="col"`, week-number cells use `th[scope="row"]`, today's cell uses `aria-current="date"`, and interactive day buttons expose `aria-pressed="true"` or `"false"`:
338
438
 
339
439
  | Class | When |
340
440
  | :--- | :--- |
341
441
  | `is-weekend` | Saturday or Sunday |
442
+ | `is-holiday` | Holiday per `holidayLocale` (default `"ja"`) |
342
443
  | `is-today` | Matches `today` |
343
444
  | `is-highlight` | Matches `highlight` |
344
445
  | `is-in-range` | Inside `range` |
345
446
  | `is-in-range-preview` | Inside `rangePreview` |
346
447
  | `is-hovered` | Matches `hoveredDate` (interactive mode) |
448
+ | `is-disabled` | Date returned `true` from `isDateDisabled` (interactive mode) |
347
449
  | `is-selected` | Matches `selected` |
348
450
  | `is-cursor` | Matches `cursorDate` |
349
451
 
@@ -355,9 +457,13 @@ import Calendar, {
355
457
  buildRangePreview,
356
458
  buildSizeStyle,
357
459
  COLOR_SCHEMES,
460
+ getBlankClasses,
358
461
  getCellClasses,
462
+ getCellStateClasses,
463
+ getSelectionCellClasses,
359
464
  InteractiveCalendar,
360
465
  isSizeName,
466
+ joinCellClassLists,
361
467
  resolveColorScheme,
362
468
  resolveTheme,
363
469
  THEMES,
@@ -371,6 +477,7 @@ import type {
371
477
  CellStateOptions,
372
478
  ColorSchemeName,
373
479
  CSSProperties,
480
+ SelectionCellOptions,
374
481
  SvelteColorScheme,
375
482
  SvelteTheme,
376
483
  ThemeName,
@@ -1,15 +1,24 @@
1
1
  <script lang="ts">
2
2
  import type { CalendarCellState, CalendarOptions } from "@typescript-calendar-lib/core";
3
3
  import {
4
+ addDays,
4
5
  buildMonthGrid,
5
6
  createDate,
6
7
  getCalendarCellState,
8
+ getISOWeek,
7
9
  getMonthName,
10
+ getWeekOfYear,
8
11
  getWeekdayHeaders,
9
12
  isSameDay,
10
13
  } from "@typescript-calendar-lib/core";
11
- import { formatCellLabel } from "@typescript-calendar-lib/web";
12
- import { getCellClasses } from "./cell-classes.js";
14
+ import {
15
+ formatCellLabel,
16
+ getBlankClasses,
17
+ getCellStateClasses,
18
+ getSelectionCellClasses,
19
+ joinCellClassLists,
20
+ uiString,
21
+ } from "@typescript-calendar-lib/web";
13
22
  import type { CalendarSize } from "./size.js";
14
23
  import { buildSizeStyle, isSizeName } from "./size.js";
15
24
  import type { CSSProperties } from "./style.js";
@@ -20,12 +29,14 @@
20
29
  SvelteTheme,
21
30
  ThemeName,
22
31
  } from "./themes.js";
23
- import { resolveColorScheme, resolveTheme } from "./themes.js";
32
+ import type { Snippet } from "svelte";
33
+ import { mergeColorScheme, resolveTheme } from "./themes.js";
24
34
 
25
35
  interface CalendarProps {
26
36
  year: number;
27
37
  month: number;
28
38
  locale?: CalendarOptions["locale"];
39
+ holidayLocale?: CalendarOptions["holidayLocale"];
29
40
  weekStart?: CalendarOptions["weekStart"];
30
41
  highlight?: Date;
31
42
  /** 範囲強調 */
@@ -34,7 +45,7 @@
34
45
  rangePreview?: { from: Date; to: Date };
35
46
  /** 今日の基準日。カラースキームの today 着色に使用 */
36
47
  today?: Date;
37
- /** 選択済み日付。該当セルに is-selected クラスと aria-selected が付く */
48
+ /** 選択済み日付。該当セルに is-selected クラスが付き、interactive 時は button に aria-pressed が付く */
38
49
  selected?: Date | null;
39
50
  /** カーソル位置の日付。該当セルに is-cursor クラスが付く */
40
51
  cursorDate?: Date | null;
@@ -48,6 +59,8 @@
48
59
  size?: CalendarSize;
49
60
  /** 狭い画面(スマホ等)でセルサイズと余白を自動調整する。既定は false(無効) */
50
61
  responsive?: boolean;
62
+ /** 行の先頭に週番号を表示する。既定は false(非表示) */
63
+ showWeekNumbers?: boolean;
51
64
  /** root 要素に追加するスタイル。CSS変数(--cal-*)で自由に上書きできる */
52
65
  style?: CSSProperties;
53
66
 
@@ -66,13 +79,16 @@
66
79
 
67
80
  // ── セル状態 ──
68
81
 
69
- /** セル内容のカスタムレンダリング。interactive 時は button の子として描画される。第4引数に該当日のデータが渡る */
82
+ /**
83
+ * セル内容のカスタムレンダリング。interactive 時は button の子として描画される。
84
+ * Snippet(markup)または文字列のどちらでも受け付ける。第4引数に該当日のデータが渡る。
85
+ */
70
86
  renderCell?: (
71
87
  day: number,
72
88
  date: Date,
73
89
  state: CalendarCellState,
74
90
  data?: unknown,
75
- ) => string;
91
+ ) => string | Snippet;
76
92
  /** 各セルに付与するユーザー定義データを解決する関数。実セルのみに呼ばれる */
77
93
  cellData?: (date: Date) => unknown;
78
94
  /** 選択不可日付の判定。true を返した日付は is-disabled クラスになり、インタラクティブ時は選択・ホバーできなくなる */
@@ -83,6 +99,7 @@
83
99
  year,
84
100
  month,
85
101
  locale = "en",
102
+ holidayLocale,
86
103
  weekStart = "sunday",
87
104
  highlight,
88
105
  range,
@@ -95,6 +112,7 @@
95
112
  colorScheme = "default",
96
113
  size = "md",
97
114
  responsive = false,
115
+ showWeekNumbers = false,
98
116
  style,
99
117
  interactive = false,
100
118
  onDateClick,
@@ -106,98 +124,198 @@
106
124
  isDateDisabled,
107
125
  }: CalendarProps = $props();
108
126
 
127
+ // interactive 時のみ APG の grid セマンティクスを付与する。静的表示では
128
+ // ネイティブの table/cell セマンティクスをそのまま使う。
129
+ const gridCellRole = $derived(interactive ? "gridcell" : undefined);
130
+ // h2 に一意な id を振り、table は aria-labelledby で参照する。
131
+ // $props.id() を使うのは同じ月のカレンダーが複数同時に置かれた場合に
132
+ // id が衝突し aria-labelledby が別のカレンダーを見指すため。
133
+ const headingId = $props.id();
134
+
109
135
  const cellDate = (day: number): Date => createDate(year, month - 1, day);
110
- const cellState = (day: number): CalendarCellState =>
111
- getCalendarCellState(cellDate(day), { today, highlight, range, isDateDisabled });
112
- const cellClass = (day: number): string | undefined =>
113
- getCellClasses(cellDate(day), {
136
+
137
+ /**
138
+ * 1セルぶんの表示データ(Date・状態・クラス・aria 用の状態)を1回の計算でまとめる。
139
+ * テンプレートはこの結果だけを参照するので、セルあたりの状態計算は1回になる。
140
+ */
141
+ interface DayCell {
142
+ date: Date;
143
+ state: CalendarCellState;
144
+ className: string | undefined;
145
+ isSelected: boolean;
146
+ isCursor: boolean;
147
+ isDisabled: boolean;
148
+ }
149
+
150
+ const dayCell = (day: number): DayCell => {
151
+ const date = cellDate(day);
152
+ const state = getCalendarCellState(date, {
114
153
  today,
154
+ holidayLocale,
115
155
  highlight,
116
156
  range,
117
- rangePreview,
118
- selected,
119
- cursorDate,
120
- hoveredDate,
121
157
  isDateDisabled,
122
- }) || undefined;
123
- const isDisabledDay = (day: number): boolean => cellState(day).isDisabled;
124
- const isSelectedDay = (day: number): boolean =>
125
- selected != null && isSameDay(cellDate(day), selected);
126
- const isTodayDay = (day: number): boolean =>
127
- today != null && isSameDay(cellDate(day), today);
128
- const isCursorDay = (day: number): boolean =>
129
- cursorDate != null && isSameDay(cellDate(day), cursorDate);
130
- const handleCellClick = (day: number) => {
158
+ });
159
+ const className =
160
+ joinCellClassLists(
161
+ getCellStateClasses(state),
162
+ getSelectionCellClasses(date, {
163
+ rangePreview,
164
+ hoveredDate,
165
+ selected,
166
+ cursorDate,
167
+ }),
168
+ ) || undefined;
169
+ return {
170
+ date,
171
+ state,
172
+ className,
173
+ isSelected: selected != null && isSameDay(date, selected),
174
+ isCursor: cursorDate != null && isSameDay(date, cursorDate),
175
+ isDisabled: state.isDisabled,
176
+ };
177
+ };
178
+
179
+ /**
180
+ * renderCell の戻り値が Snippet なら {@render}、文字列ならテキストとして扱う。
181
+ * Svelte 5 の Snippet は関数なので typeof で判別できる。
182
+ */
183
+ const isSnippet = (value: string | Snippet): value is Snippet =>
184
+ typeof value === "function";
185
+ const weekOf = (row: (number | null)[]): number | null => {
186
+ const firstIdx = row.findIndex((d) => d !== null);
187
+ if (firstIdx === -1) return null;
188
+ const firstDate = createDate(year, month - 1, row[firstIdx]!);
189
+ const weekStartDate = addDays(firstDate, -firstIdx);
190
+ return weekStart === "monday"
191
+ ? getISOWeek(weekStartDate)
192
+ : getWeekOfYear(weekStartDate);
193
+ };
194
+ const handleCellClick = (cell: DayCell) => {
131
195
  if (interactive && onDateClick) {
132
- const date = cellDate(day);
133
- onDateClick(date, cellData?.(date));
196
+ onDateClick(cell.date, cellData?.(cell.date));
134
197
  }
135
198
  };
136
- const handleCellHover = (day: number) => {
137
- if (interactive && onDateHover) onDateHover(cellDate(day));
199
+ const handleCellHover = (cell: DayCell) => {
200
+ if (interactive && onDateHover) onDateHover(cell.date);
138
201
  };
139
202
  const handleMouseLeave = () => {
140
203
  if (interactive && onDateLeave) onDateLeave();
141
204
  };
142
205
 
206
+ // グリッド・曜日ヘッダーは月単位で不変。$derived なので年月が変わったときだけ再計算される
207
+ const grid = $derived(buildMonthGrid(year, month, weekStart));
208
+ const weekdays = $derived(getWeekdayHeaders(locale, weekStart));
209
+ // 週番号は表示時のみ計算する。グリッドと同じ月単位の依存
210
+ const weekNumbers = $derived(showWeekNumbers ? grid.map(weekOf) : null);
211
+ // 行ヘッダーの accessible name は行ごとに一意でなければ識別できないため
212
+ // ラベル+週番号を組み合わせる(aria-label が内容の数字を上書きしないように)。
213
+ // 関数にして locale・weekNumbers を追跡する
214
+ const weekAriaLabel = (i: number): string | undefined =>
215
+ weekNumbers !== null
216
+ ? `${uiString("weekNumber", locale)} ${weekNumbers[i]}`
217
+ : undefined;
218
+
143
219
  // style 属性は文字列のみ受け付けるためオブジェクトを直列化する(--cal-* 変数含む)
144
220
  const rootStyle = $derived(
145
221
  styleObjectToString({
146
- ...resolveColorScheme(colorScheme),
222
+ ...mergeColorScheme(colorScheme),
147
223
  ...buildSizeStyle(size),
148
224
  ...style,
149
225
  }),
150
226
  );
151
227
  </script>
152
228
 
229
+ <!-- svelte-ignore a11y_no_static_element_interactions -->
230
+ <!-- onmouseleave はホバー状態クリア用の補助イベント、onkeydown は
231
+ InteractiveCalendar が table 上で扱う。root 自体は widget ではないため
232
+ ARIA role は持たない。 -->
153
233
  <div
154
234
  class="calendar {resolveTheme(theme).className}{isSizeName(size) ? ` calendar-size-${size}` : ""}{interactive ? " calendar-interactive" : ""}{responsive ? " calendar-responsive" : ""}"
155
235
  style={rootStyle}
156
- role={interactive ? "group" : undefined}
157
236
  onmouseleave={interactive ? handleMouseLeave : undefined}
158
237
  onkeydown={onkeydown}
159
238
  >
160
239
  <div class="calendar-header">
161
- <h2>{getMonthName(locale, month)} {year}</h2>
240
+ <!--
241
+ 見出しをそのまま live region にする。別の要素に同じ文言を複製すると
242
+ アクセシビリティツリー上で月名が二重に存在するため。
243
+ interactive 時は月が変わったことをここで読み上げる(APG Date Picker Dialog)。
244
+ -->
245
+ <h2
246
+ id={headingId}
247
+ aria-live={interactive ? "polite" : undefined}
248
+ aria-atomic={interactive ? "true" : undefined}
249
+ >
250
+ {getMonthName(locale, month)} {year}
251
+ </h2>
162
252
  </div>
163
- <table>
253
+ <!-- interactive 時のみ APG の grid セマンティクスを付与する。静的表示では
254
+ ネイティブの table/cell セマンティクスをそのまま使う。 -->
255
+ <table role={interactive ? "grid" : undefined} aria-labelledby={headingId}>
164
256
  <thead>
165
257
  <tr>
166
- {#each getWeekdayHeaders(locale, weekStart) as day (day)}
167
- <th>{day}</th>
258
+ {#if showWeekNumbers}
259
+ <th class="calendar-week" scope="col" aria-label={uiString("weekNumber", locale)}></th>
260
+ {/if}
261
+ {#each weekdays as day (day)}
262
+ <th scope="col">{day}</th>
168
263
  {/each}
169
264
  </tr>
170
265
  </thead>
171
266
  <tbody>
172
- {#each buildMonthGrid(year, month, weekStart) as row, i (i)}
267
+ {#each grid as row, i (i)}
173
268
  {#if !row.every((d) => d === null)}
174
269
  <tr>
270
+ {#if showWeekNumbers}
271
+ <th
272
+ class="calendar-week"
273
+ scope="row"
274
+ aria-label={weekAriaLabel(i)}
275
+ >{#if weekNumbers !== null}{weekNumbers[i]}{/if}</th>
276
+ {/if}
175
277
  {#each row as day, j (j)}
176
278
  {#if day === null}
177
- <td></td>
279
+ <td role={gridCellRole} class={getBlankClasses()}></td>
178
280
  {:else}
179
- <td class={cellClass(day)} aria-disabled={isDisabledDay(day) || undefined}>
281
+ {@const cell = dayCell(day)}
282
+ <td
283
+ role={gridCellRole}
284
+ class={cell.className}
285
+ aria-selected={cell.isSelected || undefined}
286
+ aria-current={cell.state.isToday ? "date" : undefined}
287
+ aria-disabled={cell.isDisabled || undefined}
288
+ >
180
289
  {#if interactive}
181
290
  <button
182
291
  type="button"
183
292
  class="calendar-day-btn"
184
- onclick={isDisabledDay(day) ? undefined : () => handleCellClick(day)}
185
- onmouseenter={isDisabledDay(day) ? undefined : () => handleCellHover(day)}
186
- tabindex={isCursorDay(day) ? 0 : -1}
187
- data-cursor={isCursorDay(day) ? "true" : undefined}
293
+ onclick={cell.isDisabled ? undefined : () => handleCellClick(cell)}
294
+ onmouseenter={cell.isDisabled ? undefined : () => handleCellHover(cell)}
295
+ tabindex={cell.isCursor ? 0 : -1}
296
+ data-cursor={cell.isCursor ? "true" : undefined}
188
297
  aria-label={formatCellLabel(locale, year, month, day)}
189
- aria-pressed={isSelectedDay(day) || undefined}
190
- aria-current={isTodayDay(day) ? "date" : undefined}
191
- disabled={isDisabledDay(day)}
298
+ aria-pressed={cell.isSelected}
299
+ disabled={cell.isDisabled}
192
300
  >
193
301
  {#if renderCell}
194
- {renderCell(day, cellDate(day), cellState(day), cellData?.(cellDate(day)))}
302
+ {@const rendered = renderCell(day, cell.date, cell.state, cellData?.(cell.date))}
303
+ {#if isSnippet(rendered)}
304
+ {@render rendered()}
305
+ {:else}
306
+ {rendered}
307
+ {/if}
195
308
  {:else}
196
309
  {day}
197
310
  {/if}
198
311
  </button>
199
312
  {:else if renderCell}
200
- {renderCell(day, cellDate(day), cellState(day), cellData?.(cellDate(day)))}
313
+ {@const rendered = renderCell(day, cell.date, cell.state, cellData?.(cell.date))}
314
+ {#if isSnippet(rendered)}
315
+ {@render rendered()}
316
+ {:else}
317
+ {rendered}
318
+ {/if}
201
319
  {:else}
202
320
  {day}
203
321
  {/if}
@@ -2,10 +2,12 @@ import type { CalendarCellState, CalendarOptions } from "@typescript-calendar-li
2
2
  import type { CalendarSize } from "./size.js";
3
3
  import type { CSSProperties } from "./style.js";
4
4
  import type { ColorSchemeName, SvelteColorScheme, SvelteTheme, ThemeName } from "./themes.js";
5
+ import type { Snippet } from "svelte";
5
6
  interface CalendarProps {
6
7
  year: number;
7
8
  month: number;
8
9
  locale?: CalendarOptions["locale"];
10
+ holidayLocale?: CalendarOptions["holidayLocale"];
9
11
  weekStart?: CalendarOptions["weekStart"];
10
12
  highlight?: Date;
11
13
  /** 範囲強調 */
@@ -20,7 +22,7 @@ interface CalendarProps {
20
22
  };
21
23
  /** 今日の基準日。カラースキームの today 着色に使用 */
22
24
  today?: Date;
23
- /** 選択済み日付。該当セルに is-selected クラスと aria-selected が付く */
25
+ /** 選択済み日付。該当セルに is-selected クラスが付き、interactive 時は button に aria-pressed が付く */
24
26
  selected?: Date | null;
25
27
  /** カーソル位置の日付。該当セルに is-cursor クラスが付く */
26
28
  cursorDate?: Date | null;
@@ -34,6 +36,8 @@ interface CalendarProps {
34
36
  size?: CalendarSize;
35
37
  /** 狭い画面(スマホ等)でセルサイズと余白を自動調整する。既定は false(無効) */
36
38
  responsive?: boolean;
39
+ /** 行の先頭に週番号を表示する。既定は false(非表示) */
40
+ showWeekNumbers?: boolean;
37
41
  /** root 要素に追加するスタイル。CSS変数(--cal-*)で自由に上書きできる */
38
42
  style?: CSSProperties;
39
43
  /** インタラクティブモードを有効にする。セルクリック・ホバー・キーボード選択が可能になる */
@@ -46,8 +50,11 @@ interface CalendarProps {
46
50
  onDateLeave?: () => void;
47
51
  /** キーボード操作。root 要素で発火(矢印キー等を InteractiveCalendar が処理する) */
48
52
  onkeydown?: (event: KeyboardEvent) => void;
49
- /** セル内容のカスタムレンダリング。interactive 時は button の子として描画される。第4引数に該当日のデータが渡る */
50
- renderCell?: (day: number, date: Date, state: CalendarCellState, data?: unknown) => string;
53
+ /**
54
+ * セル内容のカスタムレンダリング。interactive 時は button の子として描画される。
55
+ * Snippet(markup)または文字列のどちらでも受け付ける。第4引数に該当日のデータが渡る。
56
+ */
57
+ renderCell?: (day: number, date: Date, state: CalendarCellState, data?: unknown) => string | Snippet;
51
58
  /** 各セルに付与するユーザー定義データを解決する関数。実セルのみに呼ばれる */
52
59
  cellData?: (date: Date) => unknown;
53
60
  /** 選択不可日付の判定。true を返した日付は is-disabled クラスになり、インタラクティブ時は選択・ホバーできなくなる */
@@ -1,6 +1,8 @@
1
1
  <script lang="ts">
2
2
  import type { CalendarCellState, CalendarOptions } from "@typescript-calendar-lib/core";
3
+ import type { SelectionMode } from "@typescript-calendar-lib/tui";
3
4
  import { keyToAction } from "@typescript-calendar-lib/tui";
5
+ import type { Snippet } from "svelte";
4
6
  import Calendar from "./Calendar.svelte";
5
7
  import { buildRangePreview } from "./range-preview.js";
6
8
  import type { CalendarSize } from "./size.js";
@@ -21,15 +23,22 @@
21
23
  /** 表示開始月 (1-12)。範囲外は正規化される */
22
24
  initialMonth?: number;
23
25
  locale?: CalendarOptions["locale"];
26
+ holidayLocale?: CalendarOptions["holidayLocale"];
24
27
  weekStart?: CalendarOptions["weekStart"];
28
+ /** 各週の先頭に週番号(ISO/年始基準)を表示する */
29
+ showWeekNumbers?: boolean;
25
30
  /** ハイライト対象日 */
26
31
  highlight?: Date;
27
32
  /** 色付け範囲 */
28
33
  range?: { from: Date; to: Date };
34
+ /** 選択方式。既定は "single"。range では 2 回のクリックで範囲が確定する */
35
+ selectionMode?: SelectionMode;
29
36
  /** 今日の基準日。省略時は初回レンダリング時刻に解決される */
30
37
  today?: Date;
31
38
  /** 選択不可日付の判定。true を返した日付は選択・カーソル移動・ホバーの対象外になる */
32
39
  isDateDisabled?: (date: Date) => boolean;
40
+ /** 表示中の年月が変わったときに呼ばれる */
41
+ onMonthChange?: (year: number, month: number) => void;
33
42
 
34
43
  // ── Calendar の見た目 ──
35
44
 
@@ -39,13 +48,13 @@
39
48
  /** 狭い画面(スマホ等)でセルサイズと余白を自動調整する */
40
49
  responsive?: boolean;
41
50
  style?: CSSProperties;
42
- /** セル内容のカスタムレンダリング。第4引数に該当日のデータが渡る */
51
+ /** セル内容のカスタムレンダリング。文字列または Snippet。第4引数に該当日のデータが渡る */
43
52
  renderCell?: (
44
53
  day: number,
45
54
  date: Date,
46
55
  state: CalendarCellState,
47
56
  data?: unknown,
48
- ) => string;
57
+ ) => string | Snippet;
49
58
  /** 各セルに付与するユーザー定義データを解決する関数。実セルのみに呼ばれる */
50
59
  cellData?: (date: Date) => unknown;
51
60
 
@@ -63,11 +72,15 @@
63
72
  initialYear,
64
73
  initialMonth,
65
74
  locale,
75
+ holidayLocale,
66
76
  weekStart,
77
+ showWeekNumbers,
67
78
  highlight,
68
79
  range,
80
+ selectionMode,
69
81
  today,
70
82
  isDateDisabled,
83
+ onMonthChange,
71
84
  theme,
72
85
  colorScheme,
73
86
  size,
@@ -84,11 +97,14 @@
84
97
  initialYear,
85
98
  initialMonth,
86
99
  locale,
100
+ holidayLocale,
87
101
  weekStart,
88
102
  today,
89
103
  highlight,
90
104
  range,
91
105
  isDateDisabled,
106
+ selectionMode,
107
+ onMonthChange,
92
108
  }));
93
109
 
94
110
  // クリック(マウス・Enter/Space 共通)で日付を選択する。
@@ -108,9 +124,9 @@
108
124
  onDateLeave?.();
109
125
  };
110
126
 
111
- // 選択済み日付とホバー日付の間を範囲プレビューとして表示する
127
+ // 確定した選択範囲があればそれを、なければ選択日〜ホバー日のプレビューを表示する
112
128
  const rangePreview = $derived(
113
- buildRangePreview(cal.selectedDate, cal.hoveredDate),
129
+ cal.selectedRange ?? buildRangePreview(cal.selectedDate, cal.hoveredDate),
114
130
  );
115
131
 
116
132
  // カーソルが動いたときだけ、そのセルへフォーカスを移す(初回マウントでは動かさない)
@@ -147,7 +163,9 @@
147
163
  year={cal.state.year}
148
164
  month={cal.state.month}
149
165
  locale={cal.state.options.locale}
166
+ holidayLocale={cal.state.options.holidayLocale}
150
167
  weekStart={cal.state.options.weekStart}
168
+ showWeekNumbers={showWeekNumbers}
151
169
  highlight={cal.state.options.highlight}
152
170
  range={cal.state.options.range}
153
171
  today={cal.state.options.today}
@@ -1,4 +1,6 @@
1
1
  import type { CalendarCellState, CalendarOptions } from "@typescript-calendar-lib/core";
2
+ import type { SelectionMode } from "@typescript-calendar-lib/tui";
3
+ import type { Snippet } from "svelte";
2
4
  import type { CalendarSize } from "./size.js";
3
5
  import type { CSSProperties } from "./style.js";
4
6
  import type { ColorSchemeName, SvelteColorScheme, SvelteTheme, ThemeName } from "./themes.js";
@@ -8,7 +10,10 @@ interface InteractiveCalendarProps {
8
10
  /** 表示開始月 (1-12)。範囲外は正規化される */
9
11
  initialMonth?: number;
10
12
  locale?: CalendarOptions["locale"];
13
+ holidayLocale?: CalendarOptions["holidayLocale"];
11
14
  weekStart?: CalendarOptions["weekStart"];
15
+ /** 各週の先頭に週番号(ISO/年始基準)を表示する */
16
+ showWeekNumbers?: boolean;
12
17
  /** ハイライト対象日 */
13
18
  highlight?: Date;
14
19
  /** 色付け範囲 */
@@ -16,18 +21,22 @@ interface InteractiveCalendarProps {
16
21
  from: Date;
17
22
  to: Date;
18
23
  };
24
+ /** 選択方式。既定は "single"。range では 2 回のクリックで範囲が確定する */
25
+ selectionMode?: SelectionMode;
19
26
  /** 今日の基準日。省略時は初回レンダリング時刻に解決される */
20
27
  today?: Date;
21
28
  /** 選択不可日付の判定。true を返した日付は選択・カーソル移動・ホバーの対象外になる */
22
29
  isDateDisabled?: (date: Date) => boolean;
30
+ /** 表示中の年月が変わったときに呼ばれる */
31
+ onMonthChange?: (year: number, month: number) => void;
23
32
  theme?: ThemeName | SvelteTheme;
24
33
  colorScheme?: ColorSchemeName | SvelteColorScheme;
25
34
  size?: CalendarSize;
26
35
  /** 狭い画面(スマホ等)でセルサイズと余白を自動調整する */
27
36
  responsive?: boolean;
28
37
  style?: CSSProperties;
29
- /** セル内容のカスタムレンダリング。第4引数に該当日のデータが渡る */
30
- renderCell?: (day: number, date: Date, state: CalendarCellState, data?: unknown) => string;
38
+ /** セル内容のカスタムレンダリング。文字列または Snippet。第4引数に該当日のデータが渡る */
39
+ renderCell?: (day: number, date: Date, state: CalendarCellState, data?: unknown) => string | Snippet;
31
40
  /** 各セルに付与するユーザー定義データを解決する関数。実セルのみに呼ばれる */
32
41
  cellData?: (date: Date) => unknown;
33
42
  /** セルクリック時(キーボードの Enter/Space 含む)。クリックした日付は自動的に選択される。第2引数に該当日のデータが渡る */
package/dist/calendar.css CHANGED
@@ -2,17 +2,20 @@
2
2
  --cal-bg: #fff;
3
3
  --cal-fg: #1e293b;
4
4
  --cal-accent: #dc2626;
5
- --cal-weekend-fg: #94a3b8;
6
- --cal-border: #e2e8f0;
5
+ --cal-weekend-fg: #586a84;
6
+ --cal-holiday-fg: #d33;
7
+ --cal-border: #aeb8c6;
7
8
  --cal-header-bg: #f8fafc;
8
9
  --cal-highlight-bg: #dc2626;
9
10
  --cal-highlight-fg: #fff;
10
11
  --cal-range-bg: #fef3c7;
12
+ --cal-range-preview-bg: #fef9c3;
13
+ --cal-hover-bg: #fef9c3;
11
14
  --cal-today-bg: #fff7ed;
12
15
  --cal-today-fg: #c2410c;
13
16
  --cal-selected-bg: #dc2626;
14
17
  --cal-selected-fg: #fff;
15
- --cal-disabled-fg: #cbd5e1;
18
+ --cal-disabled-fg: #5b799d;
16
19
  --cal-cell-w: 2.25rem;
17
20
  --cal-cell-h: 2rem;
18
21
  --cal-font-size: .875rem;
@@ -43,6 +46,14 @@
43
46
  font-weight: 600;
44
47
  }
45
48
 
49
+ .calendar th.calendar-week, .calendar td.calendar-week {
50
+ width: calc(var(--cal-cell-w) * .6);
51
+ min-width: 1.25rem;
52
+ color: var(--cal-weekend-fg);
53
+ font-size: .75rem;
54
+ font-weight: 400;
55
+ }
56
+
46
57
  .calendar .calendar-header {
47
58
  text-align: center;
48
59
  margin-bottom: .5rem;
@@ -60,9 +71,12 @@
60
71
  color: var(--cal-weekend-fg);
61
72
  }
62
73
 
74
+ .calendar td.is-holiday {
75
+ color: var(--cal-holiday-fg, #d33);
76
+ }
77
+
63
78
  .calendar td.is-disabled {
64
79
  color: var(--cal-disabled-fg, var(--cal-weekend-fg));
65
- opacity: .5;
66
80
  text-decoration: line-through;
67
81
  }
68
82
 
@@ -155,7 +169,7 @@
155
169
  border: 1px solid var(--cal-border);
156
170
  }
157
171
 
158
- .calendar-theme-modern td:empty {
172
+ .calendar-theme-modern td.is-blank {
159
173
  background: none;
160
174
  border-color: #0000;
161
175
  }
@@ -210,11 +224,11 @@
210
224
  border-radius: 999px;
211
225
  }
212
226
 
213
- .calendar-theme-rounded td:empty {
227
+ .calendar-theme-rounded td.is-blank {
214
228
  background: none;
215
229
  }
216
230
 
217
- .calendar-interactive {
231
+ .calendar-interactive:focus:not(:focus-visible) {
218
232
  outline: none;
219
233
  }
220
234
 
@@ -330,8 +344,63 @@
330
344
  @media (width <= 360px) {
331
345
  .calendar-responsive {
332
346
  --cal-cell-w: 1.5rem;
333
- --cal-cell-h: 1.375rem;
347
+ --cal-cell-h: 1.5rem;
334
348
  --cal-font-size: .6875rem;
335
349
  --cal-header-font-size: .875rem;
336
350
  }
337
351
  }
352
+
353
+ .calendar-sr-only {
354
+ clip-path: inset(50%);
355
+ white-space: nowrap;
356
+ border: 0;
357
+ width: 1px;
358
+ height: 1px;
359
+ margin: -1px;
360
+ padding: 0;
361
+ position: absolute;
362
+ overflow: hidden;
363
+ }
364
+
365
+ @media (prefers-reduced-motion: reduce) {
366
+ .calendar-interactive td button.calendar-day-btn {
367
+ transition: none;
368
+ }
369
+
370
+ .calendar-interactive td button.calendar-day-btn:active {
371
+ transform: none;
372
+ }
373
+ }
374
+
375
+ @media (forced-colors: active) {
376
+ .calendar td.is-today, .calendar td.is-highlight, .calendar td.is-in-range, .calendar td.is-in-range-preview {
377
+ color: canvastext;
378
+ forced-color-adjust: none;
379
+ background: canvas;
380
+ }
381
+
382
+ .calendar td.is-in-range, .calendar td.is-in-range-preview {
383
+ border: 1px dashed highlight;
384
+ }
385
+
386
+ .calendar td.is-in-range-preview {
387
+ border-style: dotted;
388
+ }
389
+
390
+ .calendar td.is-selected {
391
+ color: highlighttext;
392
+ forced-color-adjust: none;
393
+ outline-offset: -2px;
394
+ background: highlight;
395
+ outline: 2px solid highlight;
396
+ }
397
+
398
+ .calendar td.is-disabled {
399
+ color: graytext;
400
+ }
401
+
402
+ .calendar td.is-cursor, .calendar-interactive td button.calendar-day-btn:focus-visible {
403
+ outline-offset: -2px;
404
+ outline: 2px solid canvastext;
405
+ }
406
+ }
package/dist/index.d.ts CHANGED
@@ -2,8 +2,8 @@ import Calendar from "./Calendar.svelte";
2
2
  import InteractiveCalendar from "./InteractiveCalendar.svelte";
3
3
  export { Calendar, InteractiveCalendar };
4
4
  export default Calendar;
5
- export type { CellStateOptions } from "./cell-classes.js";
6
- export { getCellClasses } from "./cell-classes.js";
5
+ export type { CellStateOptions, SelectionCellOptions, } from "@typescript-calendar-lib/web";
6
+ export { getBlankClasses, getCellClasses, getCellStateClasses, getSelectionCellClasses, joinCellClassLists, } from "@typescript-calendar-lib/web";
7
7
  export { buildRangePreview } from "./range-preview.js";
8
8
  export type { CalendarCustomSize, CalendarSize, CalendarSizeName, } from "./size.js";
9
9
  export { buildSizeStyle, isSizeName } from "./size.js";
package/dist/index.js CHANGED
@@ -2,7 +2,7 @@ import Calendar from "./Calendar.svelte";
2
2
  import InteractiveCalendar from "./InteractiveCalendar.svelte";
3
3
  export { Calendar, InteractiveCalendar };
4
4
  export default Calendar;
5
- export { getCellClasses } from "./cell-classes.js";
5
+ export { getBlankClasses, getCellClasses, getCellStateClasses, getSelectionCellClasses, joinCellClassLists, } from "@typescript-calendar-lib/web";
6
6
  export { buildRangePreview } from "./range-preview.js";
7
7
  export { buildSizeStyle, isSizeName } from "./size.js";
8
8
  export { COLOR_SCHEMES, resolveColorScheme, resolveTheme, THEMES, } from "./themes.js";
package/dist/themes.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import type { WebColorScheme, WebTheme } from "@typescript-calendar-lib/web";
2
- export type { ColorSchemeName, ThemeName, } from "@typescript-calendar-lib/web";
3
- export { COLOR_SCHEMES, resolveColorScheme, resolveTheme, THEMES, } from "@typescript-calendar-lib/web";
2
+ export type { ColorSchemeName, MergedColorScheme, ThemeName, } from "@typescript-calendar-lib/web";
3
+ export { COLOR_SCHEMES, mergeColorScheme, resolveColorScheme, resolveTheme, THEMES, } from "@typescript-calendar-lib/web";
4
4
  /** Svelte の見た目定義(web の WebTheme と同一構造) */
5
5
  export type SvelteTheme = WebTheme;
6
6
  /** CSS カスタムプロパティ(--cal-*)の値マップ */
package/dist/themes.js CHANGED
@@ -1,4 +1,4 @@
1
1
  // ─── テーマ ───────────────────────────────────────────────
2
2
  // テーマ定義・カラースキームは web パッケージに集約済み。
3
3
  // Svelte 固有の型名(SvelteTheme / SvelteColorScheme)だけをこの層で別名定義する。
4
- export { COLOR_SCHEMES, resolveColorScheme, resolveTheme, THEMES, } from "@typescript-calendar-lib/web";
4
+ export { COLOR_SCHEMES, mergeColorScheme, resolveColorScheme, resolveTheme, THEMES, } from "@typescript-calendar-lib/web";
@@ -1,7 +1,10 @@
1
+ import type { DateRange } from "@typescript-calendar-lib/core";
1
2
  import type { CalendarState, CalendarStateOptions, Direction, MonthDirection } from "@typescript-calendar-lib/tui";
2
- export interface UseCalendarStateOptions extends Pick<CalendarStateOptions, "locale" | "weekStart" | "today" | "highlight" | "range" | "isDateDisabled"> {
3
+ export interface UseCalendarStateOptions extends Pick<CalendarStateOptions, "locale" | "holidayLocale" | "weekStart" | "today" | "highlight" | "range" | "isDateDisabled" | "selectionMode"> {
3
4
  initialYear?: number;
4
5
  initialMonth?: number;
6
+ /** 表示中の年月が変わったときに呼ばれる */
7
+ onMonthChange?: (year: number, month: number) => void;
5
8
  }
6
9
  /**
7
10
  * `useCalendarState` の first 引数。
@@ -19,6 +22,12 @@ export interface UseCalendarStateReturn {
19
22
  /** 前月/翌月へ移動 */
20
23
  goNext: () => void;
21
24
  goPrev: () => void;
25
+ /** 前年/翌年へ移動 */
26
+ navigateYear: (direction: MonthDirection) => void;
27
+ /** 指定した年月へジャンプ */
28
+ goToMonth: (year: number, month: number) => void;
29
+ /** 指定した日付の月へジャンプし、カーソルをその日付に置く */
30
+ goToDate: (date: Date) => void;
22
31
  /** 今日の月へジャンプ */
23
32
  goToday: () => void;
24
33
  /** カーソル位置の日付を選択 */
@@ -27,12 +36,16 @@ export interface UseCalendarStateReturn {
27
36
  setCursorToDate: (date: Date) => void;
28
37
  /** 指定した日付を選択し、カーソルもそこへ移動する(当月に無ければ何もしない) */
29
38
  selectDateAt: (date: Date) => void;
39
+ /** from/to の範囲を選択範囲として直接設定する(順序は自動で整列される) */
40
+ selectRange: (from: Date, to: Date) => void;
30
41
  /** 選択を解除 */
31
42
  clearSelection: () => void;
32
43
  /** カーソル位置の日付(null の場合あり) */
33
44
  cursorDate: Date | null;
34
45
  /** 選択済み日付(null の場合あり) */
35
46
  selectedDate: Date | null;
47
+ /** 確定した選択範囲(未確定・single モードでは null) */
48
+ selectedRange: DateRange | null;
36
49
  /** ホバー中の日付(null の場合あり) */
37
50
  hoveredDate: Date | null;
38
51
  /** ホバー日付を更新する */
@@ -1,4 +1,4 @@
1
- import { clearSelection, createCalendarState, getCursorDate, getSelectedDate, goToToday, moveCursor, navigateMonth, sameStateOptions, selectDate, selectDateAt, setCursorToDate, updateStateOptions, } from "@typescript-calendar-lib/tui";
1
+ import { clearSelection, createCalendarState, getCursorDate, getSelectedDate, getSelectedRange, goToDate, goToMonth, goToToday, moveCursor, navigateMonth, navigateYear, sameStateOptions, selectDate, selectDateAt, selectRange, setCursorToDate, updateStateOptions, } from "@typescript-calendar-lib/tui";
2
2
  /** undefined を含む日付を値(時刻)で比較する */
3
3
  /**
4
4
  * tui の不変状態機械を包む Svelte 5 (runes) のリアクティブ状態。
@@ -50,12 +50,13 @@ export function useCalendarState(options = {}) {
50
50
  prev = next;
51
51
  state = updateStateOptions(state, next, previous);
52
52
  });
53
- // 表示月が変わったら、古い月を指すホバーをクリアする
53
+ // 表示月が変わったら onMonthChange を通知し、古い月を指すホバーをクリアする
54
54
  let prevMonthKey = null;
55
55
  $effect(() => {
56
56
  const key = `${state.year}-${state.month}`;
57
57
  if (prevMonthKey !== null && key !== prevMonthKey) {
58
58
  hoveredDate = null;
59
+ getter().onMonthChange?.(state.year, state.month);
59
60
  }
60
61
  prevMonthKey = key;
61
62
  });
@@ -69,6 +70,9 @@ export function useCalendarState(options = {}) {
69
70
  get selectedDate() {
70
71
  return getSelectedDate(state);
71
72
  },
73
+ get selectedRange() {
74
+ return getSelectedRange(state);
75
+ },
72
76
  get hoveredDate() {
73
77
  return hoveredDate;
74
78
  },
@@ -81,6 +85,15 @@ export function useCalendarState(options = {}) {
81
85
  goPrev: () => {
82
86
  state = navigateMonth(state, "prev");
83
87
  },
88
+ navigateYear: (direction) => {
89
+ state = navigateYear(state, direction);
90
+ },
91
+ goToMonth: (year, month) => {
92
+ state = goToMonth(state, year, month);
93
+ },
94
+ goToDate: (date) => {
95
+ state = goToDate(state, date);
96
+ },
84
97
  goToday: () => {
85
98
  state = goToToday(state);
86
99
  },
@@ -93,6 +106,9 @@ export function useCalendarState(options = {}) {
93
106
  selectDateAt: (date) => {
94
107
  state = selectDateAt(state, date);
95
108
  },
109
+ selectRange: (from, to) => {
110
+ state = selectRange(state, from, to);
111
+ },
96
112
  clearSelection: () => {
97
113
  state = clearSelection(state);
98
114
  },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@typescript-calendar-lib/svelte",
3
- "version": "0.5.2",
3
+ "version": "0.5.4",
4
4
  "description": "Svelte 5 components and reactive state for typescript-calendar-lib",
5
5
  "type": "module",
6
6
  "repository": {
@@ -34,9 +34,9 @@
34
34
  "svelte": "^5"
35
35
  },
36
36
  "dependencies": {
37
- "@typescript-calendar-lib/core": "0.5.2",
38
- "@typescript-calendar-lib/tui": "0.5.2",
39
- "@typescript-calendar-lib/web": "0.5.2"
37
+ "@typescript-calendar-lib/core": "^0.5.4",
38
+ "@typescript-calendar-lib/tui": "^0.5.4",
39
+ "@typescript-calendar-lib/web": "^0.5.4"
40
40
  },
41
41
  "devDependencies": {
42
42
  "@sveltejs/package": "^2.5.8",
@@ -1,23 +0,0 @@
1
- export interface CellStateOptions {
2
- today?: Date;
3
- highlight?: Date;
4
- range?: {
5
- from: Date;
6
- to: Date;
7
- };
8
- /** 範囲プレビュー(ホバー等の候補範囲)。is-in-range-preview クラスになる */
9
- rangePreview?: {
10
- from: Date;
11
- to: Date;
12
- };
13
- /** ホバー中の日付。is-hovered クラスになる */
14
- hoveredDate?: Date | null;
15
- /** 選択済み日付。該当セルに is-selected クラスが付く */
16
- selected?: Date | null;
17
- /** カーソル位置の日付。該当セルに is-cursor クラスが付く */
18
- cursorDate?: Date | null;
19
- /** 選択不可日付の判定。該当セルに is-disabled クラスが付く */
20
- isDateDisabled?: (date: Date) => boolean;
21
- }
22
- /** 日付セルの状態(週末・今日・ハイライト・範囲・選択・カーソル)に応じたCSSクラスを組み立てる */
23
- export declare function getCellClasses(date: Date, options: CellStateOptions): string;
@@ -1,27 +0,0 @@
1
- import { getCalendarCellState, isDateInRange, isSameDay, } from "@typescript-calendar-lib/core";
2
- // 範囲の from > to は不正入力として RangeError(isDateInRange が検証する)
3
- /** 日付セルの状態(週末・今日・ハイライト・範囲・選択・カーソル)に応じたCSSクラスを組み立てる */
4
- export function getCellClasses(date, options) {
5
- const state = getCalendarCellState(date, options);
6
- const classes = [];
7
- if (state.isWeekend)
8
- classes.push("is-weekend");
9
- if (state.isToday)
10
- classes.push("is-today");
11
- if (state.isHighlight)
12
- classes.push("is-highlight");
13
- if (state.isInRange)
14
- classes.push("is-in-range");
15
- if (options.rangePreview !== undefined &&
16
- isDateInRange(date, options.rangePreview))
17
- classes.push("is-in-range-preview");
18
- if (state.isDisabled)
19
- classes.push("is-disabled");
20
- if (options.hoveredDate != null && isSameDay(date, options.hoveredDate))
21
- classes.push("is-hovered");
22
- if (options.selected != null && isSameDay(date, options.selected))
23
- classes.push("is-selected");
24
- if (options.cursorDate != null && isSameDay(date, options.cursorDate))
25
- classes.push("is-cursor");
26
- return classes.join(" ");
27
- }