@typescript-calendar-lib/svelte 0.2.7
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 +260 -0
- package/dist/Calendar.svelte +134 -0
- package/dist/Calendar.svelte.d.ts +35 -0
- package/dist/calendar.css +296 -0
- package/dist/cell-classes.d.ts +10 -0
- package/dist/cell-classes.js +16 -0
- package/dist/global.d.ts +6 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.js +7 -0
- package/dist/size.d.ts +13 -0
- package/dist/size.js +17 -0
- package/dist/style.d.ts +4 -0
- package/dist/style.js +10 -0
- package/dist/themes.d.ts +15 -0
- package/dist/themes.js +135 -0
- package/dist/useCalendarState.svelte.d.ts +60 -0
- package/dist/useCalendarState.svelte.js +115 -0
- package/package.json +50 -0
package/README.md
ADDED
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
# @typescript-calendar-lib/svelte
|
|
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.
|
|
4
|
+
|
|
5
|
+
> **Warning: Peer dependency** — Requires Svelte 5 (runes). The package doesn't bundle Svelte; install it in your app:
|
|
6
|
+
>
|
|
7
|
+
> ```sh
|
|
8
|
+
> npm install svelte @typescript-calendar-lib/svelte
|
|
9
|
+
> ```
|
|
10
|
+
|
|
11
|
+
## Installation
|
|
12
|
+
|
|
13
|
+
```sh
|
|
14
|
+
npm install @typescript-calendar-lib/svelte
|
|
15
|
+
# or
|
|
16
|
+
bun add @typescript-calendar-lib/svelte
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Quick Start
|
|
20
|
+
|
|
21
|
+
```svelte
|
|
22
|
+
<script lang="ts">
|
|
23
|
+
import Calendar from "@typescript-calendar-lib/svelte";
|
|
24
|
+
import "@typescript-calendar-lib/svelte/calendar.css"; // required — component styles
|
|
25
|
+
</script>
|
|
26
|
+
|
|
27
|
+
<Calendar year={2026} month={9} colorScheme="ocean" theme="modern" />
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
## Component API
|
|
31
|
+
|
|
32
|
+
### `Calendar`
|
|
33
|
+
|
|
34
|
+
```svelte
|
|
35
|
+
<Calendar year={2026} month={9} />
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
| Prop | Type | Default | Description |
|
|
39
|
+
| :--- | :--- | :--- | :--- |
|
|
40
|
+
| `year` | `number` | — (required) | Calendar year |
|
|
41
|
+
| `month` | `number` | — (required) | Month, 1-indexed (`1`–`12`) |
|
|
42
|
+
| `locale` | `"en" \| "ja" \| "es" \| "de" \| "fr" \| "ko" \| "zh"` | `"en"` | Language |
|
|
43
|
+
| `weekStart` | `"sunday" \| "monday"` | `"sunday"` | First day of the week |
|
|
44
|
+
| `highlight` | `Date` | — | Date to highlight |
|
|
45
|
+
| `range` | `{ from: Date; to: Date }` | — | Dates to emphasize |
|
|
46
|
+
| `today` | `Date` | — | Reference date for "today" styling |
|
|
47
|
+
| `theme` | `ThemeName \| SvelteTheme` | `"default"` | CSS class-based theme |
|
|
48
|
+
| `colorScheme` | `ColorSchemeName \| SvelteColorScheme` | `"default"` | CSS variable-based colors |
|
|
49
|
+
| `size` | `CalendarSize` | `"md"` | Cell size |
|
|
50
|
+
| `style` | `CSSProperties` | — | Extra styles for the root element |
|
|
51
|
+
| `interactive` | `boolean` | `false` | Enable cell click/hover/keyboard selection |
|
|
52
|
+
| `onDateClick` | `(date: Date) => void` | — | Called when a day cell is clicked (or Enter/Space pressed) |
|
|
53
|
+
| `onDateHover` | `(date: Date) => void` | — | Called when a day cell is hovered |
|
|
54
|
+
|
|
55
|
+
The component exports as both named `Calendar` and default.
|
|
56
|
+
|
|
57
|
+
## Interactive Mode
|
|
58
|
+
|
|
59
|
+
Set `interactive` to make day cells clickable. Each cell becomes a `<button class="calendar-day-btn">` — clickable, hoverable, and keyboard-accessible (Enter / Space):
|
|
60
|
+
|
|
61
|
+
```svelte
|
|
62
|
+
<Calendar
|
|
63
|
+
year={2026}
|
|
64
|
+
month={9}
|
|
65
|
+
interactive
|
|
66
|
+
onDateClick={(date) => console.log("Selected", date)}
|
|
67
|
+
onDateHover={(date) => console.log("Hovered", date)}
|
|
68
|
+
/>
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
## `useCalendarState` hook
|
|
72
|
+
|
|
73
|
+
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.
|
|
74
|
+
|
|
75
|
+
> **Reactive options:** Pass a **getter function** instead of a plain object if any option should track reactive props or `$state` values. A plain object is snapshot-only.
|
|
76
|
+
>
|
|
77
|
+
> ```svelte
|
|
78
|
+
> <script lang="ts">
|
|
79
|
+
> import Calendar, { useCalendarState } from "@typescript-calendar-lib/svelte";
|
|
80
|
+
> import "@typescript-calendar-lib/svelte/calendar.css";
|
|
81
|
+
>
|
|
82
|
+
> let { highlight, today } = $props();
|
|
83
|
+
>
|
|
84
|
+
> const cal = useCalendarState(() => ({
|
|
85
|
+
> initialYear: 2026,
|
|
86
|
+
> initialMonth: 9,
|
|
87
|
+
> today,
|
|
88
|
+
> highlight,
|
|
89
|
+
> }));
|
|
90
|
+
> </script>
|
|
91
|
+
>
|
|
92
|
+
> <button onclick={cal.goPrev}>‹</button>
|
|
93
|
+
> <Calendar year={cal.state.year} month={cal.state.month} interactive />
|
|
94
|
+
> <button onclick={cal.goNext}>›</button>
|
|
95
|
+
> ```
|
|
96
|
+
|
|
97
|
+
### Return value
|
|
98
|
+
|
|
99
|
+
| Field | Type | Description |
|
|
100
|
+
| :--- | :--- | :--- |
|
|
101
|
+
| `state` | `CalendarState` | Current calendar state (from tui) |
|
|
102
|
+
| `goNext` | `() => void` | Move to next month |
|
|
103
|
+
| `goPrev` | `() => void` | Move to previous month |
|
|
104
|
+
| `goToday` | `() => void` | Jump to today's month |
|
|
105
|
+
| `moveCursor` | `(direction: Direction) => void` | Move cursor: `"up"` / `"down"` / `"left"` / `"right"` |
|
|
106
|
+
| `selectDate` | `() => void` | Select the date under the cursor |
|
|
107
|
+
| `clearSelection` | `() => void` | Clear the selection |
|
|
108
|
+
| `cursorDate` | `Date \| null` | Date under the cursor |
|
|
109
|
+
| `selectedDate` | `Date \| null` | Currently selected date |
|
|
110
|
+
|
|
111
|
+
## Themes
|
|
112
|
+
|
|
113
|
+
### `ThemeName`
|
|
114
|
+
|
|
115
|
+
```ts
|
|
116
|
+
type ThemeName = "default" | "modern" | "minimal" | "rounded" | "retro";
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
Themes are implemented as CSS classes applied to the root element:
|
|
120
|
+
|
|
121
|
+
- `calendar-theme-default`
|
|
122
|
+
- `calendar-theme-modern`
|
|
123
|
+
- `calendar-theme-minimal`
|
|
124
|
+
- `calendar-theme-rounded`
|
|
125
|
+
- `calendar-theme-retro`
|
|
126
|
+
|
|
127
|
+
Pass a custom theme as an object:
|
|
128
|
+
|
|
129
|
+
```ts
|
|
130
|
+
const myTheme: SvelteTheme = { className: "my-calendar" };
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
### `SvelteTheme`
|
|
134
|
+
|
|
135
|
+
```ts
|
|
136
|
+
interface SvelteTheme {
|
|
137
|
+
className: string;
|
|
138
|
+
}
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
### `THEMES` / `resolveTheme(theme?)`
|
|
142
|
+
|
|
143
|
+
Predefined theme map and resolver.
|
|
144
|
+
|
|
145
|
+
## Color Schemes
|
|
146
|
+
|
|
147
|
+
### `ColorSchemeName`
|
|
148
|
+
|
|
149
|
+
```ts
|
|
150
|
+
type ColorSchemeName =
|
|
151
|
+
| "default" | "ocean" | "forest" | "sunset"
|
|
152
|
+
| "mono" | "midnight" | "blossom";
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
### `SvelteColorScheme`
|
|
156
|
+
|
|
157
|
+
A color scheme is a map of `--cal-*` CSS custom properties:
|
|
158
|
+
|
|
159
|
+
```ts
|
|
160
|
+
type SvelteColorScheme = Record<`--cal-${string}`, string>;
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
Available variables:
|
|
164
|
+
|
|
165
|
+
| Variable | Purpose |
|
|
166
|
+
| :--- | :--- |
|
|
167
|
+
| `--cal-bg` | Background |
|
|
168
|
+
| `--cal-fg` | Foreground |
|
|
169
|
+
| `--cal-accent` | Accent (highlight) |
|
|
170
|
+
| `--cal-weekend-fg` | Weekend foreground |
|
|
171
|
+
| `--cal-border` | Borders |
|
|
172
|
+
| `--cal-header-bg` | Header background |
|
|
173
|
+
| `--cal-highlight-bg` | Highlight background |
|
|
174
|
+
| `--cal-highlight-fg` | Highlight foreground |
|
|
175
|
+
| `--cal-range-bg` | Range background |
|
|
176
|
+
| `--cal-today-bg` | Today background |
|
|
177
|
+
| `--cal-today-fg` | Today foreground |
|
|
178
|
+
|
|
179
|
+
Pass a custom scheme directly:
|
|
180
|
+
|
|
181
|
+
```ts
|
|
182
|
+
const myScheme: SvelteColorScheme = {
|
|
183
|
+
"--cal-bg": "#ffffff",
|
|
184
|
+
"--cal-fg": "#1e293b",
|
|
185
|
+
"--cal-accent": "#6366f1",
|
|
186
|
+
};
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
### `COLOR_SCHEMES` / `resolveColorScheme(scheme?)`
|
|
190
|
+
|
|
191
|
+
Predefined schemes and resolver.
|
|
192
|
+
|
|
193
|
+
## Sizing
|
|
194
|
+
|
|
195
|
+
### `CalendarSize`
|
|
196
|
+
|
|
197
|
+
```ts
|
|
198
|
+
type CalendarSize = "sm" | "md" | "lg" | { width?: number | string; height?: number | string };
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
| Size | Cell dimensions |
|
|
202
|
+
| :--- | :--- |
|
|
203
|
+
| `"sm"` | `1.75rem` × `1.5rem` |
|
|
204
|
+
| `"md"` | `2.25rem` × `2rem` |
|
|
205
|
+
| `"lg"` | `3.125rem` × `2.75rem` |
|
|
206
|
+
|
|
207
|
+
Custom objects set the `--cal-cell-w` / `--cal-cell-h` CSS variables (numbers become px, strings pass through):
|
|
208
|
+
|
|
209
|
+
```svelte
|
|
210
|
+
<Calendar year={2026} month={9} size={{ width: 48, height: 40 }} />
|
|
211
|
+
```
|
|
212
|
+
|
|
213
|
+
### `buildSizeStyle(size?)`
|
|
214
|
+
|
|
215
|
+
Returns `CSSProperties` for the given size name or custom object.
|
|
216
|
+
|
|
217
|
+
## CSS
|
|
218
|
+
|
|
219
|
+
The stylesheet must be imported once in your application:
|
|
220
|
+
|
|
221
|
+
```svelte
|
|
222
|
+
<script lang="ts">
|
|
223
|
+
import "@typescript-calendar-lib/svelte/calendar.css";
|
|
224
|
+
</script>
|
|
225
|
+
```
|
|
226
|
+
|
|
227
|
+
## Exports
|
|
228
|
+
|
|
229
|
+
```ts
|
|
230
|
+
import Calendar, {
|
|
231
|
+
Calendar,
|
|
232
|
+
buildSizeStyle,
|
|
233
|
+
COLOR_SCHEMES,
|
|
234
|
+
getCellClasses,
|
|
235
|
+
isSizeName,
|
|
236
|
+
resolveColorScheme,
|
|
237
|
+
resolveTheme,
|
|
238
|
+
THEMES,
|
|
239
|
+
useCalendarState,
|
|
240
|
+
} from "@typescript-calendar-lib/svelte";
|
|
241
|
+
|
|
242
|
+
import type {
|
|
243
|
+
CalendarCustomSize,
|
|
244
|
+
CalendarSize,
|
|
245
|
+
CalendarSizeName,
|
|
246
|
+
CellStateOptions,
|
|
247
|
+
ColorSchemeName,
|
|
248
|
+
CSSProperties,
|
|
249
|
+
SvelteColorScheme,
|
|
250
|
+
SvelteTheme,
|
|
251
|
+
ThemeName,
|
|
252
|
+
UseCalendarStateOptions,
|
|
253
|
+
UseCalendarStateOptionsInput,
|
|
254
|
+
UseCalendarStateReturn,
|
|
255
|
+
} from "@typescript-calendar-lib/svelte";
|
|
256
|
+
```
|
|
257
|
+
|
|
258
|
+
## License
|
|
259
|
+
|
|
260
|
+
MIT
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import type { CalendarOptions } from "@typescript-calendar-lib/core";
|
|
3
|
+
import {
|
|
4
|
+
buildMonthGrid,
|
|
5
|
+
createDate,
|
|
6
|
+
getMonthName,
|
|
7
|
+
getWeekdayHeaders,
|
|
8
|
+
} from "@typescript-calendar-lib/core";
|
|
9
|
+
import { getCellClasses } from "./cell-classes.js";
|
|
10
|
+
import type { CalendarSize } from "./size.js";
|
|
11
|
+
import { buildSizeStyle, isSizeName } from "./size.js";
|
|
12
|
+
import type { CSSProperties } from "./style.js";
|
|
13
|
+
import { styleObjectToString } from "./style.js";
|
|
14
|
+
import type {
|
|
15
|
+
ColorSchemeName,
|
|
16
|
+
SvelteColorScheme,
|
|
17
|
+
SvelteTheme,
|
|
18
|
+
ThemeName,
|
|
19
|
+
} from "./themes.js";
|
|
20
|
+
import { resolveColorScheme, resolveTheme } from "./themes.js";
|
|
21
|
+
|
|
22
|
+
interface CalendarProps {
|
|
23
|
+
year: number;
|
|
24
|
+
month: number;
|
|
25
|
+
locale?: CalendarOptions["locale"];
|
|
26
|
+
weekStart?: CalendarOptions["weekStart"];
|
|
27
|
+
highlight?: Date;
|
|
28
|
+
/** 範囲強調 */
|
|
29
|
+
range?: { from: Date; to: Date };
|
|
30
|
+
/** 今日の基準日。カラースキームの today 着色に使用 */
|
|
31
|
+
today?: Date;
|
|
32
|
+
/** 見た目テーマ。既定は "default" */
|
|
33
|
+
theme?: ThemeName | SvelteTheme;
|
|
34
|
+
/** カラースキーム。既定は "default" */
|
|
35
|
+
colorScheme?: ColorSchemeName | SvelteColorScheme;
|
|
36
|
+
/** セルサイズ。既定は "md"。{ width, height } で自由に指定できる */
|
|
37
|
+
size?: CalendarSize;
|
|
38
|
+
/** root 要素に追加するスタイル。CSS変数(--cal-*)で自由に上書きできる */
|
|
39
|
+
style?: CSSProperties;
|
|
40
|
+
|
|
41
|
+
// ── インタラクション ──
|
|
42
|
+
|
|
43
|
+
/** インタラクティブモードを有効にする。セルクリック・ホバー・キーボード選択が可能になる */
|
|
44
|
+
interactive?: boolean;
|
|
45
|
+
/** セルクリック時のコールバック */
|
|
46
|
+
onDateClick?: (date: Date) => void;
|
|
47
|
+
/** セルホバー時のコールバック */
|
|
48
|
+
onDateHover?: (date: Date) => void;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
let {
|
|
52
|
+
year,
|
|
53
|
+
month,
|
|
54
|
+
locale = "en",
|
|
55
|
+
weekStart = "sunday",
|
|
56
|
+
highlight,
|
|
57
|
+
range,
|
|
58
|
+
today,
|
|
59
|
+
theme = "default",
|
|
60
|
+
colorScheme = "default",
|
|
61
|
+
size = "md",
|
|
62
|
+
style,
|
|
63
|
+
interactive = false,
|
|
64
|
+
onDateClick,
|
|
65
|
+
onDateHover,
|
|
66
|
+
}: CalendarProps = $props();
|
|
67
|
+
|
|
68
|
+
const cellDate = (day: number): Date => createDate(year, month - 1, day);
|
|
69
|
+
const cellClass = (day: number): string | undefined =>
|
|
70
|
+
getCellClasses(cellDate(day), { today, highlight, range }) || undefined;
|
|
71
|
+
const handleCellClick = (day: number) => {
|
|
72
|
+
if (interactive && onDateClick) onDateClick(cellDate(day));
|
|
73
|
+
};
|
|
74
|
+
const handleCellHover = (day: number) => {
|
|
75
|
+
if (interactive && onDateHover) onDateHover(cellDate(day));
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
// style 属性は文字列のみ受け付けるためオブジェクトを直列化する(--cal-* 変数含む)
|
|
79
|
+
const rootStyle = $derived(
|
|
80
|
+
styleObjectToString({
|
|
81
|
+
...resolveColorScheme(colorScheme),
|
|
82
|
+
...buildSizeStyle(size),
|
|
83
|
+
...style,
|
|
84
|
+
}),
|
|
85
|
+
);
|
|
86
|
+
</script>
|
|
87
|
+
|
|
88
|
+
<div
|
|
89
|
+
class="calendar {resolveTheme(theme).className}{isSizeName(size) ? ` calendar-size-${size}` : ""}{interactive ? " calendar-interactive" : ""}"
|
|
90
|
+
style={rootStyle}
|
|
91
|
+
>
|
|
92
|
+
<div class="calendar-header">
|
|
93
|
+
<h2>{getMonthName(locale, month)} {year}</h2>
|
|
94
|
+
</div>
|
|
95
|
+
<table>
|
|
96
|
+
<thead>
|
|
97
|
+
<tr>
|
|
98
|
+
{#each getWeekdayHeaders(locale, weekStart) as day (day)}
|
|
99
|
+
<th>{day}</th>
|
|
100
|
+
{/each}
|
|
101
|
+
</tr>
|
|
102
|
+
</thead>
|
|
103
|
+
<tbody>
|
|
104
|
+
{#each buildMonthGrid(year, month, weekStart) as row, i (i)}
|
|
105
|
+
{#if !row.every((d) => d === null)}
|
|
106
|
+
<tr>
|
|
107
|
+
{#each row as day, j (j)}
|
|
108
|
+
{#if day === null}
|
|
109
|
+
<td></td>
|
|
110
|
+
{:else}
|
|
111
|
+
<td class={cellClass(day)}>
|
|
112
|
+
{#if interactive}
|
|
113
|
+
<button
|
|
114
|
+
type="button"
|
|
115
|
+
class="calendar-day-btn"
|
|
116
|
+
onclick={() => handleCellClick(day)}
|
|
117
|
+
onmouseenter={() => handleCellHover(day)}
|
|
118
|
+
tabindex="0"
|
|
119
|
+
aria-label={`${getMonthName(locale, month)} ${day}, ${year}`}
|
|
120
|
+
>
|
|
121
|
+
{day}
|
|
122
|
+
</button>
|
|
123
|
+
{:else}
|
|
124
|
+
{day}
|
|
125
|
+
{/if}
|
|
126
|
+
</td>
|
|
127
|
+
{/if}
|
|
128
|
+
{/each}
|
|
129
|
+
</tr>
|
|
130
|
+
{/if}
|
|
131
|
+
{/each}
|
|
132
|
+
</tbody>
|
|
133
|
+
</table>
|
|
134
|
+
</div>
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type { 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 CalendarProps {
|
|
6
|
+
year: number;
|
|
7
|
+
month: number;
|
|
8
|
+
locale?: CalendarOptions["locale"];
|
|
9
|
+
weekStart?: CalendarOptions["weekStart"];
|
|
10
|
+
highlight?: Date;
|
|
11
|
+
/** 範囲強調 */
|
|
12
|
+
range?: {
|
|
13
|
+
from: Date;
|
|
14
|
+
to: Date;
|
|
15
|
+
};
|
|
16
|
+
/** 今日の基準日。カラースキームの today 着色に使用 */
|
|
17
|
+
today?: Date;
|
|
18
|
+
/** 見た目テーマ。既定は "default" */
|
|
19
|
+
theme?: ThemeName | SvelteTheme;
|
|
20
|
+
/** カラースキーム。既定は "default" */
|
|
21
|
+
colorScheme?: ColorSchemeName | SvelteColorScheme;
|
|
22
|
+
/** セルサイズ。既定は "md"。{ width, height } で自由に指定できる */
|
|
23
|
+
size?: CalendarSize;
|
|
24
|
+
/** root 要素に追加するスタイル。CSS変数(--cal-*)で自由に上書きできる */
|
|
25
|
+
style?: CSSProperties;
|
|
26
|
+
/** インタラクティブモードを有効にする。セルクリック・ホバー・キーボード選択が可能になる */
|
|
27
|
+
interactive?: boolean;
|
|
28
|
+
/** セルクリック時のコールバック */
|
|
29
|
+
onDateClick?: (date: Date) => void;
|
|
30
|
+
/** セルホバー時のコールバック */
|
|
31
|
+
onDateHover?: (date: Date) => void;
|
|
32
|
+
}
|
|
33
|
+
declare const Calendar: import("svelte").Component<CalendarProps, {}, "">;
|
|
34
|
+
type Calendar = ReturnType<typeof Calendar>;
|
|
35
|
+
export default Calendar;
|
|
@@ -0,0 +1,296 @@
|
|
|
1
|
+
/* ─── Base ─────────────────────────────────────────────── */
|
|
2
|
+
|
|
3
|
+
.calendar {
|
|
4
|
+
--cal-bg: #ffffff;
|
|
5
|
+
--cal-fg: #1e293b;
|
|
6
|
+
--cal-accent: #dc2626;
|
|
7
|
+
--cal-weekend-fg: #94a3b8;
|
|
8
|
+
--cal-border: #e2e8f0;
|
|
9
|
+
--cal-header-bg: #f8fafc;
|
|
10
|
+
--cal-highlight-bg: #dc2626;
|
|
11
|
+
--cal-highlight-fg: #ffffff;
|
|
12
|
+
--cal-range-bg: #fef3c7;
|
|
13
|
+
--cal-today-bg: #fff7ed;
|
|
14
|
+
--cal-today-fg: #c2410c;
|
|
15
|
+
|
|
16
|
+
/* セルサイズ(size propや style の CSS 変数で上書きできる) */
|
|
17
|
+
--cal-cell-w: 2.25rem;
|
|
18
|
+
--cal-cell-h: 2rem;
|
|
19
|
+
--cal-font-size: 0.875rem;
|
|
20
|
+
--cal-header-font-size: 1.125rem;
|
|
21
|
+
|
|
22
|
+
display: inline-block;
|
|
23
|
+
padding: 0.75rem 1rem;
|
|
24
|
+
background: var(--cal-bg);
|
|
25
|
+
color: var(--cal-fg);
|
|
26
|
+
font-family: ui-sans-serif, system-ui, sans-serif;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
.calendar table {
|
|
30
|
+
border-collapse: collapse;
|
|
31
|
+
border-spacing: 0;
|
|
32
|
+
font-variant-numeric: tabular-nums;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
.calendar th,
|
|
36
|
+
.calendar td {
|
|
37
|
+
width: var(--cal-cell-w);
|
|
38
|
+
height: var(--cal-cell-h);
|
|
39
|
+
padding: 0.125rem 0.375rem;
|
|
40
|
+
text-align: center;
|
|
41
|
+
font-size: var(--cal-font-size);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
.calendar th {
|
|
45
|
+
color: var(--cal-accent);
|
|
46
|
+
font-weight: 600;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
.calendar .calendar-header {
|
|
50
|
+
text-align: center;
|
|
51
|
+
margin-bottom: 0.5rem;
|
|
52
|
+
font-size: 1rem;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
.calendar .calendar-header h2 {
|
|
56
|
+
margin: 0;
|
|
57
|
+
font-size: var(--cal-header-font-size);
|
|
58
|
+
font-weight: 700;
|
|
59
|
+
color: var(--cal-fg);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
.calendar td.is-weekend {
|
|
63
|
+
color: var(--cal-weekend-fg);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
.calendar td.is-today {
|
|
67
|
+
background: var(--cal-today-bg);
|
|
68
|
+
color: var(--cal-today-fg);
|
|
69
|
+
font-weight: 700;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
.calendar td.is-highlight {
|
|
73
|
+
background: var(--cal-highlight-bg);
|
|
74
|
+
color: var(--cal-highlight-fg);
|
|
75
|
+
font-weight: 700;
|
|
76
|
+
border-radius: 0.375rem;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
.calendar td.is-in-range {
|
|
80
|
+
background: var(--cal-range-bg);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/* ─── Size ────────────────────────────────────────────── */
|
|
84
|
+
|
|
85
|
+
.calendar-size-sm {
|
|
86
|
+
--cal-cell-w: 1.75rem;
|
|
87
|
+
--cal-cell-h: 1.5rem;
|
|
88
|
+
--cal-font-size: 0.75rem;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
.calendar-size-lg {
|
|
92
|
+
--cal-cell-w: 3.125rem;
|
|
93
|
+
--cal-cell-h: 2.75rem;
|
|
94
|
+
--cal-font-size: 1rem;
|
|
95
|
+
--cal-header-font-size: 1.25rem;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/* ─── Theme: default ───────────────────────────────────── */
|
|
99
|
+
|
|
100
|
+
.calendar-theme-default table {
|
|
101
|
+
border: 1px solid var(--cal-border);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
.calendar-theme-default th {
|
|
105
|
+
border-bottom: 2px solid var(--cal-border);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
.calendar-theme-default td {
|
|
109
|
+
border: 1px solid var(--cal-border);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
.calendar-theme-default td.is-highlight {
|
|
113
|
+
border-radius: 0;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/* ─── Theme: modern ────────────────────────────────────── */
|
|
117
|
+
|
|
118
|
+
.calendar-theme-modern {
|
|
119
|
+
border-radius: 1rem;
|
|
120
|
+
box-shadow:
|
|
121
|
+
0 1px 3px rgb(0 0 0 / 0.1),
|
|
122
|
+
0 8px 24px rgb(0 0 0 / 0.08);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
.calendar-theme-modern .calendar-header {
|
|
126
|
+
background: var(--cal-header-bg);
|
|
127
|
+
margin: -0.75rem -1rem 0.75rem;
|
|
128
|
+
padding: 0.75rem 1rem;
|
|
129
|
+
border-radius: 1rem 1rem 0 0;
|
|
130
|
+
border-bottom: 1px solid var(--cal-border);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
.calendar-theme-modern table {
|
|
134
|
+
border: 1px solid var(--cal-border);
|
|
135
|
+
border-radius: 0.5rem;
|
|
136
|
+
overflow: hidden;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
.calendar-theme-modern th {
|
|
140
|
+
background: var(--cal-header-bg);
|
|
141
|
+
border-bottom: 2px solid var(--cal-border);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
.calendar-theme-modern td {
|
|
145
|
+
border: 1px solid var(--cal-border);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
.calendar-theme-modern td:empty {
|
|
149
|
+
border-color: transparent;
|
|
150
|
+
background: transparent;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
.calendar-theme-modern td.is-today {
|
|
154
|
+
border-radius: 0.375rem;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
.calendar-theme-modern td.is-highlight {
|
|
158
|
+
box-shadow: 0 1px 2px rgb(0 0 0 / 0.15);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/* ─── Theme: minimal ───────────────────────────────────── */
|
|
162
|
+
|
|
163
|
+
.calendar-theme-minimal {
|
|
164
|
+
padding: 0.5rem;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
.calendar-theme-minimal .calendar-header {
|
|
168
|
+
margin-bottom: 0.25rem;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
.calendar-theme-minimal table {
|
|
172
|
+
border: 0;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
.calendar-theme-minimal th {
|
|
176
|
+
border-bottom: 1px solid var(--cal-border);
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
.calendar-theme-minimal td {
|
|
180
|
+
border: 0;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
.calendar-theme-minimal td.is-highlight {
|
|
184
|
+
border-radius: 0.25rem;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/* ─── Theme: rounded ───────────────────────────────────── */
|
|
188
|
+
|
|
189
|
+
.calendar-theme-rounded {
|
|
190
|
+
border-radius: 1rem;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
.calendar-theme-rounded table {
|
|
194
|
+
border-collapse: separate;
|
|
195
|
+
border-spacing: 0.25rem;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
.calendar-theme-rounded th {
|
|
199
|
+
border: 0;
|
|
200
|
+
border-radius: 999px;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
.calendar-theme-rounded td {
|
|
204
|
+
border: 0;
|
|
205
|
+
border-radius: 999px;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
.calendar-theme-rounded td.is-today,
|
|
209
|
+
.calendar-theme-rounded td.is-highlight,
|
|
210
|
+
.calendar-theme-rounded td.is-in-range {
|
|
211
|
+
border-radius: 999px;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
.calendar-theme-rounded td:empty {
|
|
215
|
+
background: transparent;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/* ─── Interactive ──────────────────────────────────────── */
|
|
219
|
+
|
|
220
|
+
.calendar-interactive {
|
|
221
|
+
outline: none;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
.calendar-interactive:focus-visible {
|
|
225
|
+
box-shadow: 0 0 0 2px var(--cal-accent);
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
.calendar-interactive td button.calendar-day-btn {
|
|
229
|
+
display: block;
|
|
230
|
+
width: 100%;
|
|
231
|
+
height: 100%;
|
|
232
|
+
margin: 0;
|
|
233
|
+
padding: 0;
|
|
234
|
+
border: 0;
|
|
235
|
+
background: transparent;
|
|
236
|
+
color: inherit;
|
|
237
|
+
font: inherit;
|
|
238
|
+
line-height: inherit;
|
|
239
|
+
text-align: center;
|
|
240
|
+
border-radius: inherit;
|
|
241
|
+
cursor: pointer;
|
|
242
|
+
transition:
|
|
243
|
+
background-color 0.15s ease,
|
|
244
|
+
color 0.15s ease,
|
|
245
|
+
transform 0.1s ease;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
.calendar-interactive td button.calendar-day-btn:hover {
|
|
249
|
+
background: var(--cal-range-bg);
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
.calendar-interactive td button.calendar-day-btn:focus-visible {
|
|
253
|
+
outline: 2px solid var(--cal-accent);
|
|
254
|
+
outline-offset: -4px;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
.calendar-interactive td button.calendar-day-btn:active {
|
|
258
|
+
transform: scale(0.95);
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
/* ─── Theme: retro ─────────────────────────────────────── */
|
|
262
|
+
|
|
263
|
+
.calendar-theme-retro {
|
|
264
|
+
padding: 0.75rem;
|
|
265
|
+
border: 2px solid var(--cal-fg);
|
|
266
|
+
border-radius: 0;
|
|
267
|
+
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
.calendar-theme-retro .calendar-header {
|
|
271
|
+
background: var(--cal-header-bg);
|
|
272
|
+
border-bottom: 3px double var(--cal-fg);
|
|
273
|
+
margin: -0.75rem -0.75rem 0.75rem;
|
|
274
|
+
padding: 0.5rem 0.75rem;
|
|
275
|
+
letter-spacing: 0.05em;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
.calendar-theme-retro table {
|
|
279
|
+
border: 2px solid var(--cal-fg);
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
.calendar-theme-retro th {
|
|
283
|
+
background: var(--cal-header-bg);
|
|
284
|
+
border: 1px solid var(--cal-fg);
|
|
285
|
+
font-weight: 700;
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
.calendar-theme-retro td {
|
|
289
|
+
border: 1px solid var(--cal-fg);
|
|
290
|
+
border-radius: 0;
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
.calendar-theme-retro td.is-highlight,
|
|
294
|
+
.calendar-theme-retro td.is-today {
|
|
295
|
+
border-radius: 0;
|
|
296
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { isDateInRange, isSameDay } from "@typescript-calendar-lib/core";
|
|
2
|
+
// 範囲の from > to は不正入力として RangeError(isDateInRange が検証する)
|
|
3
|
+
/** 日付セルの状態(週末・今日・ハイライト・範囲)に応じたCSSクラスを組み立てる */
|
|
4
|
+
export function getCellClasses(date, options) {
|
|
5
|
+
const classes = [];
|
|
6
|
+
const dow = date.getDay();
|
|
7
|
+
if (dow === 0 || dow === 6)
|
|
8
|
+
classes.push("is-weekend");
|
|
9
|
+
if (options.today && isSameDay(date, options.today))
|
|
10
|
+
classes.push("is-today");
|
|
11
|
+
if (options.highlight && isSameDay(date, options.highlight))
|
|
12
|
+
classes.push("is-highlight");
|
|
13
|
+
if (options.range && isDateInRange(date, options.range))
|
|
14
|
+
classes.push("is-in-range");
|
|
15
|
+
return classes.join(" ");
|
|
16
|
+
}
|
package/dist/global.d.ts
ADDED
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import Calendar from "./Calendar.svelte";
|
|
2
|
+
export { Calendar };
|
|
3
|
+
export default Calendar;
|
|
4
|
+
export type { CellStateOptions } from "./cell-classes.js";
|
|
5
|
+
export { getCellClasses } from "./cell-classes.js";
|
|
6
|
+
export type { CalendarCustomSize, CalendarSize, CalendarSizeName, } from "./size.js";
|
|
7
|
+
export { buildSizeStyle, isSizeName } from "./size.js";
|
|
8
|
+
export type { CSSProperties } from "./style.js";
|
|
9
|
+
export type { ColorSchemeName, SvelteColorScheme, SvelteTheme, ThemeName, } from "./themes.js";
|
|
10
|
+
export { COLOR_SCHEMES, resolveColorScheme, resolveTheme, THEMES, } from "./themes.js";
|
|
11
|
+
export type { UseCalendarStateOptions, UseCalendarStateOptionsInput, UseCalendarStateReturn, } from "./useCalendarState.svelte.js";
|
|
12
|
+
export { useCalendarState } from "./useCalendarState.svelte.js";
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import Calendar from "./Calendar.svelte";
|
|
2
|
+
export { Calendar };
|
|
3
|
+
export default Calendar;
|
|
4
|
+
export { getCellClasses } from "./cell-classes.js";
|
|
5
|
+
export { buildSizeStyle, isSizeName } from "./size.js";
|
|
6
|
+
export { COLOR_SCHEMES, resolveColorScheme, resolveTheme, THEMES, } from "./themes.js";
|
|
7
|
+
export { useCalendarState } from "./useCalendarState.svelte.js";
|
package/dist/size.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { CSSProperties } from "./style.js";
|
|
2
|
+
/** 組み込みサイズ名 */
|
|
3
|
+
export type CalendarSizeName = "sm" | "md" | "lg";
|
|
4
|
+
/** カスタムセルサイズ。数値は px、文字列は CSS 長さのまま渡す */
|
|
5
|
+
export interface CalendarCustomSize {
|
|
6
|
+
width?: number | string;
|
|
7
|
+
height?: number | string;
|
|
8
|
+
}
|
|
9
|
+
/** セルサイズ指定 */
|
|
10
|
+
export type CalendarSize = CalendarSizeName | CalendarCustomSize;
|
|
11
|
+
/** 組み込みサイズ名のみ真を返す(未知の文字列はカスタムサイズとして扱わない) */
|
|
12
|
+
export declare function isSizeName(size: CalendarSize): size is CalendarSizeName;
|
|
13
|
+
export declare function buildSizeStyle(size: CalendarSize): CSSProperties;
|
package/dist/size.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/** 組み込みサイズ名のみ真を返す(未知の文字列はカスタムサイズとして扱わない) */
|
|
2
|
+
export function isSizeName(size) {
|
|
3
|
+
return typeof size === "string" && ["sm", "md", "lg"].includes(size);
|
|
4
|
+
}
|
|
5
|
+
function toCssLength(value) {
|
|
6
|
+
return typeof value === "number" ? `${value}px` : value;
|
|
7
|
+
}
|
|
8
|
+
export function buildSizeStyle(size) {
|
|
9
|
+
if (isSizeName(size))
|
|
10
|
+
return {};
|
|
11
|
+
const style = {};
|
|
12
|
+
if (size.width !== undefined)
|
|
13
|
+
style["--cal-cell-w"] = toCssLength(size.width);
|
|
14
|
+
if (size.height !== undefined)
|
|
15
|
+
style["--cal-cell-h"] = toCssLength(size.height);
|
|
16
|
+
return style;
|
|
17
|
+
}
|
package/dist/style.d.ts
ADDED
package/dist/style.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/** オブジェクト形式の style を style 属性文字列に変換する(camelCase → kebab-case) */
|
|
2
|
+
export function styleObjectToString(style) {
|
|
3
|
+
const parts = [];
|
|
4
|
+
for (const [key, value] of Object.entries(style)) {
|
|
5
|
+
if (value === undefined)
|
|
6
|
+
continue;
|
|
7
|
+
parts.push(`${key.replace(/[A-Z]/g, (c) => `-${c.toLowerCase()}`)}: ${value}`);
|
|
8
|
+
}
|
|
9
|
+
return parts.join("; ");
|
|
10
|
+
}
|
package/dist/themes.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/** 組み込みテーマ名。カスタムテーマは SvelteTheme オブジェクトを直接渡せる */
|
|
2
|
+
export type ThemeName = "default" | "modern" | "minimal" | "rounded" | "retro";
|
|
3
|
+
/** Svelte の見た目定義。テーマは主に CSS クラスで切り替える */
|
|
4
|
+
export interface SvelteTheme {
|
|
5
|
+
/** root 要素に追加するクラス */
|
|
6
|
+
className: string;
|
|
7
|
+
}
|
|
8
|
+
export declare const THEMES: Record<ThemeName, SvelteTheme>;
|
|
9
|
+
export declare function resolveTheme(theme?: ThemeName | SvelteTheme): SvelteTheme;
|
|
10
|
+
/** 組み込みカラースキーム名。カスタムは CSS 変数マップを直接渡せる */
|
|
11
|
+
export type ColorSchemeName = "default" | "ocean" | "forest" | "sunset" | "mono" | "midnight" | "blossom";
|
|
12
|
+
/** CSS カスタムプロパティ(--cal-*)の値マップ */
|
|
13
|
+
export type SvelteColorScheme = Record<`--cal-${string}`, string>;
|
|
14
|
+
export declare const COLOR_SCHEMES: Record<ColorSchemeName, SvelteColorScheme>;
|
|
15
|
+
export declare function resolveColorScheme(scheme?: ColorSchemeName | SvelteColorScheme): SvelteColorScheme;
|
package/dist/themes.js
ADDED
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
// ─── テーマ ───────────────────────────────────────────────
|
|
2
|
+
export const THEMES = {
|
|
3
|
+
default: { className: "calendar-theme-default" },
|
|
4
|
+
modern: { className: "calendar-theme-modern" },
|
|
5
|
+
minimal: { className: "calendar-theme-minimal" },
|
|
6
|
+
rounded: { className: "calendar-theme-rounded" },
|
|
7
|
+
retro: { className: "calendar-theme-retro" },
|
|
8
|
+
};
|
|
9
|
+
export function resolveTheme(theme) {
|
|
10
|
+
if (theme === undefined)
|
|
11
|
+
return THEMES.default;
|
|
12
|
+
if (typeof theme === "string")
|
|
13
|
+
return THEMES[theme] ?? THEMES.default;
|
|
14
|
+
return theme;
|
|
15
|
+
}
|
|
16
|
+
/** 各カラースキームで定義する CSS 変数(SCHEME_VALUES の並び順の定義) */
|
|
17
|
+
const SCHEME_KEYS = [
|
|
18
|
+
"--cal-bg",
|
|
19
|
+
"--cal-fg",
|
|
20
|
+
"--cal-accent",
|
|
21
|
+
"--cal-weekend-fg",
|
|
22
|
+
"--cal-border",
|
|
23
|
+
"--cal-header-bg",
|
|
24
|
+
"--cal-highlight-bg",
|
|
25
|
+
"--cal-highlight-fg",
|
|
26
|
+
"--cal-range-bg",
|
|
27
|
+
"--cal-today-bg",
|
|
28
|
+
"--cal-today-fg",
|
|
29
|
+
];
|
|
30
|
+
/** カラースキームごとの値を SCHEME_KEYS の並びで定義する */
|
|
31
|
+
const SCHEME_VALUES = {
|
|
32
|
+
default: [
|
|
33
|
+
"#ffffff",
|
|
34
|
+
"#1e293b",
|
|
35
|
+
"#dc2626",
|
|
36
|
+
"#94a3b8",
|
|
37
|
+
"#e2e8f0",
|
|
38
|
+
"#f8fafc",
|
|
39
|
+
"#dc2626",
|
|
40
|
+
"#ffffff",
|
|
41
|
+
"#fef3c7",
|
|
42
|
+
"#fff7ed",
|
|
43
|
+
"#c2410c",
|
|
44
|
+
],
|
|
45
|
+
ocean: [
|
|
46
|
+
"#f0f9ff",
|
|
47
|
+
"#0f172a",
|
|
48
|
+
"#0e7490",
|
|
49
|
+
"#7dd3fc",
|
|
50
|
+
"#bae6fd",
|
|
51
|
+
"#e0f2fe",
|
|
52
|
+
"#0e7490",
|
|
53
|
+
"#ffffff",
|
|
54
|
+
"#dff3fe",
|
|
55
|
+
"#cffafe",
|
|
56
|
+
"#155e75",
|
|
57
|
+
],
|
|
58
|
+
forest: [
|
|
59
|
+
"#f0fdf4",
|
|
60
|
+
"#052e16",
|
|
61
|
+
"#15803d",
|
|
62
|
+
"#86efac",
|
|
63
|
+
"#bbf7d0",
|
|
64
|
+
"#dcfce7",
|
|
65
|
+
"#15803d",
|
|
66
|
+
"#ffffff",
|
|
67
|
+
"#dcfce7",
|
|
68
|
+
"#f0fdf4",
|
|
69
|
+
"#166534",
|
|
70
|
+
],
|
|
71
|
+
sunset: [
|
|
72
|
+
"#fffaf5",
|
|
73
|
+
"#431407",
|
|
74
|
+
"#ea580c",
|
|
75
|
+
"#fdba74",
|
|
76
|
+
"#fed7aa",
|
|
77
|
+
"#ffedd5",
|
|
78
|
+
"#ea580c",
|
|
79
|
+
"#ffffff",
|
|
80
|
+
"#ffedd5",
|
|
81
|
+
"#fff7ed",
|
|
82
|
+
"#9a3412",
|
|
83
|
+
],
|
|
84
|
+
mono: [
|
|
85
|
+
"#ffffff",
|
|
86
|
+
"#111827",
|
|
87
|
+
"#374151",
|
|
88
|
+
"#d1d5db",
|
|
89
|
+
"#e5e7eb",
|
|
90
|
+
"#f3f4f6",
|
|
91
|
+
"#111827",
|
|
92
|
+
"#ffffff",
|
|
93
|
+
"#f3f4f6",
|
|
94
|
+
"#e5e7eb",
|
|
95
|
+
"#111827",
|
|
96
|
+
],
|
|
97
|
+
midnight: [
|
|
98
|
+
"#0f172a",
|
|
99
|
+
"#e2e8f0",
|
|
100
|
+
"#38bdf8",
|
|
101
|
+
"#475569",
|
|
102
|
+
"#1e293b",
|
|
103
|
+
"#1e293b",
|
|
104
|
+
"#38bdf8",
|
|
105
|
+
"#0f172a",
|
|
106
|
+
"#0c4a6e",
|
|
107
|
+
"#0c4a6e",
|
|
108
|
+
"#7dd3fc",
|
|
109
|
+
],
|
|
110
|
+
blossom: [
|
|
111
|
+
"#fffaff",
|
|
112
|
+
"#500724",
|
|
113
|
+
"#db2777",
|
|
114
|
+
"#f9a8d4",
|
|
115
|
+
"#fbcfe8",
|
|
116
|
+
"#fdf2f8",
|
|
117
|
+
"#db2777",
|
|
118
|
+
"#ffffff",
|
|
119
|
+
"#fce7f3",
|
|
120
|
+
"#fdf2f8",
|
|
121
|
+
"#be185d",
|
|
122
|
+
],
|
|
123
|
+
};
|
|
124
|
+
export const COLOR_SCHEMES = Object.fromEntries(Object.entries(SCHEME_VALUES).map(([name, values]) => [
|
|
125
|
+
name,
|
|
126
|
+
Object.fromEntries(SCHEME_KEYS.map((key, i) => [key, values[i]])),
|
|
127
|
+
]));
|
|
128
|
+
export function resolveColorScheme(scheme) {
|
|
129
|
+
if (scheme === undefined)
|
|
130
|
+
return COLOR_SCHEMES.default;
|
|
131
|
+
if (typeof scheme === "string") {
|
|
132
|
+
return COLOR_SCHEMES[scheme] ?? COLOR_SCHEMES.default;
|
|
133
|
+
}
|
|
134
|
+
return scheme;
|
|
135
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import type { CalendarState, CalendarStateOptions, Direction, MonthDirection } from "@typescript-calendar-lib/tui";
|
|
2
|
+
export interface UseCalendarStateOptions extends Pick<CalendarStateOptions, "locale" | "weekStart" | "today" | "highlight" | "range"> {
|
|
3
|
+
initialYear?: number;
|
|
4
|
+
initialMonth?: number;
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* `useCalendarState` の first 引数。
|
|
8
|
+
* - 変更しない設定: オブジェクトを直接渡す
|
|
9
|
+
* - props や $state の値に追従させる: `() => ({ ... })` の getter を渡す。
|
|
10
|
+
* ($derived オブジェクトを渡しても、追跡は宣言したファイル内でのみ
|
|
11
|
+
* 有効なため、別モジュールのフックには伝わらない点に注意)
|
|
12
|
+
*/
|
|
13
|
+
export type UseCalendarStateOptionsInput = UseCalendarStateOptions | (() => UseCalendarStateOptions);
|
|
14
|
+
export interface UseCalendarStateReturn {
|
|
15
|
+
/** 現在のカレンダー状態 */
|
|
16
|
+
state: CalendarState;
|
|
17
|
+
/** カーソルを指定方向に移動 (wrap around) */
|
|
18
|
+
moveCursor: (direction: Direction) => void;
|
|
19
|
+
/** 前月/翌月へ移動 */
|
|
20
|
+
goNext: () => void;
|
|
21
|
+
goPrev: () => void;
|
|
22
|
+
/** 今日の月へジャンプ */
|
|
23
|
+
goToday: () => void;
|
|
24
|
+
/** カーソル位置の日付を選択 */
|
|
25
|
+
selectDate: () => void;
|
|
26
|
+
/** 選択を解除 */
|
|
27
|
+
clearSelection: () => void;
|
|
28
|
+
/** カーソル位置の日付(null の場合あり) */
|
|
29
|
+
cursorDate: Date | null;
|
|
30
|
+
/** 選択済み日付(null の場合あり) */
|
|
31
|
+
selectedDate: Date | null;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* tui の不変状態機械を包む Svelte 5 (runes) のリアクティブ状態。
|
|
35
|
+
* カレンダーのインタラクティブ操作をシンプルに利用できる。
|
|
36
|
+
*
|
|
37
|
+
* リアクティブに追従させたい設定(コンポーネントの props や $state の値)が
|
|
38
|
+
* ある場合は、`options` にリテラルではなく getter 関数を渡す:
|
|
39
|
+
*
|
|
40
|
+
* ```svelte
|
|
41
|
+
* <script>
|
|
42
|
+
* let { highlight, today } = $props();
|
|
43
|
+
* const cal = useCalendarState(() => ({
|
|
44
|
+
* initialYear: 2026,
|
|
45
|
+
* initialMonth: 9,
|
|
46
|
+
* today,
|
|
47
|
+
* highlight,
|
|
48
|
+
* }));
|
|
49
|
+
* </script>
|
|
50
|
+
* ```
|
|
51
|
+
*
|
|
52
|
+
* getter が返す値が変わると、カーソル・選択を保ったまま状態が再構築される。
|
|
53
|
+
* 値が同じままの再評価では何もしない。プレーンなオブジェクトを渡した場合は
|
|
54
|
+
* 初期値としてのみ使われ、以降は更新されない。
|
|
55
|
+
*
|
|
56
|
+
* `today` を省略した場合、最初に解決された値が状態に固定され、以降も
|
|
57
|
+
* 引き継がれる(毎回 `new Date()` を渡さなくてよい)。
|
|
58
|
+
*/
|
|
59
|
+
export declare function useCalendarState(options?: UseCalendarStateOptionsInput): UseCalendarStateReturn;
|
|
60
|
+
export type { Direction, MonthDirection };
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { buildMonthData, clearSelection, createCalendarState, getCursorDate, getSelectedDate, goToToday, moveCursor, navigateMonth, rebuildState, resolveOptions, selectDate, } from "@typescript-calendar-lib/tui";
|
|
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
|
+
/**
|
|
18
|
+
* tui の不変状態機械を包む Svelte 5 (runes) のリアクティブ状態。
|
|
19
|
+
* カレンダーのインタラクティブ操作をシンプルに利用できる。
|
|
20
|
+
*
|
|
21
|
+
* リアクティブに追従させたい設定(コンポーネントの props や $state の値)が
|
|
22
|
+
* ある場合は、`options` にリテラルではなく getter 関数を渡す:
|
|
23
|
+
*
|
|
24
|
+
* ```svelte
|
|
25
|
+
* <script>
|
|
26
|
+
* let { highlight, today } = $props();
|
|
27
|
+
* const cal = useCalendarState(() => ({
|
|
28
|
+
* initialYear: 2026,
|
|
29
|
+
* initialMonth: 9,
|
|
30
|
+
* today,
|
|
31
|
+
* highlight,
|
|
32
|
+
* }));
|
|
33
|
+
* </script>
|
|
34
|
+
* ```
|
|
35
|
+
*
|
|
36
|
+
* getter が返す値が変わると、カーソル・選択を保ったまま状態が再構築される。
|
|
37
|
+
* 値が同じままの再評価では何もしない。プレーンなオブジェクトを渡した場合は
|
|
38
|
+
* 初期値としてのみ使われ、以降は更新されない。
|
|
39
|
+
*
|
|
40
|
+
* `today` を省略した場合、最初に解決された値が状態に固定され、以降も
|
|
41
|
+
* 引き継がれる(毎回 `new Date()` を渡さなくてよい)。
|
|
42
|
+
*/
|
|
43
|
+
export function useCalendarState(options = {}) {
|
|
44
|
+
const getter = typeof options === "function" ? options : () => options;
|
|
45
|
+
let state = $state(createCalendarState({
|
|
46
|
+
initialYear: getter().initialYear,
|
|
47
|
+
initialMonth: getter().initialMonth,
|
|
48
|
+
today: getter().today,
|
|
49
|
+
locale: getter().locale,
|
|
50
|
+
weekStart: getter().weekStart,
|
|
51
|
+
highlight: getter().highlight,
|
|
52
|
+
range: getter().range,
|
|
53
|
+
}));
|
|
54
|
+
// options の「値の変化」を検知して状態を再構築する。
|
|
55
|
+
// getter を effect 内で呼び出すことで、呼び出し元ファイルで追跡される
|
|
56
|
+
// props / $state への読み取りが依存として登録され、値が変わると
|
|
57
|
+
// この effect が再実行される。値が同じままの再評価では何もしない。
|
|
58
|
+
let prev = getter();
|
|
59
|
+
$effect(() => {
|
|
60
|
+
const next = getter();
|
|
61
|
+
if (sameOptionsValue(prev, next))
|
|
62
|
+
return;
|
|
63
|
+
// initialYear/initialMonth は「初期値」。値が実際に変わった時だけ適用し、
|
|
64
|
+
// 変わっていない場合はナビゲーション後の現在位置を維持する。
|
|
65
|
+
const prevInitialYear = prev.initialYear;
|
|
66
|
+
const prevInitialMonth = prev.initialMonth;
|
|
67
|
+
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);
|
|
85
|
+
});
|
|
86
|
+
return {
|
|
87
|
+
get state() {
|
|
88
|
+
return state;
|
|
89
|
+
},
|
|
90
|
+
get cursorDate() {
|
|
91
|
+
return getCursorDate(state);
|
|
92
|
+
},
|
|
93
|
+
get selectedDate() {
|
|
94
|
+
return getSelectedDate(state);
|
|
95
|
+
},
|
|
96
|
+
moveCursor: (direction) => {
|
|
97
|
+
state = moveCursor(state, direction);
|
|
98
|
+
},
|
|
99
|
+
goNext: () => {
|
|
100
|
+
state = navigateMonth(state, "next");
|
|
101
|
+
},
|
|
102
|
+
goPrev: () => {
|
|
103
|
+
state = navigateMonth(state, "prev");
|
|
104
|
+
},
|
|
105
|
+
goToday: () => {
|
|
106
|
+
state = goToToday(state);
|
|
107
|
+
},
|
|
108
|
+
selectDate: () => {
|
|
109
|
+
state = selectDate(state);
|
|
110
|
+
},
|
|
111
|
+
clearSelection: () => {
|
|
112
|
+
state = clearSelection(state);
|
|
113
|
+
},
|
|
114
|
+
};
|
|
115
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@typescript-calendar-lib/svelte",
|
|
3
|
+
"version": "0.2.7",
|
|
4
|
+
"description": "Svelte 5 components and reactive state for typescript-calendar-lib",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"svelte": "./dist/index.js",
|
|
7
|
+
"main": "./dist/index.js",
|
|
8
|
+
"module": "./dist/index.js",
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
|
+
"exports": {
|
|
11
|
+
".": {
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
13
|
+
"svelte": "./dist/index.js",
|
|
14
|
+
"default": "./dist/index.js"
|
|
15
|
+
},
|
|
16
|
+
"./calendar.css": "./dist/calendar.css"
|
|
17
|
+
},
|
|
18
|
+
"files": [
|
|
19
|
+
"dist"
|
|
20
|
+
],
|
|
21
|
+
"scripts": {
|
|
22
|
+
"build": "svelte-package -i src",
|
|
23
|
+
"prepack": "pnpm build",
|
|
24
|
+
"typecheck": "svelte-check --tsconfig ./tsconfig.json --fail-on-warnings"
|
|
25
|
+
},
|
|
26
|
+
"keywords": [
|
|
27
|
+
"calendar",
|
|
28
|
+
"svelte",
|
|
29
|
+
"component",
|
|
30
|
+
"ui"
|
|
31
|
+
],
|
|
32
|
+
"license": "MIT",
|
|
33
|
+
"peerDependencies": {
|
|
34
|
+
"svelte": "^5"
|
|
35
|
+
},
|
|
36
|
+
"dependencies": {
|
|
37
|
+
"@typescript-calendar-lib/core": "workspace:*",
|
|
38
|
+
"@typescript-calendar-lib/tui": "workspace:*"
|
|
39
|
+
},
|
|
40
|
+
"devDependencies": {
|
|
41
|
+
"@sveltejs/package": "^2.5.8",
|
|
42
|
+
"@sveltejs/vite-plugin-svelte": "^6",
|
|
43
|
+
"@testing-library/jest-dom": "^7.0.1",
|
|
44
|
+
"@testing-library/svelte": "^5",
|
|
45
|
+
"jsdom": "^26",
|
|
46
|
+
"svelte-check": "^4",
|
|
47
|
+
"typescript": "^5.9.0",
|
|
48
|
+
"vitest": "^5"
|
|
49
|
+
}
|
|
50
|
+
}
|