|
|
|
@ -1,58 +1,155 @@
|
|
|
|
# React Calendar Component
|
|
|
|
# react-calendario
|
|
|
|
|
|
|
|
|
|
|
|
A modern, flexible calendar component built with React, TypeScript, and SCSS modules.
|
|
|
|
A modern, flexible, and highly customizable calendar component library for React with TypeScript support.
|
|
|
|
|
|
|
|
|
|
|
|
## Demo
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Check out the live demo at [calendar.code.meow.ch](https://calendar.code.meow.ch)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## Features
|
|
|
|
## Features
|
|
|
|
|
|
|
|
|
|
|
|
- 📅 Multiple calendar views:
|
|
|
|
- 📅 **Multiple Views**: Year, Month, Week, and custom date ranges
|
|
|
|
- Full year view with configurable month grid
|
|
|
|
- 🎨 **Highly Customizable**: Multiple header styles, sizes, and visual options
|
|
|
|
- Single month view with customizable layout
|
|
|
|
- 🔍 **Interactive**: Date selection, range picking, and click handlers
|
|
|
|
- Week view with interactive options
|
|
|
|
- 📱 **Responsive**: Works on all screen sizes
|
|
|
|
- Date range picker with hover preview
|
|
|
|
- 🎯 **TypeScript**: Full type safety and IntelliSense support
|
|
|
|
- 🎨 Visual customization:
|
|
|
|
- 🎨 **Themeable**: Customizable colors and styles
|
|
|
|
- Four header styles: expanded, compacted, tiny, and numeric
|
|
|
|
- 📦 **Lightweight**: Minimal dependencies
|
|
|
|
- Configurable day sizes (XS to XL)
|
|
|
|
|
|
|
|
- Adjustable font proportions
|
|
|
|
|
|
|
|
- Optional magnify effect for selected dates
|
|
|
|
|
|
|
|
- Weekend highlighting
|
|
|
|
|
|
|
|
- 🔧 Functional options:
|
|
|
|
|
|
|
|
- Month cutoff handling (dimmed, truncated, or show all)
|
|
|
|
|
|
|
|
- Interactive date range selection
|
|
|
|
|
|
|
|
- Hover states and preview
|
|
|
|
|
|
|
|
- Responsive design for all screen sizes
|
|
|
|
|
|
|
|
- 🛠 Technical features:
|
|
|
|
|
|
|
|
- Built with React + TypeScript
|
|
|
|
|
|
|
|
- SCSS modules for styling
|
|
|
|
|
|
|
|
- date-fns for date manipulation
|
|
|
|
|
|
|
|
- Zero external dependencies beyond core requirements
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## Installation
|
|
|
|
## Installation
|
|
|
|
|
|
|
|
|
|
|
|
```bash
|
|
|
|
```bash
|
|
|
|
npm install
|
|
|
|
npm install react-calendario date-fns classnames
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
## Development
|
|
|
|
or
|
|
|
|
|
|
|
|
|
|
|
|
```bash
|
|
|
|
```bash
|
|
|
|
npm run dev
|
|
|
|
yarn add react-calendario date-fns classnames
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## Quick Start
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```tsx
|
|
|
|
|
|
|
|
import { Month } from 'react-calendario';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function App() {
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
|
|
|
<Month
|
|
|
|
|
|
|
|
date={new Date()}
|
|
|
|
|
|
|
|
dayHeaderStyle="expanded"
|
|
|
|
|
|
|
|
size="l"
|
|
|
|
|
|
|
|
/>
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
}
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
## Usage Examples
|
|
|
|
## Components
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
### Year
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Display a full year calendar with customizable month grid.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```tsx
|
|
|
|
|
|
|
|
import { Year } from 'react-calendario';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<Year
|
|
|
|
|
|
|
|
year={2024}
|
|
|
|
|
|
|
|
dayHeaderStyle="tiny"
|
|
|
|
|
|
|
|
monthCutoff="truncate"
|
|
|
|
|
|
|
|
size="m"
|
|
|
|
|
|
|
|
fontProportion={100}
|
|
|
|
|
|
|
|
magnify={true}
|
|
|
|
|
|
|
|
/>
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
### Month
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Display a single month with various layout options.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```tsx
|
|
|
|
|
|
|
|
import { Month } from 'react-calendario';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<Month
|
|
|
|
|
|
|
|
date={new Date()}
|
|
|
|
|
|
|
|
dayHeaderStyle="expanded"
|
|
|
|
|
|
|
|
direction="column"
|
|
|
|
|
|
|
|
monthCutoff="dimmed"
|
|
|
|
|
|
|
|
size="xl"
|
|
|
|
|
|
|
|
/>
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
### Week
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Display a single week view.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```tsx
|
|
|
|
|
|
|
|
import { Week } from 'react-calendario';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<Week
|
|
|
|
|
|
|
|
startDate={new Date()}
|
|
|
|
|
|
|
|
headerStyle="compacted"
|
|
|
|
|
|
|
|
referenceMonth={0}
|
|
|
|
|
|
|
|
size="l"
|
|
|
|
|
|
|
|
/>
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
### DateRange
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Display a custom date range with optional selection and interactivity.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```tsx
|
|
|
|
|
|
|
|
import { DateRange } from 'react-calendario';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<DateRange
|
|
|
|
|
|
|
|
from={new Date(2024, 0, 15)}
|
|
|
|
|
|
|
|
to={new Date(2024, 0, 22)}
|
|
|
|
|
|
|
|
included={true}
|
|
|
|
|
|
|
|
selected={true}
|
|
|
|
|
|
|
|
headerStyle="tiny"
|
|
|
|
|
|
|
|
size="l"
|
|
|
|
|
|
|
|
onDateClick={(date) => console.log('Clicked:', date)}
|
|
|
|
|
|
|
|
activeDates={[new Date(2024, 0, 17)]}
|
|
|
|
|
|
|
|
/>
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## Props
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
### Common Props
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
All components share these common props:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| Prop | Type | Default | Description |
|
|
|
|
|
|
|
|
|------|------|---------|-------------|
|
|
|
|
|
|
|
|
| `size` | `'xl' \| 'l' \| 'm' \| 's' \| 'xs' \| 'xxs'` | `'l'` | Day cell size |
|
|
|
|
|
|
|
|
| `fontProportion` | `number` | `100` | Font size percentage (10-100) |
|
|
|
|
|
|
|
|
| `magnify` | `boolean` | `false` | Enable magnify effect on selection |
|
|
|
|
|
|
|
|
| `weekendDays` | `number[]` | `[6, 0]` | Weekend day indices (0=Sunday) |
|
|
|
|
|
|
|
|
|
|
|
|
### Date Range Picker
|
|
|
|
### Header Styles
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| Style | Description |
|
|
|
|
|
|
|
|
|-------|-------------|
|
|
|
|
|
|
|
|
| `'expanded'` | Full day names (e.g., "Monday") |
|
|
|
|
|
|
|
|
| `'compacted'` | Three-letter abbreviations (e.g., "Mon") |
|
|
|
|
|
|
|
|
| `'tiny'` | Two-letter abbreviations (e.g., "Mo") |
|
|
|
|
|
|
|
|
| `'none'` | Numbers only |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
### Size Options
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
- `'xxs'`: Extra extra small (20px height, 1:1.5 header:content ratio)
|
|
|
|
|
|
|
|
- `'xs'`: Extra small (24px height)
|
|
|
|
|
|
|
|
- `'s'`: Small (28px height)
|
|
|
|
|
|
|
|
- `'m'`: Medium (32px height)
|
|
|
|
|
|
|
|
- `'l'`: Large (36px height)
|
|
|
|
|
|
|
|
- `'xl'`: Extra large (48px height)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## Advanced Usage
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
### Date Range Selection
|
|
|
|
|
|
|
|
|
|
|
|
```tsx
|
|
|
|
```tsx
|
|
|
|
import React, { useState } from 'react';
|
|
|
|
import { useState } from 'react';
|
|
|
|
import { Year } from './components/calendar/Year';
|
|
|
|
import { Year } from 'react-calendario';
|
|
|
|
import { DateRange } from './types/calendar';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function DateRangePicker() {
|
|
|
|
function DateRangePicker() {
|
|
|
|
const [dateRange, setDateRange] = useState<DateRange>({
|
|
|
|
const [dateRange, setDateRange] = useState({
|
|
|
|
startDate: null,
|
|
|
|
startDate: null,
|
|
|
|
endDate: null,
|
|
|
|
endDate: null,
|
|
|
|
selecting: false,
|
|
|
|
selecting: false,
|
|
|
|
@ -60,158 +157,85 @@ function DateRangePicker() {
|
|
|
|
anchorDate: null
|
|
|
|
anchorDate: null
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
const handleDateSelect = (date: Date) => {
|
|
|
|
const handleDateSelect = (date) => {
|
|
|
|
setDateRange(prev => {
|
|
|
|
// Your range selection logic
|
|
|
|
if (!prev.selecting) {
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
|
|
|
startDate: date,
|
|
|
|
|
|
|
|
endDate: date,
|
|
|
|
|
|
|
|
selecting: true,
|
|
|
|
|
|
|
|
hoverDate: date,
|
|
|
|
|
|
|
|
anchorDate: date
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Complete the selection
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
|
|
|
startDate: prev.anchorDate,
|
|
|
|
|
|
|
|
endDate: date,
|
|
|
|
|
|
|
|
selecting: false,
|
|
|
|
|
|
|
|
hoverDate: null,
|
|
|
|
|
|
|
|
anchorDate: null
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
return (
|
|
|
|
<Year
|
|
|
|
<Year
|
|
|
|
year={2024}
|
|
|
|
year={2024}
|
|
|
|
dayHeaderStyle="tiny"
|
|
|
|
|
|
|
|
monthCutoff="truncate"
|
|
|
|
|
|
|
|
weekendDays={[6, 0]}
|
|
|
|
|
|
|
|
dateRange={dateRange}
|
|
|
|
dateRange={dateRange}
|
|
|
|
onDateSelect={handleDateSelect}
|
|
|
|
onDateSelect={handleDateSelect}
|
|
|
|
onDateHover={(date) => setDateRange(prev => ({ ...prev, hoverDate: date }))}
|
|
|
|
onDateHover={(date) => setDateRange(prev => ({ ...prev, hoverDate: date }))}
|
|
|
|
size="l"
|
|
|
|
|
|
|
|
fontProportion={100}
|
|
|
|
|
|
|
|
magnify={true}
|
|
|
|
|
|
|
|
/>
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
### Single Month View
|
|
|
|
### Interactive Date Range with Active States
|
|
|
|
|
|
|
|
|
|
|
|
```tsx
|
|
|
|
```tsx
|
|
|
|
import React from 'react';
|
|
|
|
import { DateRange } from 'react-calendario';
|
|
|
|
import { Month } from './components/calendar/Month';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function SingleMonth() {
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
|
|
|
<Month
|
|
|
|
|
|
|
|
date={new Date()}
|
|
|
|
|
|
|
|
dayHeaderStyle="expanded"
|
|
|
|
|
|
|
|
direction="column"
|
|
|
|
|
|
|
|
monthCutoff="dimmed"
|
|
|
|
|
|
|
|
weekendDays={[6, 0]}
|
|
|
|
|
|
|
|
size="xl"
|
|
|
|
|
|
|
|
fontProportion={100}
|
|
|
|
|
|
|
|
/>
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
### Week View
|
|
|
|
function InteractiveCalendar() {
|
|
|
|
|
|
|
|
const [activeDates, setActiveDates] = useState([]);
|
|
|
|
|
|
|
|
|
|
|
|
```tsx
|
|
|
|
const handleDateClick = (date) => {
|
|
|
|
import React from 'react';
|
|
|
|
setActiveDates(prev => {
|
|
|
|
import { Week } from './components/calendar/Week';
|
|
|
|
const exists = prev.some(d => d.getTime() === date.getTime());
|
|
|
|
|
|
|
|
return exists
|
|
|
|
|
|
|
|
? prev.filter(d => d.getTime() !== date.getTime())
|
|
|
|
|
|
|
|
: [...prev, date];
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
function WeekView() {
|
|
|
|
|
|
|
|
return (
|
|
|
|
return (
|
|
|
|
<Week
|
|
|
|
<DateRange
|
|
|
|
startDate={new Date()}
|
|
|
|
from={new Date(2024, 0, 1)}
|
|
|
|
headerStyle="compacted"
|
|
|
|
to={new Date(2024, 0, 31)}
|
|
|
|
referenceMonth={new Date().getMonth()}
|
|
|
|
onDateClick={handleDateClick}
|
|
|
|
weekendDays={[6, 0]}
|
|
|
|
activeDates={activeDates}
|
|
|
|
size="l"
|
|
|
|
|
|
|
|
fontProportion={100}
|
|
|
|
|
|
|
|
/>
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
### Compact Year View
|
|
|
|
## Styling
|
|
|
|
|
|
|
|
|
|
|
|
```tsx
|
|
|
|
The components use CSS modules and CSS variables for theming. You can override the default styles by:
|
|
|
|
import React from 'react';
|
|
|
|
|
|
|
|
import { Year } from './components/calendar/Year';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function CompactYear() {
|
|
|
|
1. Using the built-in props for common customizations
|
|
|
|
return (
|
|
|
|
2. Overriding CSS variables in your global styles
|
|
|
|
<Year
|
|
|
|
3. Applying custom CSS classes
|
|
|
|
year={2024}
|
|
|
|
|
|
|
|
dayHeaderStyle="tiny"
|
|
|
|
|
|
|
|
monthCutoff="truncate"
|
|
|
|
|
|
|
|
weekendDays={[6, 0]}
|
|
|
|
|
|
|
|
size="xs"
|
|
|
|
|
|
|
|
fontProportion={80}
|
|
|
|
|
|
|
|
magnify={true}
|
|
|
|
|
|
|
|
/>
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## Component Props
|
|
|
|
### CSS Variables
|
|
|
|
|
|
|
|
|
|
|
|
### Year Component
|
|
|
|
|
|
|
|
```typescript
|
|
|
|
|
|
|
|
interface YearProps {
|
|
|
|
|
|
|
|
year: number;
|
|
|
|
|
|
|
|
dayHeaderStyle: 'expanded' | 'compacted' | 'tiny' | 'none';
|
|
|
|
|
|
|
|
monthDayOfWeekHeaderStyle?: HeaderStyle;
|
|
|
|
|
|
|
|
monthCutoff?: 'dimmed' | 'truncate' | undefined;
|
|
|
|
|
|
|
|
weekendDays?: number[];
|
|
|
|
|
|
|
|
dateRange?: DateRange;
|
|
|
|
|
|
|
|
onDateSelect?: (date: Date) => void;
|
|
|
|
|
|
|
|
onDateHover?: (date: Date) => void;
|
|
|
|
|
|
|
|
size?: 'xl' | 'l' | 'm' | 's' | 'xs';
|
|
|
|
|
|
|
|
fontProportion?: number;
|
|
|
|
|
|
|
|
magnify?: boolean;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
### Month Component
|
|
|
|
```css
|
|
|
|
```typescript
|
|
|
|
:root {
|
|
|
|
interface MonthProps {
|
|
|
|
--color-primary: #2196f3;
|
|
|
|
date: Date;
|
|
|
|
--color-primary-dark: #1976d2;
|
|
|
|
dayHeaderStyle: HeaderStyle;
|
|
|
|
--color-selected: #e3f2fd;
|
|
|
|
direction: 'row' | 'column';
|
|
|
|
/* ... and more */
|
|
|
|
monthCutoff?: MonthCutoffType;
|
|
|
|
|
|
|
|
weekendDays?: number[];
|
|
|
|
|
|
|
|
dateRange?: DateRange;
|
|
|
|
|
|
|
|
onDateSelect?: (date: Date) => void;
|
|
|
|
|
|
|
|
onDateHover?: (date: Date) => void;
|
|
|
|
|
|
|
|
size?: DaySize;
|
|
|
|
|
|
|
|
fontProportion?: number;
|
|
|
|
|
|
|
|
magnify?: boolean;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
## Styling
|
|
|
|
## TypeScript
|
|
|
|
|
|
|
|
|
|
|
|
The component uses SCSS modules for styling. Key style files:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
- `src/components/calendar/shared/_colors.scss`: Color variables
|
|
|
|
|
|
|
|
- `src/components/calendar/shared/_variables.scss`: Layout variables
|
|
|
|
|
|
|
|
- Individual component SCSS modules for specific styling
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## Browser Support
|
|
|
|
Full TypeScript support with exported types:
|
|
|
|
|
|
|
|
|
|
|
|
- Chrome (latest)
|
|
|
|
```tsx
|
|
|
|
- Firefox (latest)
|
|
|
|
import type {
|
|
|
|
- Safari (latest)
|
|
|
|
DateRange,
|
|
|
|
- Edge (latest)
|
|
|
|
HeaderStyle,
|
|
|
|
|
|
|
|
DaySize,
|
|
|
|
|
|
|
|
MonthCutoffType
|
|
|
|
|
|
|
|
} from 'react-calendario';
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
## License
|
|
|
|
## License
|
|
|
|
|
|
|
|
|
|
|
|
MIT
|
|
|
|
MIT
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## Contributing
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Contributions are welcome! Please feel free to submit a Pull Request.
|