0.0.4 â€ĸ Published 2 years ago

slek v0.0.4

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

image

Slek npm bundle size npm

An unstyled calendar component for React.

Features

  • đŸ› ī¸ Customizable
  • 🌟 Lightweight
  • â˜ī¸ 1 dependency
  • â†Šī¸ RTL support

Installation

npm install slek

Usage

import Calendar, {useCalendar} from 'slek';

const { date, setDate } = useCalendar(new Date());

<Calendar date={date} onChange={((d) => setDate(d))}>
  <Calendar.Header>
    <Calendar.Headline />
    <Calendar.NextButton />
    <Calendar.PrevButton />
  </Calendar.Header>
  <Calendar.Grid>
      {(props) => <MyCell>{props.value}</MyCell>}
  </Calendar.Grid>
</Calendar>

API Reference

The Calendar component is composed of several parts. All parts accept basic HTML element props (style, className...), including ref. Each part has a specific data-attribute (starting with slek-) that can be used for easy styling.

Calendar

slek-root - The main component.

PropTypeDefaultDescription
dateDate | Date[]--The date to display in the calendar
minDateDate--The minimum date that can be selected
maxDateDate--The maximum date that can be selected
weekStartnumber0The day of the week to start on (0 = Sunday, 1 = Monday...)
localestring"en"The locale to use for formatting dates
disabledbooleanfalseWhether the calendar is disabled
onChange(date: Date | Date[]) => void--Callback when the date changes

Under the hood, we are using dayjs to do all the magic, so if you want to change the local make sure to import that locale from the dayjs package. (See more info here)

Header

slek-header - The header wrapper of the calendar, should contain the headline and the navigation buttons.

PropTypeDefaultDescription
refReact.RefObject--The ref of the header element

Headline

slek-headline - The headline of the calendar, displays the current month and year.

PropTypeDefaultDescription
monthStyle"full" | "short""full"The style of the month name
disableClickbooleanfalseWhether the headline is clickable
refReact.RefObject--The ref of the headline element
onClick() => void--Callback when the headline is clicked

NextButton

slek-next-button - The next button of the calendar, navigates to the next month or next 10 years. Can also be used as a wrapper for a custom next button.

PropTypeDefaultDescription
refReact.RefObject--The ref of the next button element
onClick() => void--Callback when the next button is clicked

PrevButton

slek-prev-button - The previous button of the calendar, navigates to the previous month or previous 10 years. Can also be used as a wrapper for a custom previous button.

PropTypeDefaultDescription
refReact.RefObject--The ref of the previous button element
onClick() => void--Callback when the previous button is clicked

Grid

slek-grid - The grid of the calendar, contains the cells.

PropTypeDefaultDescription
mode"single" | "range" | "multiple""single"The selection mode of the calendar
direction"ltr" | "rtl""ltr"The direction of the calendar
weekdayStyle"full" | "short" | "min""min"The style of the weekday names
monthStyle"full" | "short""short"The style of the month name
showOutsideDatesbooleantrueWhether to show dates from the previous and next months
refReact.RefObject--The ref of the grid element

The children of the Grid component is a render prop that receives the following props:

PropTypeDescription
valuestring | numberThe value of the cell
type"date" | "month" | "year"The type of the cell
outsideDatebooleanWhether the cell is from the previous or next month
todaybooleanWhether the cell is today
disabledbooleanWhether the cell is disabled
invalidbooleanWhether the cell is invalid (based on the min/max dates)
selectedbooleanWhether the cell is selected
betweenbooleanWhether the cell is between two selected dates (only in range mode)

Cell

slek-cell - The cell of the calendar, used as a wrapper for a custom cell. For convenience, the Cell includes those data attributes:

Data attributeValues
data-outside-date"true" | "false"
data-today"true" | "false"
data-disabled"true" | "false"
data-invalid"true" | "false"
data-selected"true" | "false"
data-between"true" | "false"
data-type"date" | "month" | "year"

Utils

useCalendar

A simple hook that returns the current date and a function to set the date. You can use it to control the date from outside the calendar. The hook accepts an optional initial date. The hook returns an object with the following properties:

PropertyTypeDescription
dateDate | Date[]The current date
setDate(date: Date | Date[]) => voidA function to set the date
selectToday() => voidA function to select today
selectCustom(date: Date) => voidA function to select a custom date
const { date, setDate } = useCalendar();

<Calendar date={date} onChange={(d) => setDate(d)}>
...
</Calendar>

Contributing

If you want to contribute to the development, please follow these steps:

Fork the repository. Create a new branch. Make your changes. Submit a pull request.

License

This project is licensed under the MIT License.

0.0.4

2 years ago

0.0.3

2 years ago

0.0.2

2 years ago

0.0.1

2 years ago