@typescript-calendar-lib/svelte 0.5.3 → 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
@@ -71,10 +71,12 @@ 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
75
  | `weekStart` | `"sunday" \| "monday"` | `"sunday"` | First day of the week |
75
76
  | `highlight` | `Date` | — | Date to highlight |
76
77
  | `range` | `{ from: Date; to: Date }` | — | Dates to emphasize |
77
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) |
78
80
  | `today` | `Date` | — | Reference date for "today" styling |
79
81
  | `selected` | `Date \| null` | — | Selected date (`.is-selected` class, `aria-pressed="true"` in interactive mode; other buttons are `"false"`) |
80
82
  | `cursorDate` | `Date \| null` | — | Cursor position (`.is-cursor` class) |
@@ -89,6 +91,7 @@ Run the component through a Svelte compiler such as Vite or SvelteKit; the packa
89
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) |
90
92
  | `onDateHover` | `(date: Date) => void` | — | Called when a day cell is hovered |
91
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.) |
92
95
  | `cellData` | `(date: Date) => unknown` | — | Resolve per-cell data; passed to `renderCell` and `onDateClick` |
93
96
  | `renderCell` | `(day, date, state, data?) => string` | — | Custom cell content; the returned string is inserted as plain text (XSS-safe) |
94
97
 
@@ -123,6 +126,18 @@ A turnkey component that wires `useCalendarState` and `Calendar` together with b
123
126
  - **Arrow keys** — moves the cursor with focus tracking
124
127
  - **PageUp/PageDown** — navigates to the previous/next month
125
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
+
126
141
  ### Props
127
142
 
128
143
  Combines `useCalendarState` options, `Calendar` visual props, and event callbacks:
@@ -132,25 +147,39 @@ Combines `useCalendarState` options, `Calendar` visual props, and event callback
132
147
  | `initialYear` | `number` | Starting year (defaults to today's year) |
133
148
  | `initialMonth` | `number` | Starting month `1`–`12` (normalized if out of range) |
134
149
  | `locale` | `Locale` | Language |
150
+ | `holidayLocale` | `Locale` | Locale used for holiday detection (defaults to `ja`) |
135
151
  | `weekStart` | `WeekStart` | First day of the week |
152
+ | `showWeekNumbers` | `boolean` | Render the leading week-number column |
136
153
  | `selectionMode` | `SelectionMode` | Selection mode: `"single"` \| `"range"` (default `"single"`; range commits on the 2nd pick) |
137
154
  | `highlight` | `Date` | Date to highlight |
138
155
  | `range` | `{ from: Date; to: Date }` | Dates to emphasize |
139
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 |
140
160
  | `theme` | `ThemeName \| SvelteTheme` | CSS class-based theme |
141
161
  | `colorScheme` | `ColorSchemeName \| SvelteColorScheme` | CSS variable-based colors |
142
162
  | `size` | `CalendarSize` | Cell size |
143
163
  | `responsive` | `boolean` | Shrink cells & padding below `480px` so the grid fits phone screens |
144
164
  | `style` | `CSSProperties` | Extra styles for the root element |
145
165
  | `cellData` | `(date: Date) => unknown` | Resolve per-cell data; passed to `renderCell` and `onDateClick` |
146
- | `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) |
147
167
  | `onDateClick` | `(date: Date, data?: unknown) => void` | Called when a date is selected; `data` is the cell's `cellData` value |
148
168
  | `onDateHover` | `(date: Date) => void` | Called on cell hover |
149
169
  | `onDateLeave` | `() => void` | Called when the mouse leaves the calendar |
150
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
+
151
180
  ## Interactive Mode
152
181
 
153
- 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 has an `aria-label`, weekday headers use `scope="col"`, and week-number cells use `th[scope="row"]`:
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):
154
183
 
155
184
  ```svelte
156
185
  <Calendar
@@ -224,6 +253,9 @@ For full interactivity (cursor movement, month navigation, selection), use the `
224
253
  | `goNext` | `() => void` | Move to next month |
225
254
  | `goPrev` | `() => void` | Move to previous month |
226
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 |
227
259
  | `moveCursor` | `(direction: Direction) => void` | Move cursor: `"up"` / `"down"` / `"left"` / `"right"` |
228
260
  | `selectDate` | `() => void` | Select the date under the cursor |
229
261
  | `selectDateAt` | `(date: Date) => void` | Move cursor to a date and select it (useful for mouse click) |
@@ -327,6 +359,7 @@ Available variables:
327
359
  | `--cal-fg` | Foreground |
328
360
  | `--cal-accent` | Accent (highlight) |
329
361
  | `--cal-weekend-fg` | Weekend foreground |
362
+ | `--cal-holiday-fg` | Holiday foreground |
330
363
  | `--cal-border` | Borders |
331
364
  | `--cal-header-bg` | Header background |
332
365
  | `--cal-highlight-bg` | Highlight background |
@@ -335,8 +368,11 @@ Available variables:
335
368
  | `--cal-range-preview-bg` | Range preview background (falls back to `--cal-range-bg`) |
336
369
  | `--cal-today-bg` | Today background |
337
370
  | `--cal-today-fg` | Today foreground |
338
- | `--cal-selected-bg` | Selected background |
339
- | `--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.
340
376
 
341
377
  Pass a custom scheme directly:
342
378
 
@@ -398,16 +434,18 @@ It's **off by default** to keep existing layouts unchanged. The calendar root ge
398
434
 
399
435
  ## Cell Classes
400
436
 
401
- Each day cell gets semantic classes you can target with CSS. The table has an `aria-label`, 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"`:
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"`:
402
438
 
403
439
  | Class | When |
404
440
  | :--- | :--- |
405
441
  | `is-weekend` | Saturday or Sunday |
442
+ | `is-holiday` | Holiday per `holidayLocale` (default `"ja"`) |
406
443
  | `is-today` | Matches `today` |
407
444
  | `is-highlight` | Matches `highlight` |
408
445
  | `is-in-range` | Inside `range` |
409
446
  | `is-in-range-preview` | Inside `rangePreview` |
410
447
  | `is-hovered` | Matches `hoveredDate` (interactive mode) |
448
+ | `is-disabled` | Date returned `true` from `isDateDisabled` (interactive mode) |
411
449
  | `is-selected` | Matches `selected` |
412
450
  | `is-cursor` | Matches `cursorDate` |
413
451
 
@@ -419,9 +457,13 @@ import Calendar, {
419
457
  buildRangePreview,
420
458
  buildSizeStyle,
421
459
  COLOR_SCHEMES,
460
+ getBlankClasses,
422
461
  getCellClasses,
462
+ getCellStateClasses,
463
+ getSelectionCellClasses,
423
464
  InteractiveCalendar,
424
465
  isSizeName,
466
+ joinCellClassLists,
425
467
  resolveColorScheme,
426
468
  resolveTheme,
427
469
  THEMES,
@@ -435,6 +477,7 @@ import type {
435
477
  CellStateOptions,
436
478
  ColorSchemeName,
437
479
  CSSProperties,
480
+ SelectionCellOptions,
438
481
  SvelteColorScheme,
439
482
  SvelteTheme,
440
483
  ThemeName,
@@ -11,8 +11,14 @@
11
11
  getWeekdayHeaders,
12
12
  isSameDay,
13
13
  } from "@typescript-calendar-lib/core";
14
- import { formatCellLabel } from "@typescript-calendar-lib/web";
15
- 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";
16
22
  import type { CalendarSize } from "./size.js";
17
23
  import { buildSizeStyle, isSizeName } from "./size.js";
18
24
  import type { CSSProperties } from "./style.js";
@@ -23,7 +29,8 @@
23
29
  SvelteTheme,
24
30
  ThemeName,
25
31
  } from "./themes.js";
26
- import { resolveColorScheme, resolveTheme } from "./themes.js";
32
+ import type { Snippet } from "svelte";
33
+ import { mergeColorScheme, resolveTheme } from "./themes.js";
27
34
 
28
35
  interface CalendarProps {
29
36
  year: number;
@@ -72,13 +79,16 @@
72
79
 
73
80
  // ── セル状態 ──
74
81
 
75
- /** セル内容のカスタムレンダリング。interactive 時は button の子として描画される。第4引数に該当日のデータが渡る */
82
+ /**
83
+ * セル内容のカスタムレンダリング。interactive 時は button の子として描画される。
84
+ * Snippet(markup)または文字列のどちらでも受け付ける。第4引数に該当日のデータが渡る。
85
+ */
76
86
  renderCell?: (
77
87
  day: number,
78
88
  date: Date,
79
89
  state: CalendarCellState,
80
90
  data?: unknown,
81
- ) => string;
91
+ ) => string | Snippet;
82
92
  /** 各セルに付与するユーザー定義データを解決する関数。実セルのみに呼ばれる */
83
93
  cellData?: (date: Date) => unknown;
84
94
  /** 選択不可日付の判定。true を返した日付は is-disabled クラスになり、インタラクティブ時は選択・ホバーできなくなる */
@@ -114,28 +124,64 @@
114
124
  isDateDisabled,
115
125
  }: CalendarProps = $props();
116
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
+
117
135
  const cellDate = (day: number): Date => createDate(year, month - 1, day);
118
- const cellState = (day: number): CalendarCellState =>
119
- getCalendarCellState(cellDate(day), { today, holidayLocale, highlight, range, isDateDisabled });
120
- const cellClass = (day: number): string | undefined =>
121
- 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, {
122
153
  today,
123
154
  holidayLocale,
124
155
  highlight,
125
156
  range,
126
- rangePreview,
127
- selected,
128
- cursorDate,
129
- hoveredDate,
130
157
  isDateDisabled,
131
- }) || undefined;
132
- const isDisabledDay = (day: number): boolean => cellState(day).isDisabled;
133
- const isSelectedDay = (day: number): boolean =>
134
- selected != null && isSameDay(cellDate(day), selected);
135
- const isTodayDay = (day: number): boolean =>
136
- today != null && isSameDay(cellDate(day), today);
137
- const isCursorDay = (day: number): boolean =>
138
- cursorDate != null && isSameDay(cellDate(day), cursorDate);
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";
139
185
  const weekOf = (row: (number | null)[]): number | null => {
140
186
  const firstIdx = row.findIndex((d) => d !== null);
141
187
  if (firstIdx === -1) return null;
@@ -145,86 +191,131 @@
145
191
  ? getISOWeek(weekStartDate)
146
192
  : getWeekOfYear(weekStartDate);
147
193
  };
148
- const handleCellClick = (day: number) => {
194
+ const handleCellClick = (cell: DayCell) => {
149
195
  if (interactive && onDateClick) {
150
- const date = cellDate(day);
151
- onDateClick(date, cellData?.(date));
196
+ onDateClick(cell.date, cellData?.(cell.date));
152
197
  }
153
198
  };
154
- const handleCellHover = (day: number) => {
155
- if (interactive && onDateHover) onDateHover(cellDate(day));
199
+ const handleCellHover = (cell: DayCell) => {
200
+ if (interactive && onDateHover) onDateHover(cell.date);
156
201
  };
157
202
  const handleMouseLeave = () => {
158
203
  if (interactive && onDateLeave) onDateLeave();
159
204
  };
160
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
+
161
219
  // style 属性は文字列のみ受け付けるためオブジェクトを直列化する(--cal-* 変数含む)
162
220
  const rootStyle = $derived(
163
221
  styleObjectToString({
164
- ...resolveColorScheme(colorScheme),
222
+ ...mergeColorScheme(colorScheme),
165
223
  ...buildSizeStyle(size),
166
224
  ...style,
167
225
  }),
168
226
  );
169
227
  </script>
170
228
 
229
+ <!-- svelte-ignore a11y_no_static_element_interactions -->
230
+ <!-- onmouseleave はホバー状態クリア用の補助イベント、onkeydown は
231
+ InteractiveCalendar が table 上で扱う。root 自体は widget ではないため
232
+ ARIA role は持たない。 -->
171
233
  <div
172
234
  class="calendar {resolveTheme(theme).className}{isSizeName(size) ? ` calendar-size-${size}` : ""}{interactive ? " calendar-interactive" : ""}{responsive ? " calendar-responsive" : ""}"
173
235
  style={rootStyle}
174
- role={interactive ? "group" : undefined}
175
236
  onmouseleave={interactive ? handleMouseLeave : undefined}
176
237
  onkeydown={onkeydown}
177
238
  >
178
239
  <div class="calendar-header">
179
- <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>
180
252
  </div>
181
- <table aria-label={`${getMonthName(locale, month)} ${year}`}>
253
+ <!-- interactive 時のみ APG の grid セマンティクスを付与する。静的表示では
254
+ ネイティブの table/cell セマンティクスをそのまま使う。 -->
255
+ <table role={interactive ? "grid" : undefined} aria-labelledby={headingId}>
182
256
  <thead>
183
257
  <tr>
184
258
  {#if showWeekNumbers}
185
- <th class="calendar-week" scope="col" aria-label="Week number"></th>
259
+ <th class="calendar-week" scope="col" aria-label={uiString("weekNumber", locale)}></th>
186
260
  {/if}
187
- {#each getWeekdayHeaders(locale, weekStart) as day (day)}
261
+ {#each weekdays as day (day)}
188
262
  <th scope="col">{day}</th>
189
263
  {/each}
190
264
  </tr>
191
265
  </thead>
192
266
  <tbody>
193
- {#each buildMonthGrid(year, month, weekStart) as row, i (i)}
267
+ {#each grid as row, i (i)}
194
268
  {#if !row.every((d) => d === null)}
195
269
  <tr>
196
270
  {#if showWeekNumbers}
197
- <th class="calendar-week" scope="row">{weekOf(row)}</th>
271
+ <th
272
+ class="calendar-week"
273
+ scope="row"
274
+ aria-label={weekAriaLabel(i)}
275
+ >{#if weekNumbers !== null}{weekNumbers[i]}{/if}</th>
198
276
  {/if}
199
277
  {#each row as day, j (j)}
200
278
  {#if day === null}
201
- <td></td>
279
+ <td role={gridCellRole} class={getBlankClasses()}></td>
202
280
  {:else}
281
+ {@const cell = dayCell(day)}
203
282
  <td
204
- class={cellClass(day)}
205
- aria-current={isTodayDay(day) ? "date" : undefined}
206
- aria-disabled={isDisabledDay(day) || undefined}
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}
207
288
  >
208
289
  {#if interactive}
209
290
  <button
210
291
  type="button"
211
292
  class="calendar-day-btn"
212
- onclick={isDisabledDay(day) ? undefined : () => handleCellClick(day)}
213
- onmouseenter={isDisabledDay(day) ? undefined : () => handleCellHover(day)}
214
- tabindex={isCursorDay(day) ? 0 : -1}
215
- 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}
216
297
  aria-label={formatCellLabel(locale, year, month, day)}
217
- aria-pressed={isSelectedDay(day)}
218
- disabled={isDisabledDay(day)}
298
+ aria-pressed={cell.isSelected}
299
+ disabled={cell.isDisabled}
219
300
  >
220
301
  {#if renderCell}
221
- {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}
222
308
  {:else}
223
309
  {day}
224
310
  {/if}
225
311
  </button>
226
312
  {:else if renderCell}
227
- {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}
228
319
  {:else}
229
320
  {day}
230
321
  {/if}
@@ -2,6 +2,7 @@ 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;
@@ -49,8 +50,11 @@ interface CalendarProps {
49
50
  onDateLeave?: () => void;
50
51
  /** キーボード操作。root 要素で発火(矢印キー等を InteractiveCalendar が処理する) */
51
52
  onkeydown?: (event: KeyboardEvent) => void;
52
- /** セル内容のカスタムレンダリング。interactive 時は button の子として描画される。第4引数に該当日のデータが渡る */
53
- 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;
54
58
  /** 各セルに付与するユーザー定義データを解決する関数。実セルのみに呼ばれる */
55
59
  cellData?: (date: Date) => unknown;
56
60
  /** 選択不可日付の判定。true を返した日付は is-disabled クラスになり、インタラクティブ時は選択・ホバーできなくなる */
@@ -2,6 +2,7 @@
2
2
  import type { CalendarCellState, CalendarOptions } from "@typescript-calendar-lib/core";
3
3
  import type { SelectionMode } from "@typescript-calendar-lib/tui";
4
4
  import { keyToAction } from "@typescript-calendar-lib/tui";
5
+ import type { Snippet } from "svelte";
5
6
  import Calendar from "./Calendar.svelte";
6
7
  import { buildRangePreview } from "./range-preview.js";
7
8
  import type { CalendarSize } from "./size.js";
@@ -24,6 +25,8 @@
24
25
  locale?: CalendarOptions["locale"];
25
26
  holidayLocale?: CalendarOptions["holidayLocale"];
26
27
  weekStart?: CalendarOptions["weekStart"];
28
+ /** 各週の先頭に週番号(ISO/年始基準)を表示する */
29
+ showWeekNumbers?: boolean;
27
30
  /** ハイライト対象日 */
28
31
  highlight?: Date;
29
32
  /** 色付け範囲 */
@@ -34,6 +37,8 @@
34
37
  today?: Date;
35
38
  /** 選択不可日付の判定。true を返した日付は選択・カーソル移動・ホバーの対象外になる */
36
39
  isDateDisabled?: (date: Date) => boolean;
40
+ /** 表示中の年月が変わったときに呼ばれる */
41
+ onMonthChange?: (year: number, month: number) => void;
37
42
 
38
43
  // ── Calendar の見た目 ──
39
44
 
@@ -43,13 +48,13 @@
43
48
  /** 狭い画面(スマホ等)でセルサイズと余白を自動調整する */
44
49
  responsive?: boolean;
45
50
  style?: CSSProperties;
46
- /** セル内容のカスタムレンダリング。第4引数に該当日のデータが渡る */
51
+ /** セル内容のカスタムレンダリング。文字列または Snippet。第4引数に該当日のデータが渡る */
47
52
  renderCell?: (
48
53
  day: number,
49
54
  date: Date,
50
55
  state: CalendarCellState,
51
56
  data?: unknown,
52
- ) => string;
57
+ ) => string | Snippet;
53
58
  /** 各セルに付与するユーザー定義データを解決する関数。実セルのみに呼ばれる */
54
59
  cellData?: (date: Date) => unknown;
55
60
 
@@ -69,11 +74,13 @@
69
74
  locale,
70
75
  holidayLocale,
71
76
  weekStart,
77
+ showWeekNumbers,
72
78
  highlight,
73
79
  range,
74
80
  selectionMode,
75
81
  today,
76
82
  isDateDisabled,
83
+ onMonthChange,
77
84
  theme,
78
85
  colorScheme,
79
86
  size,
@@ -97,6 +104,7 @@
97
104
  range,
98
105
  isDateDisabled,
99
106
  selectionMode,
107
+ onMonthChange,
100
108
  }));
101
109
 
102
110
  // クリック(マウス・Enter/Space 共通)で日付を選択する。
@@ -157,6 +165,7 @@
157
165
  locale={cal.state.options.locale}
158
166
  holidayLocale={cal.state.options.holidayLocale}
159
167
  weekStart={cal.state.options.weekStart}
168
+ showWeekNumbers={showWeekNumbers}
160
169
  highlight={cal.state.options.highlight}
161
170
  range={cal.state.options.range}
162
171
  today={cal.state.options.today}
@@ -1,5 +1,6 @@
1
1
  import type { CalendarCellState, CalendarOptions } from "@typescript-calendar-lib/core";
2
2
  import type { SelectionMode } from "@typescript-calendar-lib/tui";
3
+ import type { Snippet } from "svelte";
3
4
  import type { CalendarSize } from "./size.js";
4
5
  import type { CSSProperties } from "./style.js";
5
6
  import type { ColorSchemeName, SvelteColorScheme, SvelteTheme, ThemeName } from "./themes.js";
@@ -11,6 +12,8 @@ interface InteractiveCalendarProps {
11
12
  locale?: CalendarOptions["locale"];
12
13
  holidayLocale?: CalendarOptions["holidayLocale"];
13
14
  weekStart?: CalendarOptions["weekStart"];
15
+ /** 各週の先頭に週番号(ISO/年始基準)を表示する */
16
+ showWeekNumbers?: boolean;
14
17
  /** ハイライト対象日 */
15
18
  highlight?: Date;
16
19
  /** 色付け範囲 */
@@ -24,14 +27,16 @@ interface InteractiveCalendarProps {
24
27
  today?: Date;
25
28
  /** 選択不可日付の判定。true を返した日付は選択・カーソル移動・ホバーの対象外になる */
26
29
  isDateDisabled?: (date: Date) => boolean;
30
+ /** 表示中の年月が変わったときに呼ばれる */
31
+ onMonthChange?: (year: number, month: number) => void;
27
32
  theme?: ThemeName | SvelteTheme;
28
33
  colorScheme?: ColorSchemeName | SvelteColorScheme;
29
34
  size?: CalendarSize;
30
35
  /** 狭い画面(スマホ等)でセルサイズと余白を自動調整する */
31
36
  responsive?: boolean;
32
37
  style?: CSSProperties;
33
- /** セル内容のカスタムレンダリング。第4引数に該当日のデータが渡る */
34
- 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;
35
40
  /** 各セルに付与するユーザー定義データを解決する関数。実セルのみに呼ばれる */
36
41
  cellData?: (date: Date) => unknown;
37
42
  /** セルクリック時(キーボードの Enter/Space 含む)。クリックした日付は自動的に選択される。第2引数に該当日のデータが渡る */
package/dist/calendar.css CHANGED
@@ -2,18 +2,20 @@
2
2
  --cal-bg: #fff;
3
3
  --cal-fg: #1e293b;
4
4
  --cal-accent: #dc2626;
5
- --cal-weekend-fg: #94a3b8;
5
+ --cal-weekend-fg: #586a84;
6
6
  --cal-holiday-fg: #d33;
7
- --cal-border: #e2e8f0;
7
+ --cal-border: #aeb8c6;
8
8
  --cal-header-bg: #f8fafc;
9
9
  --cal-highlight-bg: #dc2626;
10
10
  --cal-highlight-fg: #fff;
11
11
  --cal-range-bg: #fef3c7;
12
+ --cal-range-preview-bg: #fef9c3;
13
+ --cal-hover-bg: #fef9c3;
12
14
  --cal-today-bg: #fff7ed;
13
15
  --cal-today-fg: #c2410c;
14
16
  --cal-selected-bg: #dc2626;
15
17
  --cal-selected-fg: #fff;
16
- --cal-disabled-fg: #cbd5e1;
18
+ --cal-disabled-fg: #5b799d;
17
19
  --cal-cell-w: 2.25rem;
18
20
  --cal-cell-h: 2rem;
19
21
  --cal-font-size: .875rem;
@@ -75,7 +77,6 @@
75
77
 
76
78
  .calendar td.is-disabled {
77
79
  color: var(--cal-disabled-fg, var(--cal-weekend-fg));
78
- opacity: .5;
79
80
  text-decoration: line-through;
80
81
  }
81
82
 
@@ -168,7 +169,7 @@
168
169
  border: 1px solid var(--cal-border);
169
170
  }
170
171
 
171
- .calendar-theme-modern td:empty {
172
+ .calendar-theme-modern td.is-blank {
172
173
  background: none;
173
174
  border-color: #0000;
174
175
  }
@@ -223,11 +224,11 @@
223
224
  border-radius: 999px;
224
225
  }
225
226
 
226
- .calendar-theme-rounded td:empty {
227
+ .calendar-theme-rounded td.is-blank {
227
228
  background: none;
228
229
  }
229
230
 
230
- .calendar-interactive {
231
+ .calendar-interactive:focus:not(:focus-visible) {
231
232
  outline: none;
232
233
  }
233
234
 
@@ -343,8 +344,63 @@
343
344
  @media (width <= 360px) {
344
345
  .calendar-responsive {
345
346
  --cal-cell-w: 1.5rem;
346
- --cal-cell-h: 1.375rem;
347
+ --cal-cell-h: 1.5rem;
347
348
  --cal-font-size: .6875rem;
348
349
  --cal-header-font-size: .875rem;
349
350
  }
350
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";
@@ -3,6 +3,8 @@ 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
+ onMonthChange?: (year: number, month: number) => void;
6
8
  }
7
9
  /**
8
10
  * `useCalendarState` の first 引数。
@@ -20,6 +22,12 @@ export interface UseCalendarStateReturn {
20
22
  /** 前月/翌月へ移動 */
21
23
  goNext: () => void;
22
24
  goPrev: () => void;
25
+ /** 前年/翌年へ移動 */
26
+ navigateYear: (direction: MonthDirection) => void;
27
+ /** 指定した年月へジャンプ */
28
+ goToMonth: (year: number, month: number) => void;
29
+ /** 指定した日付の月へジャンプし、カーソルをその日付に置く */
30
+ goToDate: (date: Date) => void;
23
31
  /** 今日の月へジャンプ */
24
32
  goToday: () => void;
25
33
  /** カーソル位置の日付を選択 */
@@ -1,4 +1,4 @@
1
- import { clearSelection, createCalendarState, getCursorDate, getSelectedDate, getSelectedRange, goToToday, moveCursor, navigateMonth, sameStateOptions, selectDate, selectDateAt, selectRange, 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
  });
@@ -84,6 +85,15 @@ export function useCalendarState(options = {}) {
84
85
  goPrev: () => {
85
86
  state = navigateMonth(state, "prev");
86
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
+ },
87
97
  goToday: () => {
88
98
  state = goToToday(state);
89
99
  },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@typescript-calendar-lib/svelte",
3
- "version": "0.5.3",
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.3",
38
- "@typescript-calendar-lib/tui": "^0.5.3",
39
- "@typescript-calendar-lib/web": "^0.5.3"
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,26 +0,0 @@
1
- import type { Locale } from "@typescript-calendar-lib/core";
2
- export interface CellStateOptions {
3
- today?: Date;
4
- /** 祝日の判定に使うロケール。省略時は "ja" */
5
- holidayLocale?: Locale;
6
- highlight?: Date;
7
- range?: {
8
- from: Date;
9
- to: Date;
10
- };
11
- /** 範囲プレビュー(ホバー等の候補範囲)。is-in-range-preview クラスになる */
12
- rangePreview?: {
13
- from: Date;
14
- to: Date;
15
- };
16
- /** ホバー中の日付。is-hovered クラスになる */
17
- hoveredDate?: Date | null;
18
- /** 選択済み日付。該当セルに is-selected クラスが付く */
19
- selected?: Date | null;
20
- /** カーソル位置の日付。該当セルに is-cursor クラスが付く */
21
- cursorDate?: Date | null;
22
- /** 選択不可日付の判定。該当セルに is-disabled クラスが付く */
23
- isDateDisabled?: (date: Date) => boolean;
24
- }
25
- /** 日付セルの状態(週末・今日・ハイライト・範囲・選択・カーソル)に応じたCSSクラスを組み立てる */
26
- export declare function getCellClasses(date: Date, options: CellStateOptions): string;
@@ -1,29 +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.isHoliday)
10
- classes.push("is-holiday");
11
- if (state.isToday)
12
- classes.push("is-today");
13
- if (state.isHighlight)
14
- classes.push("is-highlight");
15
- if (state.isInRange)
16
- classes.push("is-in-range");
17
- if (options.rangePreview !== undefined &&
18
- isDateInRange(date, options.rangePreview))
19
- classes.push("is-in-range-preview");
20
- if (state.isDisabled)
21
- classes.push("is-disabled");
22
- if (options.hoveredDate != null && isSameDay(date, options.hoveredDate))
23
- classes.push("is-hovered");
24
- if (options.selected != null && isSameDay(date, options.selected))
25
- classes.push("is-selected");
26
- if (options.cursorDate != null && isSameDay(date, options.cursorDate))
27
- classes.push("is-cursor");
28
- return classes.join(" ");
29
- }