## Getting Started
### Minimal Setup
1. **Installation**:
```bash
composer require caiquebispo/livewire-calendar
Publish the config (if needed):
php artisan vendor:publish --provider="CaiqueBispo\LivewireCalendar\LivewireCalendarServiceProvider" --tag="config"
First Usage: Add the component to a Livewire blade file:
@livewire('calendar')
Or use it directly in a Livewire component:
use CaiqueBispo\LivewireCalendar\LivewireCalendar;
public function mount() {
$this->calendar = new LivewireCalendar();
}
public function render() {
return view('livewire.calendar-component', [
'calendar' => $this->calendar
]);
}
Basic Configuration:
Define default settings in config/livewire-calendar.php:
'default_view' => 'month',
'start_day' => 0, // Monday
'locale' => 'en',
@livewire('calendar', [
'view' => 'month',
'year' => now()->year,
'month' => now()->month,
'events' => [
['date' => '2023-12-25', 'title' => 'Christmas'],
['date' => '2024-01-01', 'title' => 'New Year']
]
])
$this->calendarEvents = [
['date' => '2023-12-25', 'title' => 'Christmas', 'color' => 'bg-red-500'],
['date' => '2023-12-31', 'title' => 'New Year\'s Eve', 'description' => 'Party time!']
];
public function getEvents() {
return Event::whereDate('date', '>=', $this->startDate)
->whereDate('date', '<=', $this->endDate)
->get(['date', 'title', 'description']);
}
public $selectedDate = null;
public function updatedSelectedDate($date) {
$this->selectedDate = $date;
$this->emit('dateSelected', $date);
}
public $startDate = null;
public $endDate = null;
public function updatedStartDate($date) {
$this->startDate = $date;
}
public function updatedEndDate($date) {
$this->endDate = $date;
}
public function nextMonth() {
$this->year = $this->month === 12 ? $this->year + 1 : $this->year;
$this->month = $this->month === 12 ? 1 : $this->month + 1;
}
public function previousMonth() {
$this->year = $this->month === 1 ? $this->year - 1 : $this->year;
$this->month = $this->month === 1 ? 12 : $this->month - 1;
}
@livewire('calendar', [
'cell_classes' => [
'today' => 'bg-blue-500 text-white',
'selected' => 'bg-green-500 text-white',
'other-month' => 'text-gray-300',
'event' => 'bg-red-200'
]
])
render method:
public function render() {
return view('livewire.custom-calendar', [
'calendar' => $this->calendar,
'customView' => true
]);
}
protected $listeners = ['refreshEvents' => 'loadEvents'];
public function loadEvents() {
$this->calendarEvents = $this->getEventsFromDatabase();
}
public function submitSelectedDate() {
$this->validate(['selectedDate' => 'required']);
// Save to database
$this->dispatch('dateSaved', $this->selectedDate);
}
public function render() {
return view('livewire.multi-calendar', [
'calendars' => [
['id' => 'work', 'title' => 'Work Calendar', 'events' => $this->workEvents],
['id' => 'personal', 'title' => 'Personal Calendar', 'events' => $this->personalEvents]
]
]);
}
use Carbon\Carbon;
public function mount() {
$this->timezone = config('app.timezone');
$this->carbon = new Carbon('now', $this->timezone);
}
public function getFormattedDate($date) {
return $this->carbon->copy()->parse($date)->format('Y-m-d');
}
public function mount() {
$this->locale = app()->getLocale();
$this->monthNames = [
'January', 'February', 'March', 'April', 'May', 'June',
'July', 'August', 'September', 'October', 'November', 'December'
];
}
@livewire('calendar', [
'aria_labels' => [
'header' => 'Calendar Navigation',
'day' => 'Day {date}',
'month' => 'Month View',
'year' => 'Year View'
]
])
z-index in Tailwind classes or ensure events are ordered by time:
$events = Event::whereDate('date', $date)->orderBy('time')->get();
public function mount() {
date_default_timezone_set(config('app.timezone'));
}
'touch_config' => [
'enabled' => true,
'min_touch_distance' => 50,
'swipe_threshold' => 0.5
]
public function getEventsForMonth($year, $month) {
return Event::whereMonth('date', $month)
->whereYear('date', $year)
->limit(100)
->get();
}
@wire:ignore.<div x-data="{ /* Alpine.js state */ }">
@livewire('calendar') <!-- Not inside wire:ignore -->
</div>
default_view config may not apply if explicitly set in the component props:
// Overrides config
public $view = 'week';
en. Ensure your locale files are published:
php artisan vendor:publish --tag=livewire-calendar-lang
module.exports = {
safelist: [
'bg-blue-500', 'text-white', 'bg-red-200',
// Add other dynamic classes used in the calendar
]
}
How can I help you explore Laravel packages today?