27
loading...
This website collects cookies to deliver better user experience
import Calendar from 'react-calendar';
import styled from 'styled-components';
function App() {
return (
<CalendarContainer>
<Calendar calendarType='US' />
</CalendarContainer>
);
}
export default App;
const CalendarContainer = styled.div`
/* ~~~ container styles ~~~ */
max-width: 600px;
margin: auto;
margin-top: 20px;
background-color: #d4f7d4;
padding: 10px;
border-radius: 3px;
`;
<div>
that holds the calendar just so the calendar is not floating in the void.Note: I also applied the calendarType
property to the calendar that sets the first day of the week to Sunday. This is how I'm used to seeing calendars, but by leaving this property off the week should start with Monday.
<Calendar />
component:import 'react-calendar/dist/Calendar.css';
CalendarContainer
styled component. React-Calendar creates elements with certain classes already applied, so we can use those as our selectors. const CalendarContainer = styled.div`
/* ~~~ container styles ~~~ */
/* ... */
/* ~~~ navigation styles ~~~ */
.react-calendar__navigation {
display: flex;
.react-calendar__navigation__label {
font-weight: bold;
}
.react-calendar__navigation__arrow {
flex-grow: 0.333;
}
}
`;
const CalendarContainer = styled.div`
/* ~~~ container styles ~~~ */
/* ~~~ navigation styles ~~~ */
/* ... */
/* ~~~ label styles ~~~ */
.react-calendar__month-view__weekdays {
text-align: center;
}
`;
const CalendarContainer = styled.div`
/* ~~~ container styles ~~~ */
/* ~~~ navigation styles ~~~ */
/* ~~~ label styles ~~~ */
/* ... */
/* ~~~ button styles ~~~ */
button {
margin: 3px;
background-color: #6f876f;
border: 0;
border-radius: 3px;
color: white;
padding: 5px 0;
&:hover {
background-color: #556b55;
}
&:active {
background-color: #a5c1a5;
}
}
`;
display: flex;
applied, which unfortunately leads to items spilling over to other rows instead of ensuring that there are always 7 days in a week. Luckily we can overwrite this behavior by using grid
:const CalendarContainer = styled.div`
/* ~~~ container styles ~~~ */
/* ~~~ navigation styles ~~~ */
/* ~~~ label styles ~~~ */
/* ~~~ button styles ~~~ */
/* ... */
/* ~~~ day grid styles ~~~ */
.react-calendar__month-view__days {
display: grid !important;
grid-template-columns: 14.2% 14.2% 14.2% 14.2% 14.2% 14.2% 14.2%;
.react-calendar__tile {
max-width: initial !important;
}
}
`;
Note that some elements that react-calendar creates have styles applied directly to the element. To overwrite these, we need the !important
rule so our class selectors can take precedence.
const CalendarContainer = styled.div`
/* ~~~ container styles ~~~ */
/* ~~~ navigation styles ~~~ */
/* ~~~ label styles ~~~ */
/* ~~~ button styles ~~~ */
/* ~~~ day grid styles ~~~ */
/* ... */
/* ~~~ neighboring month & weekend styles ~~~ */
.react-calendar__month-view__days__day--neighboringMonth {
opacity: 0.7;
}
.react-calendar__month-view__days__day--weekend {
color: #dfdfdf;
}
`;
const CalendarContainer = styled.div`
/* ~~~ container styles ~~~ */
/* ~~~ navigation styles ~~~ */
/* ~~~ label styles ~~~ */
/* ~~~ button styles ~~~ */
/* ~~~ day grid styles ~~~ */
/* ~~~ neighboring month & weekend styles ~~~ */
/* ... */
/* ~~~ active day styles ~~~ */
.react-calendar__tile--range {
box-shadow: 0 0 6px 2px black;
}
`;
grid
. We'll show the list of months in a grid of 3 columns by 4 rows. The 10 buttons in the decade and century views will be in a grid of 5 columns by 2 rows.const CalendarContainer = styled.div`
/* ~~~ container styles ~~~ */
/* ~~~ navigation styles ~~~ */
/* ~~~ label styles ~~~ */
/* ~~~ button styles ~~~ */
/* ~~~ day grid styles ~~~ */
/* ~~~ neighboring month & weekend styles ~~~ */
/* ~~~ active day styles ~~~ */
/* ... */
/* ~~~ other view styles ~~~ */
.react-calendar__year-view__months,
.react-calendar__decade-view__years,
.react-calendar__century-view__decades {
display: grid !important;
grid-template-columns: 20% 20% 20% 20% 20%;
&.react-calendar__year-view__months {
grid-template-columns: 33.3% 33.3% 33.3%;
}
.react-calendar__tile {
max-width: initial !important;
}
}
`;