@day-kit/vue 0.0.0-stage → 1.1.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/LICENSE +21 -0
- package/README.md +82 -2
- package/dist/index.d.ts +25 -0
- package/dist/index.js +66 -0
- package/dist/index.js.map +1 -0
- package/package.json +53 -3
- package/src/calendar-state-source.ts +5 -0
- package/src/index.ts +3 -0
- package/src/track-calls.ts +17 -0
- package/src/use-calendar-state.ts +23 -0
- package/src/use-calendar.ts +64 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 day-kit contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,83 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @day-kit/vue
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Vue 3 bindings for [`@day-kit/core`](https://github.com/lorof/day-kit/tree/main/packages/core#readme).
|
|
4
|
+
|
|
5
|
+
```sh
|
|
6
|
+
npm install @day-kit/core @day-kit/vue
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
<!-- example: examples/vue-date-picker/src/date-picker.vue -->
|
|
10
|
+
|
|
11
|
+
```vue
|
|
12
|
+
<script setup lang="ts">
|
|
13
|
+
import { monthViewFeature } from '@day-kit/core'
|
|
14
|
+
import { createGregorianDateEngine } from '@day-kit/core/temporal-gregorian'
|
|
15
|
+
import { useCalendar } from '@day-kit/vue'
|
|
16
|
+
import { computed } from 'vue'
|
|
17
|
+
|
|
18
|
+
// Module scope in a real app: the engine and the feature list must be stable.
|
|
19
|
+
const dateEngine = createGregorianDateEngine()
|
|
20
|
+
const features = [monthViewFeature] as const
|
|
21
|
+
|
|
22
|
+
const props = defineProps<{ timeZone: string }>()
|
|
23
|
+
const emit = defineEmits<{ change: [date: Date | null] }>()
|
|
24
|
+
|
|
25
|
+
// A getter keeps the options reactive (here: the timeZone prop).
|
|
26
|
+
const calendar = useCalendar(() => ({
|
|
27
|
+
dateEngine,
|
|
28
|
+
features,
|
|
29
|
+
locale: 'en-US',
|
|
30
|
+
timeZone: props.timeZone,
|
|
31
|
+
selectionMode: 'single',
|
|
32
|
+
onSelectionChange: (date: Date | null) => {
|
|
33
|
+
emit('change', date)
|
|
34
|
+
},
|
|
35
|
+
}))
|
|
36
|
+
const month = computed(() => calendar.getMonthModel())
|
|
37
|
+
</script>
|
|
38
|
+
|
|
39
|
+
<template>
|
|
40
|
+
<div>
|
|
41
|
+
<div class="toolbar">
|
|
42
|
+
<h2>{{ month.label }}</h2>
|
|
43
|
+
<button type="button" aria-label="Previous month" @click="calendar.previous">‹</button>
|
|
44
|
+
<button type="button" aria-label="Next month" @click="calendar.next">›</button>
|
|
45
|
+
</div>
|
|
46
|
+
<div class="weekdays">
|
|
47
|
+
<div v-for="weekday in month.weekdays" :key="weekday.weekday">{{ weekday.shortLabel }}</div>
|
|
48
|
+
</div>
|
|
49
|
+
<div class="days">
|
|
50
|
+
<template v-for="week in month.weeks" :key="week.id">
|
|
51
|
+
<button
|
|
52
|
+
v-for="day in week.days"
|
|
53
|
+
:key="day.id"
|
|
54
|
+
type="button"
|
|
55
|
+
:disabled="day.isDisabled"
|
|
56
|
+
:aria-label="day.fullLabel"
|
|
57
|
+
:aria-pressed="day.isSelected"
|
|
58
|
+
:data-today="day.isToday || undefined"
|
|
59
|
+
:data-outside="day.isOutsideMonth || undefined"
|
|
60
|
+
@click="day.select"
|
|
61
|
+
>
|
|
62
|
+
{{ day.label }}
|
|
63
|
+
</button>
|
|
64
|
+
</template>
|
|
65
|
+
</div>
|
|
66
|
+
</div>
|
|
67
|
+
</template>
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
- `useCalendar(options)` accepts a plain object, a ref or a getter. Use a getter or a ref for options
|
|
71
|
+
that change (props, refs). Every method call on the returned calendar is tracked, so templates and
|
|
72
|
+
`computed` values update when the state or the options change.
|
|
73
|
+
- State can be uncontrolled, fully controlled (`state` + `onStateChange`) or controlled per slice.
|
|
74
|
+
- `useCalendarState(calendar, selector, isEqual?)` returns a readonly ref that changes only when the
|
|
75
|
+
selected value changes.
|
|
76
|
+
- SSR: pass `timeZone` and a fixed `clock` (or `initialState.date`) for hydration-stable output.
|
|
77
|
+
- All `@day-kit/*` packages share one version: use the same version of `@day-kit/core` and
|
|
78
|
+
`@day-kit/vue`.
|
|
79
|
+
|
|
80
|
+
Full documentation: the [repository README](https://github.com/lorof/day-kit#readme) and
|
|
81
|
+
[docs/](https://github.com/lorof/day-kit/tree/main/docs).
|
|
82
|
+
|
|
83
|
+
MIT
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { AnyCalendarFeature, Calendar, CalendarOptions, CalendarParams, SelectionMode } from "@day-kit/core";
|
|
2
|
+
import { MaybeRefOrGetter, Ref } from "vue";
|
|
3
|
+
/**
|
|
4
|
+
* Creates a calendar for the current component (or effect scope) and keeps it in sync with `options`.
|
|
5
|
+
*
|
|
6
|
+
* - `options` is a plain object, a ref or a getter. Use a getter (`() => ({ locale: locale.value, … })`)
|
|
7
|
+
* or a ref for options that change; a plain object is read once.
|
|
8
|
+
* - Every method call on the returned calendar is tracked, so templates and `computed` values that read
|
|
9
|
+
* models update when the calendar state or the options change.
|
|
10
|
+
* - Option changes are applied before the component re-renders; option-driven transitions (e.g. a
|
|
11
|
+
* time-zone change) and their callbacks run then too, never during render.
|
|
12
|
+
* - The subscription is removed when the scope is disposed.
|
|
13
|
+
*/
|
|
14
|
+
export declare function useCalendar<const TFeatures extends readonly AnyCalendarFeature[], const TMode extends SelectionMode = "none">(options: MaybeRefOrGetter<CalendarOptions<TFeatures, CalendarParams<TMode>>>): Calendar<TFeatures, CalendarParams<TMode>>;
|
|
15
|
+
/** The subscription surface of a calendar instance. */
|
|
16
|
+
interface CalendarStateSource<TState> {
|
|
17
|
+
readonly getState: () => TState;
|
|
18
|
+
readonly subscribe: (listener: () => void) => () => void;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* A ref with a slice of calendar state. It changes only when the selected value changes according to
|
|
22
|
+
* `isEqual` (default `Object.is`), so dependents do not update for unrelated state changes.
|
|
23
|
+
*/
|
|
24
|
+
export declare function useCalendarState<TState, TSelected>(calendar: CalendarStateSource<TState>, selector: (state: TState) => TSelected, isEqual?: (a: TSelected, b: TSelected) => boolean): Readonly<Ref<TSelected>>;
|
|
25
|
+
export type { CalendarStateSource };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { createCalendar } from "@day-kit/core";
|
|
2
|
+
import { getCurrentScope, onScopeDispose, shallowReadonly, shallowRef, toRaw, toValue, watch } from "vue";
|
|
3
|
+
// src/track-calls.ts
|
|
4
|
+
/**
|
|
5
|
+
* Returns a frozen copy of `target` whose methods call `track()` first. Reading a model through any
|
|
6
|
+
* method inside a render or `computed` therefore registers a Vue dependency on the calendar.
|
|
7
|
+
*/
|
|
8
|
+
function trackCalls(target, track) {
|
|
9
|
+
const tracked = {};
|
|
10
|
+
for (const [key, value] of Object.entries(target)) tracked[key] = typeof value === "function" ? (...args) => {
|
|
11
|
+
track();
|
|
12
|
+
return value(...args);
|
|
13
|
+
} : value;
|
|
14
|
+
return Object.freeze(tracked);
|
|
15
|
+
}
|
|
16
|
+
// src/use-calendar.ts
|
|
17
|
+
/**
|
|
18
|
+
* Creates a calendar for the current component (or effect scope) and keeps it in sync with `options`.
|
|
19
|
+
*
|
|
20
|
+
* - `options` is a plain object, a ref or a getter. Use a getter (`() => ({ locale: locale.value, … })`)
|
|
21
|
+
* or a ref for options that change; a plain object is read once.
|
|
22
|
+
* - Every method call on the returned calendar is tracked, so templates and `computed` values that read
|
|
23
|
+
* models update when the calendar state or the options change.
|
|
24
|
+
* - Option changes are applied before the component re-renders; option-driven transitions (e.g. a
|
|
25
|
+
* time-zone change) and their callbacks run then too, never during render.
|
|
26
|
+
* - The subscription is removed when the scope is disposed.
|
|
27
|
+
*/
|
|
28
|
+
function useCalendar(options) {
|
|
29
|
+
const read = () => toRaw(toValue(options));
|
|
30
|
+
const calendar = createCalendar(read());
|
|
31
|
+
const version = shallowRef(0);
|
|
32
|
+
const invalidate = () => {
|
|
33
|
+
version.value++;
|
|
34
|
+
};
|
|
35
|
+
const unsubscribe = calendar.subscribe(invalidate);
|
|
36
|
+
watch(read, (next) => {
|
|
37
|
+
calendar.setOptions(next, { notify: false });
|
|
38
|
+
calendar.commitOptions();
|
|
39
|
+
invalidate();
|
|
40
|
+
});
|
|
41
|
+
if (getCurrentScope() !== void 0) onScopeDispose(() => {
|
|
42
|
+
unsubscribe();
|
|
43
|
+
calendar.destroy();
|
|
44
|
+
});
|
|
45
|
+
return trackCalls(calendar, () => version.value);
|
|
46
|
+
}
|
|
47
|
+
// src/use-calendar-state.ts
|
|
48
|
+
/**
|
|
49
|
+
* A ref with a slice of calendar state. It changes only when the selected value changes according to
|
|
50
|
+
* `isEqual` (default `Object.is`), so dependents do not update for unrelated state changes.
|
|
51
|
+
*/
|
|
52
|
+
function useCalendarState(calendar, selector, isEqual = Object.is) {
|
|
53
|
+
let current = selector(calendar.getState());
|
|
54
|
+
const selected = shallowRef(current);
|
|
55
|
+
const unsubscribe = calendar.subscribe(() => {
|
|
56
|
+
const next = selector(calendar.getState());
|
|
57
|
+
if (isEqual(current, next)) return;
|
|
58
|
+
current = next;
|
|
59
|
+
selected.value = next;
|
|
60
|
+
});
|
|
61
|
+
if (getCurrentScope() !== void 0) onScopeDispose(unsubscribe);
|
|
62
|
+
return shallowReadonly(selected);
|
|
63
|
+
}
|
|
64
|
+
export { useCalendar, useCalendarState };
|
|
65
|
+
|
|
66
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../src/track-calls.ts","../src/use-calendar.ts","../src/use-calendar-state.ts"],"sourcesContent":["/**\n * Returns a frozen copy of `target` whose methods call `track()` first. Reading a model through any\n * method inside a render or `computed` therefore registers a Vue dependency on the calendar.\n */\nexport function trackCalls<TTarget extends object>(target: TTarget, track: () => unknown): TTarget {\n const tracked: Record<string, unknown> = {}\n for (const [key, value] of Object.entries(target)) {\n tracked[key] =\n typeof value === 'function'\n ? (...args: unknown[]): unknown => {\n track()\n return (value as (...parameters: unknown[]) => unknown)(...args)\n }\n : value\n }\n return Object.freeze(tracked) as TTarget\n}\n","import {\n createCalendar,\n type AnyCalendarFeature,\n type Calendar,\n type CalendarOptions,\n type CalendarParams,\n type SelectionMode,\n} from '@day-kit/core'\nimport {\n getCurrentScope,\n onScopeDispose,\n shallowRef,\n toRaw,\n toValue,\n watch,\n type MaybeRefOrGetter,\n} from 'vue'\nimport { trackCalls } from './track-calls'\n\n/**\n * Creates a calendar for the current component (or effect scope) and keeps it in sync with `options`.\n *\n * - `options` is a plain object, a ref or a getter. Use a getter (`() => ({ locale: locale.value, … })`)\n * or a ref for options that change; a plain object is read once.\n * - Every method call on the returned calendar is tracked, so templates and `computed` values that read\n * models update when the calendar state or the options change.\n * - Option changes are applied before the component re-renders; option-driven transitions (e.g. a\n * time-zone change) and their callbacks run then too, never during render.\n * - The subscription is removed when the scope is disposed.\n */\nexport function useCalendar<\n const TFeatures extends readonly AnyCalendarFeature[],\n const TMode extends SelectionMode = 'none',\n>(\n options: MaybeRefOrGetter<CalendarOptions<TFeatures, CalendarParams<TMode>>>,\n): Calendar<TFeatures, CalendarParams<TMode>> {\n // A reactive() options object is unwrapped, so the calendar compares the consumer's raw arrays and\n // functions by reference.\n const read = () => toRaw(toValue(options))\n const calendar = createCalendar<TFeatures, TMode>(read())\n\n const version = shallowRef(0)\n const invalidate = (): void => {\n version.value++\n }\n const unsubscribe = calendar.subscribe(invalidate)\n\n watch(read, (next) => {\n calendar.setOptions(next, { notify: false })\n calendar.commitOptions()\n // Options can change models (locale, time zone) without changing the state snapshot.\n invalidate()\n })\n\n if (getCurrentScope() !== undefined) {\n onScopeDispose(() => {\n unsubscribe()\n calendar.destroy()\n })\n }\n\n // Reading the version inside a render or computed makes it depend on this calendar.\n return trackCalls(calendar, () => version.value)\n}\n","import { getCurrentScope, onScopeDispose, shallowReadonly, shallowRef, type Ref } from 'vue'\nimport type { CalendarStateSource } from './calendar-state-source'\n\n/**\n * A ref with a slice of calendar state. It changes only when the selected value changes according to\n * `isEqual` (default `Object.is`), so dependents do not update for unrelated state changes.\n */\nexport function useCalendarState<TState, TSelected>(\n calendar: CalendarStateSource<TState>,\n selector: (state: TState) => TSelected,\n isEqual: (a: TSelected, b: TSelected) => boolean = Object.is,\n): Readonly<Ref<TSelected>> {\n let current = selector(calendar.getState())\n const selected = shallowRef(current)\n const unsubscribe = calendar.subscribe(() => {\n const next = selector(calendar.getState())\n if (isEqual(current, next)) return\n current = next\n selected.value = next\n })\n if (getCurrentScope() !== undefined) onScopeDispose(unsubscribe)\n return shallowReadonly(selected)\n}\n"],"mappings":";;;;;;;AAIA,SAAgB,WAAmC,QAAiB,OAA+B;CACjG,MAAM,UAAmC,CAAC;CAC1C,KAAK,MAAM,CAAC,KAAK,UAAU,OAAO,QAAQ,MAAM,GAC9C,QAAQ,OACN,OAAO,UAAU,cACZ,GAAG,SAA6B;EAC/B,MAAM;EACN,OAAQ,MAAgD,GAAG,IAAI;CACjE,IACA;CAER,OAAO,OAAO,OAAO,OAAO;AAC9B;;;;;;;;;;;;;ACcA,SAAgB,YAId,SAC4C;CAG5C,MAAM,aAAa,MAAM,QAAQ,OAAO,CAAC;CACzC,MAAM,WAAW,eAAiC,KAAK,CAAC;CAExD,MAAM,UAAU,WAAW,CAAC;CAC5B,MAAM,mBAAyB;EAC7B,QAAQ;CACV;CACA,MAAM,cAAc,SAAS,UAAU,UAAU;CAEjD,MAAM,OAAO,SAAS;EACpB,SAAS,WAAW,MAAM,EAAE,QAAQ,MAAM,CAAC;EAC3C,SAAS,cAAc;EAEvB,WAAW;CACb,CAAC;CAED,IAAI,gBAAgB,MAAM,KAAA,GACxB,qBAAqB;EACnB,YAAY;EACZ,SAAS,QAAQ;CACnB,CAAC;CAIH,OAAO,WAAW,gBAAgB,QAAQ,KAAK;AACjD;;;;;;ACxDA,SAAgB,iBACd,UACA,UACA,UAAmD,OAAO,IAChC;CAC1B,IAAI,UAAU,SAAS,SAAS,SAAS,CAAC;CAC1C,MAAM,WAAW,WAAW,OAAO;CACnC,MAAM,cAAc,SAAS,gBAAgB;EAC3C,MAAM,OAAO,SAAS,SAAS,SAAS,CAAC;EACzC,IAAI,QAAQ,SAAS,IAAI,GAAG;EAC5B,UAAU;EACV,SAAS,QAAQ;CACnB,CAAC;CACD,IAAI,gBAAgB,MAAM,KAAA,GAAW,eAAe,WAAW;CAC/D,OAAO,gBAAgB,QAAQ;AACjC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,56 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@day-kit/vue",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "1.1.0",
|
|
4
|
+
"description": "Vue 3 bindings for @day-kit/core.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/lorof/day-kit.git",
|
|
9
|
+
"directory": "packages/vue"
|
|
10
|
+
},
|
|
11
|
+
"homepage": "https://github.com/lorof/day-kit#readme",
|
|
12
|
+
"bugs": {
|
|
13
|
+
"url": "https://github.com/lorof/day-kit/issues"
|
|
14
|
+
},
|
|
15
|
+
"keywords": [
|
|
16
|
+
"calendar",
|
|
17
|
+
"headless",
|
|
18
|
+
"vue",
|
|
19
|
+
"date-picker",
|
|
20
|
+
"scheduler"
|
|
21
|
+
],
|
|
22
|
+
"engines": {
|
|
23
|
+
"node": ">=20.19"
|
|
24
|
+
},
|
|
25
|
+
"type": "module",
|
|
26
|
+
"sideEffects": false,
|
|
27
|
+
"exports": {
|
|
28
|
+
".": {
|
|
29
|
+
"@day-kit/source": "./src/index.ts",
|
|
30
|
+
"types": "./dist/index.d.ts",
|
|
31
|
+
"default": "./dist/index.js"
|
|
32
|
+
},
|
|
33
|
+
"./package.json": "./package.json"
|
|
34
|
+
},
|
|
35
|
+
"files": [
|
|
36
|
+
"dist",
|
|
37
|
+
"src"
|
|
38
|
+
],
|
|
39
|
+
"dependencies": {
|
|
40
|
+
"@day-kit/core": "1.1.0"
|
|
41
|
+
},
|
|
42
|
+
"peerDependencies": {
|
|
43
|
+
"vue": "^3.5.0"
|
|
44
|
+
},
|
|
45
|
+
"devDependencies": {
|
|
46
|
+
"@vue/test-utils": "2.6.0",
|
|
47
|
+
"jsdom": "31.0.1",
|
|
48
|
+
"vue": "3.5.46"
|
|
49
|
+
},
|
|
50
|
+
"scripts": {
|
|
51
|
+
"build": "tsdown",
|
|
52
|
+
"dev": "tsdown --watch",
|
|
53
|
+
"typecheck": "tsc --noEmit",
|
|
54
|
+
"test": "vitest run"
|
|
55
|
+
}
|
|
6
56
|
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns a frozen copy of `target` whose methods call `track()` first. Reading a model through any
|
|
3
|
+
* method inside a render or `computed` therefore registers a Vue dependency on the calendar.
|
|
4
|
+
*/
|
|
5
|
+
export function trackCalls<TTarget extends object>(target: TTarget, track: () => unknown): TTarget {
|
|
6
|
+
const tracked: Record<string, unknown> = {}
|
|
7
|
+
for (const [key, value] of Object.entries(target)) {
|
|
8
|
+
tracked[key] =
|
|
9
|
+
typeof value === 'function'
|
|
10
|
+
? (...args: unknown[]): unknown => {
|
|
11
|
+
track()
|
|
12
|
+
return (value as (...parameters: unknown[]) => unknown)(...args)
|
|
13
|
+
}
|
|
14
|
+
: value
|
|
15
|
+
}
|
|
16
|
+
return Object.freeze(tracked) as TTarget
|
|
17
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { getCurrentScope, onScopeDispose, shallowReadonly, shallowRef, type Ref } from 'vue'
|
|
2
|
+
import type { CalendarStateSource } from './calendar-state-source'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* A ref with a slice of calendar state. It changes only when the selected value changes according to
|
|
6
|
+
* `isEqual` (default `Object.is`), so dependents do not update for unrelated state changes.
|
|
7
|
+
*/
|
|
8
|
+
export function useCalendarState<TState, TSelected>(
|
|
9
|
+
calendar: CalendarStateSource<TState>,
|
|
10
|
+
selector: (state: TState) => TSelected,
|
|
11
|
+
isEqual: (a: TSelected, b: TSelected) => boolean = Object.is,
|
|
12
|
+
): Readonly<Ref<TSelected>> {
|
|
13
|
+
let current = selector(calendar.getState())
|
|
14
|
+
const selected = shallowRef(current)
|
|
15
|
+
const unsubscribe = calendar.subscribe(() => {
|
|
16
|
+
const next = selector(calendar.getState())
|
|
17
|
+
if (isEqual(current, next)) return
|
|
18
|
+
current = next
|
|
19
|
+
selected.value = next
|
|
20
|
+
})
|
|
21
|
+
if (getCurrentScope() !== undefined) onScopeDispose(unsubscribe)
|
|
22
|
+
return shallowReadonly(selected)
|
|
23
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createCalendar,
|
|
3
|
+
type AnyCalendarFeature,
|
|
4
|
+
type Calendar,
|
|
5
|
+
type CalendarOptions,
|
|
6
|
+
type CalendarParams,
|
|
7
|
+
type SelectionMode,
|
|
8
|
+
} from '@day-kit/core'
|
|
9
|
+
import {
|
|
10
|
+
getCurrentScope,
|
|
11
|
+
onScopeDispose,
|
|
12
|
+
shallowRef,
|
|
13
|
+
toRaw,
|
|
14
|
+
toValue,
|
|
15
|
+
watch,
|
|
16
|
+
type MaybeRefOrGetter,
|
|
17
|
+
} from 'vue'
|
|
18
|
+
import { trackCalls } from './track-calls'
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Creates a calendar for the current component (or effect scope) and keeps it in sync with `options`.
|
|
22
|
+
*
|
|
23
|
+
* - `options` is a plain object, a ref or a getter. Use a getter (`() => ({ locale: locale.value, … })`)
|
|
24
|
+
* or a ref for options that change; a plain object is read once.
|
|
25
|
+
* - Every method call on the returned calendar is tracked, so templates and `computed` values that read
|
|
26
|
+
* models update when the calendar state or the options change.
|
|
27
|
+
* - Option changes are applied before the component re-renders; option-driven transitions (e.g. a
|
|
28
|
+
* time-zone change) and their callbacks run then too, never during render.
|
|
29
|
+
* - The subscription is removed when the scope is disposed.
|
|
30
|
+
*/
|
|
31
|
+
export function useCalendar<
|
|
32
|
+
const TFeatures extends readonly AnyCalendarFeature[],
|
|
33
|
+
const TMode extends SelectionMode = 'none',
|
|
34
|
+
>(
|
|
35
|
+
options: MaybeRefOrGetter<CalendarOptions<TFeatures, CalendarParams<TMode>>>,
|
|
36
|
+
): Calendar<TFeatures, CalendarParams<TMode>> {
|
|
37
|
+
// A reactive() options object is unwrapped, so the calendar compares the consumer's raw arrays and
|
|
38
|
+
// functions by reference.
|
|
39
|
+
const read = () => toRaw(toValue(options))
|
|
40
|
+
const calendar = createCalendar<TFeatures, TMode>(read())
|
|
41
|
+
|
|
42
|
+
const version = shallowRef(0)
|
|
43
|
+
const invalidate = (): void => {
|
|
44
|
+
version.value++
|
|
45
|
+
}
|
|
46
|
+
const unsubscribe = calendar.subscribe(invalidate)
|
|
47
|
+
|
|
48
|
+
watch(read, (next) => {
|
|
49
|
+
calendar.setOptions(next, { notify: false })
|
|
50
|
+
calendar.commitOptions()
|
|
51
|
+
// Options can change models (locale, time zone) without changing the state snapshot.
|
|
52
|
+
invalidate()
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
if (getCurrentScope() !== undefined) {
|
|
56
|
+
onScopeDispose(() => {
|
|
57
|
+
unsubscribe()
|
|
58
|
+
calendar.destroy()
|
|
59
|
+
})
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// Reading the version inside a render or computed makes it depend on this calendar.
|
|
63
|
+
return trackCalls(calendar, () => version.value)
|
|
64
|
+
}
|