@typescript-calendar-lib/svelte 0.3.0 → 0.4.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
@@ -11,6 +11,8 @@ A `<Calendar />` Svelte 5 component with CSS-based themes and color schemes, plu
11
11
  ## Installation
12
12
 
13
13
  ```sh
14
+ pnpm add @typescript-calendar-lib/svelte
15
+ # or
14
16
  npm install @typescript-calendar-lib/svelte
15
17
  # or
16
18
  bun add @typescript-calendar-lib/svelte
@@ -44,6 +46,8 @@ bun add @typescript-calendar-lib/svelte
44
46
  | `highlight` | `Date` | — | Date to highlight |
45
47
  | `range` | `{ from: Date; to: Date }` | — | Dates to emphasize |
46
48
  | `today` | `Date` | — | Reference date for "today" styling |
49
+ | `selected` | `Date \| null` | — | Selected date (`.is-selected` class, `aria-pressed` in interactive mode) |
50
+ | `cursorDate` | `Date \| null` | — | Cursor position (`.is-cursor` class) |
47
51
  | `theme` | `ThemeName \| SvelteTheme` | `"default"` | CSS class-based theme |
48
52
  | `colorScheme` | `ColorSchemeName \| SvelteColorScheme` | `"default"` | CSS variable-based colors |
49
53
  | `size` | `CalendarSize` | `"md"` | Cell size |
@@ -56,13 +60,15 @@ The component exports as both named `Calendar` and default.
56
60
 
57
61
  ## Interactive Mode
58
62
 
59
- Set `interactive` to make day cells clickable. Each cell becomes a `<button class="calendar-day-btn">` — clickable, hoverable, and keyboard-accessible (Enter / Space):
63
+ 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"`:
60
64
 
61
65
  ```svelte
62
66
  <Calendar
63
67
  year={2026}
64
68
  month={9}
65
69
  interactive
70
+ selected={cal.selectedDate}
71
+ cursorDate={cal.cursorDate}
66
72
  onDateClick={(date) => console.log("Selected", date)}
67
73
  onDateHover={(date) => console.log("Hovered", date)}
68
74
  />
@@ -90,7 +96,13 @@ For full interactivity (cursor movement, month navigation, selection), use the `
90
96
  > </script>
91
97
  >
92
98
  > <button onclick={cal.goPrev}>‹</button>
93
- > <Calendar year={cal.state.year} month={cal.state.month} interactive />
99
+ > <Calendar
100
+ > year={cal.state.year}
101
+ > month={cal.state.month}
102
+ > selected={cal.selectedDate}
103
+ > cursorDate={cal.cursorDate}
104
+ > interactive
105
+ > />
94
106
  > <button onclick={cal.goNext}>›</button>
95
107
  > ```
96
108
 
@@ -175,6 +187,8 @@ Available variables:
175
187
  | `--cal-range-bg` | Range background |
176
188
  | `--cal-today-bg` | Today background |
177
189
  | `--cal-today-fg` | Today foreground |
190
+ | `--cal-selected-bg` | Selected background |
191
+ | `--cal-selected-fg` | Selected foreground |
178
192
 
179
193
  Pass a custom scheme directly:
180
194
 
@@ -5,6 +5,7 @@
5
5
  createDate,
6
6
  getMonthName,
7
7
  getWeekdayHeaders,
8
+ isSameDay,
8
9
  } from "@typescript-calendar-lib/core";
9
10
  import { getCellClasses } from "./cell-classes.js";
10
11
  import type { CalendarSize } from "./size.js";
@@ -29,6 +30,10 @@
29
30
  range?: { from: Date; to: Date };
30
31
  /** 今日の基準日。カラースキームの today 着色に使用 */
31
32
  today?: Date;
33
+ /** 選択済み日付。該当セルに is-selected クラスと aria-selected が付く */
34
+ selected?: Date | null;
35
+ /** カーソル位置の日付。該当セルに is-cursor クラスが付く */
36
+ cursorDate?: Date | null;
32
37
  /** 見た目テーマ。既定は "default" */
33
38
  theme?: ThemeName | SvelteTheme;
34
39
  /** カラースキーム。既定は "default" */
@@ -56,6 +61,8 @@
56
61
  highlight,
57
62
  range,
58
63
  today,
64
+ selected,
65
+ cursorDate,
59
66
  theme = "default",
60
67
  colorScheme = "default",
61
68
  size = "md",
@@ -67,7 +74,17 @@
67
74
 
68
75
  const cellDate = (day: number): Date => createDate(year, month - 1, day);
69
76
  const cellClass = (day: number): string | undefined =>
70
- getCellClasses(cellDate(day), { today, highlight, range }) || undefined;
77
+ getCellClasses(cellDate(day), {
78
+ today,
79
+ highlight,
80
+ range,
81
+ selected,
82
+ cursorDate,
83
+ }) || undefined;
84
+ const isSelectedDay = (day: number): boolean =>
85
+ selected != null && isSameDay(cellDate(day), selected);
86
+ const isTodayDay = (day: number): boolean =>
87
+ today != null && isSameDay(cellDate(day), today);
71
88
  const handleCellClick = (day: number) => {
72
89
  if (interactive && onDateClick) onDateClick(cellDate(day));
73
90
  };
@@ -117,6 +134,8 @@
117
134
  onmouseenter={() => handleCellHover(day)}
118
135
  tabindex="0"
119
136
  aria-label={`${getMonthName(locale, month)} ${day}, ${year}`}
137
+ aria-pressed={isSelectedDay(day) || undefined}
138
+ aria-current={isTodayDay(day) ? "date" : undefined}
120
139
  >
121
140
  {day}
122
141
  </button>
@@ -15,6 +15,10 @@ interface CalendarProps {
15
15
  };
16
16
  /** 今日の基準日。カラースキームの today 着色に使用 */
17
17
  today?: Date;
18
+ /** 選択済み日付。該当セルに is-selected クラスと aria-selected が付く */
19
+ selected?: Date | null;
20
+ /** カーソル位置の日付。該当セルに is-cursor クラスが付く */
21
+ cursorDate?: Date | null;
18
22
  /** 見た目テーマ。既定は "default" */
19
23
  theme?: ThemeName | SvelteTheme;
20
24
  /** カラースキーム。既定は "default" */
package/dist/calendar.css CHANGED
@@ -12,6 +12,8 @@
12
12
  --cal-range-bg: #fef3c7;
13
13
  --cal-today-bg: #fff7ed;
14
14
  --cal-today-fg: #c2410c;
15
+ --cal-selected-bg: #dc2626;
16
+ --cal-selected-fg: #ffffff;
15
17
 
16
18
  /* セルサイズ(size propや style の CSS 変数で上書きできる) */
17
19
  --cal-cell-w: 2.25rem;
@@ -80,6 +82,17 @@
80
82
  background: var(--cal-range-bg);
81
83
  }
82
84
 
85
+ .calendar td.is-selected {
86
+ background: var(--cal-selected-bg);
87
+ color: var(--cal-selected-fg);
88
+ font-weight: 700;
89
+ }
90
+
91
+ .calendar td.is-cursor {
92
+ outline: 2px solid var(--cal-accent);
93
+ outline-offset: -2px;
94
+ }
95
+
83
96
  /* ─── Size ────────────────────────────────────────────── */
84
97
 
85
98
  .calendar-size-sm {
@@ -207,7 +220,8 @@
207
220
 
208
221
  .calendar-theme-rounded td.is-today,
209
222
  .calendar-theme-rounded td.is-highlight,
210
- .calendar-theme-rounded td.is-in-range {
223
+ .calendar-theme-rounded td.is-in-range,
224
+ .calendar-theme-rounded td.is-selected {
211
225
  border-radius: 999px;
212
226
  }
213
227
 
@@ -249,6 +263,10 @@
249
263
  background: var(--cal-range-bg);
250
264
  }
251
265
 
266
+ .calendar-interactive td.is-selected button.calendar-day-btn:hover {
267
+ background: var(--cal-selected-bg);
268
+ }
269
+
252
270
  .calendar-interactive td button.calendar-day-btn:focus-visible {
253
271
  outline: 2px solid var(--cal-accent);
254
272
  outline-offset: -4px;
@@ -5,6 +5,10 @@ export interface CellStateOptions {
5
5
  from: Date;
6
6
  to: Date;
7
7
  };
8
+ /** 選択済み日付。該当セルに is-selected クラスが付く */
9
+ selected?: Date | null;
10
+ /** カーソル位置の日付。該当セルに is-cursor クラスが付く */
11
+ cursorDate?: Date | null;
8
12
  }
9
- /** 日付セルの状態(週末・今日・ハイライト・範囲)に応じたCSSクラスを組み立てる */
13
+ /** 日付セルの状態(週末・今日・ハイライト・範囲・選択・カーソル)に応じたCSSクラスを組み立てる */
10
14
  export declare function getCellClasses(date: Date, options: CellStateOptions): string;
@@ -1,16 +1,20 @@
1
- import { isDateInRange, isSameDay } from "@typescript-calendar-lib/core";
1
+ import { getCalendarCellState, isSameDay } from "@typescript-calendar-lib/core";
2
2
  // 範囲の from > to は不正入力として RangeError(isDateInRange が検証する)
3
- /** 日付セルの状態(週末・今日・ハイライト・範囲)に応じたCSSクラスを組み立てる */
3
+ /** 日付セルの状態(週末・今日・ハイライト・範囲・選択・カーソル)に応じたCSSクラスを組み立てる */
4
4
  export function getCellClasses(date, options) {
5
+ const state = getCalendarCellState(date, options);
5
6
  const classes = [];
6
- const dow = date.getDay();
7
- if (dow === 0 || dow === 6)
7
+ if (state.isWeekend)
8
8
  classes.push("is-weekend");
9
- if (options.today && isSameDay(date, options.today))
9
+ if (state.isToday)
10
10
  classes.push("is-today");
11
- if (options.highlight && isSameDay(date, options.highlight))
11
+ if (state.isHighlight)
12
12
  classes.push("is-highlight");
13
- if (options.range && isDateInRange(date, options.range))
13
+ if (state.isInRange)
14
14
  classes.push("is-in-range");
15
+ if (options.selected != null && isSameDay(date, options.selected))
16
+ classes.push("is-selected");
17
+ if (options.cursorDate != null && isSameDay(date, options.cursorDate))
18
+ classes.push("is-cursor");
15
19
  return classes.join(" ");
16
20
  }
package/dist/themes.js CHANGED
@@ -26,6 +26,8 @@ const SCHEME_KEYS = [
26
26
  "--cal-range-bg",
27
27
  "--cal-today-bg",
28
28
  "--cal-today-fg",
29
+ "--cal-selected-bg",
30
+ "--cal-selected-fg",
29
31
  ];
30
32
  /** カラースキームごとの値を SCHEME_KEYS の並びで定義する */
31
33
  const SCHEME_VALUES = {
@@ -41,6 +43,8 @@ const SCHEME_VALUES = {
41
43
  "#fef3c7",
42
44
  "#fff7ed",
43
45
  "#c2410c",
46
+ "#dc2626",
47
+ "#ffffff",
44
48
  ],
45
49
  ocean: [
46
50
  "#f0f9ff",
@@ -54,6 +58,8 @@ const SCHEME_VALUES = {
54
58
  "#dff3fe",
55
59
  "#cffafe",
56
60
  "#155e75",
61
+ "#0e7490",
62
+ "#ffffff",
57
63
  ],
58
64
  forest: [
59
65
  "#f0fdf4",
@@ -67,6 +73,8 @@ const SCHEME_VALUES = {
67
73
  "#dcfce7",
68
74
  "#f0fdf4",
69
75
  "#166534",
76
+ "#15803d",
77
+ "#ffffff",
70
78
  ],
71
79
  sunset: [
72
80
  "#fffaf5",
@@ -80,6 +88,8 @@ const SCHEME_VALUES = {
80
88
  "#ffedd5",
81
89
  "#fff7ed",
82
90
  "#9a3412",
91
+ "#ea580c",
92
+ "#ffffff",
83
93
  ],
84
94
  mono: [
85
95
  "#ffffff",
@@ -93,6 +103,8 @@ const SCHEME_VALUES = {
93
103
  "#f3f4f6",
94
104
  "#e5e7eb",
95
105
  "#111827",
106
+ "#111827",
107
+ "#ffffff",
96
108
  ],
97
109
  midnight: [
98
110
  "#0f172a",
@@ -106,6 +118,8 @@ const SCHEME_VALUES = {
106
118
  "#0c4a6e",
107
119
  "#0c4a6e",
108
120
  "#7dd3fc",
121
+ "#38bdf8",
122
+ "#0f172a",
109
123
  ],
110
124
  blossom: [
111
125
  "#fffaff",
@@ -119,6 +133,8 @@ const SCHEME_VALUES = {
119
133
  "#fce7f3",
120
134
  "#fdf2f8",
121
135
  "#be185d",
136
+ "#db2777",
137
+ "#ffffff",
122
138
  ],
123
139
  };
124
140
  export const COLOR_SCHEMES = Object.fromEntries(Object.entries(SCHEME_VALUES).map(([name, values]) => [
@@ -30,6 +30,7 @@ export interface UseCalendarStateReturn {
30
30
  /** 選択済み日付(null の場合あり) */
31
31
  selectedDate: Date | null;
32
32
  }
33
+ /** undefined を含む日付を値(時刻)で比較する */
33
34
  /**
34
35
  * tui の不変状態機械を包む Svelte 5 (runes) のリアクティブ状態。
35
36
  * カレンダーのインタラクティブ操作をシンプルに利用できる。
@@ -1,19 +1,5 @@
1
- import { buildMonthData, clearSelection, createCalendarState, getCursorDate, getSelectedDate, goToToday, moveCursor, navigateMonth, rebuildState, resolveOptions, selectDate, } from "@typescript-calendar-lib/tui";
1
+ import { clearSelection, createCalendarState, getCursorDate, getSelectedDate, goToToday, moveCursor, navigateMonth, sameStateOptions, selectDate, updateStateOptions, } from "@typescript-calendar-lib/tui";
2
2
  /** undefined を含む日付を値(時刻)で比較する */
3
- function sameDateValue(a, b) {
4
- return (a?.getTime() ?? -1) === (b?.getTime() ?? -1);
5
- }
6
- /** オプションの「値」が等しいか比較する(Date は getTime、参照ではない) */
7
- function sameOptionsValue(a, b) {
8
- return (a.initialYear === b.initialYear &&
9
- a.initialMonth === b.initialMonth &&
10
- a.locale === b.locale &&
11
- a.weekStart === b.weekStart &&
12
- sameDateValue(a.today, b.today) &&
13
- sameDateValue(a.highlight, b.highlight) &&
14
- sameDateValue(a.range?.from, b.range?.from) &&
15
- sameDateValue(a.range?.to, b.range?.to));
16
- }
17
3
  /**
18
4
  * tui の不変状態機械を包む Svelte 5 (runes) のリアクティブ状態。
19
5
  * カレンダーのインタラクティブ操作をシンプルに利用できる。
@@ -58,30 +44,11 @@ export function useCalendarState(options = {}) {
58
44
  let prev = getter();
59
45
  $effect(() => {
60
46
  const next = getter();
61
- if (sameOptionsValue(prev, next))
47
+ if (sameStateOptions(prev, next))
62
48
  return;
63
- // initialYear/initialMonth は「初期値」。値が実際に変わった時だけ適用し、
64
- // 変わっていない場合はナビゲーション後の現在位置を維持する。
65
- const prevInitialYear = prev.initialYear;
66
- const prevInitialMonth = prev.initialMonth;
49
+ const previous = prev;
67
50
  prev = next;
68
- const resolved = resolveOptions({
69
- // today が省略されたら前回の値を引き継ぐ(状態に固定された今日を維持)
70
- today: next.today ?? state.options.today,
71
- locale: next.locale ?? state.options.locale,
72
- weekStart: next.weekStart ?? state.options.weekStart,
73
- highlight: next.highlight,
74
- range: next.range,
75
- });
76
- const year = next.initialYear !== undefined && next.initialYear !== prevInitialYear
77
- ? next.initialYear
78
- : state.year;
79
- const month = next.initialMonth !== undefined && next.initialMonth !== prevInitialMonth
80
- ? next.initialMonth
81
- : state.month;
82
- // 範囲外の month は rebuildState 側で正規化される(state と monthData の乖離を防ぐ)
83
- const monthData = buildMonthData(year, month, resolved);
84
- state = rebuildState(year, month, state.cursor, state.selectedDate, resolved, monthData);
51
+ state = updateStateOptions(state, next, previous);
85
52
  });
86
53
  return {
87
54
  get state() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@typescript-calendar-lib/svelte",
3
- "version": "0.3.0",
3
+ "version": "0.4.2",
4
4
  "description": "Svelte 5 components and reactive state for typescript-calendar-lib",
5
5
  "type": "module",
6
6
  "repository": {
@@ -33,12 +33,12 @@
33
33
  "svelte": "^5"
34
34
  },
35
35
  "dependencies": {
36
- "@typescript-calendar-lib/core": "0.3.0",
37
- "@typescript-calendar-lib/tui": "0.3.0"
36
+ "@typescript-calendar-lib/core": "0.4.2",
37
+ "@typescript-calendar-lib/tui": "0.4.2"
38
38
  },
39
39
  "devDependencies": {
40
40
  "@sveltejs/package": "^2.5.8",
41
- "@sveltejs/vite-plugin-svelte": "^6",
41
+ "@sveltejs/vite-plugin-svelte": "^7.3.0",
42
42
  "@testing-library/jest-dom": "^7.0.1",
43
43
  "@testing-library/svelte": "^5",
44
44
  "jsdom": "^26",