@gobrand/react-calendar 0.0.16 → 0.0.18

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/dist/index.cjs CHANGED
@@ -1,14 +1,17 @@
1
1
  'use strict';
2
2
 
3
- var react = require('react');
3
+ var React = require('react');
4
4
  var calendarCore = require('@gobrand/calendar-core');
5
5
 
6
- // src/index.ts
7
- function useCalendar(options) {
8
- const [calendarRef] = react.useState(() => ({
6
+ function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
7
+
8
+ var React__default = /*#__PURE__*/_interopDefault(React);
9
+
10
+ function useCreateCalendar(options) {
11
+ const [calendarRef] = React.useState(() => ({
9
12
  current: calendarCore.createCalendar(options)
10
13
  }));
11
- const [state, setState] = react.useState(() => calendarRef.current.getState());
14
+ const [state, setState] = React.useState(() => calendarRef.current.getState());
12
15
  calendarRef.current.setOptions((prev) => ({
13
16
  ...prev,
14
17
  ...options,
@@ -24,8 +27,60 @@ function useCalendar(options) {
24
27
  }));
25
28
  return calendarRef.current;
26
29
  }
30
+ var CalendarContext = React.createContext(null);
31
+ function CalendarProvider({
32
+ calendar,
33
+ children
34
+ }) {
35
+ return React__default.default.createElement(
36
+ CalendarContext.Provider,
37
+ { value: calendar },
38
+ children
39
+ );
40
+ }
41
+ function useCalendar() {
42
+ const calendar = React.useContext(CalendarContext);
43
+ if (!calendar) {
44
+ throw new Error(
45
+ "useCalendar must be used within a CalendarProvider. Wrap your component tree with <CalendarProvider calendar={calendar}>."
46
+ );
47
+ }
48
+ return calendar;
49
+ }
50
+ function useView(options) {
51
+ const { data, name, calendar: explicitCalendar } = options;
52
+ const contextCalendar = React.useContext(CalendarContext);
53
+ const calendar = explicitCalendar ?? contextCalendar;
54
+ if (!calendar) {
55
+ throw new Error(
56
+ "No calendar found. Either wrap your component tree in <CalendarProvider> or pass calendar explicitly: useView({ calendar, data })."
57
+ );
58
+ }
59
+ const state = calendar.getState();
60
+ const effectiveView = name ?? state.currentView;
61
+ return React.useMemo(() => {
62
+ switch (effectiveView) {
63
+ case "month":
64
+ return { type: "month", data: calendar.getMonth(data) };
65
+ case "week":
66
+ return { type: "week", data: calendar.getWeek(data) };
67
+ case "day":
68
+ return { type: "day", data: calendar.getDay(data) };
69
+ default:
70
+ throw new Error(`Unknown view: ${effectiveView}`);
71
+ }
72
+ }, [
73
+ calendar,
74
+ data,
75
+ state.referenceDate.toString(),
76
+ effectiveView
77
+ ]);
78
+ }
27
79
 
80
+ exports.CalendarProvider = CalendarProvider;
28
81
  exports.useCalendar = useCalendar;
82
+ exports.useCreateCalendar = useCreateCalendar;
83
+ exports.useView = useView;
29
84
  Object.keys(calendarCore).forEach(function (k) {
30
85
  if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
31
86
  enumerable: true,
package/dist/index.d.ts CHANGED
@@ -1,4 +1,81 @@
1
+ import React from 'react';
1
2
  export * from '@gobrand/calendar-core';
2
- import { type Calendar, type CalendarOptions } from '@gobrand/calendar-core';
3
- export declare function useCalendar<TOptions extends CalendarOptions<any>>(options: TOptions): Calendar<TOptions['data'] extends (infer TItem)[] ? TItem : never, TOptions>;
3
+ import { type Calendar, type CalendarInstance, type CalendarOptions, type ViewType, type ViewResultFor } from '@gobrand/calendar-core';
4
+ /**
5
+ * Creates and manages a calendar instance with React state.
6
+ *
7
+ * @example
8
+ * const calendar = useCreateCalendar({
9
+ * views: { month: { accessor: { getDate: (p) => p.date } } },
10
+ * timeZone: 'America/New_York',
11
+ * });
12
+ */
13
+ export declare function useCreateCalendar<TItem, TOptions extends CalendarOptions<TItem>>(options: TOptions): Calendar<TItem, TOptions>;
14
+ export interface CalendarProviderProps<TItem, TOptions extends CalendarOptions<TItem>> {
15
+ calendar: Calendar<TItem, TOptions>;
16
+ children: React.ReactNode;
17
+ }
18
+ /**
19
+ * Provides a calendar instance to descendant components via context.
20
+ *
21
+ * @example
22
+ * const calendar = useCreateCalendar({ views, timeZone });
23
+ *
24
+ * return (
25
+ * <CalendarProvider calendar={calendar}>
26
+ * <CalendarToolbar />
27
+ * <Suspense fallback={<Loading />}>
28
+ * <CalendarGrid />
29
+ * </Suspense>
30
+ * </CalendarProvider>
31
+ * );
32
+ */
33
+ export declare function CalendarProvider<TItem, TOptions extends CalendarOptions<TItem>>({ calendar, children, }: CalendarProviderProps<TItem, TOptions>): React.FunctionComponentElement<React.ProviderProps<CalendarInstance<unknown> | null>>;
34
+ /**
35
+ * Gets the calendar instance from context with typed item data.
36
+ * Must be used within a CalendarProvider.
37
+ *
38
+ * @example
39
+ * function CalendarToolbar() {
40
+ * const calendar = useCalendar<Post>();
41
+ * // calendar.getMonth(posts) returns CalendarMonth<Post>
42
+ * // Items are typed as Post, not any
43
+ * return (
44
+ * <div>
45
+ * <span>{calendar.getTitle()}</span>
46
+ * <button onClick={() => calendar.next()}>Next</button>
47
+ * </div>
48
+ * );
49
+ * }
50
+ */
51
+ export declare function useCalendar<TItem = unknown>(): CalendarInstance<TItem>;
52
+ export interface UseViewOptions<TItem, V extends ViewType | undefined = undefined> {
53
+ /** The data items to place on the calendar */
54
+ data: TItem[];
55
+ /** Override the current view. If not specified, uses calendar.currentView */
56
+ name?: V;
57
+ /** Explicit calendar instance. If not specified, uses context */
58
+ calendar?: CalendarInstance<TItem>;
59
+ }
60
+ /**
61
+ * Gets memoized view data for the calendar.
62
+ *
63
+ * @example
64
+ * // Dynamic view - returns discriminated union based on currentView
65
+ * const view = useView({ data: posts });
66
+ * if (view.type === 'month') {
67
+ * return <MonthGrid month={view.data} />;
68
+ * }
69
+ *
70
+ * @example
71
+ * // Specific view - returns narrowed type
72
+ * const month = useView({ data: posts, name: 'month' });
73
+ * // month.type is always 'month', month.data is CalendarMonth<Post>
74
+ *
75
+ * @example
76
+ * // Dual view
77
+ * const month = useView({ data: posts, name: 'month' });
78
+ * const week = useView({ data: posts, name: 'week' });
79
+ */
80
+ export declare function useView<TItem, V extends ViewType | undefined = undefined>(options: UseViewOptions<TItem, V>): ViewResultFor<TItem, V>;
4
81
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA,cAAc,wBAAwB,CAAC;AAEvC,OAAO,EAEL,KAAK,QAAQ,EACb,KAAK,eAAe,EACrB,MAAM,wBAAwB,CAAC;AAEhC,wBAAgB,WAAW,CAAC,QAAQ,SAAS,eAAe,CAAC,GAAG,CAAC,EAC/D,OAAO,EAAE,QAAQ,GAChB,QAAQ,CACT,QAAQ,CAAC,MAAM,CAAC,SAAS,CAAC,MAAM,KAAK,CAAC,EAAE,GAAG,KAAK,GAAG,KAAK,EACxD,QAAQ,CACT,CA6BA"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA,OAAO,KAAuD,MAAM,OAAO,CAAC;AAE5E,cAAc,wBAAwB,CAAC;AAEvC,OAAO,EAEL,KAAK,QAAQ,EACb,KAAK,gBAAgB,EACrB,KAAK,eAAe,EACpB,KAAK,QAAQ,EACb,KAAK,aAAa,EACnB,MAAM,wBAAwB,CAAC;AAMhC;;;;;;;;GAQG;AACH,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,QAAQ,SAAS,eAAe,CAAC,KAAK,CAAC,EAC9E,OAAO,EAAE,QAAQ,GAChB,QAAQ,CAAC,KAAK,EAAE,QAAQ,CAAC,CA0B3B;AAQD,MAAM,WAAW,qBAAqB,CAAC,KAAK,EAAE,QAAQ,SAAS,eAAe,CAAC,KAAK,CAAC;IACnF,QAAQ,EAAE,QAAQ,CAAC,KAAK,EAAE,QAAQ,CAAC,CAAC;IACpC,QAAQ,EAAE,KAAK,CAAC,SAAS,CAAC;CAC3B;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,QAAQ,SAAS,eAAe,CAAC,KAAK,CAAC,EAAE,EAC/E,QAAQ,EACR,QAAQ,GACT,EAAE,qBAAqB,CAAC,KAAK,EAAE,QAAQ,CAAC,yFAMxC;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,WAAW,CAAC,KAAK,GAAG,OAAO,KAAK,gBAAgB,CAAC,KAAK,CAAC,CAWtE;AAMD,MAAM,WAAW,cAAc,CAAC,KAAK,EAAE,CAAC,SAAS,QAAQ,GAAG,SAAS,GAAG,SAAS;IAC/E,8CAA8C;IAC9C,IAAI,EAAE,KAAK,EAAE,CAAC;IACd,6EAA6E;IAC7E,IAAI,CAAC,EAAE,CAAC,CAAC;IACT,iEAAiE;IACjE,QAAQ,CAAC,EAAE,gBAAgB,CAAC,KAAK,CAAC,CAAC;CACpC;AAED;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,OAAO,CAAC,KAAK,EAAE,CAAC,SAAS,QAAQ,GAAG,SAAS,GAAG,SAAS,EACvE,OAAO,EAAE,cAAc,CAAC,KAAK,EAAE,CAAC,CAAC,GAChC,aAAa,CAAC,KAAK,EAAE,CAAC,CAAC,CAmCzB"}
package/dist/index.js CHANGED
@@ -1,9 +1,8 @@
1
- import { useState } from 'react';
1
+ import React, { createContext, useState, useContext, useMemo } from 'react';
2
2
  import { createCalendar } from '@gobrand/calendar-core';
3
3
  export * from '@gobrand/calendar-core';
4
4
 
5
- // src/index.ts
6
- function useCalendar(options) {
5
+ function useCreateCalendar(options) {
7
6
  const [calendarRef] = useState(() => ({
8
7
  current: createCalendar(options)
9
8
  }));
@@ -23,5 +22,54 @@ function useCalendar(options) {
23
22
  }));
24
23
  return calendarRef.current;
25
24
  }
25
+ var CalendarContext = createContext(null);
26
+ function CalendarProvider({
27
+ calendar,
28
+ children
29
+ }) {
30
+ return React.createElement(
31
+ CalendarContext.Provider,
32
+ { value: calendar },
33
+ children
34
+ );
35
+ }
36
+ function useCalendar() {
37
+ const calendar = useContext(CalendarContext);
38
+ if (!calendar) {
39
+ throw new Error(
40
+ "useCalendar must be used within a CalendarProvider. Wrap your component tree with <CalendarProvider calendar={calendar}>."
41
+ );
42
+ }
43
+ return calendar;
44
+ }
45
+ function useView(options) {
46
+ const { data, name, calendar: explicitCalendar } = options;
47
+ const contextCalendar = useContext(CalendarContext);
48
+ const calendar = explicitCalendar ?? contextCalendar;
49
+ if (!calendar) {
50
+ throw new Error(
51
+ "No calendar found. Either wrap your component tree in <CalendarProvider> or pass calendar explicitly: useView({ calendar, data })."
52
+ );
53
+ }
54
+ const state = calendar.getState();
55
+ const effectiveView = name ?? state.currentView;
56
+ return useMemo(() => {
57
+ switch (effectiveView) {
58
+ case "month":
59
+ return { type: "month", data: calendar.getMonth(data) };
60
+ case "week":
61
+ return { type: "week", data: calendar.getWeek(data) };
62
+ case "day":
63
+ return { type: "day", data: calendar.getDay(data) };
64
+ default:
65
+ throw new Error(`Unknown view: ${effectiveView}`);
66
+ }
67
+ }, [
68
+ calendar,
69
+ data,
70
+ state.referenceDate.toString(),
71
+ effectiveView
72
+ ]);
73
+ }
26
74
 
27
- export { useCalendar };
75
+ export { CalendarProvider, useCalendar, useCreateCalendar, useView };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gobrand/react-calendar",
3
- "version": "0.0.16",
3
+ "version": "0.0.18",
4
4
  "description": "React hooks and components for building calendars using the Temporal API",
5
5
  "private": false,
6
6
  "publishConfig": {
@@ -34,7 +34,7 @@
34
34
  "homepage": "https://github.com/go-brand/calendar#readme",
35
35
  "dependencies": {
36
36
  "@js-temporal/polyfill": "^0.5.1",
37
- "@gobrand/calendar-core": "^0.0.16"
37
+ "@gobrand/calendar-core": "^0.0.18"
38
38
  },
39
39
  "peerDependencies": {
40
40
  "react": "^18.0.0 || ^19.0.0"