@typescript-calendar-lib/svelte 0.4.1 → 0.5.0
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 +112 -1
- package/dist/Calendar.svelte +70 -11
- package/dist/Calendar.svelte.d.ts +22 -3
- package/dist/InteractiveCalendar.svelte +172 -0
- package/dist/InteractiveCalendar.svelte.d.ts +42 -0
- package/dist/calendar.css +97 -74
- package/dist/cell-classes.d.ts +9 -0
- package/dist/cell-classes.js +8 -1
- package/dist/index.d.ts +3 -1
- package/dist/index.js +3 -1
- package/dist/range-preview.d.ts +1 -0
- package/dist/range-preview.js +4 -0
- package/dist/size.d.ts +2 -13
- package/dist/size.js +3 -17
- package/dist/themes.d.ts +6 -14
- package/dist/themes.js +3 -150
- package/dist/useCalendarState.svelte.d.ts +15 -1
- package/dist/useCalendarState.svelte.js +31 -10
- package/package.json +8 -6
package/README.md
CHANGED
|
@@ -45,19 +45,78 @@ bun add @typescript-calendar-lib/svelte
|
|
|
45
45
|
| `weekStart` | `"sunday" \| "monday"` | `"sunday"` | First day of the week |
|
|
46
46
|
| `highlight` | `Date` | — | Date to highlight |
|
|
47
47
|
| `range` | `{ from: Date; to: Date }` | — | Dates to emphasize |
|
|
48
|
+
| `rangePreview` | `{ from: Date; to: Date }` | — | Candidate range preview (hover preview), rendered with the `is-in-range-preview` class |
|
|
48
49
|
| `today` | `Date` | — | Reference date for "today" styling |
|
|
49
50
|
| `selected` | `Date \| null` | — | Selected date (`.is-selected` class, `aria-pressed` in interactive mode) |
|
|
50
51
|
| `cursorDate` | `Date \| null` | — | Cursor position (`.is-cursor` class) |
|
|
52
|
+
| `hoveredDate` | `Date \| null` | — | Hovered date (`.is-hovered` class) |
|
|
51
53
|
| `theme` | `ThemeName \| SvelteTheme` | `"default"` | CSS class-based theme |
|
|
52
54
|
| `colorScheme` | `ColorSchemeName \| SvelteColorScheme` | `"default"` | CSS variable-based colors |
|
|
53
55
|
| `size` | `CalendarSize` | `"md"` | Cell size |
|
|
56
|
+
| `responsive` | `boolean` | `false` | Shrink cells & padding below `480px` so the grid fits phone screens |
|
|
54
57
|
| `style` | `CSSProperties` | — | Extra styles for the root element |
|
|
55
58
|
| `interactive` | `boolean` | `false` | Enable cell click/hover/keyboard selection |
|
|
56
|
-
| `onDateClick` | `(date: Date) => void` | — | Called when a day cell is clicked (or Enter/Space pressed) |
|
|
59
|
+
| `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) |
|
|
57
60
|
| `onDateHover` | `(date: Date) => void` | — | Called when a day cell is hovered |
|
|
61
|
+
| `onDateLeave` | `() => void` | — | Called when the mouse leaves the calendar |
|
|
62
|
+
| `cellData` | `(date: Date) => unknown` | — | Resolve per-cell data; passed to `renderCell` and `onDateClick` |
|
|
63
|
+
| `renderCell` | `(day, date, state, data?) => string` | — | Custom cell content; the returned string is inserted as plain text (XSS-safe) |
|
|
58
64
|
|
|
59
65
|
The component exports as both named `Calendar` and default.
|
|
60
66
|
|
|
67
|
+
## InteractiveCalendar
|
|
68
|
+
|
|
69
|
+
A turnkey component that wires `useCalendarState` and `Calendar` together with built-in mouse and keyboard handling. No manual event wiring required:
|
|
70
|
+
|
|
71
|
+
```svelte
|
|
72
|
+
<script lang="ts">
|
|
73
|
+
import InteractiveCalendar from "@typescript-calendar-lib/svelte";
|
|
74
|
+
import "@typescript-calendar-lib/svelte/calendar.css";
|
|
75
|
+
</script>
|
|
76
|
+
|
|
77
|
+
<InteractiveCalendar
|
|
78
|
+
initialYear={2026}
|
|
79
|
+
initialMonth={9}
|
|
80
|
+
theme="modern"
|
|
81
|
+
colorScheme="ocean"
|
|
82
|
+
onDateClick={(date, data) => console.log("Selected", date, data)}
|
|
83
|
+
onDateLeave={() => console.log("Mouse left")}
|
|
84
|
+
/>
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
### Behavior
|
|
88
|
+
|
|
89
|
+
- **Click** — selects the date and moves the cursor there (Enter/Space via keyboard behaves identically)
|
|
90
|
+
- **Hover** — adds `is-hovered` class to the hovered cell
|
|
91
|
+
- **Mouse leave** — clears the hover state
|
|
92
|
+
- **Selected + hover** — shows `is-in-range-preview` between the selected and hovered dates (reversed hover is sorted correctly)
|
|
93
|
+
- **Arrow keys** — moves the cursor with focus tracking
|
|
94
|
+
- **PageUp/PageDown** — navigates to the previous/next month
|
|
95
|
+
|
|
96
|
+
### Props
|
|
97
|
+
|
|
98
|
+
Combines `useCalendarState` options, `Calendar` visual props, and event callbacks:
|
|
99
|
+
|
|
100
|
+
| Prop | Type | Description |
|
|
101
|
+
| :--- | :--- | :--- |
|
|
102
|
+
| `initialYear` | `number` | Starting year (defaults to today's year) |
|
|
103
|
+
| `initialMonth` | `number` | Starting month `1`–`12` (normalized if out of range) |
|
|
104
|
+
| `locale` | `Locale` | Language |
|
|
105
|
+
| `weekStart` | `WeekStart` | First day of the week |
|
|
106
|
+
| `highlight` | `Date` | Date to highlight |
|
|
107
|
+
| `range` | `{ from: Date; to: Date }` | Dates to emphasize |
|
|
108
|
+
| `today` | `Date` | Reference date for "today" styling |
|
|
109
|
+
| `theme` | `ThemeName \| SvelteTheme` | CSS class-based theme |
|
|
110
|
+
| `colorScheme` | `ColorSchemeName \| SvelteColorScheme` | CSS variable-based colors |
|
|
111
|
+
| `size` | `CalendarSize` | Cell size |
|
|
112
|
+
| `responsive` | `boolean` | Shrink cells & padding below `480px` so the grid fits phone screens |
|
|
113
|
+
| `style` | `CSSProperties` | Extra styles for the root element |
|
|
114
|
+
| `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) |
|
|
116
|
+
| `onDateClick` | `(date: Date, data?: unknown) => void` | Called when a date is selected; `data` is the cell's `cellData` value |
|
|
117
|
+
| `onDateHover` | `(date: Date) => void` | Called on cell hover |
|
|
118
|
+
| `onDateLeave` | `() => void` | Called when the mouse leaves the calendar |
|
|
119
|
+
|
|
61
120
|
## Interactive Mode
|
|
62
121
|
|
|
63
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"`:
|
|
@@ -74,6 +133,26 @@ Set `interactive` to make day cells clickable. Each cell becomes a `<button clas
|
|
|
74
133
|
/>
|
|
75
134
|
```
|
|
76
135
|
|
|
136
|
+
## Per-cell data & custom cells
|
|
137
|
+
|
|
138
|
+
`cellData` attaches your own data to specific dates; consume it in `renderCell` (4th argument) or `onDateClick` (2nd argument). `cellData` is called once per real day cell (never for empty cells), and returning `undefined` means "no data":
|
|
139
|
+
|
|
140
|
+
```svelte
|
|
141
|
+
<Calendar
|
|
142
|
+
year={2026}
|
|
143
|
+
month={9}
|
|
144
|
+
interactive
|
|
145
|
+
cellData={(date) => (date.getDate() === 15 ? "meeting" : undefined)}
|
|
146
|
+
renderCell={(day, date, state, data) =>
|
|
147
|
+
data === undefined ? String(day) : `${day} (${data})`}
|
|
148
|
+
onDateClick={(date, data) => console.log(date, data)} // data: "meeting" | undefined
|
|
149
|
+
/>
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
`renderCell` receives `(day, date, state, data)` — `state` is the core `CalendarCellState` (`isWeekend`, `isToday`, …). The returned string is inserted **as plain text**, never as HTML, so it's XSS-safe.
|
|
153
|
+
|
|
154
|
+
`cellData`/`renderCell` are render-layer options — they are **not** part of `useCalendarState` (`Calendar` re-renders from its props, so hook-level cell data would have no visible effect).
|
|
155
|
+
|
|
77
156
|
## `useCalendarState` hook
|
|
78
157
|
|
|
79
158
|
For full interactivity (cursor movement, month navigation, selection), use the `useCalendarState` hook. It wraps the `@typescript-calendar-lib/tui` state machine as Svelte 5 runes state.
|
|
@@ -116,9 +195,13 @@ For full interactivity (cursor movement, month navigation, selection), use the `
|
|
|
116
195
|
| `goToday` | `() => void` | Jump to today's month |
|
|
117
196
|
| `moveCursor` | `(direction: Direction) => void` | Move cursor: `"up"` / `"down"` / `"left"` / `"right"` |
|
|
118
197
|
| `selectDate` | `() => void` | Select the date under the cursor |
|
|
198
|
+
| `selectDateAt` | `(date: Date) => void` | Move cursor to a date and select it (useful for mouse click) |
|
|
199
|
+
| `setCursorToDate` | `(date: Date) => void` | Move the cursor to a specific date (no-op if outside current month) |
|
|
119
200
|
| `clearSelection` | `() => void` | Clear the selection |
|
|
120
201
|
| `cursorDate` | `Date \| null` | Date under the cursor |
|
|
121
202
|
| `selectedDate` | `Date \| null` | Currently selected date |
|
|
203
|
+
| `hoveredDate` | `Date \| null` | Currently hovered date |
|
|
204
|
+
| `setHoveredDate` | `(date: Date \| null) => void` | Set or clear the hovered date |
|
|
122
205
|
|
|
123
206
|
## Themes
|
|
124
207
|
|
|
@@ -185,6 +268,7 @@ Available variables:
|
|
|
185
268
|
| `--cal-highlight-bg` | Highlight background |
|
|
186
269
|
| `--cal-highlight-fg` | Highlight foreground |
|
|
187
270
|
| `--cal-range-bg` | Range background |
|
|
271
|
+
| `--cal-range-preview-bg` | Range preview background (falls back to `--cal-range-bg`) |
|
|
188
272
|
| `--cal-today-bg` | Today background |
|
|
189
273
|
| `--cal-today-fg` | Today foreground |
|
|
190
274
|
| `--cal-selected-bg` | Selected background |
|
|
@@ -238,14 +322,41 @@ The stylesheet must be imported once in your application:
|
|
|
238
322
|
</script>
|
|
239
323
|
```
|
|
240
324
|
|
|
325
|
+
## Responsive
|
|
326
|
+
|
|
327
|
+
The 7-column grid has fixed cell sizes, so on narrow phone screens it can overflow. Pass `responsive` to make the calendar shrink its cells, fonts, and padding below `480px` (and a step tighter below `360px`) and fill the viewport width:
|
|
328
|
+
|
|
329
|
+
```svelte
|
|
330
|
+
<Calendar year={2026} month={9} responsive />
|
|
331
|
+
```
|
|
332
|
+
|
|
333
|
+
It's **off by default** to keep existing layouts unchanged. The calendar root gets a `calendar-responsive` class that drives CSS media queries — you can also add that class yourself, or use the `style` prop / your own CSS to tune the breakpoints further.
|
|
334
|
+
|
|
335
|
+
## Cell Classes
|
|
336
|
+
|
|
337
|
+
Each day cell gets semantic classes you can target with CSS:
|
|
338
|
+
|
|
339
|
+
| Class | When |
|
|
340
|
+
| :--- | :--- |
|
|
341
|
+
| `is-weekend` | Saturday or Sunday |
|
|
342
|
+
| `is-today` | Matches `today` |
|
|
343
|
+
| `is-highlight` | Matches `highlight` |
|
|
344
|
+
| `is-in-range` | Inside `range` |
|
|
345
|
+
| `is-in-range-preview` | Inside `rangePreview` |
|
|
346
|
+
| `is-hovered` | Matches `hoveredDate` (interactive mode) |
|
|
347
|
+
| `is-selected` | Matches `selected` |
|
|
348
|
+
| `is-cursor` | Matches `cursorDate` |
|
|
349
|
+
|
|
241
350
|
## Exports
|
|
242
351
|
|
|
243
352
|
```ts
|
|
244
353
|
import Calendar, {
|
|
245
354
|
Calendar,
|
|
355
|
+
buildRangePreview,
|
|
246
356
|
buildSizeStyle,
|
|
247
357
|
COLOR_SCHEMES,
|
|
248
358
|
getCellClasses,
|
|
359
|
+
InteractiveCalendar,
|
|
249
360
|
isSizeName,
|
|
250
361
|
resolveColorScheme,
|
|
251
362
|
resolveTheme,
|
package/dist/Calendar.svelte
CHANGED
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
<script lang="ts">
|
|
2
|
-
import type { CalendarOptions } from "@typescript-calendar-lib/core";
|
|
2
|
+
import type { CalendarCellState, CalendarOptions } from "@typescript-calendar-lib/core";
|
|
3
3
|
import {
|
|
4
4
|
buildMonthGrid,
|
|
5
5
|
createDate,
|
|
6
|
+
getCalendarCellState,
|
|
6
7
|
getMonthName,
|
|
7
8
|
getWeekdayHeaders,
|
|
8
9
|
isSameDay,
|
|
9
10
|
} from "@typescript-calendar-lib/core";
|
|
11
|
+
import { formatCellLabel } from "@typescript-calendar-lib/web";
|
|
10
12
|
import { getCellClasses } from "./cell-classes.js";
|
|
11
13
|
import type { CalendarSize } from "./size.js";
|
|
12
14
|
import { buildSizeStyle, isSizeName } from "./size.js";
|
|
@@ -28,18 +30,24 @@
|
|
|
28
30
|
highlight?: Date;
|
|
29
31
|
/** 範囲強調 */
|
|
30
32
|
range?: { from: Date; to: Date };
|
|
33
|
+
/** 範囲プレビュー(ホバー等の候補範囲)。is-in-range-preview クラスで視覚化される */
|
|
34
|
+
rangePreview?: { from: Date; to: Date };
|
|
31
35
|
/** 今日の基準日。カラースキームの today 着色に使用 */
|
|
32
36
|
today?: Date;
|
|
33
37
|
/** 選択済み日付。該当セルに is-selected クラスと aria-selected が付く */
|
|
34
38
|
selected?: Date | null;
|
|
35
39
|
/** カーソル位置の日付。該当セルに is-cursor クラスが付く */
|
|
36
40
|
cursorDate?: Date | null;
|
|
41
|
+
/** ホバー中の日付。該当セルに is-hovered クラスが付く */
|
|
42
|
+
hoveredDate?: Date | null;
|
|
37
43
|
/** 見た目テーマ。既定は "default" */
|
|
38
44
|
theme?: ThemeName | SvelteTheme;
|
|
39
45
|
/** カラースキーム。既定は "default" */
|
|
40
46
|
colorScheme?: ColorSchemeName | SvelteColorScheme;
|
|
41
47
|
/** セルサイズ。既定は "md"。{ width, height } で自由に指定できる */
|
|
42
48
|
size?: CalendarSize;
|
|
49
|
+
/** 狭い画面(スマホ等)でセルサイズと余白を自動調整する。既定は false(無効) */
|
|
50
|
+
responsive?: boolean;
|
|
43
51
|
/** root 要素に追加するスタイル。CSS変数(--cal-*)で自由に上書きできる */
|
|
44
52
|
style?: CSSProperties;
|
|
45
53
|
|
|
@@ -47,10 +55,28 @@
|
|
|
47
55
|
|
|
48
56
|
/** インタラクティブモードを有効にする。セルクリック・ホバー・キーボード選択が可能になる */
|
|
49
57
|
interactive?: boolean;
|
|
50
|
-
/**
|
|
51
|
-
onDateClick?: (date: Date) => void;
|
|
58
|
+
/** セルクリック時のコールバック。第2引数に該当日のデータ(cellData の戻り値)が渡る */
|
|
59
|
+
onDateClick?: (date: Date, data?: unknown) => void;
|
|
52
60
|
/** セルホバー時のコールバック */
|
|
53
61
|
onDateHover?: (date: Date) => void;
|
|
62
|
+
/** カレンダーからマウスが離れたときのコールバック */
|
|
63
|
+
onDateLeave?: () => void;
|
|
64
|
+
/** キーボード操作。root 要素で発火(矢印キー等を InteractiveCalendar が処理する) */
|
|
65
|
+
onkeydown?: (event: KeyboardEvent) => void;
|
|
66
|
+
|
|
67
|
+
// ── セル状態 ──
|
|
68
|
+
|
|
69
|
+
/** セル内容のカスタムレンダリング。interactive 時は button の子として描画される。第4引数に該当日のデータが渡る */
|
|
70
|
+
renderCell?: (
|
|
71
|
+
day: number,
|
|
72
|
+
date: Date,
|
|
73
|
+
state: CalendarCellState,
|
|
74
|
+
data?: unknown,
|
|
75
|
+
) => string;
|
|
76
|
+
/** 各セルに付与するユーザー定義データを解決する関数。実セルのみに呼ばれる */
|
|
77
|
+
cellData?: (date: Date) => unknown;
|
|
78
|
+
/** 選択不可日付の判定。true を返した日付は is-disabled クラスになり、インタラクティブ時は選択・ホバーできなくなる */
|
|
79
|
+
isDateDisabled?: (date: Date) => boolean;
|
|
54
80
|
}
|
|
55
81
|
|
|
56
82
|
let {
|
|
@@ -60,37 +86,59 @@
|
|
|
60
86
|
weekStart = "sunday",
|
|
61
87
|
highlight,
|
|
62
88
|
range,
|
|
89
|
+
rangePreview,
|
|
63
90
|
today,
|
|
64
91
|
selected,
|
|
65
92
|
cursorDate,
|
|
93
|
+
hoveredDate,
|
|
66
94
|
theme = "default",
|
|
67
95
|
colorScheme = "default",
|
|
68
96
|
size = "md",
|
|
97
|
+
responsive = false,
|
|
69
98
|
style,
|
|
70
99
|
interactive = false,
|
|
71
100
|
onDateClick,
|
|
72
101
|
onDateHover,
|
|
102
|
+
onDateLeave,
|
|
103
|
+
onkeydown,
|
|
104
|
+
renderCell,
|
|
105
|
+
cellData,
|
|
106
|
+
isDateDisabled,
|
|
73
107
|
}: CalendarProps = $props();
|
|
74
108
|
|
|
75
109
|
const cellDate = (day: number): Date => createDate(year, month - 1, day);
|
|
110
|
+
const cellState = (day: number): CalendarCellState =>
|
|
111
|
+
getCalendarCellState(cellDate(day), { today, highlight, range, isDateDisabled });
|
|
76
112
|
const cellClass = (day: number): string | undefined =>
|
|
77
113
|
getCellClasses(cellDate(day), {
|
|
78
114
|
today,
|
|
79
115
|
highlight,
|
|
80
116
|
range,
|
|
117
|
+
rangePreview,
|
|
81
118
|
selected,
|
|
82
119
|
cursorDate,
|
|
120
|
+
hoveredDate,
|
|
121
|
+
isDateDisabled,
|
|
83
122
|
}) || undefined;
|
|
123
|
+
const isDisabledDay = (day: number): boolean => cellState(day).isDisabled;
|
|
84
124
|
const isSelectedDay = (day: number): boolean =>
|
|
85
125
|
selected != null && isSameDay(cellDate(day), selected);
|
|
86
126
|
const isTodayDay = (day: number): boolean =>
|
|
87
127
|
today != null && isSameDay(cellDate(day), today);
|
|
128
|
+
const isCursorDay = (day: number): boolean =>
|
|
129
|
+
cursorDate != null && isSameDay(cellDate(day), cursorDate);
|
|
88
130
|
const handleCellClick = (day: number) => {
|
|
89
|
-
if (interactive && onDateClick)
|
|
131
|
+
if (interactive && onDateClick) {
|
|
132
|
+
const date = cellDate(day);
|
|
133
|
+
onDateClick(date, cellData?.(date));
|
|
134
|
+
}
|
|
90
135
|
};
|
|
91
136
|
const handleCellHover = (day: number) => {
|
|
92
137
|
if (interactive && onDateHover) onDateHover(cellDate(day));
|
|
93
138
|
};
|
|
139
|
+
const handleMouseLeave = () => {
|
|
140
|
+
if (interactive && onDateLeave) onDateLeave();
|
|
141
|
+
};
|
|
94
142
|
|
|
95
143
|
// style 属性は文字列のみ受け付けるためオブジェクトを直列化する(--cal-* 変数含む)
|
|
96
144
|
const rootStyle = $derived(
|
|
@@ -103,8 +151,11 @@
|
|
|
103
151
|
</script>
|
|
104
152
|
|
|
105
153
|
<div
|
|
106
|
-
class="calendar {resolveTheme(theme).className}{isSizeName(size) ? ` calendar-size-${size}` : ""}{interactive ? " calendar-interactive" : ""}"
|
|
154
|
+
class="calendar {resolveTheme(theme).className}{isSizeName(size) ? ` calendar-size-${size}` : ""}{interactive ? " calendar-interactive" : ""}{responsive ? " calendar-responsive" : ""}"
|
|
107
155
|
style={rootStyle}
|
|
156
|
+
role={interactive ? "group" : undefined}
|
|
157
|
+
onmouseleave={interactive ? handleMouseLeave : undefined}
|
|
158
|
+
onkeydown={onkeydown}
|
|
108
159
|
>
|
|
109
160
|
<div class="calendar-header">
|
|
110
161
|
<h2>{getMonthName(locale, month)} {year}</h2>
|
|
@@ -125,20 +176,28 @@
|
|
|
125
176
|
{#if day === null}
|
|
126
177
|
<td></td>
|
|
127
178
|
{:else}
|
|
128
|
-
<td class={cellClass(day)}>
|
|
179
|
+
<td class={cellClass(day)} aria-disabled={isDisabledDay(day) || undefined}>
|
|
129
180
|
{#if interactive}
|
|
130
181
|
<button
|
|
131
182
|
type="button"
|
|
132
183
|
class="calendar-day-btn"
|
|
133
|
-
onclick={() => handleCellClick(day)}
|
|
134
|
-
onmouseenter={() => handleCellHover(day)}
|
|
135
|
-
tabindex=
|
|
136
|
-
|
|
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}
|
|
188
|
+
aria-label={formatCellLabel(locale, year, month, day)}
|
|
137
189
|
aria-pressed={isSelectedDay(day) || undefined}
|
|
138
190
|
aria-current={isTodayDay(day) ? "date" : undefined}
|
|
191
|
+
disabled={isDisabledDay(day)}
|
|
139
192
|
>
|
|
140
|
-
{
|
|
193
|
+
{#if renderCell}
|
|
194
|
+
{renderCell(day, cellDate(day), cellState(day), cellData?.(cellDate(day)))}
|
|
195
|
+
{:else}
|
|
196
|
+
{day}
|
|
197
|
+
{/if}
|
|
141
198
|
</button>
|
|
199
|
+
{:else if renderCell}
|
|
200
|
+
{renderCell(day, cellDate(day), cellState(day), cellData?.(cellDate(day)))}
|
|
142
201
|
{:else}
|
|
143
202
|
{day}
|
|
144
203
|
{/if}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { CalendarOptions } from "@typescript-calendar-lib/core";
|
|
1
|
+
import type { CalendarCellState, CalendarOptions } from "@typescript-calendar-lib/core";
|
|
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";
|
|
@@ -13,26 +13,45 @@ interface CalendarProps {
|
|
|
13
13
|
from: Date;
|
|
14
14
|
to: Date;
|
|
15
15
|
};
|
|
16
|
+
/** 範囲プレビュー(ホバー等の候補範囲)。is-in-range-preview クラスで視覚化される */
|
|
17
|
+
rangePreview?: {
|
|
18
|
+
from: Date;
|
|
19
|
+
to: Date;
|
|
20
|
+
};
|
|
16
21
|
/** 今日の基準日。カラースキームの today 着色に使用 */
|
|
17
22
|
today?: Date;
|
|
18
23
|
/** 選択済み日付。該当セルに is-selected クラスと aria-selected が付く */
|
|
19
24
|
selected?: Date | null;
|
|
20
25
|
/** カーソル位置の日付。該当セルに is-cursor クラスが付く */
|
|
21
26
|
cursorDate?: Date | null;
|
|
27
|
+
/** ホバー中の日付。該当セルに is-hovered クラスが付く */
|
|
28
|
+
hoveredDate?: Date | null;
|
|
22
29
|
/** 見た目テーマ。既定は "default" */
|
|
23
30
|
theme?: ThemeName | SvelteTheme;
|
|
24
31
|
/** カラースキーム。既定は "default" */
|
|
25
32
|
colorScheme?: ColorSchemeName | SvelteColorScheme;
|
|
26
33
|
/** セルサイズ。既定は "md"。{ width, height } で自由に指定できる */
|
|
27
34
|
size?: CalendarSize;
|
|
35
|
+
/** 狭い画面(スマホ等)でセルサイズと余白を自動調整する。既定は false(無効) */
|
|
36
|
+
responsive?: boolean;
|
|
28
37
|
/** root 要素に追加するスタイル。CSS変数(--cal-*)で自由に上書きできる */
|
|
29
38
|
style?: CSSProperties;
|
|
30
39
|
/** インタラクティブモードを有効にする。セルクリック・ホバー・キーボード選択が可能になる */
|
|
31
40
|
interactive?: boolean;
|
|
32
|
-
/**
|
|
33
|
-
onDateClick?: (date: Date) => void;
|
|
41
|
+
/** セルクリック時のコールバック。第2引数に該当日のデータ(cellData の戻り値)が渡る */
|
|
42
|
+
onDateClick?: (date: Date, data?: unknown) => void;
|
|
34
43
|
/** セルホバー時のコールバック */
|
|
35
44
|
onDateHover?: (date: Date) => void;
|
|
45
|
+
/** カレンダーからマウスが離れたときのコールバック */
|
|
46
|
+
onDateLeave?: () => void;
|
|
47
|
+
/** キーボード操作。root 要素で発火(矢印キー等を InteractiveCalendar が処理する) */
|
|
48
|
+
onkeydown?: (event: KeyboardEvent) => void;
|
|
49
|
+
/** セル内容のカスタムレンダリング。interactive 時は button の子として描画される。第4引数に該当日のデータが渡る */
|
|
50
|
+
renderCell?: (day: number, date: Date, state: CalendarCellState, data?: unknown) => string;
|
|
51
|
+
/** 各セルに付与するユーザー定義データを解決する関数。実セルのみに呼ばれる */
|
|
52
|
+
cellData?: (date: Date) => unknown;
|
|
53
|
+
/** 選択不可日付の判定。true を返した日付は is-disabled クラスになり、インタラクティブ時は選択・ホバーできなくなる */
|
|
54
|
+
isDateDisabled?: (date: Date) => boolean;
|
|
36
55
|
}
|
|
37
56
|
declare const Calendar: import("svelte").Component<CalendarProps, {}, "">;
|
|
38
57
|
type Calendar = ReturnType<typeof Calendar>;
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import type { CalendarCellState, CalendarOptions } from "@typescript-calendar-lib/core";
|
|
3
|
+
import { keyToAction } from "@typescript-calendar-lib/tui";
|
|
4
|
+
import Calendar from "./Calendar.svelte";
|
|
5
|
+
import { buildRangePreview } from "./range-preview.js";
|
|
6
|
+
import type { CalendarSize } from "./size.js";
|
|
7
|
+
import type { CSSProperties } from "./style.js";
|
|
8
|
+
import type {
|
|
9
|
+
ColorSchemeName,
|
|
10
|
+
SvelteColorScheme,
|
|
11
|
+
SvelteTheme,
|
|
12
|
+
ThemeName,
|
|
13
|
+
} from "./themes.js";
|
|
14
|
+
import { useCalendarState } from "./useCalendarState.svelte.js";
|
|
15
|
+
|
|
16
|
+
interface InteractiveCalendarProps {
|
|
17
|
+
// ── useCalendarState オプション ──
|
|
18
|
+
|
|
19
|
+
/** 表示開始年。既定は today の年 */
|
|
20
|
+
initialYear?: number;
|
|
21
|
+
/** 表示開始月 (1-12)。範囲外は正規化される */
|
|
22
|
+
initialMonth?: number;
|
|
23
|
+
locale?: CalendarOptions["locale"];
|
|
24
|
+
weekStart?: CalendarOptions["weekStart"];
|
|
25
|
+
/** ハイライト対象日 */
|
|
26
|
+
highlight?: Date;
|
|
27
|
+
/** 色付け範囲 */
|
|
28
|
+
range?: { from: Date; to: Date };
|
|
29
|
+
/** 今日の基準日。省略時は初回レンダリング時刻に解決される */
|
|
30
|
+
today?: Date;
|
|
31
|
+
/** 選択不可日付の判定。true を返した日付は選択・カーソル移動・ホバーの対象外になる */
|
|
32
|
+
isDateDisabled?: (date: Date) => boolean;
|
|
33
|
+
|
|
34
|
+
// ── Calendar の見た目 ──
|
|
35
|
+
|
|
36
|
+
theme?: ThemeName | SvelteTheme;
|
|
37
|
+
colorScheme?: ColorSchemeName | SvelteColorScheme;
|
|
38
|
+
size?: CalendarSize;
|
|
39
|
+
/** 狭い画面(スマホ等)でセルサイズと余白を自動調整する */
|
|
40
|
+
responsive?: boolean;
|
|
41
|
+
style?: CSSProperties;
|
|
42
|
+
/** セル内容のカスタムレンダリング。第4引数に該当日のデータが渡る */
|
|
43
|
+
renderCell?: (
|
|
44
|
+
day: number,
|
|
45
|
+
date: Date,
|
|
46
|
+
state: CalendarCellState,
|
|
47
|
+
data?: unknown,
|
|
48
|
+
) => string;
|
|
49
|
+
/** 各セルに付与するユーザー定義データを解決する関数。実セルのみに呼ばれる */
|
|
50
|
+
cellData?: (date: Date) => unknown;
|
|
51
|
+
|
|
52
|
+
// ── イベント ──
|
|
53
|
+
|
|
54
|
+
/** セルクリック時(キーボードの Enter/Space 含む)。クリックした日付は自動的に選択される。第2引数に該当日のデータが渡る */
|
|
55
|
+
onDateClick?: (date: Date, data?: unknown) => void;
|
|
56
|
+
/** セルホバー時 */
|
|
57
|
+
onDateHover?: (date: Date) => void;
|
|
58
|
+
/** カレンダーからマウスが離れたとき */
|
|
59
|
+
onDateLeave?: () => void;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
let {
|
|
63
|
+
initialYear,
|
|
64
|
+
initialMonth,
|
|
65
|
+
locale,
|
|
66
|
+
weekStart,
|
|
67
|
+
highlight,
|
|
68
|
+
range,
|
|
69
|
+
today,
|
|
70
|
+
isDateDisabled,
|
|
71
|
+
theme,
|
|
72
|
+
colorScheme,
|
|
73
|
+
size,
|
|
74
|
+
responsive,
|
|
75
|
+
style,
|
|
76
|
+
renderCell,
|
|
77
|
+
cellData,
|
|
78
|
+
onDateClick,
|
|
79
|
+
onDateHover,
|
|
80
|
+
onDateLeave,
|
|
81
|
+
}: InteractiveCalendarProps = $props();
|
|
82
|
+
|
|
83
|
+
const cal = useCalendarState(() => ({
|
|
84
|
+
initialYear,
|
|
85
|
+
initialMonth,
|
|
86
|
+
locale,
|
|
87
|
+
weekStart,
|
|
88
|
+
today,
|
|
89
|
+
highlight,
|
|
90
|
+
range,
|
|
91
|
+
isDateDisabled,
|
|
92
|
+
}));
|
|
93
|
+
|
|
94
|
+
// クリック(マウス・Enter/Space 共通)で日付を選択する。
|
|
95
|
+
// 従来の onDateClick コールバックも引き続き発火し、第2引数に該当日のデータが渡る。
|
|
96
|
+
const handleDateClick = (date: Date, data?: unknown) => {
|
|
97
|
+
cal.selectDateAt(date);
|
|
98
|
+
onDateClick?.(date, data);
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
const handleDateHover = (date: Date) => {
|
|
102
|
+
cal.setHoveredDate(date);
|
|
103
|
+
onDateHover?.(date);
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
const handleDateLeave = () => {
|
|
107
|
+
cal.setHoveredDate(null);
|
|
108
|
+
onDateLeave?.();
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
// 選択済み日付とホバー日付の間を範囲プレビューとして表示する
|
|
112
|
+
const rangePreview = $derived(
|
|
113
|
+
buildRangePreview(cal.selectedDate, cal.hoveredDate),
|
|
114
|
+
);
|
|
115
|
+
|
|
116
|
+
// カーソルが動いたときだけ、そのセルへフォーカスを移す(初回マウントでは動かさない)
|
|
117
|
+
let root: HTMLDivElement | undefined = $state();
|
|
118
|
+
let prevCursor = cal.state.cursor;
|
|
119
|
+
$effect(() => {
|
|
120
|
+
const next = cal.state.cursor;
|
|
121
|
+
if (next === prevCursor) return;
|
|
122
|
+
prevCursor = next;
|
|
123
|
+
root
|
|
124
|
+
?.querySelector<HTMLButtonElement>('[data-cursor="true"]')
|
|
125
|
+
?.focus();
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
const handleKeyDown = (event: KeyboardEvent) => {
|
|
129
|
+
const action = keyToAction(event.key);
|
|
130
|
+
if (action === undefined) return;
|
|
131
|
+
event.preventDefault();
|
|
132
|
+
switch (action) {
|
|
133
|
+
case "prev":
|
|
134
|
+
cal.goPrev();
|
|
135
|
+
break;
|
|
136
|
+
case "next":
|
|
137
|
+
cal.goNext();
|
|
138
|
+
break;
|
|
139
|
+
default:
|
|
140
|
+
cal.moveCursor(action);
|
|
141
|
+
}
|
|
142
|
+
};
|
|
143
|
+
</script>
|
|
144
|
+
|
|
145
|
+
<div bind:this={root} class="interactive-calendar">
|
|
146
|
+
<Calendar
|
|
147
|
+
year={cal.state.year}
|
|
148
|
+
month={cal.state.month}
|
|
149
|
+
locale={cal.state.options.locale}
|
|
150
|
+
weekStart={cal.state.options.weekStart}
|
|
151
|
+
highlight={cal.state.options.highlight}
|
|
152
|
+
range={cal.state.options.range}
|
|
153
|
+
today={cal.state.options.today}
|
|
154
|
+
isDateDisabled={isDateDisabled}
|
|
155
|
+
theme={theme}
|
|
156
|
+
colorScheme={colorScheme}
|
|
157
|
+
size={size}
|
|
158
|
+
responsive={responsive}
|
|
159
|
+
style={style}
|
|
160
|
+
interactive
|
|
161
|
+
renderCell={renderCell}
|
|
162
|
+
cellData={cellData}
|
|
163
|
+
selected={cal.selectedDate}
|
|
164
|
+
cursorDate={cal.cursorDate}
|
|
165
|
+
hoveredDate={cal.hoveredDate}
|
|
166
|
+
rangePreview={rangePreview}
|
|
167
|
+
onDateClick={handleDateClick}
|
|
168
|
+
onDateHover={handleDateHover}
|
|
169
|
+
onDateLeave={handleDateLeave}
|
|
170
|
+
onkeydown={handleKeyDown}
|
|
171
|
+
/>
|
|
172
|
+
</div>
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import type { CalendarCellState, CalendarOptions } from "@typescript-calendar-lib/core";
|
|
2
|
+
import type { CalendarSize } from "./size.js";
|
|
3
|
+
import type { CSSProperties } from "./style.js";
|
|
4
|
+
import type { ColorSchemeName, SvelteColorScheme, SvelteTheme, ThemeName } from "./themes.js";
|
|
5
|
+
interface InteractiveCalendarProps {
|
|
6
|
+
/** 表示開始年。既定は today の年 */
|
|
7
|
+
initialYear?: number;
|
|
8
|
+
/** 表示開始月 (1-12)。範囲外は正規化される */
|
|
9
|
+
initialMonth?: number;
|
|
10
|
+
locale?: CalendarOptions["locale"];
|
|
11
|
+
weekStart?: CalendarOptions["weekStart"];
|
|
12
|
+
/** ハイライト対象日 */
|
|
13
|
+
highlight?: Date;
|
|
14
|
+
/** 色付け範囲 */
|
|
15
|
+
range?: {
|
|
16
|
+
from: Date;
|
|
17
|
+
to: Date;
|
|
18
|
+
};
|
|
19
|
+
/** 今日の基準日。省略時は初回レンダリング時刻に解決される */
|
|
20
|
+
today?: Date;
|
|
21
|
+
/** 選択不可日付の判定。true を返した日付は選択・カーソル移動・ホバーの対象外になる */
|
|
22
|
+
isDateDisabled?: (date: Date) => boolean;
|
|
23
|
+
theme?: ThemeName | SvelteTheme;
|
|
24
|
+
colorScheme?: ColorSchemeName | SvelteColorScheme;
|
|
25
|
+
size?: CalendarSize;
|
|
26
|
+
/** 狭い画面(スマホ等)でセルサイズと余白を自動調整する */
|
|
27
|
+
responsive?: boolean;
|
|
28
|
+
style?: CSSProperties;
|
|
29
|
+
/** セル内容のカスタムレンダリング。第4引数に該当日のデータが渡る */
|
|
30
|
+
renderCell?: (day: number, date: Date, state: CalendarCellState, data?: unknown) => string;
|
|
31
|
+
/** 各セルに付与するユーザー定義データを解決する関数。実セルのみに呼ばれる */
|
|
32
|
+
cellData?: (date: Date) => unknown;
|
|
33
|
+
/** セルクリック時(キーボードの Enter/Space 含む)。クリックした日付は自動的に選択される。第2引数に該当日のデータが渡る */
|
|
34
|
+
onDateClick?: (date: Date, data?: unknown) => void;
|
|
35
|
+
/** セルホバー時 */
|
|
36
|
+
onDateHover?: (date: Date) => void;
|
|
37
|
+
/** カレンダーからマウスが離れたとき */
|
|
38
|
+
onDateLeave?: () => void;
|
|
39
|
+
}
|
|
40
|
+
declare const InteractiveCalendar: import("svelte").Component<InteractiveCalendarProps, {}, "">;
|
|
41
|
+
type InteractiveCalendar = ReturnType<typeof InteractiveCalendar>;
|
|
42
|
+
export default InteractiveCalendar;
|