@@ -19,9 +19,9 @@ defmodule ComponentsGuideWeb.CalendarComponent do
19
19
end_date = Date . end_of_month ( date )
20
20
date_range = Date . range ( start_date , end_date )
21
21
22
- day_inset = Date . day_of_week ( start_date )
23
- day_offset = 1 - day_inset
24
- max_week = div ( end_date . day + day_inset + 5 , 7 )
22
+ day_of_week = Date . day_of_week ( start_date )
23
+ day_offset = 1 - day_of_week
24
+ max_week = div ( end_date . day + day_of_week + 5 , 7 )
25
25
26
26
assigns =
27
27
assigns
@@ -30,12 +30,16 @@ defmodule ComponentsGuideWeb.CalendarComponent do
30
30
year: year ,
31
31
month: month ,
32
32
day: day ,
33
+ current: Date . new! ( year , month , day ) ,
34
+ day_of_week: day_of_week ,
33
35
day_offset: day_offset
34
36
} )
35
37
|> Map . put_new ( :extra , fn _ -> "" end )
36
38
37
39
~H"""
38
- < h2 class = "text-center " > <%= Calendar . strftime ( @ date_range . first , "%B %Y" ) %> </ h2 >
40
+ <%= if false do %>
41
+ < h2 class = "text-center " > <%= Calendar . strftime ( @ date_range . first , "%B %Y" ) %> </ h2 >
42
+ <% end %>
39
43
< table class = "text-center " >
40
44
< thead class = "border-0 " >
41
45
< tr >
@@ -49,13 +53,13 @@ defmodule ComponentsGuideWeb.CalendarComponent do
49
53
</ tr >
50
54
</ thead >
51
55
< tbody >
52
- <%= for week_n <- 1 .. max_week do %>
56
+ <%= for week_n <- 1 .. max_week , false do %>
53
57
< tr class = "min-h-16 " >
54
- <%= for weekday <- 1 .. 7 do %>
55
- <% day = weekday + day_offset + ( ( week_n - 1 ) * 7 ) %>
58
+ <%= for weekday <- 0 .. 6 do %>
59
+ <% day = weekday + 1 + day_offset + ( ( week_n - 1 ) * 7 ) %>
56
60
<%= if day in @ date_range . first . day .. @ date_range . last . day do %>
57
- <% current = day == @ day %>
58
- < td aria-current = { if current , do: "date" , else: "false" } class = { td_class ( % { current: current , weekday: weekday } ) } >
61
+ <% current? = day == @ day %>
62
+ < td aria-current = { if current? , do: "date" , else: "false" } class = { td_class ( % { current? : current? , weekday: weekday } ) } >
59
63
< div class = "text-sm " > <%= day %> </ div >
60
64
<%= @ extra . ( Date . new! ( year , month , day ) ) %>
61
65
</ td >
@@ -65,12 +69,29 @@ defmodule ComponentsGuideWeb.CalendarComponent do
65
69
<% end %>
66
70
</ tr >
67
71
<% end %>
72
+ <%= for week_offset <- - 4 .. 4 do %>
73
+ < tr class = "min-h-16 group " >
74
+ <%= for weekday <- 0 .. 6 do %>
75
+ <% date = Date . add ( @ current , ( week_offset - 1 ) * 7 + weekday - day_offset ) %>
76
+ <% current? = week_offset == 0 && day_of_week == weekday %>
77
+ < td aria-current = { if current? , do: "date" , else: "false" } class = { td_class ( % { current?: current? , weekday: weekday } ) } >
78
+ < div class = { cell_text_class ( week_offset ) } > <%= Calendar . strftime ( date , "%d %b" ) %> </ div >
79
+ <%= @ extra . ( date ) %>
80
+ </ td >
81
+ <% end %>
82
+ </ tr >
83
+ <% end %>
68
84
</ tbody >
69
85
</ table >
70
86
"""
71
87
end
72
88
73
- defp td_class ( % { current: true } ) , do: "bg-green-900 text-green-100"
74
- defp td_class ( % { weekday: weekday } ) when weekday in [ 6 , 7 ] , do: "bg-black/40"
89
+ defp td_class ( % { current? : true } ) , do: "bg-green-900 text-green-100"
90
+ defp td_class ( % { weekday: weekday } ) when weekday in [ 5 , 6 ] , do: "bg-black/40"
75
91
defp td_class ( _ ) , do: "bg-black"
92
+
93
+ defp cell_text_class ( 0 ) , do: "text-sm opacity-100"
94
+ defp cell_text_class ( week_offset ) when week_offset in [ - 1 , 1 ] , do: "text-sm opacity-75"
95
+ defp cell_text_class ( week_offset ) when week_offset in [ - 2 , 2 ] , do: "text-sm opacity-60"
96
+ defp cell_text_class ( _ ) , do: "text-sm opacity-50"
76
97
end
0 commit comments