@@ -19,9 +19,9 @@ defmodule ComponentsGuideWeb.CalendarComponent do
1919 end_date = Date . end_of_month ( date )
2020 date_range = Date . range ( start_date , end_date )
2121
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 )
2525
2626 assigns =
2727 assigns
@@ -30,12 +30,16 @@ defmodule ComponentsGuideWeb.CalendarComponent do
3030 year: year ,
3131 month: month ,
3232 day: day ,
33+ current: Date . new! ( year , month , day ) ,
34+ day_of_week: day_of_week ,
3335 day_offset: day_offset
3436 } )
3537 |> Map . put_new ( :extra , fn _ -> "" end )
3638
3739 ~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 %>
3943 < table class = "text-center " >
4044 < thead class = "border-0 " >
4145 < tr >
@@ -49,13 +53,13 @@ defmodule ComponentsGuideWeb.CalendarComponent do
4953 </ tr >
5054 </ thead >
5155 < tbody >
52- <%= for week_n <- 1 .. max_week do %>
56+ <%= for week_n <- 1 .. max_week , false do %>
5357 < 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 ) %>
5660 <%= 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 } ) } >
5963 < div class = "text-sm " > <%= day %> </ div >
6064 <%= @ extra . ( Date . new! ( year , month , day ) ) %>
6165 </ td >
@@ -65,12 +69,29 @@ defmodule ComponentsGuideWeb.CalendarComponent do
6569 <% end %>
6670 </ tr >
6771 <% 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 %>
6884 </ tbody >
6985 </ table >
7086 """
7187 end
7288
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"
7591 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"
7697end
0 commit comments