use-lilius

repository·main·Indexed 42 days ago

https://github.com/avarios/use-lilius

A headless calendar hook for React (v2.0.5) that provides state management and logic for calendar interfaces without prescribing UI or styling. It includes the useLilius hook for managing date/time logic, a 3D calendar matrix for rendering, and utility methods for date selection, range management, and view navigation.

Tokens
3.5K
Snippets
13
Records
24
Agent score
54%

What's inside use-lilius

  1. Get started with use-lilius

    main

    use-lilius is a headless calendar hook for React. Because it is headless, it provides the logic and state management for a calendar without providing any UI components. You are responsible for rendering the calendar using your own components and styling.

    For detailed API documentation, refer to the API docs. For visual examples of how to implement the hook with UI libraries like Chakra UI, visit the use-lilius Storybook.

  2. Implement a single-select datepicker using Chakra UI

    main

    You can implement a single-select datepicker by using the SingleSelect component. This component is built using Chakra UI as its styling foundation. In a Storybook environment, it can be rendered as a standalone story to demonstrate its single-selection functionality.

    import { SingleSelect } from "./single-select";
    
    // Usage in a component or story
    <SingleSelect />
  3. Access calendar state and viewing properties

    main

    The Returns interface provides access to the current state of the calendar. Use these properties to render the calendar UI and track user selections:

    • calendar: A Date[][][] matrix representing the days based on the current viewing date. This is the primary data structure for rendering the calendar grid.
    • selected: An array of Date objects representing the currently selected dates.
    • viewing: The Date object representing the current view. Note that only the month and year components of this date are used to determine the calendar matrix.
    • setSelected: A React state dispatcher to manually override the selected dates.
    • setViewing: A React state dispatcher to manually set the viewing date. Only the month and year of the provided date are used.
  4. Manage date selections

    main

    Use the following methods to control which dates are selected in the calendar:

    • select(date, replaceExisting?): Selects one or more dates. date can be a single Date or an array of Date[]. If replaceExisting is true, it clears previous selections.
    • deselect(date): Deselects one or more dates. date can be a single Date or an array of Date[].
    • selectRange(start, end, replaceExisting?): Selects an inclusive range of dates between start and end.
    • deselectRange(start, end): Deselects an inclusive range of dates between start and end.
    • toggle(date, replaceExisting?): Toggles the selection state of a specific date.
    • clearSelected(): Resets all selected dates to an empty array [].
    • isSelected(date): Returns true if the provided date is currently in the selected array.
    • setSelected(dates): Directly overrides the current selection with a new array of Date objects.
  5. Configure the useLilius Options interface

    main

    When initializing useLilius, you can provide an optional Options object to customize the calendar's behavior, including how many months are displayed, which dates are pre-selected, the initial view, and the start of the week. All properties are optional and have default values.

    const options: Options = {
      numberOfMonths: 2,
      selected: [new Date()],
      viewing: new Date(),
      weekStartsOn: Day.MONDAY
    };
  6. Navigate the calendar view

    main

    Use these methods to change the month or year currently being displayed in the calendar matrix:

    • viewToday(): Sets the viewing date to today.
    • viewMonth(month): Sets the viewing date to a specific Month enum value.
    • viewYear(year): Sets the viewing date to a specific year (e.g., 2024).
    • viewNextMonth(): Advances the view to the next month.
    • viewPreviousMonth(): Moves the view to the previous month.
    • viewNextYear(): Advances the view to the next year.
    • viewPreviousYear(): Moves the view to the previous year.
  7. Use the useLilius hook

    main

    The useLilius function is the primary entry point for the library. It accepts an optional Options object and returns a Returns object containing the tools needed to manage date/time logic within a component.

    Refer to the Options interface for available configuration parameters and the Returns interface to understand the available utility methods and state provided by the hook.

    // Basic usage pattern
    const { /* values from Returns interface */ } = useLilius(options);
  8. Configure useLilius options

    main

    When calling useLilius, you can provide an Options object to customize the initial state and behavior:

    • weekStartsOn: Defines which day a week starts on within the calendar matrix. Defaults to Day.SUNDAY.
    • viewing: The initial date the calendar focuses on. Defaults to new Date().
    • selected: An array of initial Date objects representing selected dates. Defaults to [].
    • numberOfMonths: The number of months to display in the calendar matrix. Defaults to 1.
    import { useLilius, Day } from 'use-lilius';
    
    const options = {
      weekStartsOn: Day.MONDAY,
      viewing: new Date(2023, 0, 1),
      selected: [new Date(2023, 0, 15)],
      numberOfMonths: 2
    };
    
    const { calendar } = useLilius(options);