@typescript-calendar-lib/svelte 0.6.1 → 0.6.2

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
@@ -1,6 +1,6 @@
1
1
  # @typescript-calendar-lib/svelte
2
2
 
3
- A `<Calendar />` Svelte 5 component with CSS-based themes and color schemes, plus a `useCalendarState` hook for full interactivity. The component is fully controlled — you pass `year` and `month` and it renders the grid.
3
+ A `<Calendar />` Svelte 5 component with CSS-based themes and color schemes, plus a `useCalendarState` hook for full interactivity. The component is fully controlled — you pass `year` + `month` (or a single `date`) and it renders the grid.
4
4
 
5
5
  > **Warning: Peer dependency** — Requires Svelte 5 (runes). The package doesn't bundle Svelte; install it in your app:
6
6
  >
@@ -66,34 +66,36 @@ Run the component through a Svelte compiler such as Vite or SvelteKit; the packa
66
66
  <Calendar year={2026} month={9} />
67
67
  ```
68
68
 
69
- | Prop | Type | Default | Description |
70
- | :--- | :--- | :--- | :--- |
71
- | `year` | `number` | — (required) | Calendar year |
72
- | `month` | `number` | — (required) | Month, 1-indexed (`1`–`12`) |
73
- | `locale` | `"en" \| "ja" \| "es" \| "de" \| "fr" \| "ko" \| "zh"` | `"en"` | Language |
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
- | `weekStart` | `"sunday" \| "monday"` | `"sunday"` | First day of the week |
76
- | `highlight` | `Date` | — | Date to highlight |
77
- | `range` | `{ from: Date; to: Date }` | — | Dates to emphasize |
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) |
80
- | `today` | `Date` | — | Reference date for "today" styling |
81
- | `selected` | `Date \| null` | — | Selected date (`.is-selected` class, `aria-pressed="true"` in interactive mode; other buttons are `"false"`) |
82
- | `cursorDate` | `Date \| null` | — | Cursor position (`.is-cursor` class) |
83
- | `hoveredDate` | `Date \| null` | — | Hovered date (`.is-hovered` class) |
84
- | `theme` | `ThemeName \| SvelteTheme` | `"default"` | CSS class-based theme |
85
- | `colorScheme` | `ColorSchemeName \| SvelteColorScheme` | `"default"` | CSS variable-based colors |
86
- | `size` | `CalendarSize` | `"md"` | Cell size |
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) |
89
- | `style` | `CSSProperties` | — | Extra styles for the root element |
90
- | `interactive` | `boolean` | `false` | Enable cell click/hover/keyboard selection |
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) |
92
- | `onDateHover` | `(date: Date) => void` | — | Called when a day cell is hovered |
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.) |
95
- | `cellData` | `(date: Date) => unknown` | — | Resolve per-cell data; passed to `renderCell` and `onDateClick` |
96
- | `renderCell` | `(day, date, state, data?) => string` | — | Custom cell content; the returned string is inserted as plain text (XSS-safe) |
69
+ | Prop | Type | Default | Description |
70
+ | :---------------- | :----------------------------------------------------- | :---------- | :---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
71
+ | `date` | `Date` | — | Shorthand for `year` + `month`; takes precedence when both are given |
72
+ | `year` | `number` | — | Calendar year (required unless `date` is given) |
73
+ | `month` | `number` | — | Month, 1-indexed (`1`–`12`) (required unless `date` is given) |
74
+ | `locale` | `"en" \| "ja" \| "es" \| "de" \| "fr" \| "ko" \| "zh"` | `"en"` | Language |
75
+ | `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) |
76
+ | `weekStart` | `"sunday" \| "monday"` | `"sunday"` | First day of the week |
77
+ | `highlight` | `Date` | — | Date to highlight |
78
+ | `range` | `{ from: Date; to: Date }` | — | Dates to emphasize |
79
+ | `rangePreview` | `{ from: Date; to: Date }` | — | Candidate range preview (hover preview), rendered with the `is-in-range-preview` class |
80
+ | `isDateDisabled` | `(date: Date) => boolean` | — | Mark dates as non-selectable in interactive mode (`is-disabled` class) |
81
+ | `today` | `Date` | — | Reference date for "today" styling |
82
+ | `selected` | `Date \| null` | — | Selected date (`.is-selected` class, `aria-pressed="true"` in interactive mode; other buttons are `"false"`) |
83
+ | `cursorDate` | `Date \| null` | — | Cursor position (`.is-cursor` class) |
84
+ | `hoveredDate` | `Date \| null` | — | Hovered date (`.is-hovered` class) |
85
+ | `theme` | `ThemeName \| SvelteTheme` | `"default"` | CSS class-based theme |
86
+ | `colorScheme` | `ColorSchemeName \| SvelteColorScheme` | `"default"` | CSS variable-based colors |
87
+ | `size` | `CalendarSize` | `"md"` | Cell size |
88
+ | `responsive` | `boolean` | `false` | Shrink cells & padding below `480px` so the grid fits phone screens |
89
+ | `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) |
90
+ | `class` | `string` | — | Extra class on the root element |
91
+ | `style` | `CSSProperties` | — | Extra styles for the root element |
92
+ | `interactive` | `boolean` | `false` | Enable cell click/hover/keyboard selection |
93
+ | `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) |
94
+ | `onDateHover` | `(date: Date) => void` | — | Called when a day cell is hovered |
95
+ | `onDateLeave` | `() => void` | — | Called when the mouse leaves the calendar |
96
+ | `onkeydown` | `(event: KeyboardEvent) => void` | — | Keyboard handler fired on the root element (arrow keys etc.) |
97
+ | `cellData` | `(date: Date) => unknown` | — | Resolve per-cell data; passed to `renderCell` and `onDateClick` |
98
+ | `renderCell` | `(day, date, state, data?) => string` | — | Custom cell content; the returned string is inserted as plain text (XSS-safe) |
97
99
 
98
100
  The component exports as both named `Calendar` and default.
99
101
 
@@ -103,7 +105,7 @@ A turnkey component that wires `useCalendarState` and `Calendar` together with b
103
105
 
104
106
  ```svelte
105
107
  <script lang="ts">
106
- import InteractiveCalendar from "@typescript-calendar-lib/svelte";
108
+ import { InteractiveCalendar } from "@typescript-calendar-lib/svelte";
107
109
  import "@typescript-calendar-lib/svelte/calendar.css";
108
110
  </script>
109
111
 
@@ -142,31 +144,32 @@ Pass `isDateDisabled` to mark dates as non-selectable. Disabled cells get the `i
142
144
 
143
145
  Combines `useCalendarState` options, `Calendar` visual props, and event callbacks:
144
146
 
145
- | Prop | Type | Description |
146
- | :--- | :--- | :--- |
147
- | `initialYear` | `number` | Starting year (defaults to today's year) |
148
- | `initialMonth` | `number` | Starting month `1`–`12` (normalized if out of range) |
149
- | `locale` | `Locale` | Language |
150
- | `holidayLocale` | `Locale` | Locale used for holiday detection (defaults to `ja`) |
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) |
154
- | `highlight` | `Date` | Date to highlight |
155
- | `range` | `{ from: Date; to: Date }` | Dates to emphasize |
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 |
160
- | `theme` | `ThemeName \| SvelteTheme` | CSS class-based theme |
161
- | `colorScheme` | `ColorSchemeName \| SvelteColorScheme` | CSS variable-based colors |
162
- | `size` | `CalendarSize` | Cell size |
163
- | `responsive` | `boolean` | Shrink cells & padding below `480px` so the grid fits phone screens |
164
- | `style` | `CSSProperties` | Extra styles for the root element |
165
- | `cellData` | `(date: Date) => unknown` | Resolve per-cell data; passed to `renderCell` and `onDateClick` |
166
- | `renderCell` | `(day, date, state, data?) => string \| Snippet` | Custom cell content (plain text is XSS-safe; a `Snippet` renders markup) |
167
- | `onDateClick` | `(date: Date, data?: unknown) => void` | Called when a date is selected; `data` is the cell's `cellData` value |
168
- | `onDateHover` | `(date: Date) => void` | Called on cell hover |
169
- | `onDateLeave` | `() => void` | Called when the mouse leaves the calendar |
147
+ | Prop | Type | Description |
148
+ | :---------------- | :----------------------------------------------- | :------------------------------------------------------------------------------------------ |
149
+ | `initialYear` | `number` | Starting year (defaults to today's year) |
150
+ | `initialMonth` | `number` | Starting month `1`–`12` (normalized if out of range) |
151
+ | `locale` | `Locale` | Language |
152
+ | `holidayLocale` | `Locale` | Locale used for holiday detection (defaults to `ja`) |
153
+ | `weekStart` | `WeekStart` | First day of the week |
154
+ | `showWeekNumbers` | `boolean` | Render the leading week-number column |
155
+ | `selectionMode` | `SelectionMode` | Selection mode: `"single"` \| `"range"` (default `"single"`; range commits on the 2nd pick) |
156
+ | `highlight` | `Date` | Date to highlight |
157
+ | `range` | `{ from: Date; to: Date }` | Dates to emphasize |
158
+ | `today` | `Date` | Reference date for "today" styling |
159
+ | `isDateDisabled` | `(date: Date) => boolean` | Mark dates as non-selectable |
160
+ | `onkeydown` | `(event: KeyboardEvent) => void` | Keyboard handler fired on the root element (arrow keys etc.) |
161
+ | `onMonthChange` | `(year: number, month: number) => void` | Called when the displayed month changes |
162
+ | `theme` | `ThemeName \| SvelteTheme` | CSS class-based theme |
163
+ | `colorScheme` | `ColorSchemeName \| SvelteColorScheme` | CSS variable-based colors |
164
+ | `size` | `CalendarSize` | Cell size |
165
+ | `responsive` | `boolean` | Shrink cells & padding below `480px` so the grid fits phone screens |
166
+ | `class` | `string` | Extra class on the root element |
167
+ | `style` | `CSSProperties` | Extra styles for the root element |
168
+ | `cellData` | `(date: Date) => unknown` | Resolve per-cell data; passed to `renderCell` and `onDateClick` |
169
+ | `renderCell` | `(day, date, state, data?) => string \| Snippet` | Custom cell content (plain text is XSS-safe; a `Snippet` renders markup) |
170
+ | `onDateClick` | `(date: Date, data?: unknown) => void` | Called when a date is selected; `data` is the cell's `cellData` value |
171
+ | `onDateHover` | `(date: Date) => void` | Called on cell hover |
172
+ | `onDateLeave` | `() => void` | Called when the mouse leaves the calendar |
170
173
 
171
174
  ## Accessibility notes
172
175
 
@@ -247,26 +250,26 @@ For full interactivity (cursor movement, month navigation, selection), use the `
247
250
 
248
251
  ### Return value
249
252
 
250
- | Field | Type | Description |
251
- | :--- | :--- | :--- |
252
- | `state` | `CalendarState` | Current calendar state (from tui) |
253
- | `goNext` | `() => void` | Move to next month |
254
- | `goPrev` | `() => void` | Move to previous month |
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 |
259
- | `moveCursor` | `(direction: Direction) => void` | Move cursor: `"up"` / `"down"` / `"left"` / `"right"` |
260
- | `selectDate` | `() => void` | Select the date under the cursor |
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) |
263
- | `setCursorToDate` | `(date: Date) => void` | Move the cursor to a specific date (no-op if outside current month) |
264
- | `clearSelection` | `() => void` | Clear the selection |
265
- | `cursorDate` | `Date \| null` | Date under the cursor |
266
- | `selectedDate` | `Date \| null` | Currently selected date |
267
- | `selectedRange` | `DateRange \| null` | Committed selection range (range mode) |
268
- | `hoveredDate` | `Date \| null` | Currently hovered date |
269
- | `setHoveredDate` | `(date: Date \| null) => void` | Set or clear the hovered date |
253
+ | Field | Type | Description |
254
+ | :---------------- | :-------------------------------------- | :------------------------------------------------------------------ |
255
+ | `state` | `CalendarState` | Current calendar state (from tui) |
256
+ | `goNext` | `() => void` | Move to next month |
257
+ | `goPrev` | `() => void` | Move to previous month |
258
+ | `goToday` | `() => void` | Jump to today's month |
259
+ | `navigateYear` | `(direction: MonthDirection) => void` | Move to the previous/next year (`"prev"` / `"next"`) |
260
+ | `goToMonth` | `(year: number, month: number) => void` | Jump to a specific year and month |
261
+ | `goToDate` | `(date: Date) => void` | Jump to a date's month and put the cursor on that date |
262
+ | `moveCursor` | `(direction: Direction) => void` | Move cursor: `"up"` / `"down"` / `"left"` / `"right"` |
263
+ | `selectDate` | `() => void` | Select the date under the cursor |
264
+ | `selectDateAt` | `(date: Date) => void` | Move cursor to a date and select it (useful for mouse click) |
265
+ | `selectRange` | `(from: Date, to: Date) => void` | Set a committed range directly (range mode) |
266
+ | `setCursorToDate` | `(date: Date) => void` | Move the cursor to a specific date (no-op if outside current month) |
267
+ | `clearSelection` | `() => void` | Clear the selection |
268
+ | `cursorDate` | `Date \| null` | Date under the cursor |
269
+ | `selectedDate` | `Date \| null` | Currently selected date |
270
+ | `selectedRange` | `DateRange \| null` | Committed selection range (range mode) |
271
+ | `hoveredDate` | `Date \| null` | Currently hovered date |
272
+ | `setHoveredDate` | `(date: Date \| null) => void` | Set or clear the hovered date |
270
273
 
271
274
  ## Range selection
272
275
 
@@ -339,8 +342,13 @@ Predefined theme map and resolver.
339
342
 
340
343
  ```ts
341
344
  type ColorSchemeName =
342
- | "default" | "ocean" | "forest" | "sunset"
343
- | "mono" | "midnight" | "blossom";
345
+ | "default"
346
+ | "ocean"
347
+ | "forest"
348
+ | "sunset"
349
+ | "mono"
350
+ | "midnight"
351
+ | "blossom";
344
352
  ```
345
353
 
346
354
  ### `SvelteColorScheme`
@@ -353,24 +361,24 @@ type SvelteColorScheme = Record<`--cal-${string}`, string>;
353
361
 
354
362
  Available variables:
355
363
 
356
- | Variable | Purpose |
357
- | :--- | :--- |
358
- | `--cal-bg` | Background |
359
- | `--cal-fg` | Foreground |
360
- | `--cal-accent` | Accent (highlight) |
361
- | `--cal-weekend-fg` | Weekend foreground |
362
- | `--cal-holiday-fg` | Holiday foreground |
363
- | `--cal-border` | Borders |
364
- | `--cal-header-bg` | Header background |
365
- | `--cal-highlight-bg` | Highlight background |
366
- | `--cal-highlight-fg` | Highlight foreground |
367
- | `--cal-range-bg` | Range background |
364
+ | Variable | Purpose |
365
+ | :----------------------- | :-------------------------------------------------------- |
366
+ | `--cal-bg` | Background |
367
+ | `--cal-fg` | Foreground |
368
+ | `--cal-accent` | Accent (highlight) |
369
+ | `--cal-weekend-fg` | Weekend foreground |
370
+ | `--cal-holiday-fg` | Holiday foreground |
371
+ | `--cal-border` | Borders |
372
+ | `--cal-header-bg` | Header background |
373
+ | `--cal-highlight-bg` | Highlight background |
374
+ | `--cal-highlight-fg` | Highlight foreground |
375
+ | `--cal-range-bg` | Range background |
368
376
  | `--cal-range-preview-bg` | Range preview background (falls back to `--cal-range-bg`) |
369
- | `--cal-today-bg` | Today background |
370
- | `--cal-today-fg` | Today 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) |
377
+ | `--cal-today-bg` | Today background |
378
+ | `--cal-today-fg` | Today foreground |
379
+ | `--cal-selected-bg` | Selected cell background (interactive mode) |
380
+ | `--cal-selected-fg` | Selected cell foreground (interactive mode) |
381
+ | `--cal-disabled-fg` | Disabled cell foreground (interactive mode) |
374
382
 
375
383
  `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.
376
384
 
@@ -393,13 +401,17 @@ Predefined schemes and resolver.
393
401
  ### `CalendarSize`
394
402
 
395
403
  ```ts
396
- type CalendarSize = "sm" | "md" | "lg" | { width?: number | string; height?: number | string };
404
+ type CalendarSize =
405
+ | "sm"
406
+ | "md"
407
+ | "lg"
408
+ | { width?: number | string; height?: number | string };
397
409
  ```
398
410
 
399
- | Size | Cell dimensions |
400
- | :--- | :--- |
401
- | `"sm"` | `1.75rem` × `1.5rem` |
402
- | `"md"` | `2.25rem` × `2rem` |
411
+ | Size | Cell dimensions |
412
+ | :----- | :--------------------- |
413
+ | `"sm"` | `1.75rem` × `1.5rem` |
414
+ | `"md"` | `2.25rem` × `2rem` |
403
415
  | `"lg"` | `3.125rem` × `2.75rem` |
404
416
 
405
417
  Custom objects set the `--cal-cell-w` / `--cal-cell-h` CSS variables (numbers become px, strings pass through):
@@ -436,18 +448,18 @@ It's **off by default** to keep existing layouts unchanged. The calendar root ge
436
448
 
437
449
  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"`:
438
450
 
439
- | Class | When |
440
- | :--- | :--- |
441
- | `is-weekend` | Saturday or Sunday |
442
- | `is-holiday` | Holiday per `holidayLocale` (default `"ja"`) |
443
- | `is-today` | Matches `today` |
444
- | `is-highlight` | Matches `highlight` |
445
- | `is-in-range` | Inside `range` |
446
- | `is-in-range-preview` | Inside `rangePreview` |
447
- | `is-hovered` | Matches `hoveredDate` (interactive mode) |
448
- | `is-disabled` | Date returned `true` from `isDateDisabled` (interactive mode) |
449
- | `is-selected` | Matches `selected` |
450
- | `is-cursor` | Matches `cursorDate` |
451
+ | Class | When |
452
+ | :-------------------- | :------------------------------------------------------------ |
453
+ | `is-weekend` | Saturday or Sunday |
454
+ | `is-holiday` | Holiday per `holidayLocale` (default `"ja"`) |
455
+ | `is-today` | Matches `today` |
456
+ | `is-highlight` | Matches `highlight` |
457
+ | `is-in-range` | Inside `range` |
458
+ | `is-in-range-preview` | Inside `rangePreview` |
459
+ | `is-hovered` | Matches `hoveredDate` (interactive mode) |
460
+ | `is-disabled` | Date returned `true` from `isDateDisabled` (interactive mode) |
461
+ | `is-selected` | Matches `selected` |
462
+ | `is-cursor` | Matches `cursorDate` |
451
463
 
452
464
  ## Exports
453
465
 
@@ -489,4 +501,4 @@ import type {
489
501
 
490
502
  ## License
491
503
 
492
- MIT
504
+ MIT
@@ -9,6 +9,7 @@
9
9
  getMonthName,
10
10
  getWeekOfYear,
11
11
  getWeekdayHeaders,
12
+ getYearMonth,
12
13
  isSameDay,
13
14
  } from "@typescript-calendar-lib/core";
14
15
  import {
@@ -33,8 +34,15 @@
33
34
  import { mergeColorScheme, resolveTheme } from "./themes.js";
34
35
 
35
36
  interface CalendarProps {
36
- year: number;
37
- month: number;
37
+ /**
38
+ * 表示する年月を `Date` で指定する。`year` / `month` の代わりに使える
39
+ * (両方を併用した場合は `date` を優先する)。
40
+ */
41
+ date?: Date;
42
+ /** 表示する年。`date` 省略時は `month` と併せて必須 */
43
+ year?: number;
44
+ /** 表示する月 (1–12)。`date` 省略時は `year` と併せて必須 */
45
+ month?: number;
38
46
  locale?: CalendarOptions["locale"];
39
47
  holidayLocale?: CalendarOptions["holidayLocale"];
40
48
  weekStart?: CalendarOptions["weekStart"];
@@ -61,6 +69,8 @@
61
69
  responsive?: boolean;
62
70
  /** 行の先頭に週番号を表示する。既定は false(非表示) */
63
71
  showWeekNumbers?: boolean;
72
+ /** root 要素に追加するクラス。レイアウトや追加スタイルのフックに使う */
73
+ class?: string;
64
74
  /** root 要素に追加するスタイル。CSS変数(--cal-*)で自由に上書きできる */
65
75
  style?: CSSProperties;
66
76
 
@@ -96,6 +106,7 @@
96
106
  }
97
107
 
98
108
  let {
109
+ date: dateProp,
99
110
  year,
100
111
  month,
101
112
  locale = "en",
@@ -113,6 +124,7 @@
113
124
  size = "md",
114
125
  responsive = false,
115
126
  showWeekNumbers = false,
127
+ class: className,
116
128
  style,
117
129
  interactive = false,
118
130
  onDateClick,
@@ -124,6 +136,16 @@
124
136
  isDateDisabled,
125
137
  }: CalendarProps = $props();
126
138
 
139
+ // date が指定されればそれを優先し、なければ year/month を使う。
140
+ // どちらも揃っていない場合は、黙って現在月を出さずに即座に失敗させる。
141
+ const yearMonth = $derived.by(() => {
142
+ if (dateProp !== undefined) return getYearMonth(dateProp);
143
+ if (year !== undefined && month !== undefined) return { year, month };
144
+ throw new TypeError(
145
+ "Calendar requires either `date` or both `year` and `month`.",
146
+ );
147
+ });
148
+
127
149
  // interactive 時のみ APG の grid セマンティクスを付与する。静的表示では
128
150
  // ネイティブの table/cell セマンティクスをそのまま使う。
129
151
  const gridCellRole = $derived(interactive ? "gridcell" : undefined);
@@ -132,7 +154,8 @@
132
154
  // id が衝突し aria-labelledby が別のカレンダーを見指すため。
133
155
  const headingId = $props.id();
134
156
 
135
- const cellDate = (day: number): Date => createDate(year, month - 1, day);
157
+ const cellDate = (day: number): Date =>
158
+ createDate(yearMonth.year, yearMonth.month - 1, day);
136
159
 
137
160
  /**
138
161
  * 1セルぶんの表示データ(Date・状態・クラス・aria 用の状態)を1回の計算でまとめる。
@@ -185,7 +208,11 @@
185
208
  const weekOf = (row: (number | null)[]): number | null => {
186
209
  const firstIdx = row.findIndex((d) => d !== null);
187
210
  if (firstIdx === -1) return null;
188
- const firstDate = createDate(year, month - 1, row[firstIdx]!);
211
+ const firstDate = createDate(
212
+ yearMonth.year,
213
+ yearMonth.month - 1,
214
+ row[firstIdx]!,
215
+ );
189
216
  const weekStartDate = addDays(firstDate, -firstIdx);
190
217
  return weekStart === "monday"
191
218
  ? getISOWeek(weekStartDate)
@@ -204,7 +231,9 @@
204
231
  };
205
232
 
206
233
  // グリッド・曜日ヘッダーは月単位で不変。$derived なので年月が変わったときだけ再計算される
207
- const grid = $derived(buildMonthGrid(year, month, weekStart));
234
+ const grid = $derived(
235
+ buildMonthGrid(yearMonth.year, yearMonth.month, weekStart),
236
+ );
208
237
  const weekdays = $derived(getWeekdayHeaders(locale, weekStart));
209
238
  // 週番号は表示時のみ計算する。グリッドと同じ月単位の依存
210
239
  const weekNumbers = $derived(showWeekNumbers ? grid.map(weekOf) : null);
@@ -224,6 +253,19 @@
224
253
  ...style,
225
254
  }),
226
255
  );
256
+
257
+ const rootClass = $derived(
258
+ [
259
+ "calendar",
260
+ resolveTheme(theme).className,
261
+ isSizeName(size) ? `calendar-size-${size}` : "",
262
+ interactive ? "calendar-interactive" : "",
263
+ responsive ? "calendar-responsive" : "",
264
+ className ?? "",
265
+ ]
266
+ .filter(Boolean)
267
+ .join(" "),
268
+ );
227
269
  </script>
228
270
 
229
271
  <!-- svelte-ignore a11y_no_static_element_interactions -->
@@ -231,7 +273,7 @@
231
273
  InteractiveCalendar が table 上で扱う。root 自体は widget ではないため
232
274
  ARIA role は持たない。 -->
233
275
  <div
234
- class="calendar {resolveTheme(theme).className}{isSizeName(size) ? ` calendar-size-${size}` : ""}{interactive ? " calendar-interactive" : ""}{responsive ? " calendar-responsive" : ""}"
276
+ class={rootClass}
235
277
  style={rootStyle}
236
278
  onmouseleave={interactive ? handleMouseLeave : undefined}
237
279
  onkeydown={onkeydown}
@@ -247,7 +289,7 @@
247
289
  aria-live={interactive ? "polite" : undefined}
248
290
  aria-atomic={interactive ? "true" : undefined}
249
291
  >
250
- {getMonthName(locale, month)} {year}
292
+ {getMonthName(locale, yearMonth.month)} {yearMonth.year}
251
293
  </h2>
252
294
  </div>
253
295
  <!-- interactive 時のみ APG の grid セマンティクスを付与する。静的表示では
@@ -294,7 +336,12 @@
294
336
  onmouseenter={cell.isDisabled ? undefined : () => handleCellHover(cell)}
295
337
  tabindex={cell.isCursor ? 0 : -1}
296
338
  data-cursor={cell.isCursor ? "true" : undefined}
297
- aria-label={formatCellLabel(locale, year, month, day)}
339
+ aria-label={formatCellLabel(
340
+ locale,
341
+ yearMonth.year,
342
+ yearMonth.month,
343
+ day,
344
+ )}
298
345
  aria-pressed={cell.isSelected}
299
346
  disabled={cell.isDisabled}
300
347
  >
@@ -4,8 +4,15 @@ import type { CSSProperties } from "./style.js";
4
4
  import type { ColorSchemeName, SvelteColorScheme, SvelteTheme, ThemeName } from "./themes.js";
5
5
  import type { Snippet } from "svelte";
6
6
  interface CalendarProps {
7
- year: number;
8
- month: number;
7
+ /**
8
+ * 表示する年月を `Date` で指定する。`year` / `month` の代わりに使える
9
+ * (両方を併用した場合は `date` を優先する)。
10
+ */
11
+ date?: Date;
12
+ /** 表示する年。`date` 省略時は `month` と併せて必須 */
13
+ year?: number;
14
+ /** 表示する月 (1–12)。`date` 省略時は `year` と併せて必須 */
15
+ month?: number;
9
16
  locale?: CalendarOptions["locale"];
10
17
  holidayLocale?: CalendarOptions["holidayLocale"];
11
18
  weekStart?: CalendarOptions["weekStart"];
@@ -38,6 +45,8 @@ interface CalendarProps {
38
45
  responsive?: boolean;
39
46
  /** 行の先頭に週番号を表示する。既定は false(非表示) */
40
47
  showWeekNumbers?: boolean;
48
+ /** root 要素に追加するクラス。レイアウトや追加スタイルのフックに使う */
49
+ class?: string;
41
50
  /** root 要素に追加するスタイル。CSS変数(--cal-*)で自由に上書きできる */
42
51
  style?: CSSProperties;
43
52
  /** インタラクティブモードを有効にする。セルクリック・ホバー・キーボード選択が可能になる */
@@ -1,6 +1,10 @@
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
+ import {
4
+ createDate,
5
+ getYearMonth,
6
+ lastDayOfMonth,
7
+ } from "@typescript-calendar-lib/core";
4
8
  import type { SelectionMode } from "@typescript-calendar-lib/tui";
5
9
  import { keyToAction } from "@typescript-calendar-lib/tui";
6
10
  import type { Snippet } from "svelte";
@@ -23,6 +27,11 @@
23
27
  initialYear?: number;
24
28
  /** 表示開始月 (1-12)。範囲外は正規化される */
25
29
  initialMonth?: number;
30
+ /**
31
+ * 表示開始位置を `Date` で指定する。`initialYear` / `initialMonth` を
32
+ * 個別に指定した場合はそちらが優先される。
33
+ */
34
+ initialDate?: Date;
26
35
  /** 制御モードの表示年。指定すると initialYear より優先され、月移動は onMonthChange に委譲される */
27
36
  year?: number;
28
37
  /** 制御モードの表示月 (1-12) */
@@ -52,6 +61,8 @@
52
61
  size?: CalendarSize;
53
62
  /** 狭い画面(スマホ等)でセルサイズと余白を自動調整する */
54
63
  responsive?: boolean;
64
+ /** root 要素に追加するクラス。レイアウトや追加スタイルのフックに使う */
65
+ class?: string;
55
66
  style?: CSSProperties;
56
67
  /** セル内容のカスタムレンダリング。文字列または Snippet。第4引数に該当日のデータが渡る */
57
68
  renderCell?: (
@@ -76,6 +87,7 @@
76
87
  let {
77
88
  initialYear,
78
89
  initialMonth,
90
+ initialDate,
79
91
  locale,
80
92
  holidayLocale,
81
93
  weekStart,
@@ -92,6 +104,7 @@
92
104
  colorScheme,
93
105
  size,
94
106
  responsive,
107
+ class: className,
95
108
  style,
96
109
  renderCell,
97
110
  cellData,
@@ -100,21 +113,28 @@
100
113
  onDateLeave,
101
114
  }: InteractiveCalendarProps = $props();
102
115
 
103
- const cal = useCalendarState(() => ({
104
- initialYear,
105
- initialMonth,
106
- year,
107
- month,
108
- locale,
109
- holidayLocale,
110
- weekStart,
111
- today,
112
- highlight,
113
- range,
114
- isDateDisabled,
115
- selectionMode,
116
- onMonthChange,
117
- }));
116
+ // initialDate は initialYear / initialMonth のショートハンド。
117
+ // 個別指定があればそちらを優先する。getter 内で解決するのは、
118
+ // 値の変化を追跡しつつ props の初期値キャプチャ警告を避けるため。
119
+ const cal = useCalendarState(() => {
120
+ const initialFromDate =
121
+ initialDate !== undefined ? getYearMonth(initialDate) : undefined;
122
+ return {
123
+ initialYear: initialYear ?? initialFromDate?.year,
124
+ initialMonth: initialMonth ?? initialFromDate?.month,
125
+ year,
126
+ month,
127
+ locale,
128
+ holidayLocale,
129
+ weekStart,
130
+ today,
131
+ highlight,
132
+ range,
133
+ isDateDisabled,
134
+ selectionMode,
135
+ onMonthChange,
136
+ };
137
+ });
118
138
 
119
139
  // クリック(マウス・Enter/Space 共通)で日付を選択する。
120
140
  // 従来の onDateClick コールバックも引き続き発火し、第2引数に該当日のデータが渡る。
@@ -198,6 +218,7 @@
198
218
  colorScheme={colorScheme}
199
219
  size={size}
200
220
  responsive={responsive}
221
+ class={className}
201
222
  style={style}
202
223
  interactive
203
224
  renderCell={renderCell}
@@ -9,6 +9,11 @@ interface InteractiveCalendarProps {
9
9
  initialYear?: number;
10
10
  /** 表示開始月 (1-12)。範囲外は正規化される */
11
11
  initialMonth?: number;
12
+ /**
13
+ * 表示開始位置を `Date` で指定する。`initialYear` / `initialMonth` を
14
+ * 個別に指定した場合はそちらが優先される。
15
+ */
16
+ initialDate?: Date;
12
17
  /** 制御モードの表示年。指定すると initialYear より優先され、月移動は onMonthChange に委譲される */
13
18
  year?: number;
14
19
  /** 制御モードの表示月 (1-12) */
@@ -38,6 +43,8 @@ interface InteractiveCalendarProps {
38
43
  size?: CalendarSize;
39
44
  /** 狭い画面(スマホ等)でセルサイズと余白を自動調整する */
40
45
  responsive?: boolean;
46
+ /** root 要素に追加するクラス。レイアウトや追加スタイルのフックに使う */
47
+ class?: string;
41
48
  style?: CSSProperties;
42
49
  /** セル内容のカスタムレンダリング。文字列または Snippet。第4引数に該当日のデータが渡る */
43
50
  renderCell?: (day: number, date: Date, state: CalendarCellState, data?: unknown) => string | Snippet;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@typescript-calendar-lib/svelte",
3
- "version": "0.6.1",
3
+ "version": "0.6.2",
4
4
  "description": "Svelte 5 components and reactive state for typescript-calendar-lib",
5
5
  "type": "module",
6
6
  "repository": {
@@ -34,19 +34,19 @@
34
34
  "svelte": "^5"
35
35
  },
36
36
  "dependencies": {
37
- "@typescript-calendar-lib/core": "^0.6.1",
38
- "@typescript-calendar-lib/tui": "^0.6.1",
39
- "@typescript-calendar-lib/web": "^0.6.1"
37
+ "@typescript-calendar-lib/core": "^0.6.2",
38
+ "@typescript-calendar-lib/tui": "^0.6.2",
39
+ "@typescript-calendar-lib/web": "^0.6.2"
40
40
  },
41
41
  "devDependencies": {
42
- "@sveltejs/package": "^2.5.8",
42
+ "@sveltejs/package": "^3.0.2",
43
43
  "@sveltejs/vite-plugin-svelte": "^7.3.1",
44
44
  "@testing-library/jest-dom": "^7.0.1",
45
45
  "@testing-library/svelte": "^5",
46
- "jsdom": "^30",
46
+ "jsdom": "^31",
47
47
  "svelte-check": "^4",
48
48
  "typescript": "^5.9.0",
49
- "vitest": "^5"
49
+ "vitest": "^5.1.0"
50
50
  },
51
51
  "author": "nazozokc",
52
52
  "homepage": "https://github.com/nazozokc/typescript-calendar-lib#readme",