@dxos/react-ui-calendar 0.8.4-main.548089c
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 +8 -0
- package/README.md +1 -0
- package/dist/lib/browser/index.mjs +265 -0
- package/dist/lib/browser/index.mjs.map +7 -0
- package/dist/lib/browser/meta.json +1 -0
- package/dist/lib/node-esm/index.mjs +267 -0
- package/dist/lib/node-esm/index.mjs.map +7 -0
- package/dist/lib/node-esm/meta.json +1 -0
- package/dist/types/src/components/Calendar/Calendar.d.ts +47 -0
- package/dist/types/src/components/Calendar/Calendar.d.ts.map +1 -0
- package/dist/types/src/components/Calendar/Calendar.stories.d.ts +27 -0
- package/dist/types/src/components/Calendar/Calendar.stories.d.ts.map +1 -0
- package/dist/types/src/components/Calendar/index.d.ts +2 -0
- package/dist/types/src/components/Calendar/index.d.ts.map +1 -0
- package/dist/types/src/components/Calendar/util.d.ts +4 -0
- package/dist/types/src/components/Calendar/util.d.ts.map +1 -0
- package/dist/types/src/components/index.d.ts +2 -0
- package/dist/types/src/components/index.d.ts.map +1 -0
- package/dist/types/src/index.d.ts +2 -0
- package/dist/types/src/index.d.ts.map +1 -0
- package/dist/types/src/translations.d.ts +9 -0
- package/dist/types/src/translations.d.ts.map +1 -0
- package/dist/types/src/types.d.ts +175 -0
- package/dist/types/src/types.d.ts.map +1 -0
- package/dist/types/tsconfig.tsbuildinfo +1 -0
- package/package.json +64 -0
- package/src/components/Calendar/Calendar.stories.tsx +76 -0
- package/src/components/Calendar/Calendar.tsx +294 -0
- package/src/components/Calendar/index.ts +5 -0
- package/src/components/Calendar/util.ts +22 -0
- package/src/components/index.ts +5 -0
- package/src/index.ts +5 -0
- package/src/translations.ts +17 -0
- package/src/types.ts +193 -0
package/package.json
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@dxos/react-ui-calendar",
|
|
3
|
+
"version": "0.8.4-main.548089c",
|
|
4
|
+
"description": "A calendar component.",
|
|
5
|
+
"homepage": "https://dxos.org",
|
|
6
|
+
"bugs": "https://github.com/dxos/dxos/issues",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"author": "DXOS.org",
|
|
9
|
+
"type": "module",
|
|
10
|
+
"exports": {
|
|
11
|
+
".": {
|
|
12
|
+
"source": "./src/index.ts",
|
|
13
|
+
"types": "./dist/types/src/index.d.ts",
|
|
14
|
+
"browser": "./dist/lib/browser/index.mjs",
|
|
15
|
+
"node": "./dist/lib/node-esm/index.mjs"
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
"types": "dist/types/src/index.d.ts",
|
|
19
|
+
"typesVersions": {
|
|
20
|
+
"*": {}
|
|
21
|
+
},
|
|
22
|
+
"files": [
|
|
23
|
+
"dist",
|
|
24
|
+
"src"
|
|
25
|
+
],
|
|
26
|
+
"dependencies": {
|
|
27
|
+
"@preact-signals/safe-react": "^0.9.0",
|
|
28
|
+
"@preact/signals-core": "^1.12.1",
|
|
29
|
+
"@radix-ui/react-context": "1.1.1",
|
|
30
|
+
"date-fns": "^3.3.1",
|
|
31
|
+
"react-resize-detector": "^11.0.1",
|
|
32
|
+
"react-virtualized": "^9.22.6",
|
|
33
|
+
"react-window": "^2.2.3",
|
|
34
|
+
"@dxos/async": "0.8.4-main.548089c",
|
|
35
|
+
"@dxos/debug": "0.8.4-main.548089c",
|
|
36
|
+
"@dxos/log": "0.8.4-main.548089c",
|
|
37
|
+
"@dxos/invariant": "0.8.4-main.548089c",
|
|
38
|
+
"@dxos/util": "0.8.4-main.548089c"
|
|
39
|
+
},
|
|
40
|
+
"devDependencies": {
|
|
41
|
+
"@types/react": "~19.2.2",
|
|
42
|
+
"@types/react-dom": "~19.2.2",
|
|
43
|
+
"@types/react-virtualized": "^9.22.3",
|
|
44
|
+
"@types/react-window": "^2.0.0",
|
|
45
|
+
"effect": "3.18.3",
|
|
46
|
+
"react": "~19.2.0",
|
|
47
|
+
"react-dom": "~19.2.0",
|
|
48
|
+
"vite": "7.1.9",
|
|
49
|
+
"@dxos/random": "0.8.4-main.548089c",
|
|
50
|
+
"@dxos/react-ui": "0.8.4-main.548089c",
|
|
51
|
+
"@dxos/react-ui-theme": "0.8.4-main.548089c",
|
|
52
|
+
"@dxos/storybook-utils": "0.8.4-main.548089c"
|
|
53
|
+
},
|
|
54
|
+
"peerDependencies": {
|
|
55
|
+
"effect": "3.13.3",
|
|
56
|
+
"react": "^19.0.0",
|
|
57
|
+
"react-dom": "^19.0.0",
|
|
58
|
+
"@dxos/react-ui": "0.8.4-main.548089c",
|
|
59
|
+
"@dxos/react-ui-theme": "0.8.4-main.548089c"
|
|
60
|
+
},
|
|
61
|
+
"publishConfig": {
|
|
62
|
+
"access": "public"
|
|
63
|
+
}
|
|
64
|
+
}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { type Meta, type StoryObj } from '@storybook/react-vite';
|
|
6
|
+
import React from 'react';
|
|
7
|
+
|
|
8
|
+
import { withTheme } from '@dxos/react-ui/testing';
|
|
9
|
+
|
|
10
|
+
import { translations } from '../../translations';
|
|
11
|
+
|
|
12
|
+
import { Calendar } from './Calendar';
|
|
13
|
+
|
|
14
|
+
const meta = {
|
|
15
|
+
title: 'ui/react-ui-calendar/Calendar',
|
|
16
|
+
component: Calendar.Grid,
|
|
17
|
+
decorators: [withTheme],
|
|
18
|
+
parameters: {
|
|
19
|
+
layout: 'centered',
|
|
20
|
+
translations,
|
|
21
|
+
},
|
|
22
|
+
} satisfies Meta<typeof Calendar.Grid>;
|
|
23
|
+
|
|
24
|
+
export default meta;
|
|
25
|
+
|
|
26
|
+
type Story = StoryObj<typeof meta>;
|
|
27
|
+
|
|
28
|
+
export const Default: Story = {
|
|
29
|
+
render: () => (
|
|
30
|
+
<Calendar.Root>
|
|
31
|
+
<Calendar.Viewport>
|
|
32
|
+
<Calendar.Header />
|
|
33
|
+
<Calendar.Grid rows={6} />
|
|
34
|
+
</Calendar.Viewport>
|
|
35
|
+
</Calendar.Root>
|
|
36
|
+
),
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
export const Border: Story = {
|
|
40
|
+
render: () => (
|
|
41
|
+
<Calendar.Root>
|
|
42
|
+
<Calendar.Viewport classNames='bg-modalSurface border border-separator rounded'>
|
|
43
|
+
<Calendar.Header />
|
|
44
|
+
<Calendar.Grid rows={6} />
|
|
45
|
+
</Calendar.Viewport>
|
|
46
|
+
</Calendar.Root>
|
|
47
|
+
),
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
export const Column: Story = {
|
|
51
|
+
render: () => (
|
|
52
|
+
<div className='absolute inset-0 flex bs-full justify-center'>
|
|
53
|
+
<Calendar.Root>
|
|
54
|
+
<Calendar.Viewport>
|
|
55
|
+
<Calendar.Header />
|
|
56
|
+
<Calendar.Grid />
|
|
57
|
+
</Calendar.Viewport>
|
|
58
|
+
</Calendar.Root>
|
|
59
|
+
</div>
|
|
60
|
+
),
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
export const Mobile: Story = {
|
|
64
|
+
render: () => (
|
|
65
|
+
<div className='absolute inset-0 flex bs-full justify-center'>
|
|
66
|
+
<div className='flex bs-full is-[400px] justify-center'>
|
|
67
|
+
<Calendar.Root>
|
|
68
|
+
<Calendar.Viewport classNames='is-full'>
|
|
69
|
+
<Calendar.Header />
|
|
70
|
+
<Calendar.Grid />
|
|
71
|
+
</Calendar.Viewport>
|
|
72
|
+
</Calendar.Root>
|
|
73
|
+
</div>
|
|
74
|
+
</div>
|
|
75
|
+
),
|
|
76
|
+
};
|
|
@@ -0,0 +1,294 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { createContext } from '@radix-ui/react-context';
|
|
6
|
+
import { type Day, addDays, differenceInWeeks, format, startOfWeek } from 'date-fns';
|
|
7
|
+
import React, {
|
|
8
|
+
type Dispatch,
|
|
9
|
+
type PropsWithChildren,
|
|
10
|
+
type SetStateAction,
|
|
11
|
+
forwardRef,
|
|
12
|
+
useCallback,
|
|
13
|
+
useEffect,
|
|
14
|
+
useImperativeHandle,
|
|
15
|
+
useMemo,
|
|
16
|
+
useRef,
|
|
17
|
+
useState,
|
|
18
|
+
} from 'react';
|
|
19
|
+
import { useResizeDetector } from 'react-resize-detector';
|
|
20
|
+
import { List, type ListProps, type ListRowRenderer } from 'react-virtualized';
|
|
21
|
+
|
|
22
|
+
import { Event } from '@dxos/async';
|
|
23
|
+
import { IconButton, type ThemedClassName, useTranslation } from '@dxos/react-ui';
|
|
24
|
+
import { mx } from '@dxos/react-ui-theme';
|
|
25
|
+
|
|
26
|
+
import { translationKey } from '../../translations';
|
|
27
|
+
|
|
28
|
+
import { getDate, isSameDay } from './util';
|
|
29
|
+
|
|
30
|
+
const maxRows = 50 * 100;
|
|
31
|
+
const start = new Date('1970-01-01');
|
|
32
|
+
const size = 48;
|
|
33
|
+
const defaultWidth = 7 * size;
|
|
34
|
+
|
|
35
|
+
//
|
|
36
|
+
// Context
|
|
37
|
+
//
|
|
38
|
+
|
|
39
|
+
type CalendarEvent = {
|
|
40
|
+
type: 'scroll';
|
|
41
|
+
date: Date;
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
type CalendarContextValue = {
|
|
45
|
+
weekStartsOn: Day;
|
|
46
|
+
event: Event<CalendarEvent>;
|
|
47
|
+
index: number | undefined;
|
|
48
|
+
setIndex: Dispatch<SetStateAction<number | undefined>>;
|
|
49
|
+
selected: Date | undefined;
|
|
50
|
+
setSelected: Dispatch<SetStateAction<Date | undefined>>;
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
const [CalendarContextProvider, useCalendarContext] = createContext<CalendarContextValue>('Calendar');
|
|
54
|
+
|
|
55
|
+
//
|
|
56
|
+
// Controller
|
|
57
|
+
//
|
|
58
|
+
|
|
59
|
+
type CalendarController = {
|
|
60
|
+
scrollTo: (date: Date) => void;
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
//
|
|
64
|
+
// Root
|
|
65
|
+
//
|
|
66
|
+
|
|
67
|
+
type CalendarRootProps = PropsWithChildren<Partial<Pick<CalendarContextValue, 'weekStartsOn'>>>;
|
|
68
|
+
|
|
69
|
+
const CalendarRoot = forwardRef<CalendarController, CalendarRootProps>(
|
|
70
|
+
({ children, weekStartsOn = 1 }, forwardedRef) => {
|
|
71
|
+
const event = useMemo(() => new Event<CalendarEvent>(), []);
|
|
72
|
+
const [selected, setSelected] = useState<Date | undefined>();
|
|
73
|
+
const [index, setIndex] = useState<number | undefined>();
|
|
74
|
+
|
|
75
|
+
useImperativeHandle(
|
|
76
|
+
forwardedRef,
|
|
77
|
+
() => ({
|
|
78
|
+
scrollTo: (date: Date) => {
|
|
79
|
+
event.emit({ type: 'scroll', date });
|
|
80
|
+
},
|
|
81
|
+
}),
|
|
82
|
+
[event],
|
|
83
|
+
);
|
|
84
|
+
|
|
85
|
+
return (
|
|
86
|
+
<CalendarContextProvider
|
|
87
|
+
weekStartsOn={weekStartsOn}
|
|
88
|
+
event={event}
|
|
89
|
+
index={index}
|
|
90
|
+
setIndex={setIndex}
|
|
91
|
+
selected={selected}
|
|
92
|
+
setSelected={setSelected}
|
|
93
|
+
>
|
|
94
|
+
{children}
|
|
95
|
+
</CalendarContextProvider>
|
|
96
|
+
);
|
|
97
|
+
},
|
|
98
|
+
);
|
|
99
|
+
|
|
100
|
+
//
|
|
101
|
+
// Viewport
|
|
102
|
+
//
|
|
103
|
+
|
|
104
|
+
type CalendarViewportProps = PropsWithChildren<ThemedClassName>;
|
|
105
|
+
|
|
106
|
+
const CalendarViewport = ({ children, classNames }: CalendarViewportProps) => {
|
|
107
|
+
return (
|
|
108
|
+
<div role='none' className={mx('flex flex-col items-center overflow-hidden bg-inputSurface', classNames)}>
|
|
109
|
+
{children}
|
|
110
|
+
</div>
|
|
111
|
+
);
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
CalendarViewport.displayName = 'CalendarContent';
|
|
115
|
+
|
|
116
|
+
//
|
|
117
|
+
// Header
|
|
118
|
+
//
|
|
119
|
+
|
|
120
|
+
type CalendarHeaderProps = ThemedClassName;
|
|
121
|
+
|
|
122
|
+
const CalendarHeader = ({ classNames }: CalendarHeaderProps) => {
|
|
123
|
+
const { t } = useTranslation(translationKey);
|
|
124
|
+
const { weekStartsOn, event, index, selected } = useCalendarContext(CalendarHeader.displayName);
|
|
125
|
+
const top = useMemo(() => getDate(start, index ?? 0, 6, weekStartsOn), [index, weekStartsOn]);
|
|
126
|
+
const today = useMemo(() => new Date(), []);
|
|
127
|
+
|
|
128
|
+
const handleToday = useCallback(() => {
|
|
129
|
+
event.emit({ type: 'scroll', date: today });
|
|
130
|
+
}, [event, start, today]);
|
|
131
|
+
|
|
132
|
+
return (
|
|
133
|
+
<div role='none' className={mx('shink-0 is-full grid grid-cols-3', classNames)} style={{ width: defaultWidth }}>
|
|
134
|
+
<div className='flex justify-start'>
|
|
135
|
+
<IconButton
|
|
136
|
+
variant='ghost'
|
|
137
|
+
size={6}
|
|
138
|
+
icon='ph--calendar--regular'
|
|
139
|
+
iconOnly
|
|
140
|
+
classNames='aspect-square'
|
|
141
|
+
label={t('today button')}
|
|
142
|
+
onClick={handleToday}
|
|
143
|
+
/>
|
|
144
|
+
</div>
|
|
145
|
+
<span className='flex justify-center p-2'>{format(selected ?? top, 'MMMM')}</span>
|
|
146
|
+
<span className='flex justify-end p-2'>{(selected ?? top).getFullYear()}</span>
|
|
147
|
+
</div>
|
|
148
|
+
);
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
CalendarHeader.displayName = 'CalendarHeader';
|
|
152
|
+
|
|
153
|
+
//
|
|
154
|
+
// Grid
|
|
155
|
+
// TODO(burdon): Key nav.
|
|
156
|
+
// TODO(burdon): Drag range.
|
|
157
|
+
//
|
|
158
|
+
|
|
159
|
+
type CalendarGridProps = ThemedClassName<{
|
|
160
|
+
rows?: number;
|
|
161
|
+
onSelect?: (event: { date: Date }) => void;
|
|
162
|
+
}>;
|
|
163
|
+
|
|
164
|
+
const CalendarGrid = ({ classNames, rows, onSelect }: CalendarGridProps) => {
|
|
165
|
+
const { weekStartsOn, event, setIndex, selected, setSelected } = useCalendarContext(CalendarGrid.displayName);
|
|
166
|
+
const { ref: containerRef, width = 0, height = 0 } = useResizeDetector();
|
|
167
|
+
const maxHeight = rows ? rows * size : undefined;
|
|
168
|
+
const listRef = useRef<List>(null);
|
|
169
|
+
const today = useMemo(() => new Date(), []);
|
|
170
|
+
|
|
171
|
+
const [initialized, setInitialized] = useState(false);
|
|
172
|
+
useEffect(() => {
|
|
173
|
+
const index = differenceInWeeks(today, start);
|
|
174
|
+
listRef.current?.scrollToRow(index);
|
|
175
|
+
}, [initialized, start, today]);
|
|
176
|
+
|
|
177
|
+
useEffect(() => {
|
|
178
|
+
return event.on((event) => {
|
|
179
|
+
switch (event.type) {
|
|
180
|
+
case 'scroll': {
|
|
181
|
+
const index = differenceInWeeks(event.date, start);
|
|
182
|
+
listRef.current?.scrollToRow(index);
|
|
183
|
+
break;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
});
|
|
187
|
+
}, [event]);
|
|
188
|
+
|
|
189
|
+
const days = useMemo(() => {
|
|
190
|
+
const weekStart = startOfWeek(new Date(), { weekStartsOn });
|
|
191
|
+
return Array.from({ length: 7 }, (_, i) => {
|
|
192
|
+
const day = addDays(weekStart, i);
|
|
193
|
+
return format(day, 'EEE'); // Short day name (Mon, Tue, etc.)
|
|
194
|
+
});
|
|
195
|
+
}, []);
|
|
196
|
+
|
|
197
|
+
const handleDaySelect = useCallback(
|
|
198
|
+
(date: Date) => {
|
|
199
|
+
setSelected((current) => (isSameDay(date, current) ? undefined : date));
|
|
200
|
+
onSelect?.({ date });
|
|
201
|
+
},
|
|
202
|
+
[onSelect],
|
|
203
|
+
);
|
|
204
|
+
|
|
205
|
+
const handleScroll = useCallback<NonNullable<ListProps['onScroll']>>((info) => {
|
|
206
|
+
setIndex(Math.round(info.scrollTop / size));
|
|
207
|
+
}, []);
|
|
208
|
+
|
|
209
|
+
const rowRenderer = useCallback<ListRowRenderer>(
|
|
210
|
+
({ key, index, style }) => {
|
|
211
|
+
const getBgColor = (date: Date) => date.getMonth() % 2 === 0 && 'bg-modalSurface';
|
|
212
|
+
return (
|
|
213
|
+
<div key={key} style={style} className='is-full grid grid-cols-[1fr_max-content_1fr] snap-center'>
|
|
214
|
+
<div className={mx(getBgColor(getDate(start, index, 0, weekStartsOn)))} />
|
|
215
|
+
<div className='grid grid-cols-7' style={{ gridTemplateColumns: `repeat(7, ${size}px)` }}>
|
|
216
|
+
{Array.from({ length: 7 }).map((_, i) => {
|
|
217
|
+
const date = getDate(start, index, i, weekStartsOn);
|
|
218
|
+
const border = isSameDay(date, selected)
|
|
219
|
+
? 'border-primary-500'
|
|
220
|
+
: isSameDay(date, today)
|
|
221
|
+
? 'border-amber-500'
|
|
222
|
+
: undefined;
|
|
223
|
+
|
|
224
|
+
return (
|
|
225
|
+
<div
|
|
226
|
+
key={i}
|
|
227
|
+
className={mx('relative flex justify-center items-center cursor-pointer', getBgColor(date))}
|
|
228
|
+
onClick={() => handleDaySelect(date)}
|
|
229
|
+
>
|
|
230
|
+
<span className='text-description'>{date.getDate()}</span>
|
|
231
|
+
{!border && date.getDate() === 1 && (
|
|
232
|
+
<span className='absolute top-0 text-xs text-description'>{format(date, 'MMM')}</span>
|
|
233
|
+
)}
|
|
234
|
+
{border && (
|
|
235
|
+
<div className={mx('absolute top-0 left-0 is-full bs-full border-2 rounded-full', border)} />
|
|
236
|
+
)}
|
|
237
|
+
</div>
|
|
238
|
+
);
|
|
239
|
+
})}
|
|
240
|
+
</div>
|
|
241
|
+
<div className={mx(getBgColor(getDate(start, index, 6, weekStartsOn)))} />
|
|
242
|
+
</div>
|
|
243
|
+
);
|
|
244
|
+
},
|
|
245
|
+
[handleDaySelect, selected, weekStartsOn],
|
|
246
|
+
);
|
|
247
|
+
|
|
248
|
+
return (
|
|
249
|
+
<div role='none' className={mx('flex flex-col bs-full is-full justify-center overflow-hidden', classNames)}>
|
|
250
|
+
{/* Day labels */}
|
|
251
|
+
<div role='none' className='flex justify-center bg-groupSurface'>
|
|
252
|
+
<div role='none' className='flex is-full grid grid-cols-7' style={{ width: defaultWidth }}>
|
|
253
|
+
{days.map((date, i) => (
|
|
254
|
+
<div key={i} role='none' className='flex justify-center p-2 text-sm font-thin'>
|
|
255
|
+
{date}
|
|
256
|
+
</div>
|
|
257
|
+
))}
|
|
258
|
+
</div>
|
|
259
|
+
</div>
|
|
260
|
+
{/* Grid */}
|
|
261
|
+
<div role='none' className='flex flex-col bs-full is-full justify-center overflow-hidden' ref={containerRef}>
|
|
262
|
+
<List
|
|
263
|
+
ref={listRef}
|
|
264
|
+
role='none'
|
|
265
|
+
// TODO(burdon): Snap isn't working.
|
|
266
|
+
className='[&>div]:snap-y scrollbar-none outline-none'
|
|
267
|
+
width={width}
|
|
268
|
+
height={maxHeight ?? height}
|
|
269
|
+
rowCount={maxRows}
|
|
270
|
+
rowHeight={size}
|
|
271
|
+
rowRenderer={rowRenderer}
|
|
272
|
+
scrollToAlignment='start'
|
|
273
|
+
onScroll={handleScroll}
|
|
274
|
+
onRowsRendered={() => setInitialized(true)}
|
|
275
|
+
/>
|
|
276
|
+
</div>
|
|
277
|
+
</div>
|
|
278
|
+
);
|
|
279
|
+
};
|
|
280
|
+
|
|
281
|
+
CalendarGrid.displayName = 'CalendarGrid';
|
|
282
|
+
|
|
283
|
+
//
|
|
284
|
+
// Calendar
|
|
285
|
+
//
|
|
286
|
+
|
|
287
|
+
export const Calendar = {
|
|
288
|
+
Root: CalendarRoot,
|
|
289
|
+
Viewport: CalendarViewport,
|
|
290
|
+
Header: CalendarHeader,
|
|
291
|
+
Grid: CalendarGrid,
|
|
292
|
+
};
|
|
293
|
+
|
|
294
|
+
export type { CalendarController, CalendarRootProps, CalendarViewportProps, CalendarHeaderProps, CalendarGridProps };
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { type Day } from 'date-fns';
|
|
6
|
+
|
|
7
|
+
export const getDate = (start: Date, weekNumber: number, dayOfWeek: number, weekStartsOn: Day): Date => {
|
|
8
|
+
const result = new Date(start);
|
|
9
|
+
const startDayOfWeek = start.getDay(); // 0 = Sunday, 1 = Monday, etc.
|
|
10
|
+
const adjustedStartDay = (startDayOfWeek === 0 ? 7 : startDayOfWeek) - weekStartsOn; // Adjust for weekStartsOn.
|
|
11
|
+
result.setDate(start.getDate() - adjustedStartDay + weekNumber * 7 + dayOfWeek);
|
|
12
|
+
return result;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export const isSameDay = (date1: Date, date2: Date | undefined): boolean => {
|
|
16
|
+
return (
|
|
17
|
+
!!date2 &&
|
|
18
|
+
date1.getFullYear() === date2.getFullYear() &&
|
|
19
|
+
date1.getMonth() === date2.getMonth() &&
|
|
20
|
+
date1.getDate() === date2.getDate()
|
|
21
|
+
);
|
|
22
|
+
};
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2023 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { type Resource } from '@dxos/react-ui';
|
|
6
|
+
|
|
7
|
+
export const translationKey = 'react-ui-calendar';
|
|
8
|
+
|
|
9
|
+
export const translations = [
|
|
10
|
+
{
|
|
11
|
+
'en-US': {
|
|
12
|
+
[translationKey]: {
|
|
13
|
+
'today button': 'Today',
|
|
14
|
+
},
|
|
15
|
+
},
|
|
16
|
+
},
|
|
17
|
+
] as const satisfies Resource[];
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { type Locale } from 'date-fns';
|
|
6
|
+
import { type RefObject } from 'react';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Calendar types and interfaces.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
export type DateString = string; // YYYY-MM-DD format
|
|
13
|
+
|
|
14
|
+
export type DisplayMode = 'days' | 'years';
|
|
15
|
+
|
|
16
|
+
export type LayoutMode = 'portrait' | 'landscape';
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Locale configuration for the calendar.
|
|
20
|
+
*/
|
|
21
|
+
export interface CalendarLocale {
|
|
22
|
+
blank?: string;
|
|
23
|
+
headerFormat?: string;
|
|
24
|
+
todayLabel?: {
|
|
25
|
+
long: string;
|
|
26
|
+
short?: string;
|
|
27
|
+
};
|
|
28
|
+
weekdays?: string[];
|
|
29
|
+
weekStartsOn?: 0 | 1 | 2 | 3 | 4 | 5 | 6;
|
|
30
|
+
locale?: Locale; // date-fns locale
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Theme configuration for the calendar.
|
|
35
|
+
*/
|
|
36
|
+
export interface CalendarTheme {
|
|
37
|
+
floatingNav?: {
|
|
38
|
+
background?: string;
|
|
39
|
+
chevron?: string;
|
|
40
|
+
color?: string;
|
|
41
|
+
};
|
|
42
|
+
headerColor?: string;
|
|
43
|
+
selectionColor?: string | ((date: DateString) => string);
|
|
44
|
+
textColor?: {
|
|
45
|
+
active?: string;
|
|
46
|
+
default?: string;
|
|
47
|
+
};
|
|
48
|
+
todayColor?: string;
|
|
49
|
+
weekdayColor?: string;
|
|
50
|
+
overlayColor?: string;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Display options for the calendar.
|
|
55
|
+
*/
|
|
56
|
+
export interface DisplayOptions {
|
|
57
|
+
hideYearsOnSelect?: boolean;
|
|
58
|
+
layout?: LayoutMode;
|
|
59
|
+
overscanMonthCount?: number;
|
|
60
|
+
shouldHeaderAnimate?: boolean;
|
|
61
|
+
showHeader?: boolean;
|
|
62
|
+
showMonthsForYears?: boolean;
|
|
63
|
+
showOverlay?: boolean;
|
|
64
|
+
showTodayHelper?: boolean;
|
|
65
|
+
showWeekdays?: boolean;
|
|
66
|
+
todayHelperRowOffset?: number;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Month data structure.
|
|
71
|
+
*/
|
|
72
|
+
export interface MonthData {
|
|
73
|
+
month: number;
|
|
74
|
+
year: number;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Month rendering data.
|
|
79
|
+
*/
|
|
80
|
+
export interface MonthRenderData {
|
|
81
|
+
date: Date;
|
|
82
|
+
rows: number[][];
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Day props for rendering.
|
|
87
|
+
*/
|
|
88
|
+
export interface DayProps {
|
|
89
|
+
date: DateString;
|
|
90
|
+
day: number;
|
|
91
|
+
month: number;
|
|
92
|
+
year: number;
|
|
93
|
+
monthShort: string;
|
|
94
|
+
currentYear: number;
|
|
95
|
+
isDisabled: boolean;
|
|
96
|
+
isToday: boolean;
|
|
97
|
+
isSelected: boolean;
|
|
98
|
+
isHighlighted?: boolean;
|
|
99
|
+
onClick?: (date: Date) => void;
|
|
100
|
+
handlers?: Record<string, any>;
|
|
101
|
+
className?: string;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Calendar props.
|
|
106
|
+
*/
|
|
107
|
+
export interface CalendarProps {
|
|
108
|
+
autoFocus?: boolean;
|
|
109
|
+
className?: string;
|
|
110
|
+
disabledDates?: Date[];
|
|
111
|
+
disabledDays?: number[];
|
|
112
|
+
display?: DisplayMode;
|
|
113
|
+
displayOptions?: DisplayOptions;
|
|
114
|
+
height?: number;
|
|
115
|
+
keyboardSupport?: boolean;
|
|
116
|
+
locale?: CalendarLocale;
|
|
117
|
+
max?: Date;
|
|
118
|
+
maxDate?: Date;
|
|
119
|
+
min?: Date;
|
|
120
|
+
minDate?: Date;
|
|
121
|
+
onScroll?: (scrollTop: number, event?: Event) => void;
|
|
122
|
+
onScrollEnd?: (scrollTop: number) => void;
|
|
123
|
+
onSelect?: (date: Date) => void;
|
|
124
|
+
onHighlightedDateChange?: (date: Date) => void;
|
|
125
|
+
rowHeight?: number;
|
|
126
|
+
scrollDate?: Date;
|
|
127
|
+
selected?: Date | DateString;
|
|
128
|
+
tabIndex?: number;
|
|
129
|
+
theme?: CalendarTheme;
|
|
130
|
+
width?: number | string;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Month props.
|
|
135
|
+
*/
|
|
136
|
+
export interface MonthProps {
|
|
137
|
+
monthDate: Date;
|
|
138
|
+
rows: number[][];
|
|
139
|
+
rowHeight: number;
|
|
140
|
+
selected?: DateString;
|
|
141
|
+
disabledDates?: DateString[];
|
|
142
|
+
disabledDays?: number[];
|
|
143
|
+
minDate: Date;
|
|
144
|
+
maxDate: Date;
|
|
145
|
+
today: Date;
|
|
146
|
+
locale: Required<CalendarLocale>;
|
|
147
|
+
theme: Required<CalendarTheme>;
|
|
148
|
+
showOverlay?: boolean;
|
|
149
|
+
isScrolling?: boolean;
|
|
150
|
+
onDayClick?: (date: Date) => void;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Hook return types for headless implementation.
|
|
155
|
+
*/
|
|
156
|
+
export interface UseCalendarReturn {
|
|
157
|
+
containerProps: {
|
|
158
|
+
ref: RefObject<HTMLDivElement>;
|
|
159
|
+
tabIndex?: number;
|
|
160
|
+
className?: string;
|
|
161
|
+
'aria-label': string;
|
|
162
|
+
};
|
|
163
|
+
display: DisplayMode;
|
|
164
|
+
locale: Required<CalendarLocale>;
|
|
165
|
+
theme: Required<CalendarTheme>;
|
|
166
|
+
displayOptions: Required<DisplayOptions>;
|
|
167
|
+
months: MonthData[];
|
|
168
|
+
today: Date;
|
|
169
|
+
scrollToDate: (date: Date, offset?: number, shouldAnimate?: boolean) => void;
|
|
170
|
+
scrollTo: (offset: number) => void;
|
|
171
|
+
getCurrentOffset: () => number;
|
|
172
|
+
getDateOffset: (date: Date) => number;
|
|
173
|
+
setDisplay: (display: DisplayMode) => void;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
export interface UseDayReturn {
|
|
177
|
+
dayProps: {
|
|
178
|
+
onClick: () => void;
|
|
179
|
+
'data-date': DateString;
|
|
180
|
+
className?: string;
|
|
181
|
+
};
|
|
182
|
+
isSelected: boolean;
|
|
183
|
+
isToday: boolean;
|
|
184
|
+
isDisabled: boolean;
|
|
185
|
+
isHighlighted: boolean;
|
|
186
|
+
selectionColor?: string;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
export interface UseMonthReturn {
|
|
190
|
+
days: DayProps[];
|
|
191
|
+
monthLabel: string;
|
|
192
|
+
showOverlay: boolean;
|
|
193
|
+
}
|