ui: The calendar looks completely wrong.
The calendar component is just not formatted or styled correctly. This project uses other shadcn components and they all work fine, but the calendar just looks and behaves incorrectly. The days aren’t positions in rows and columns but clump to the left, and the hover and selected effects don’t appear at all. I’ve been trying to solve this for a while and I just can’t find the reason. I’ve added a picture of the appearance of the calendar and some code from the project:
The Calendar Component
"use client"
import * as React from "react"
import { ChevronLeft, ChevronRight } from "lucide-react"
import { DayPicker } from "react-day-picker"
import { cn } from "@/lib/utils"
import { buttonVariants } from "@/shadcn/components/ui/button"
export type CalendarProps = React.ComponentProps<typeof DayPicker>
function Calendar({
className,
classNames,
showOutsideDays = true,
...props
}: CalendarProps) {
return (
<DayPicker
showOutsideDays={showOutsideDays}
className={cn("p-3", className)}
classNames={{
months: "flex flex-col sm:flex-row space-y-4 sm:space-x-4 sm:space-y-0",
month: "space-y-4",
caption: "flex justify-center pt-1 relative items-center",
caption_label: "text-sm font-medium",
nav: "space-x-1 flex items-center",
nav_button: cn(
buttonVariants({ variant: "outline" }),
"h-7 w-7 bg-transparent p-0 opacity-50 hover:opacity-100"
),
nav_button_previous: "absolute left-1",
nav_button_next: "absolute right-1",
table: "w-full border-collapse space-y-1",
head_row: "flex",
head_cell:
"text-slate-500 rounded-md w-9 font-normal text-[0.8rem] dark:text-slate-400",
row: "flex w-full mt-2",
cell: "text-center text-sm p-0 relative [&:has([aria-selected])]:bg-slate-100 first:[&:has([aria-selected])]:rounded-l-md last:[&:has([aria-selected])]:rounded-r-md focus-within:relative focus-within:z-20 dark:[&:has([aria-selected])]:bg-slate-800",
day: cn(
buttonVariants({ variant: "ghost" }),
"h-9 w-9 p-0 font-normal aria-selected:opacity-100"
),
day_selected:
"bg-slate-900 text-slate-50 hover:bg-slate-900 hover:text-slate-50 focus:bg-slate-900 focus:text-slate-50 dark:bg-slate-50 dark:text-slate-900 dark:hover:bg-slate-50 dark:hover:text-slate-900 dark:focus:bg-slate-50 dark:focus:text-slate-900",
day_today: "bg-slate-100 text-slate-900 dark:bg-slate-800 dark:text-slate-50",
day_outside: "text-slate-500 opacity-50 dark:text-slate-400",
day_disabled: "text-slate-500 opacity-50 dark:text-slate-400",
day_range_middle:
"aria-selected:bg-slate-100 aria-selected:text-slate-900 dark:aria-selected:bg-slate-800 dark:aria-selected:text-slate-50",
day_hidden: "invisible",
...classNames,
}}
components={{
IconLeft: ({ ...props }) => <ChevronLeft className="h-4 w-4" />,
IconRight: ({ ...props }) => <ChevronRight className="h-4 w-4" />,
}}
{...props}
/>
)
}
Calendar.displayName = "Calendar"
export { Calendar }
My DatePicker component
"use client"
import * as React from "react"
import { format } from "date-fns"
import { Calendar as CalendarIcon } from "lucide-react"
import { cn } from "@/lib/utils"
import { Button } from "@/shadcn/components/ui/button"
import { Popover, PopoverTrigger, PopoverContent } from "@/shadcn/components/ui/popover"
import { Calendar } from "@/shadcn/components/ui/calendar"
export function DatePicker() {
const [date, setDate] = React.useState<Date>()
return (
<Popover>
<PopoverTrigger asChild>
<Button
variant={"outline"}
className={cn(
"w-[280px] justify-start text-left font-normal",
!date && "text-muted-foreground"
)}
>
<CalendarIcon className="mr-2 h-4 w-4" />
{date ? format(date, "PPP") : <span>Pick a date</span>}
</Button>
</PopoverTrigger>
<PopoverContent className="w-auto p-0">
<Calendar
mode="single"
selected={date}
onSelect={setDate}
initialFocus
/>
</PopoverContent>
</Popover>
)
}
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Comments: 24
Reverse
react-day-pickerback to the old version^8.9.1seems to fix the issuesBased on what @MahmoudElkayal shared right above, here is my working code of
<Calendar>component.The component needs to be upgraded, https://daypicker.dev/upgrading
I can confirm that the issue still occurs in the latest versions of shadcn/react-day-picker looking very similar to the images from July 2024.
The solution by sibteali786 fixed it. There is no need to complicate the code, as I had no styling issues.
Versions used:
Before:
After:
some changes since react day picker 9.10
UI.ButtonPreviousandUI.ButtonNextare updatedThat’s work, but @shadcn needs to review to add compatibility with this new major version of react-day-picker
Hello, I was facing this issue and was using @sibteali786 solution, but I had some minor styling issues. Then, I found that react-day-picker provides a working solution in their styling docs. They refer to this link: https://date-picker.luca-felix.com/. Tested, and it works perfectly.
First install
react-day-pickerversion 9Then replace your calendar component in
components/ui/calendar.tsxIf you experience a crash when changing the month, replace this line:
with :
I can confirm that this is still a valid issue. I’ve followed the installation guide and just used the provided example code and this is how it looks
Inspecting the elements within the date picker it seems like none of the provided classnames adds actual styling. Some CSS scoping issues maybe?
It might be due to your DayPicker import in your Calendar component. You’re importing DayPicker from “react-day-picker” but did you mean to select DatePicker from “@/shadcn/components/ui/calendar”?
Could it be as simple as adding
import 'react-day-picker/dist/style.css';in the Calendar component generated by shadcn?I also got a wrong looking calendar but then realized that the DayPicker CSS import was missing. After manually adding it, it worked. According to the React Day Picker docs:
That’s working for me, you save my time, thank you @liawagner
to fix this issue uninstall the date-fns package. It will solve the issue.
then install shadcn calendar component
I can confirm it works with
react-day-picker@9.2.1.@YoonHan legend! thanks